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"
21 #include "grn_alloc.h"
22 #include "grn_ctx_impl.h"
23
24 #ifdef HAVE_EXECINFO_H
25 # include <execinfo.h>
26 #endif
27
28 static uint32_t alloc_count = 0;
29
30 static bool grn_fail_malloc_enable = false;
31 static double grn_fail_malloc_prob = 0.0;
32 static bool grn_fail_malloc_location = false;
33 static char *grn_fail_malloc_func = NULL;
34 static char *grn_fail_malloc_file = NULL;
35 static int grn_fail_malloc_line = 0;
36 static int grn_fail_malloc_max_count = -1;
37
38 #define GRN_ADD_ALLOC_COUNT(count) \
39 do { \
40 uint32_t alloced; \
41 GRN_ATOMIC_ADD_EX(&alloc_count, count, alloced); \
42 } while (0)
43
44 uint32_t
45 grn_alloc_count(void)
46 {
47 return alloc_count;
48 }
49
50 void
51 grn_alloc_init_from_env(void)
52 {
53 {
54 char grn_fail_malloc_enable_env[GRN_ENV_BUFFER_SIZE];
55 grn_getenv("GRN_FAIL_MALLOC_ENABLE",
56 grn_fail_malloc_enable_env,
57 GRN_ENV_BUFFER_SIZE);
58 if (strcmp(grn_fail_malloc_enable_env, "yes") == 0) {
59 grn_fail_malloc_enable = true;
60 } else {
61 grn_fail_malloc_enable = false;
62 }
63 }
64 {
65 char grn_fail_malloc_prob_env[GRN_ENV_BUFFER_SIZE];
66 grn_getenv("GRN_FAIL_MALLOC_PROB",
67 grn_fail_malloc_prob_env,
68 GRN_ENV_BUFFER_SIZE);
69 if (grn_fail_malloc_prob_env[0]) {
70 char grn_fail_malloc_seed_env[GRN_ENV_BUFFER_SIZE];
71 grn_fail_malloc_prob = strtod(grn_fail_malloc_prob_env, 0) * RAND_MAX;
72 grn_getenv("GRN_FAIL_MALLOC_SEED",
73 grn_fail_malloc_seed_env,
74 GRN_ENV_BUFFER_SIZE);
75 if (grn_fail_malloc_seed_env[0]) {
76 srand((unsigned int)atoi(grn_fail_malloc_seed_env));
77 } else {
78 srand((unsigned int)time(NULL));
79 }
80 }
81 }
82 {
83 static char grn_fail_malloc_func_env[GRN_ENV_BUFFER_SIZE];
84 grn_getenv("GRN_FAIL_MALLOC_FUNC",
85 grn_fail_malloc_func_env,
86 GRN_ENV_BUFFER_SIZE);
87 if (grn_fail_malloc_func_env[0]) {
88 grn_fail_malloc_location = true;
89 grn_fail_malloc_func = grn_fail_malloc_func_env;
90 }
91 }
92 {
93 static char grn_fail_malloc_file_env[GRN_ENV_BUFFER_SIZE];
94 grn_getenv("GRN_FAIL_MALLOC_FILE",
95 grn_fail_malloc_file_env,
96 GRN_ENV_BUFFER_SIZE);
97 if (grn_fail_malloc_file_env[0]) {
98 grn_fail_malloc_location = true;
99 grn_fail_malloc_file = grn_fail_malloc_file_env;
100 }
101 }
102 {
103 char grn_fail_malloc_line_env[GRN_ENV_BUFFER_SIZE];
104 grn_getenv("GRN_FAIL_MALLOC_LINE",
105 grn_fail_malloc_line_env,
106 GRN_ENV_BUFFER_SIZE);
107 if (grn_fail_malloc_line_env[0]) {
108 grn_fail_malloc_location = true;
109 grn_fail_malloc_line = atoi(grn_fail_malloc_line_env);
110 }
111 }
112 {
113 char grn_fail_malloc_max_count_env[GRN_ENV_BUFFER_SIZE];
114 grn_getenv("GRN_FAIL_MALLOC_MAX_COUNT",
115 grn_fail_malloc_max_count_env,
116 GRN_ENV_BUFFER_SIZE);
117 if (grn_fail_malloc_max_count_env[0]) {
118 grn_fail_malloc_max_count = atoi(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_init(void)
336 {
337 }
338
339 void
340 grn_alloc_info_fin(void)
341 {
342 }
343
344 # define grn_alloc_info_add(address, size, file, line, func)
345 # define grn_alloc_info_change(old_address, new_address, size)
346 # define grn_alloc_info_check(ctx, address)
347
348 void
349 grn_alloc_info_dump(grn_ctx *ctx)
350 {
351 }
352
353 void
354 grn_alloc_info_free(grn_ctx *ctx)
355 {
356 }
357 #endif
358
359 #define GRN_CTX_SEGMENT_SIZE (1U << 22)
360 #define GRN_CTX_SEGMENT_MASK (GRN_CTX_SEGMENT_SIZE - 1)
361
362 #define GRN_CTX_SEGMENT_WORD (1U << 31)
363 #define GRN_CTX_SEGMENT_VLEN (1U << 30)
364 #define GRN_CTX_SEGMENT_LIFO (1U << 29)
365 #define GRN_CTX_SEGMENT_DIRTY (1U << 28)
366
367 void
368 grn_alloc_init_ctx_impl(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_impl(grn_ctx *ctx)
377 {
378 int i;
379 grn_io_mapinfo *mi;
380 for (i = 0, mi = ctx->impl->segs; i < GRN_CTX_N_SEGMENTS; i++, mi++) {
381 if (mi->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->count & GRN_CTX_SEGMENT_VLEN) {
385 grn_io_anon_unmap(ctx,
386 mi,
387 (size_t)((int64_t)(mi->nref) * grn_pagesize));
388 } else {
389 grn_io_anon_unmap(ctx, mi, GRN_CTX_SEGMENT_SIZE);
390 }
391 }
392 }
393 }
394
395 #define ALIGN_SIZE (1 << 3)
396 #define ALIGN_MASK ((size_t)(ALIGN_SIZE - 1))
397 #define GRN_CTX_ALLOC_CLEAR 1
398
399 static void *
400 grn_ctx_alloc(grn_ctx *ctx,
401 size_t size,
402 int flags,
403 const char *file,
404 int line,
405 const char *func)
406 {
407 void *res = NULL;
408 if (!ctx) {
409 return res;
410 }
411 if (!ctx->impl) {
412 if (ERRP(ctx, GRN_ERROR)) {
413 return res;
414 }
415 }
416 CRITICAL_SECTION_ENTER(ctx->impl->lock);
417 {
418 int32_t i;
419 int32_t *header;
420 grn_io_mapinfo *mi;
421 size = ((size + ALIGN_MASK) & ~ALIGN_MASK) + ALIGN_SIZE;
422 if (size > GRN_CTX_SEGMENT_SIZE) {
423 uint64_t npages =
424 (uint64_t)(((int64_t)size + (grn_pagesize - 1)) / grn_pagesize);
425 size_t aligned_size;
426 if (npages >= (1LL << 32)) {
427 MERR("too long request size=%" GRN_FMT_SIZE, size);
428 goto exit;
429 }
430 for (i = 0, mi = ctx->impl->segs;; i++, mi++) {
431 if (i >= GRN_CTX_N_SEGMENTS) {
432 MERR("all segments are full");
433 goto exit;
434 }
435 if (!mi->map) {
436 break;
437 }
438 }
439 aligned_size = (size_t)(grn_pagesize * ((int64_t)npages));
440 if (!grn_io_anon_map(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->nref = (uint32_t)npages;
446 mi->count = GRN_CTX_SEGMENT_VLEN;
447 ctx->impl->currseg = -1;
448 header = mi->map;
449 header[0] = i;
450 header[1] = (int32_t)size;
451 } else {
452 i = ctx->impl->currseg;
453 if (i >= 0) {
454 mi = &ctx->impl->segs[i];
455 }
456 if (i < 0 || size + mi->nref > GRN_CTX_SEGMENT_SIZE) {
457 for (i = 0, mi = ctx->impl->segs;; i++, mi++) {
458 if (i >= GRN_CTX_N_SEGMENTS) {
459 MERR("all segments are full");
460 goto exit;
461 }
462 if (!mi->map) {
463 break;
464 }
465 }
466 if (!grn_io_anon_map(ctx, mi, GRN_CTX_SEGMENT_SIZE)) {
467 goto exit;
468 }
469 /* GRN_LOG(ctx, GRN_LOG_NOTICE, "map i=%d", i); */
470 mi->nref = 0;
471 mi->count = GRN_CTX_SEGMENT_WORD;
472 ctx->impl->currseg = i;
473 }
474 header = (int32_t *)((byte *)mi->map + mi->nref);
475 mi->nref += (uint32_t)size;
476 mi->count++;
477 header[0] = i;
478 header[1] = (int32_t)size;
479 if ((flags & GRN_CTX_ALLOC_CLEAR) &&
480 (mi->count & GRN_CTX_SEGMENT_DIRTY) && (size > ALIGN_SIZE)) {
481 memset(&header[2], 0, size - ALIGN_SIZE);
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_LEAVE(ctx->impl->lock);
496 return res;
497 }
498
499 void *
500 grn_ctx_malloc(
501 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
502 {
503 return grn_ctx_alloc(ctx, size, 0, file, line, func);
504 }
505
506 void *
507 grn_ctx_calloc(
508 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
509 {
510 return grn_ctx_alloc(ctx, size, GRN_CTX_ALLOC_CLEAR, file, line, func);
511 }
512
513 void *
514 grn_ctx_realloc(grn_ctx *ctx,
515 void *ptr,
516 size_t size,
517 const char *file,
518 int line,
519 const char *func)
520 {
521 void *res = NULL;
522 if (size) {
523 /* todo : expand if possible */
524 res = grn_ctx_alloc(ctx, size, 0, file, line, func);
525 if (res && ptr) {
526 int32_t *header = &((int32_t *)ptr)[-2];
527 size_t size_ = (size_t)(header[1]);
528 grn_memcpy(res, ptr, size_ > size ? size : size_);
529 grn_ctx_free(ctx, ptr, file, line, func);
530 }
531 } else {
532 grn_ctx_free(ctx, ptr, file, line, func);
533 }
534 return res;
535 }
536
537 char *
538 grn_ctx_strdup(
539 grn_ctx *ctx, const char *s, const char *file, int line, const char *func)
540 {
541 void *res = NULL;
542 if (s) {
543 size_t size = strlen(s) + 1;
544 if ((res = grn_ctx_alloc(ctx, size, 0, file, line, func))) {
545 grn_memcpy(res, s, size);
546 }
547 }
548 return res;
549 }
550
551 void
552 grn_ctx_free(
553 grn_ctx *ctx, void *ptr, const char *file, int line, const char *func)
554 {
555 if (!ctx) {
556 return;
557 }
558 if (!ctx->impl) {
559 ERR(GRN_INVALID_ARGUMENT, "ctx without impl passed.");
560 return;
561 }
562 CRITICAL_SECTION_ENTER(ctx->impl->lock);
563 if (ptr) {
564 int32_t *header = &((int32_t *)ptr)[-2];
565
566 if (header[0] >= GRN_CTX_N_SEGMENTS) {
567 ERR(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_t i = header[0];
587 grn_io_mapinfo *mi = &ctx->impl->segs[i];
588 if (mi->count & GRN_CTX_SEGMENT_VLEN) {
589 if (mi->map != header) {
590 ERR(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_unmap(ctx,
599 mi,
600 (size_t)((int64_t)(mi->nref) * grn_pagesize));
601 mi->map = NULL;
602 } else {
603 if (!mi->map) {
604 ERR(GRN_INVALID_ARGUMENT,
605 "invalid ptr passed... ptr=%p seg=%d",
606 ptr,
607 i);
608 goto exit;
609 }
610 mi->count--;
611 if (!(mi->count & GRN_CTX_SEGMENT_MASK)) {
612 // GRN_LOG(ctx, GRN_LOG_NOTICE, "umap i=%d", i);
613 if (i == ctx->impl->currseg) {
614 mi->count |= GRN_CTX_SEGMENT_DIRTY;
615 mi->nref = 0;
616 } else {
617 grn_io_anon_unmap(ctx, mi, GRN_CTX_SEGMENT_SIZE);
618 mi->map = NULL;
619 }
620 }
621 }
622 }
623 }
624 exit:
625 CRITICAL_SECTION_LEAVE(ctx->impl->lock);
626 }
627
628 void *
629 grn_ctx_alloc_lifo(
630 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
631 {
632 if (!ctx) {
633 return NULL;
634 }
635 if (!ctx->impl) {
636 if (ERRP(ctx, GRN_ERROR)) {
637 return NULL;
638 }
639 }
640 {
641 int32_t i = ctx->impl->lifoseg;
642 grn_io_mapinfo *mi = &ctx->impl->segs[i];
643 if (size > GRN_CTX_SEGMENT_SIZE) {
644 uint64_t npages =
645 (uint64_t)(((int64_t)size + (grn_pagesize - 1)) / grn_pagesize);
646 size_t aligned_size;
647 if (npages >= (1LL << 32)) {
648 MERR("too long request size=%" GRN_FMT_SIZE, size);
649 return NULL;
650 }
651 for (;;) {
652 if (++i >= GRN_CTX_N_SEGMENTS) {
653 MERR("all segments are full");
654 return NULL;
655 }
656 mi++;
657 if (!mi->map) {
658 break;
659 }
660 }
661 aligned_size = (size_t)(grn_pagesize * (int64_t)npages);
662 if (!grn_io_anon_map(ctx, mi, aligned_size)) {
663 return NULL;
664 }
665 mi->nref = (uint32_t)npages;
666 mi->count = GRN_CTX_SEGMENT_VLEN | GRN_CTX_SEGMENT_LIFO;
667 ctx->impl->lifoseg = i;
668 return mi->map;
669 } else {
670 size = (size + ALIGN_MASK) & ~ALIGN_MASK;
671 if (i < 0 || (mi->count & GRN_CTX_SEGMENT_VLEN) ||
672 size + mi->nref > GRN_CTX_SEGMENT_SIZE) {
673 for (;;) {
674 if (++i >= GRN_CTX_N_SEGMENTS) {
675 MERR("all segments are full");
676 return NULL;
677 }
678 if (!(++mi)->map) {
679 break;
680 }
681 }
682 if (!grn_io_anon_map(ctx, mi, GRN_CTX_SEGMENT_SIZE)) {
683 return NULL;
684 }
685 mi->nref = 0;
686 mi->count = GRN_CTX_SEGMENT_WORD | GRN_CTX_SEGMENT_LIFO;
687 ctx->impl->lifoseg = i;
688 }
689 {
690 uint32_t u = mi->nref;
691 mi->nref += (uint32_t)size;
692 return (byte *)mi->map + u;
693 }
694 }
695 }
696 }
697
698 void
699 grn_ctx_free_lifo(
700 grn_ctx *ctx, void *ptr, const char *file, int line, const char *func)
701 {
702 if (!ctx) {
703 return;
704 }
705 if (!ctx->impl) {
706 ERR(GRN_INVALID_ARGUMENT, "ctx without impl passed.");
707 return;
708 }
709 {
710 int32_t i = ctx->impl->lifoseg, done = 0;
711 grn_io_mapinfo *mi = &ctx->impl->segs[i];
712 if (i < 0) {
713 ERR(GRN_INVALID_ARGUMENT, "lifo buffer is void");
714 return;
715 }
716 for (; i >= 0; i--, mi--) {
717 if (!(mi->count & GRN_CTX_SEGMENT_LIFO)) {
718 continue;
719 }
720 if (done) {
721 break;
722 }
723 if (mi->count & GRN_CTX_SEGMENT_VLEN) {
724 if (mi->map == ptr) {
725 done = 1;
726 }
727 grn_io_anon_unmap(ctx,
728 mi,
729 (size_t)((int64_t)(mi->nref) * grn_pagesize));
730 mi->map = NULL;
731 } else {
732 if (mi->map == ptr) {
733 done = 1;
734 } else {
735 if (mi->map < ptr && ptr < (void *)((byte *)mi->map + mi->nref)) {
736 mi->nref = (uint32_t)((uintptr_t)ptr - (uintptr_t)mi->map);
737 break;
738 }
739 }
740 grn_io_anon_unmap(ctx, mi, GRN_CTX_SEGMENT_SIZE);
741 mi->map = NULL;
742 }
743 }
744 ctx->impl->lifoseg = i;
745 }
746 }
747
748 void *
749 grn_malloc(
750 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
751 {
752 if (grn_fail_malloc_should_fail(size, file, line, func)) {
753 return grn_malloc_fail(ctx, size, file, line, func);
754 } else {
755 return grn_malloc_default(ctx, size, file, line, func);
756 }
757 }
758
759 void *
760 grn_calloc(
761 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
762 {
763 if (grn_fail_malloc_should_fail(size, file, line, func)) {
764 return grn_calloc_fail(ctx, size, file, line, func);
765 } else {
766 return grn_calloc_default(ctx, size, file, line, func);
767 }
768 }
769
770 void *
771 grn_realloc(grn_ctx *ctx,
772 void *ptr,
773 size_t size,
774 const char *file,
775 int line,
776 const char *func)
777 {
778 if (grn_fail_malloc_should_fail(size, file, line, func)) {
779 return grn_realloc_fail(ctx, ptr, size, file, line, func);
780 } else {
781 return grn_realloc_default(ctx, ptr, size, file, line, func);
782 }
783 }
784
785 char *
786 grn_strdup(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_t size = 0;
794 if (grn_fail_malloc_should_fail(size, file, line, func)) {
795 return grn_strdup_fail(ctx, string, file, line, func);
796 } else {
797 return grn_strdup_default(ctx, string, file, line, func);
798 }
799 }
800
801 void
802 grn_free(grn_ctx *ctx, void *ptr, const char *file, int line, const char *func)
803 {
804 grn_free_default(ctx, ptr, file, line, func);
805 }
806
807 void *
808 grn_malloc_default(
809 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
810 {
811 if (!ctx) {
812 return NULL;
813 }
814 {
815 void *res = malloc(size);
816 if (res) {
817 GRN_ADD_ALLOC_COUNT(1);
818 grn_alloc_info_add(res, size, file, line, func);
819 } else {
820 if (!(res = malloc(size))) {
821 MERR("[alloc][malloc] failed to allocate: "
822 "size:%" GRN_FMT_SIZE ", "
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_COUNT(1);
834 grn_alloc_info_add(res, size, file, line, func);
835 }
836 }
837 return res;
838 }
839 }
840
841 void *
842 grn_calloc_default(
843 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
844 {
845 if (!ctx) {
846 return NULL;
847 }
848 {
849 void *res = calloc(size, 1);
850 if (res) {
851 GRN_ADD_ALLOC_COUNT(1);
852 grn_alloc_info_add(res, size, file, line, func);
853 } else {
854 if (!(res = calloc(size, 1))) {
855 MERR("[alloc][calloc] failed to allocate: "
856 "size:%" GRN_FMT_SIZE ", "
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_COUNT(1);
868 grn_alloc_info_add(res, size, file, line, func);
869 }
870 }
871 return res;
872 }
873 }
874
875 void
876 grn_free_default(
877 grn_ctx *ctx, void *ptr, const char *file, int line, const char *func)
878 {
879 if (!ctx) {
880 return;
881 }
882 grn_alloc_info_check(ctx, ptr);
883 {
884 if (ptr) {
885 GRN_ADD_ALLOC_COUNT(-1);
886 } else {
887 GRN_LOG(ctx,
888 GRN_LOG_ALERT,
889 "free fail (%p) (%s:%d) <%u>",
890 ptr,
891 file,
892 line,
893 alloc_count);
894 }
895 free(ptr);
896 }
897 }
898
899 void *
900 grn_realloc_default(grn_ctx *ctx,
901 void *ptr,
902 size_t size,
903 const char *file,
904 int line,
905 const char *func)
906 {
907 void *res;
908 if (!ctx) {
909 return NULL;
910 }
911 if (size) {
912 if (!(res = realloc(ptr, size))) {
913 if (!(res = realloc(ptr, size))) {
914 MERR("[alloc][realloc] failed to allocate: "
915 "address:%p, "
916 "size:%" GRN_FMT_SIZE ", "
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 NULL;
928 }
929 }
930 if (ptr) {
931 grn_alloc_info_change(ptr, res, size);
932 } else {
933 GRN_ADD_ALLOC_COUNT(1);
934 grn_alloc_info_add(res, size, file, line, func);
935 }
936 } else {
937 if (!ptr) {
938 return NULL;
939 }
940 grn_alloc_info_check(ctx, ptr);
941 GRN_ADD_ALLOC_COUNT(-1);
942 free(ptr);
943 res = NULL;
944 }
945 return res;
946 }
947
948 char *
949 grn_strdup_default(
950 grn_ctx *ctx, const char *s, const char *file, int line, const char *func)
951 {
952 if (!ctx) {
953 return NULL;
954 }
955 {
956 char *res = grn_strdup_raw(s);
957 if (res) {
958 GRN_ADD_ALLOC_COUNT(1);
959 grn_alloc_info_add(res, strlen(res) + 1, file, line, func);
960 } else {
961 if (!(res = grn_strdup_raw(s))) {
962 MERR("[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_COUNT(1);
975 grn_alloc_info_add(res, strlen(res) + 1, file, line, func);
976 }
977 }
978 return res;
979 }
980 }
981
982 bool
983 grn_fail_malloc_should_fail(size_t size,
984 const char *file,
985 int line,
986 const char *func)
987 {
988 if (!grn_fail_malloc_enable) {
989 return false;
990 }
991
992 if (grn_fail_malloc_location) {
993 if (grn_fail_malloc_file) {
994 if (strcmp(file, grn_fail_malloc_file) != 0) {
995 return false;
996 }
997 }
998 if (grn_fail_malloc_line > 0) {
999 if (line != grn_fail_malloc_line) {
1000 return false;
1001 }
1002 }
1003 if (grn_fail_malloc_func) {
1004 if (strcmp(func, grn_fail_malloc_func) != 0) {
1005 return false;
1006 }
1007 }
1008 return true;
1009 }
1010
1011 if (grn_fail_malloc_prob > 0.0 && grn_fail_malloc_prob >= rand()) {
1012 return true;
1013 }
1014
1015 if (grn_fail_malloc_max_count >= 0 &&
1016 alloc_count >= (uint32_t)grn_fail_malloc_max_count) {
1017 return true;
1018 }
1019
1020 return false;
1021 }
1022
1023 void *
1024 grn_malloc_fail(
1025 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
1026 {
1027 MERR("[alloc][fail][malloc] <%u>: <%" GRN_FMT_SIZE ">: %s:%d: %s",
1028 alloc_count,
1029 size,
1030 file,
1031 line,
1032 func);
1033 return NULL;
1034 }
1035
1036 void *
1037 grn_calloc_fail(
1038 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
1039 {
1040 MERR("[alloc][fail][calloc] <%u>: <%" GRN_FMT_SIZE ">: %s:%d: %s",
1041 alloc_count,
1042 size,
1043 file,
1044 line,
1045 func);
1046 return NULL;
1047 }
1048
1049 void *
1050 grn_realloc_fail(grn_ctx *ctx,
1051 void *ptr,
1052 size_t size,
1053 const char *file,
1054 int line,
1055 const char *func)
1056 {
1057 MERR("[alloc][fail][realloc] <%u>: <%p:%" GRN_FMT_SIZE ">: %s:%d: %s",
1058 alloc_count,
1059 ptr,
1060 size,
1061 file,
1062 line,
1063 func);
1064 return NULL;
1065 }
1066
1067 char *
1068 grn_strdup_fail(
1069 grn_ctx *ctx, const char *s, const char *file, int line, const char *func)
1070 {
1071 MERR("[alloc][fail][strdup] <%u>: <%" GRN_FMT_SIZE ">: %s:%d: %s: <%s>",
1072 alloc_count,
1073 s ? strlen(s) : 0,
1074 file,
1075 line,
1076 func,
1077 s ? s : "(null)");
1078 return NULL;
1079 }