Debian Codegraph @

lib/alloc.c

raw symbols SCIP
1 /*
2 Copyright(C) 2009-2016 Brazil
3 Copyright(C) 2019-2023 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn.h"cxx . . $ `<file>/lib/grn.h`/
21 #include "grn_alloc.h"cxx . . $ `<file>/lib/grn_alloc.h`/
22 #include "grn_ctx_impl.h"cxx . . $ `<file>/lib/grn_ctx_impl.h`/
23
24 #ifdef HAVE_EXECINFO_Hcxx . . $ `build/config.h:128:9`!
25 # include <execinfo.h>
26 #endif
27
28 static uint32_tcxx . . $ uint32_t# alloc_countcxx . . $ alloc_count. = 0;
29
30 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! grn_fail_malloc_enablecxx . . $ grn_fail_malloc_enable. = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
31 static double grn_fail_malloc_probcxx . . $ grn_fail_malloc_prob. = 0.0;
32 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! grn_fail_malloc_locationcxx . . $ grn_fail_malloc_location. = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
33 static char *grn_fail_malloc_funccxx . . $ grn_fail_malloc_func. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
34 static char *grn_fail_malloc_filecxx . . $ grn_fail_malloc_file. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
35 static int grn_fail_malloc_linecxx . . $ grn_fail_malloc_line. = 0;
36 static int grn_fail_malloc_max_countcxx . . $ grn_fail_malloc_max_count. = -1;
37
38 #define GRN_ADD_ALLOC_COUNTcxx . . $ `lib/alloc.c:38:9`!(count) \
39 do { \
40 uint32_t alloced; \
41 GRN_ATOMIC_ADD_EXcxx . . $ `lib/grn.h:420:11`!(&alloc_count, count, alloced); \
42 } while (0)
43
44 uint32_tcxx . . $ uint32_t#
45 grn_alloc_countcxx . . $ grn_alloc_count(db52760e79bf9054).(void)
46 {
47 return alloc_countcxx . . $ alloc_count.;
48 }
49
50 void
51 grn_alloc_init_from_envcxx . . $ grn_alloc_init_from_env(49f6e7a06ebc5aa8).(void)
52 {
53 {
54 char grn_fail_malloc_enable_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!];
55 grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_FAIL_MALLOC_ENABLE",
56 grn_fail_malloc_enable_env,
57 GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!);
58 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(grn_fail_malloc_enable_env, "yes") == 0) {
59 grn_fail_malloc_enablecxx . . $ grn_fail_malloc_enable. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
60 } else {
61 grn_fail_malloc_enablecxx . . $ grn_fail_malloc_enable. = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
62 }
63 }
64 {
65 char grn_fail_malloc_prob_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!];
66 grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_FAIL_MALLOC_PROB",
67 grn_fail_malloc_prob_env,
68 GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!);
69 if (grn_fail_malloc_prob_env[0]) {
70 char grn_fail_malloc_seed_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!];
71 grn_fail_malloc_probcxx . . $ grn_fail_malloc_prob. = strtodcxx . . $ strtod(3dfe5620a0d3160e).(grn_fail_malloc_prob_env, 0) * RAND_MAXcxx . . $ `<external>/10696550936717951914/stdlib.h:91:9`!;
72 grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_FAIL_MALLOC_SEED",
73 grn_fail_malloc_seed_env,
74 GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!);
75 if (grn_fail_malloc_seed_env[0]) {
76 srandcxx . . $ srand(cb2f890c2fdad230).((unsigned int)atoicxx . . $ atoi(5c4306d8553b3696).(grn_fail_malloc_seed_env));
77 } else {
78 srandcxx . . $ srand(cb2f890c2fdad230).((unsigned int)timecxx . . $ time(7f38134db6521d23).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!));
79 }
80 }
81 }
82 {
83 static char grn_fail_malloc_func_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!];
84 grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_FAIL_MALLOC_FUNC",
85 grn_fail_malloc_func_env,
86 GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!);
87 if (grn_fail_malloc_func_env[0]) {
88 grn_fail_malloc_locationcxx . . $ grn_fail_malloc_location. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
89 grn_fail_malloc_funccxx . . $ grn_fail_malloc_func. = grn_fail_malloc_func_env;
90 }
91 }
92 {
93 static char grn_fail_malloc_file_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!];
94 grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_FAIL_MALLOC_FILE",
95 grn_fail_malloc_file_env,
96 GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!);
97 if (grn_fail_malloc_file_env[0]) {
98 grn_fail_malloc_locationcxx . . $ grn_fail_malloc_location. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
99 grn_fail_malloc_filecxx . . $ grn_fail_malloc_file. = grn_fail_malloc_file_env;
100 }
101 }
102 {
103 char grn_fail_malloc_line_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!];
104 grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_FAIL_MALLOC_LINE",
105 grn_fail_malloc_line_env,
106 GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!);
107 if (grn_fail_malloc_line_env[0]) {
108 grn_fail_malloc_locationcxx . . $ grn_fail_malloc_location. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
109 grn_fail_malloc_linecxx . . $ grn_fail_malloc_line. = atoicxx . . $ atoi(5c4306d8553b3696).(grn_fail_malloc_line_env);
110 }
111 }
112 {
113 char grn_fail_malloc_max_count_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!];
114 grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_FAIL_MALLOC_MAX_COUNT",
115 grn_fail_malloc_max_count_env,
116 GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!);
117 if (grn_fail_malloc_max_count_env[0]) {
118 grn_fail_malloc_max_countcxx . . $ grn_fail_malloc_max_count. = atoicxx . . $ atoi(5c4306d8553b3696).(grn_fail_malloc_max_count_env);
119 }
120 }
121 }
122
123 #ifdef GRN_WITH_MEMORY_DEBUG
124 static grn_critical_section grn_alloc_info_lock;
125
126 void
127 grn_alloc_info_init(void)
128 {
129 CRITICAL_SECTION_INIT(grn_alloc_info_lock);
130 }
131
132 void
133 grn_alloc_info_fin(void)
134 {
135 CRITICAL_SECTION_FIN(grn_alloc_info_lock);
136 }
137
138 static inline void
139 grn_alloc_info_set_backtrace(char *buffer, size_t size)
140 {
141 # ifdef HAVE_BACKTRACE
142 # define N_TRACE_LEVEL 100
143 static void *trace[N_TRACE_LEVEL];
144 char **symbols;
145 int i, n, rest;
146
147 rest = size;
148 n = backtrace(trace, N_TRACE_LEVEL);
149 symbols = backtrace_symbols(trace, n);
150 if (symbols) {
151 for (i = 0; i < n; i++) {
152 int symbol_length;
153
154 symbol_length = strlen(symbols[i]);
155 if (symbol_length + 2 > rest) {
156 break;
157 }
158 grn_memcpy(buffer, symbols[i], symbol_length);
159 buffer += symbol_length;
160 rest -= symbol_length;
161 buffer[0] = '\n';
162 buffer++;
163 rest--;
164 buffer[0] = '\0';
165 rest--;
166 }
167 free(symbols);
168 } else {
169 buffer[0] = '\0';
170 }
171 # undef N_TRACE_LEVEL
172 # else /* HAVE_BACKTRACE */
173 buffer[0] = '\0';
174 # endif /* HAVE_BACKTRACE */
175 }
176
177 static inline void
178 grn_alloc_info_add(
179 void *address, size_t size, const char *file, int line, const char *func)
180 {
181 grn_ctx *ctx;
182 grn_alloc_info *new_alloc_info;
183
184 ctx = &grn_gctx;
185 if (!ctx->impl) {
186 return;
187 }
188
189 CRITICAL_SECTION_ENTER(grn_alloc_info_lock);
190 new_alloc_info = malloc(sizeof(grn_alloc_info));
191 if (new_alloc_info) {
192 new_alloc_info->address = address;
193 new_alloc_info->size = size;
194 new_alloc_info->freed = false;
195 grn_alloc_info_set_backtrace(new_alloc_info->alloc_backtrace,
196 sizeof(new_alloc_info->alloc_backtrace));
197 if (file) {
198 new_alloc_info->file = strdup(file);
199 } else {
200 new_alloc_info->file = NULL;
201 }
202 new_alloc_info->line = line;
203 if (func) {
204 new_alloc_info->func = strdup(func);
205 } else {
206 new_alloc_info->func = NULL;
207 }
208 new_alloc_info->next = ctx->impl->alloc_info;
209 ctx->impl->alloc_info = new_alloc_info;
210 }
211 CRITICAL_SECTION_LEAVE(grn_alloc_info_lock);
212 }
213
214 static inline void
215 grn_alloc_info_change(void *old_address, void *new_address, size_t size)
216 {
217 grn_ctx *ctx;
218 grn_alloc_info *alloc_info;
219
220 ctx = &grn_gctx;
221 if (!ctx->impl) {
222 return;
223 }
224
225 CRITICAL_SECTION_ENTER(grn_alloc_info_lock);
226 alloc_info = ctx->impl->alloc_info;
227 for (; alloc_info; alloc_info = alloc_info->next) {
228 if (alloc_info->address == old_address) {
229 alloc_info->address = new_address;
230 alloc_info->size = size;
231 grn_alloc_info_set_backtrace(alloc_info->alloc_backtrace,
232 sizeof(alloc_info->alloc_backtrace));
233 }
234 }
235 CRITICAL_SECTION_LEAVE(grn_alloc_info_lock);
236 }
237
238 void
239 grn_alloc_info_dump(grn_ctx *ctx)
240 {
241 if (!ctx) {
242 return;
243 }
244 if (!ctx->impl) {
245 return;
246 }
247
248 int i = 0;
249 size_t total = 0;
250 grn_alloc_info *alloc_info = ctx->impl->alloc_info;
251 for (; alloc_info; alloc_info = alloc_info->next) {
252 if (alloc_info->freed) {
253 printf("address[%d][freed]: %p(%" GRN_FMT_SIZE ")\n",
254 i,
255 alloc_info->address,
256 alloc_info->size);
257 } else {
258 printf("address[%d][not-freed]: %p(%" GRN_FMT_SIZE "): %s:%d: %s()\n%s",
259 i,
260 alloc_info->address,
261 alloc_info->size,
262 alloc_info->file ? alloc_info->file : "(unknown)",
263 alloc_info->line,
264 alloc_info->func ? alloc_info->func : "(unknown)",
265 alloc_info->alloc_backtrace);
266 total += alloc_info->size;
267 }
268 i++;
269 }
270 printf("total: %" GRN_FMT_SIZE ":%d\n", total, i);
271 }
272
273 static inline void
274 grn_alloc_info_check(grn_ctx *ctx, void *address)
275 {
276 grn_alloc_info *alloc_info;
277
278 if (!grn_gctx.impl) {
279 return;
280 }
281 /* grn_alloc_info_dump(ctx); */
282
283 CRITICAL_SECTION_ENTER(grn_alloc_info_lock);
284 alloc_info = grn_gctx.impl->alloc_info;
285 for (; alloc_info; alloc_info = alloc_info->next) {
286 if (alloc_info->address == address) {
287 if (alloc_info->freed) {
288 GRN_LOG(ctx,
289 GRN_LOG_WARNING,
290 "double free: %p(%" GRN_FMT_SIZE "):\n"
291 "alloc backtrace:\n"
292 "%sfree backtrace:\n"
293 "%s",
294 alloc_info->address,
295 alloc_info->size,
296 alloc_info->alloc_backtrace,
297 alloc_info->free_backtrace);
298 } else {
299 alloc_info->freed = true;
300 grn_alloc_info_set_backtrace(alloc_info->free_backtrace,
301 sizeof(alloc_info->free_backtrace));
302 }
303 break;
304 }
305 }
306 CRITICAL_SECTION_LEAVE(grn_alloc_info_lock);
307 }
308
309 void
310 grn_alloc_info_free(grn_ctx *ctx)
311 {
312 grn_alloc_info *alloc_info;
313
314 if (!ctx) {
315 return;
316 }
317 if (!ctx->impl) {
318 return;
319 }
320
321 alloc_info = ctx->impl->alloc_info;
322 while (alloc_info) {
323 grn_alloc_info *current_alloc_info = alloc_info;
324 alloc_info = alloc_info->next;
325 current_alloc_info->next = NULL;
326 free(current_alloc_info->file);
327 free(current_alloc_info->func);
328 free(current_alloc_info);
329 }
330 ctx->impl->alloc_info = NULL;
331 }
332
333 #else
334 void
335 grn_alloc_info_initcxx . . $ grn_alloc_info_init(49f6e7a06ebc5aa8).(void)
336 {
337 }
338
339 void
340 grn_alloc_info_fincxx . . $ grn_alloc_info_fin(49f6e7a06ebc5aa8).(void)
341 {
342 }
343
344 # define grn_alloc_info_addcxx . . $ `lib/alloc.c:344:11`!(address, size, file, line, func)
345 # define grn_alloc_info_changecxx . . $ `lib/alloc.c:345:11`!(old_address, new_address, size)
346 # define grn_alloc_info_checkcxx . . $ `lib/alloc.c:346:11`!(ctx, address)
347
348 void
349 grn_alloc_info_dumpcxx . . $ grn_alloc_info_dump(b578cd6e53d5fe91).(grn_ctxcxx . . $ grn_ctx# *ctx)
350 {
351 }
352
353 void
354 grn_alloc_info_freecxx . . $ grn_alloc_info_free(b578cd6e53d5fe91).(grn_ctxcxx . . $ grn_ctx# *ctx)
355 {
356 }
357 #endif
358
359 #define GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`! (1U << 22)
360 #define GRN_CTX_SEGMENT_MASKcxx . . $ `lib/alloc.c:360:9`! (GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`! - 1)
361
362 #define GRN_CTX_SEGMENT_WORDcxx . . $ `lib/alloc.c:362:9`! (1U << 31)
363 #define GRN_CTX_SEGMENT_VLENcxx . . $ `lib/alloc.c:363:9`! (1U << 30)
364 #define GRN_CTX_SEGMENT_LIFOcxx . . $ `lib/alloc.c:364:9`! (1U << 29)
365 #define GRN_CTX_SEGMENT_DIRTYcxx . . $ `lib/alloc.c:365:9`! (1U << 28)
366
367 void
368 grn_alloc_init_ctx_implcxx . . $ grn_alloc_init_ctx_impl(b578cd6e53d5fe91).(grn_ctxcxx . . $ grn_ctx# *ctx)
369 {
370 #ifdef GRN_WITH_MEMORY_DEBUG
371 ctx->impl->alloc_info = NULL;
372 #endif
373 }
374
375 void
376 grn_alloc_fin_ctx_implcxx . . $ grn_alloc_fin_ctx_impl(b578cd6e53d5fe91).(grn_ctxcxx . . $ grn_ctx# *ctx)
377 {
378 int i;
379 grn_io_mapinfocxx . . $ grn_io_mapinfo# *mi;
380 for (i = 0, mi = ctx->implcxx . . $ _grn_ctx#impl.->segscxx . . $ _grn_ctx_impl#segs.; i < GRN_CTX_N_SEGMENTScxx . . $ `lib/grn_ctx_impl.h:51:9`!; i++, mi++) {
381 if (mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.) {
382 // GRN_LOG(ctx, GRN_LOG_NOTICE, "unmap in ctx_fin(%d,%d,%d)", i,
383 // (mi->count & GRN_CTX_SEGMENT_MASK), mi->nref);
384 if (mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. & GRN_CTX_SEGMENT_VLENcxx . . $ `lib/alloc.c:363:9`!) {
385 grn_io_anon_unmapcxx . . $ grn_io_anon_unmap(e34ce909368ead97).(ctx,
386 mi,
387 (size_tcxx . . $ size_t#)((int64_tcxx . . $ int64_t#)(mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref.) * grn_pagesizecxx . . $ grn_pagesize.));
388 } else {
389 grn_io_anon_unmapcxx . . $ grn_io_anon_unmap(e34ce909368ead97).(ctx, mi, GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`!);
390 }
391 }
392 }
393 }
394
395 #define ALIGN_SIZEcxx . . $ `lib/alloc.c:395:9`! (1 << 3)
396 #define ALIGN_MASKcxx . . $ `lib/alloc.c:396:9`! ((size_t)(ALIGN_SIZEcxx . . $ `lib/alloc.c:395:9`! - 1))
397 #define GRN_CTX_ALLOC_CLEARcxx . . $ `lib/alloc.c:397:9`! 1
398
399 static void *
400 grn_ctx_alloccxx . . $ grn_ctx_alloc(5d4796c6b7784819).(grn_ctxcxx . . $ grn_ctx# *ctx,
401 size_tcxx . . $ size_t# size,
402 int flags,
403 const char *file,
404 int line,
405 const char *func)
406 {
407 void *res = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
408 if (!ctx) {
409 return res;
410 }
411 if (!ctx->implcxx . . $ _grn_ctx#impl.) {
412 if (ERRPcxx . . $ `lib/grn_error.h:149:9`!(ctx, GRN_ERRORcxx . . $ `lib/grn_error.h:33:9`!)) {
413 return res;
414 }
415 }
416 CRITICAL_SECTION_ENTERcxx . . $ `lib/grn.h:227:11`!(ctx->impl->lock);
417 {
418 int32_tcxx . . $ int32_t# i;
419 int32_tcxx . . $ int32_t# *header;
420 grn_io_mapinfocxx . . $ grn_io_mapinfo# *mi;
421 size = ((size + ALIGN_MASKcxx . . $ `lib/alloc.c:396:9`!) & ~ALIGN_MASKcxx . . $ `lib/alloc.c:396:9`!) + ALIGN_SIZEcxx . . $ `lib/alloc.c:395:9`!;
422 if (size > GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`!) {
423 uint64_tcxx . . $ uint64_t# npages =
424 (uint64_tcxx . . $ uint64_t#)(((int64_tcxx . . $ int64_t#)size + (grn_pagesizecxx . . $ grn_pagesize. - 1)) / grn_pagesizecxx . . $ grn_pagesize.);
425 size_tcxx . . $ size_t# aligned_size;
426 if (npages >= (1LL << 32)) {
427 MERRcxx . . $ `lib/grn_error.h:158:9`!("too long request size=%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`!, size);
428 goto exit;
429 }
430 for (i = 0, mi = ctx->implcxx . . $ _grn_ctx#impl.->segscxx . . $ _grn_ctx_impl#segs.;; i++, mi++) {
431 if (i >= GRN_CTX_N_SEGMENTScxx . . $ `lib/grn_ctx_impl.h:51:9`!) {
432 MERRcxx . . $ `lib/grn_error.h:158:9`!("all segments are full");
433 goto exit;
434 }
435 if (!mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.) {
436 break;
437 }
438 }
439 aligned_size = (size_tcxx . . $ size_t#)(grn_pagesizecxx . . $ grn_pagesize. * ((int64_tcxx . . $ int64_t#)npages));
440 if (!grn_io_anon_mapcxx . . $ grn_io_anon_map(9de643cca07c394).(ctx, mi, aligned_size)) {
441 goto exit;
442 }
443 /* GRN_LOG(ctx, GRN_LOG_NOTICE, "map i=%d (%d)", i, npages *
444 * grn_pagesize); */
445 mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. = (uint32_tcxx . . $ uint32_t#)npages;
446 mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. = GRN_CTX_SEGMENT_VLENcxx . . $ `lib/alloc.c:363:9`!;
447 ctx->implcxx . . $ _grn_ctx#impl.->currsegcxx . . $ _grn_ctx_impl#currseg. = -1;
448 header = mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.;
449 header[0] = i;
450 header[1] = (int32_tcxx . . $ int32_t#)size;
451 } else {
452 i = ctx->implcxx . . $ _grn_ctx#impl.->currsegcxx . . $ _grn_ctx_impl#currseg.;
453 if (i >= 0) {
454 mi = &ctx->implcxx . . $ _grn_ctx#impl.->segscxx . . $ _grn_ctx_impl#segs.[i];
455 }
456 if (i < 0 || size + mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. > GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`!) {
457 for (i = 0, mi = ctx->implcxx . . $ _grn_ctx#impl.->segscxx . . $ _grn_ctx_impl#segs.;; i++, mi++) {
458 if (i >= GRN_CTX_N_SEGMENTScxx . . $ `lib/grn_ctx_impl.h:51:9`!) {
459 MERRcxx . . $ `lib/grn_error.h:158:9`!("all segments are full");
460 goto exit;
461 }
462 if (!mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.) {
463 break;
464 }
465 }
466 if (!grn_io_anon_mapcxx . . $ grn_io_anon_map(9de643cca07c394).(ctx, mi, GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`!)) {
467 goto exit;
468 }
469 /* GRN_LOG(ctx, GRN_LOG_NOTICE, "map i=%d", i); */
470 mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. = 0;
471 mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. = GRN_CTX_SEGMENT_WORDcxx . . $ `lib/alloc.c:362:9`!;
472 ctx->implcxx . . $ _grn_ctx#impl.->currsegcxx . . $ _grn_ctx_impl#currseg. = i;
473 }
474 header = (int32_tcxx . . $ int32_t# *)((bytecxx . . $ byte# *)mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. + mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref.);
475 mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. += (uint32_tcxx . . $ uint32_t#)size;
476 mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count.++;
477 header[0] = i;
478 header[1] = (int32_tcxx . . $ int32_t#)size;
479 if ((flags & GRN_CTX_ALLOC_CLEARcxx . . $ `lib/alloc.c:397:9`!) &&
480 (mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. & GRN_CTX_SEGMENT_DIRTYcxx . . $ `lib/alloc.c:365:9`!) && (size > ALIGN_SIZEcxx . . $ `lib/alloc.c:395:9`!)) {
481 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&header[2], 0, size - ALIGN_SIZEcxx . . $ `lib/alloc.c:395:9`!);
482 }
483 }
484 /*
485 {
486 char g = (ctx == &grn_gctx) ? 'g' : ' ';
487 GRN_LOG(ctx, GRN_LOG_NOTICE, "+%c(%p) %s:%d(%s) (%d:%d)%p mi(%d:%d)", g,
488 ctx, file, line, func, header[0], header[1], &header[2], mi->nref,
489 (mi->count & GRN_CTX_SEGMENT_MASK));
490 }
491 */
492 res = &header[2];
493 }
494 exit:
495 CRITICAL_SECTION_LEAVEcxx . . $ `lib/grn.h:228:11`!(ctx->impl->lock);
496 return res;
497 }
498
499 void *
500 grn_ctx_malloccxx . . $ grn_ctx_malloc(464175534efa3de7).(
501 grn_ctxcxx . . $ grn_ctx# *ctx, size_tcxx . . $ size_t# size, const char *file, int line, const char *func)
502 {
503 return grn_ctx_alloccxx . . $ grn_ctx_alloc(5d4796c6b7784819).(ctx, size, 0, file, line, func);
504 }
505
506 void *
507 grn_ctx_calloccxx . . $ grn_ctx_calloc(464175534efa3de7).(
508 grn_ctxcxx . . $ grn_ctx# *ctx, size_tcxx . . $ size_t# size, const char *file, int line, const char *func)
509 {
510 return grn_ctx_alloccxx . . $ grn_ctx_alloc(5d4796c6b7784819).(ctx, size, GRN_CTX_ALLOC_CLEARcxx . . $ `lib/alloc.c:397:9`!, file, line, func);
511 }
512
513 void *
514 grn_ctx_realloccxx . . $ grn_ctx_realloc(81c005e7023c2b15).(grn_ctxcxx . . $ grn_ctx# *ctx,
515 void *ptr,
516 size_tcxx . . $ size_t# size,
517 const char *file,
518 int line,
519 const char *func)
520 {
521 void *res = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
522 if (size) {
523 /* todo : expand if possible */
524 res = grn_ctx_alloccxx . . $ grn_ctx_alloc(5d4796c6b7784819).(ctx, size, 0, file, line, func);
525 if (res && ptr) {
526 int32_tcxx . . $ int32_t# *header = &((int32_tcxx . . $ int32_t# *)ptr)[-2];
527 size_tcxx . . $ size_t# size_ = (size_tcxx . . $ size_t#)(header[1]);
528 grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(res, ptr, size_ > size ? size : size_);
529 grn_ctx_freecxx . . $ grn_ctx_free(7cadacd43fda8113).(ctx, ptr, file, line, func);
530 }
531 } else {
532 grn_ctx_freecxx . . $ grn_ctx_free(7cadacd43fda8113).(ctx, ptr, file, line, func);
533 }
534 return res;
535 }
536
537 char *
538 grn_ctx_strdupcxx . . $ grn_ctx_strdup(2ef96f5eb2f74054).(
539 grn_ctxcxx . . $ grn_ctx# *ctx, const char *s, const char *file, int line, const char *func)
540 {
541 void *res = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
542 if (s) {
543 size_tcxx . . $ size_t# size = strlencxx . . $ strlen(751346f8406fd082).(s) + 1;
544 if ((res = grn_ctx_alloccxx . . $ grn_ctx_alloc(5d4796c6b7784819).(ctx, size, 0, file, line, func))) {
545 grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(res, s, size);
546 }
547 }
548 return res;
549 }
550
551 void
552 grn_ctx_freecxx . . $ grn_ctx_free(7cadacd43fda8113).(
553 grn_ctxcxx . . $ grn_ctx# *ctx, void *ptr, const char *file, int line, const char *func)
554 {
555 if (!ctx) {
556 return;
557 }
558 if (!ctx->implcxx . . $ _grn_ctx#impl.) {
559 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, "ctx without impl passed.");
560 return;
561 }
562 CRITICAL_SECTION_ENTERcxx . . $ `lib/grn.h:227:11`!(ctx->impl->lock);
563 if (ptr) {
564 int32_tcxx . . $ int32_t# *header = &((int32_tcxx . . $ int32_t# *)ptr)[-2];
565
566 if (header[0] >= GRN_CTX_N_SEGMENTScxx . . $ `lib/grn_ctx_impl.h:51:9`!) {
567 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT,
568 "invalid ptr passed. ptr=%p seg=%d",
569 ptr,
570 *header);
571 goto exit;
572 }
573 /*
574 {
575 int32_t i = header[0];
576 char c = 'X', g = (ctx == &grn_gctx) ? 'g' : ' ';
577 grn_io_mapinfo *mi = &ctx->impl->segs[i];
578 if (!(mi->count & GRN_CTX_SEGMENT_VLEN) &&
579 mi->map <= (void *)header && (char *)header < ((char *)mi->map +
580 GRN_CTX_SEGMENT_SIZE)) { c = '-'; } GRN_LOG(ctx, GRN_LOG_NOTICE, "%c%c(%p)
581 %s:%d(%s) (%d:%d)%p mi(%d:%d)", c, g, ctx, file, line, func, header[0],
582 header[1], &header[2], mi->nref, (mi->count & GRN_CTX_SEGMENT_MASK));
583 }
584 */
585 {
586 int32_tcxx . . $ int32_t# i = header[0];
587 grn_io_mapinfocxx . . $ grn_io_mapinfo# *mi = &ctx->implcxx . . $ _grn_ctx#impl.->segscxx . . $ _grn_ctx_impl#segs.[i];
588 if (mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. & GRN_CTX_SEGMENT_VLENcxx . . $ `lib/alloc.c:363:9`!) {
589 if (mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. != header) {
590 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT,
591 "invalid ptr passed.. ptr=%p seg=%d",
592 ptr,
593 i);
594 goto exit;
595 }
596 // GRN_LOG(ctx, GRN_LOG_NOTICE, "umap i=%d (%d)", i, mi->nref *
597 // grn_pagesize);
598 grn_io_anon_unmapcxx . . $ grn_io_anon_unmap(e34ce909368ead97).(ctx,
599 mi,
600 (size_tcxx . . $ size_t#)((int64_tcxx . . $ int64_t#)(mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref.) * grn_pagesizecxx . . $ grn_pagesize.));
601 mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
602 } else {
603 if (!mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.) {
604 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT,
605 "invalid ptr passed... ptr=%p seg=%d",
606 ptr,
607 i);
608 goto exit;
609 }
610 mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count.--;
611 if (!(mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. & GRN_CTX_SEGMENT_MASKcxx . . $ `lib/alloc.c:360:9`!)) {
612 // GRN_LOG(ctx, GRN_LOG_NOTICE, "umap i=%d", i);
613 if (i == ctx->implcxx . . $ _grn_ctx#impl.->currsegcxx . . $ _grn_ctx_impl#currseg.) {
614 mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. |= GRN_CTX_SEGMENT_DIRTYcxx . . $ `lib/alloc.c:365:9`!;
615 mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. = 0;
616 } else {
617 grn_io_anon_unmapcxx . . $ grn_io_anon_unmap(e34ce909368ead97).(ctx, mi, GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`!);
618 mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
619 }
620 }
621 }
622 }
623 }
624 exit:
625 CRITICAL_SECTION_LEAVEcxx . . $ `lib/grn.h:228:11`!(ctx->impl->lock);
626 }
627
628 void *
629 grn_ctx_alloc_lifocxx . . $ grn_ctx_alloc_lifo(464175534efa3de7).(
630 grn_ctxcxx . . $ grn_ctx# *ctx, size_tcxx . . $ size_t# size, const char *file, int line, const char *func)
631 {
632 if (!ctx) {
633 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
634 }
635 if (!ctx->implcxx . . $ _grn_ctx#impl.) {
636 if (ERRPcxx . . $ `lib/grn_error.h:149:9`!(ctx, GRN_ERRORcxx . . $ `lib/grn_error.h:33:9`!)) {
637 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
638 }
639 }
640 {
641 int32_tcxx . . $ int32_t# i = ctx->implcxx . . $ _grn_ctx#impl.->lifosegcxx . . $ _grn_ctx_impl#lifoseg.;
642 grn_io_mapinfocxx . . $ grn_io_mapinfo# *mi = &ctx->implcxx . . $ _grn_ctx#impl.->segscxx . . $ _grn_ctx_impl#segs.[i];
643 if (size > GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`!) {
644 uint64_tcxx . . $ uint64_t# npages =
645 (uint64_tcxx . . $ uint64_t#)(((int64_tcxx . . $ int64_t#)size + (grn_pagesizecxx . . $ grn_pagesize. - 1)) / grn_pagesizecxx . . $ grn_pagesize.);
646 size_tcxx . . $ size_t# aligned_size;
647 if (npages >= (1LL << 32)) {
648 MERRcxx . . $ `lib/grn_error.h:158:9`!("too long request size=%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`!, size);
649 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
650 }
651 for (;;) {
652 if (++i >= GRN_CTX_N_SEGMENTScxx . . $ `lib/grn_ctx_impl.h:51:9`!) {
653 MERRcxx . . $ `lib/grn_error.h:158:9`!("all segments are full");
654 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
655 }
656 mi++;
657 if (!mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.) {
658 break;
659 }
660 }
661 aligned_size = (size_tcxx . . $ size_t#)(grn_pagesizecxx . . $ grn_pagesize. * (int64_tcxx . . $ int64_t#)npages);
662 if (!grn_io_anon_mapcxx . . $ grn_io_anon_map(9de643cca07c394).(ctx, mi, aligned_size)) {
663 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
664 }
665 mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. = (uint32_tcxx . . $ uint32_t#)npages;
666 mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. = GRN_CTX_SEGMENT_VLENcxx . . $ `lib/alloc.c:363:9`! | GRN_CTX_SEGMENT_LIFOcxx . . $ `lib/alloc.c:364:9`!;
667 ctx->implcxx . . $ _grn_ctx#impl.->lifosegcxx . . $ _grn_ctx_impl#lifoseg. = i;
668 return mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.;
669 } else {
670 size = (size + ALIGN_MASKcxx . . $ `lib/alloc.c:396:9`!) & ~ALIGN_MASKcxx . . $ `lib/alloc.c:396:9`!;
671 if (i < 0 || (mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. & GRN_CTX_SEGMENT_VLENcxx . . $ `lib/alloc.c:363:9`!) ||
672 size + mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. > GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`!) {
673 for (;;) {
674 if (++i >= GRN_CTX_N_SEGMENTScxx . . $ `lib/grn_ctx_impl.h:51:9`!) {
675 MERRcxx . . $ `lib/grn_error.h:158:9`!("all segments are full");
676 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
677 }
678 if (!(++mi)->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.) {
679 break;
680 }
681 }
682 if (!grn_io_anon_mapcxx . . $ grn_io_anon_map(9de643cca07c394).(ctx, mi, GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`!)) {
683 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
684 }
685 mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. = 0;
686 mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. = GRN_CTX_SEGMENT_WORDcxx . . $ `lib/alloc.c:362:9`! | GRN_CTX_SEGMENT_LIFOcxx . . $ `lib/alloc.c:364:9`!;
687 ctx->implcxx . . $ _grn_ctx#impl.->lifosegcxx . . $ _grn_ctx_impl#lifoseg. = i;
688 }
689 {
690 uint32_tcxx . . $ uint32_t# u = mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref.;
691 mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. += (uint32_tcxx . . $ uint32_t#)size;
692 return (bytecxx . . $ byte# *)mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. + u;
693 }
694 }
695 }
696 }
697
698 void
699 grn_ctx_free_lifocxx . . $ grn_ctx_free_lifo(7cadacd43fda8113).(
700 grn_ctxcxx . . $ grn_ctx# *ctx, void *ptr, const char *file, int line, const char *func)
701 {
702 if (!ctx) {
703 return;
704 }
705 if (!ctx->implcxx . . $ _grn_ctx#impl.) {
706 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, "ctx without impl passed.");
707 return;
708 }
709 {
710 int32_tcxx . . $ int32_t# i = ctx->implcxx . . $ _grn_ctx#impl.->lifosegcxx . . $ _grn_ctx_impl#lifoseg., done = 0;
711 grn_io_mapinfocxx . . $ grn_io_mapinfo# *mi = &ctx->implcxx . . $ _grn_ctx#impl.->segscxx . . $ _grn_ctx_impl#segs.[i];
712 if (i < 0) {
713 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, "lifo buffer is void");
714 return;
715 }
716 for (; i >= 0; i--, mi--) {
717 if (!(mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. & GRN_CTX_SEGMENT_LIFOcxx . . $ `lib/alloc.c:364:9`!)) {
718 continue;
719 }
720 if (done) {
721 break;
722 }
723 if (mi->countcxx . . $ $anonymous_type_fee031cfc08e20b0_3#count. & GRN_CTX_SEGMENT_VLENcxx . . $ `lib/alloc.c:363:9`!) {
724 if (mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. == ptr) {
725 done = 1;
726 }
727 grn_io_anon_unmapcxx . . $ grn_io_anon_unmap(e34ce909368ead97).(ctx,
728 mi,
729 (size_tcxx . . $ size_t#)((int64_tcxx . . $ int64_t#)(mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref.) * grn_pagesizecxx . . $ grn_pagesize.));
730 mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
731 } else {
732 if (mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. == ptr) {
733 done = 1;
734 } else {
735 if (mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. < ptr && ptr < (void *)((bytecxx . . $ byte# *)mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. + mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref.)) {
736 mi->nrefcxx . . $ $anonymous_type_fee031cfc08e20b0_3#nref. = (uint32_tcxx . . $ uint32_t#)((uintptr_tcxx . . $ uintptr_t#)ptr - (uintptr_tcxx . . $ uintptr_t#)mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map.);
737 break;
738 }
739 }
740 grn_io_anon_unmapcxx . . $ grn_io_anon_unmap(e34ce909368ead97).(ctx, mi, GRN_CTX_SEGMENT_SIZEcxx . . $ `lib/alloc.c:359:9`!);
741 mi->mapcxx . . $ $anonymous_type_fee031cfc08e20b0_3#map. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
742 }
743 }
744 ctx->implcxx . . $ _grn_ctx#impl.->lifosegcxx . . $ _grn_ctx_impl#lifoseg. = i;
745 }
746 }
747
748 void *
749 grn_malloccxx . . $ grn_malloc(464175534efa3de7).(
750 grn_ctxcxx . . $ grn_ctx# *ctx, size_tcxx . . $ size_t# size, const char *file, int line, const char *func)
751 {
752 if (grn_fail_malloc_should_failcxx . . $ grn_fail_malloc_should_fail(8ab3715db869f8d3).(size, file, line, func)) {
753 return grn_malloc_failcxx . . $ grn_malloc_fail(464175534efa3de7).(ctx, size, file, line, func);
754 } else {
755 return grn_malloc_defaultcxx . . $ grn_malloc_default(464175534efa3de7).(ctx, size, file, line, func);
756 }
757 }
758
759 void *
760 grn_calloccxx . . $ grn_calloc(464175534efa3de7).(
761 grn_ctxcxx . . $ grn_ctx# *ctx, size_tcxx . . $ size_t# size, const char *file, int line, const char *func)
762 {
763 if (grn_fail_malloc_should_failcxx . . $ grn_fail_malloc_should_fail(8ab3715db869f8d3).(size, file, line, func)) {
764 return grn_calloc_failcxx . . $ grn_calloc_fail(464175534efa3de7).(ctx, size, file, line, func);
765 } else {
766 return grn_calloc_defaultcxx . . $ grn_calloc_default(464175534efa3de7).(ctx, size, file, line, func);
767 }
768 }
769
770 void *
771 grn_realloccxx . . $ grn_realloc(81c005e7023c2b15).(grn_ctxcxx . . $ grn_ctx# *ctx,
772 void *ptr,
773 size_tcxx . . $ size_t# size,
774 const char *file,
775 int line,
776 const char *func)
777 {
778 if (grn_fail_malloc_should_failcxx . . $ grn_fail_malloc_should_fail(8ab3715db869f8d3).(size, file, line, func)) {
779 return grn_realloc_failcxx . . $ grn_realloc_fail(81c005e7023c2b15).(ctx, ptr, size, file, line, func);
780 } else {
781 return grn_realloc_defaultcxx . . $ grn_realloc_default(81c005e7023c2b15).(ctx, ptr, size, file, line, func);
782 }
783 }
784
785 char *
786 grn_strdupcxx . . $ grn_strdup(2ef96f5eb2f74054).(grn_ctxcxx . . $ grn_ctx# *ctx,
787 const char *string,
788 const char *file,
789 int line,
790 const char *func)
791 {
792 /* TODO: strlen(string) when we use size in grn_fail_malloc_should_fail(). */
793 size_tcxx . . $ size_t# size = 0;
794 if (grn_fail_malloc_should_failcxx . . $ grn_fail_malloc_should_fail(8ab3715db869f8d3).(size, file, line, func)) {
795 return grn_strdup_failcxx . . $ grn_strdup_fail(2ef96f5eb2f74054).(ctx, string, file, line, func);
796 } else {
797 return grn_strdup_defaultcxx . . $ grn_strdup_default(2ef96f5eb2f74054).(ctx, string, file, line, func);
798 }
799 }
800
801 void
802 grn_freecxx . . $ grn_free(7cadacd43fda8113).(grn_ctxcxx . . $ grn_ctx# *ctx, void *ptr, const char *file, int line, const char *func)
803 {
804 grn_free_defaultcxx . . $ grn_free_default(7cadacd43fda8113).(ctx, ptr, file, line, func);
805 }
806
807 void *
808 grn_malloc_defaultcxx . . $ grn_malloc_default(464175534efa3de7).(
809 grn_ctxcxx . . $ grn_ctx# *ctx, size_tcxx . . $ size_t# size, const char *file, int line, const char *func)
810 {
811 if (!ctx) {
812 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
813 }
814 {
815 void *res = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(size);
816 if (res) {
817 GRN_ADD_ALLOC_COUNTcxx . . $ `lib/alloc.c:38:9`!(1);
818 grn_alloc_info_addcxx . . $ `lib/alloc.c:344:11`!(res, size, file, line, func);
819 } else {
820 if (!(res = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(size))) {
821 MERRcxx . . $ `lib/grn_error.h:158:9`!("[alloc][malloc] failed to allocate: "
822 "size:%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ", "
823 "file:%s, "
824 "line:%d, "
825 "alloc_count:%u, "
826 "message:%s",
827 size,
828 file,
829 line,
830 alloc_count,
831 grn_error_get_current_system_message());
832 } else {
833 GRN_ADD_ALLOC_COUNTcxx . . $ `lib/alloc.c:38:9`!(1);
834 grn_alloc_info_addcxx . . $ `lib/alloc.c:344:11`!(res, size, file, line, func);
835 }
836 }
837 return res;
838 }
839 }
840
841 void *
842 grn_calloc_defaultcxx . . $ grn_calloc_default(464175534efa3de7).(
843 grn_ctxcxx . . $ grn_ctx# *ctx, size_tcxx . . $ size_t# size, const char *file, int line, const char *func)
844 {
845 if (!ctx) {
846 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
847 }
848 {
849 void *res = calloccxx . . $ calloc(63a7bf2941bfcd7a).(size, 1);
850 if (res) {
851 GRN_ADD_ALLOC_COUNTcxx . . $ `lib/alloc.c:38:9`!(1);
852 grn_alloc_info_addcxx . . $ `lib/alloc.c:344:11`!(res, size, file, line, func);
853 } else {
854 if (!(res = calloccxx . . $ calloc(63a7bf2941bfcd7a).(size, 1))) {
855 MERRcxx . . $ `lib/grn_error.h:158:9`!("[alloc][calloc] failed to allocate: "
856 "size:%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ", "
857 "file:%s, "
858 "line:%d, "
859 "alloc_count:%u, "
860 "message:%s",
861 size,
862 file,
863 line,
864 alloc_count,
865 grn_error_get_current_system_message());
866 } else {
867 GRN_ADD_ALLOC_COUNTcxx . . $ `lib/alloc.c:38:9`!(1);
868 grn_alloc_info_addcxx . . $ `lib/alloc.c:344:11`!(res, size, file, line, func);
869 }
870 }
871 return res;
872 }
873 }
874
875 void
876 grn_free_defaultcxx . . $ grn_free_default(7cadacd43fda8113).(
877 grn_ctxcxx . . $ grn_ctx# *ctx, void *ptr, const char *file, int line, const char *func)
878 {
879 if (!ctx) {
880 return;
881 }
882 grn_alloc_info_checkcxx . . $ `lib/alloc.c:346:11`!(ctx, ptr);
883 {
884 if (ptr) {
885 GRN_ADD_ALLOC_COUNTcxx . . $ `lib/alloc.c:38:9`!(-1);
886 } else {
887 GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx,
888 GRN_LOG_ALERT,
889 "free fail (%p) (%s:%d) <%u>",
890 ptr,
891 file,
892 line,
893 alloc_count);
894 }
895 freecxx . . $ free(5d22bdacc48458e8).(ptr);
896 }
897 }
898
899 void *
900 grn_realloc_defaultcxx . . $ grn_realloc_default(81c005e7023c2b15).(grn_ctxcxx . . $ grn_ctx# *ctx,
901 void *ptr,
902 size_tcxx . . $ size_t# size,
903 const char *file,
904 int line,
905 const char *func)
906 {
907 void *res;
908 if (!ctx) {
909 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
910 }
911 if (size) {
912 if (!(res = realloccxx . . $ realloc(417ab160f6baadc6).(ptr, size))) {
913 if (!(res = realloccxx . . $ realloc(417ab160f6baadc6).(ptr, size))) {
914 MERRcxx . . $ `lib/grn_error.h:158:9`!("[alloc][realloc] failed to allocate: "
915 "address:%p, "
916 "size:%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ", "
917 "file:%s, "
918 "line:%d, "
919 "alloc_count:%u, "
920 "message:%s",
921 ptr,
922 size,
923 file,
924 line,
925 alloc_count,
926 grn_error_get_current_system_message());
927 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
928 }
929 }
930 if (ptr) {
931 grn_alloc_info_changecxx . . $ `lib/alloc.c:345:11`!(ptr, res, size);
932 } else {
933 GRN_ADD_ALLOC_COUNTcxx . . $ `lib/alloc.c:38:9`!(1);
934 grn_alloc_info_addcxx . . $ `lib/alloc.c:344:11`!(res, size, file, line, func);
935 }
936 } else {
937 if (!ptr) {
938 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
939 }
940 grn_alloc_info_checkcxx . . $ `lib/alloc.c:346:11`!(ctx, ptr);
941 GRN_ADD_ALLOC_COUNTcxx . . $ `lib/alloc.c:38:9`!(-1);
942 freecxx . . $ free(5d22bdacc48458e8).(ptr);
943 res = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
944 }
945 return res;
946 }
947
948 char *
949 grn_strdup_defaultcxx . . $ grn_strdup_default(2ef96f5eb2f74054).(
950 grn_ctxcxx . . $ grn_ctx# *ctx, const char *s, const char *file, int line, const char *func)
951 {
952 if (!ctx) {
953 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
954 }
955 {
956 char *res = grn_strdup_rawcxx . . $ `include/groonga/portability.h:82:11`!(s);
957 if (res) {
958 GRN_ADD_ALLOC_COUNTcxx . . $ `lib/alloc.c:38:9`!(1);
959 grn_alloc_info_addcxx . . $ `lib/alloc.c:344:11`!(res, strlen(res) + 1, file, line, func);
960 } else {
961 if (!(res = grn_strdup_rawcxx . . $ `include/groonga/portability.h:82:11`!(s))) {
962 MERRcxx . . $ `lib/grn_error.h:158:9`!("[alloc][strdup] failed to duplicate: "
963 "address:%p, "
964 "file:%s, "
965 "line:%d, "
966 "alloc_count:%u, "
967 "message:%s",
968 s,
969 file,
970 line,
971 alloc_count,
972 grn_error_get_current_system_message());
973 } else {
974 GRN_ADD_ALLOC_COUNTcxx . . $ `lib/alloc.c:38:9`!(1);
975 grn_alloc_info_addcxx . . $ `lib/alloc.c:344:11`!(res, strlen(res) + 1, file, line, func);
976 }
977 }
978 return res;
979 }
980 }
981
982 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
983 grn_fail_malloc_should_failcxx . . $ grn_fail_malloc_should_fail(8ab3715db869f8d3).(size_tcxx . . $ size_t# size,
984 const char *file,
985 int line,
986 const char *func)
987 {
988 if (!grn_fail_malloc_enablecxx . . $ grn_fail_malloc_enable.) {
989 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
990 }
991
992 if (grn_fail_malloc_locationcxx . . $ grn_fail_malloc_location.) {
993 if (grn_fail_malloc_filecxx . . $ grn_fail_malloc_file.) {
994 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(file, grn_fail_malloc_filecxx . . $ grn_fail_malloc_file.) != 0) {
995 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
996 }
997 }
998 if (grn_fail_malloc_linecxx . . $ grn_fail_malloc_line. > 0) {
999 if (line != grn_fail_malloc_linecxx . . $ grn_fail_malloc_line.) {
1000 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
1001 }
1002 }
1003 if (grn_fail_malloc_funccxx . . $ grn_fail_malloc_func.) {
1004 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(func, grn_fail_malloc_funccxx . . $ grn_fail_malloc_func.) != 0) {
1005 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
1006 }
1007 }
1008 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
1009 }
1010
1011 if (grn_fail_malloc_probcxx . . $ grn_fail_malloc_prob. > 0.0 && grn_fail_malloc_probcxx . . $ grn_fail_malloc_prob. >= randcxx . . $ rand(b126dc7c1de90089).()) {
1012 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
1013 }
1014
1015 if (grn_fail_malloc_max_countcxx . . $ grn_fail_malloc_max_count. >= 0 &&
1016 alloc_countcxx . . $ alloc_count. >= (uint32_tcxx . . $ uint32_t#)grn_fail_malloc_max_countcxx . . $ grn_fail_malloc_max_count.) {
1017 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
1018 }
1019
1020 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
1021 }
1022
1023 void *
1024 grn_malloc_failcxx . . $ grn_malloc_fail(464175534efa3de7).(
1025 grn_ctxcxx . . $ grn_ctx# *ctx, size_tcxx . . $ size_t# size, const char *file, int line, const char *func)
1026 {
1027 MERRcxx . . $ `lib/grn_error.h:158:9`!("[alloc][fail][malloc] <%u>: <%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ">: %s:%d: %s",
1028 alloc_count,
1029 size,
1030 file,
1031 line,
1032 func);
1033 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1034 }
1035
1036 void *
1037 grn_calloc_failcxx . . $ grn_calloc_fail(464175534efa3de7).(
1038 grn_ctxcxx . . $ grn_ctx# *ctx, size_tcxx . . $ size_t# size, const char *file, int line, const char *func)
1039 {
1040 MERRcxx . . $ `lib/grn_error.h:158:9`!("[alloc][fail][calloc] <%u>: <%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ">: %s:%d: %s",
1041 alloc_count,
1042 size,
1043 file,
1044 line,
1045 func);
1046 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1047 }
1048
1049 void *
1050 grn_realloc_failcxx . . $ grn_realloc_fail(81c005e7023c2b15).(grn_ctxcxx . . $ grn_ctx# *ctx,
1051 void *ptr,
1052 size_tcxx . . $ size_t# size,
1053 const char *file,
1054 int line,
1055 const char *func)
1056 {
1057 MERRcxx . . $ `lib/grn_error.h:158:9`!("[alloc][fail][realloc] <%u>: <%p:%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ">: %s:%d: %s",
1058 alloc_count,
1059 ptr,
1060 size,
1061 file,
1062 line,
1063 func);
1064 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1065 }
1066
1067 char *
1068 grn_strdup_failcxx . . $ grn_strdup_fail(2ef96f5eb2f74054).(
1069 grn_ctxcxx . . $ grn_ctx# *ctx, const char *s, const char *file, int line, const char *func)
1070 {
1071 MERRcxx . . $ `lib/grn_error.h:158:9`!("[alloc][fail][strdup] <%u>: <%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! ">: %s:%d: %s: <%s>",
1072 alloc_count,
1073 s ? strlen(s) : 0,
1074 file,
1075 line,
1076 func,
1077 s ? s : "(null)");
1078 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1079 }