2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
16 #include "private/gc_priv.h"
17 #include "gc_inline.h" /* for GC_malloc_kind */
22 /* Allocate reclaim list for kind: */
23 /* Return TRUE on success */
24 STATIC GC_bool GC_alloc_reclaim_list(struct obj_kind *kind)
26 struct hblk ** result = (struct hblk **)
27 GC_scratch_alloc((MAXOBJGRANULES+1) * sizeof(struct hblk *));
28 if (result == 0) return(FALSE);
29 BZERO(result, (MAXOBJGRANULES+1)*sizeof(struct hblk *));
30 kind -> ok_reclaim_list = result;
34 /* Allocate a large block of size lb bytes. */
35 /* The block is not cleared. */
36 /* Flags is 0 or IGNORE_OFF_PAGE. */
37 /* EXTRA_BYTES were already added to lb. */
38 GC_INNER ptr_t GC_alloc_large(size_t lb, int k, unsigned flags)
43 GC_bool retry = FALSE;
45 GC_ASSERT(I_HOLD_LOCK());
46 lb = ROUNDUP_GRANULE_SIZE(lb);
47 n_blocks = OBJ_SZ_TO_BLOCKS_CHECKED(lb);
48 if (!EXPECT(GC_is_initialized, TRUE)) {
50 UNLOCK(); /* just to unset GC_lock_holder */
54 /* Do our share of marking work */
55 if (GC_incremental && !GC_dont_gc)
56 GC_collect_a_little_inner((int)n_blocks);
57 h = GC_allochblk(lb, k, flags);
61 h = GC_allochblk(lb, k, flags);
64 while (0 == h && GC_collect_or_expand(n_blocks, flags != 0, retry)) {
65 h = GC_allochblk(lb, k, flags);
71 size_t total_bytes = n_blocks * HBLKSIZE;
73 GC_large_allocd_bytes += total_bytes;
74 if (GC_large_allocd_bytes > GC_max_large_allocd_bytes)
75 GC_max_large_allocd_bytes = GC_large_allocd_bytes;
77 /* FIXME: Do we need some way to reset GC_max_large_allocd_bytes? */
78 result = h -> hb_body;
83 /* Allocate a large block of size lb bytes. Clear if appropriate. */
84 /* EXTRA_BYTES were already added to lb. */
85 STATIC ptr_t GC_alloc_large_and_clear(size_t lb, int k, unsigned flags)
89 GC_ASSERT(I_HOLD_LOCK());
90 result = GC_alloc_large(lb, k, flags);
92 && (GC_debugging_started || GC_obj_kinds[k].ok_init)) {
93 word n_blocks = OBJ_SZ_TO_BLOCKS(lb);
95 /* Clear the whole block, in case of GC_realloc call. */
96 BZERO(result, n_blocks * HBLKSIZE);
101 /* Fill in additional entries in GC_size_map, including the i-th one. */
102 /* Note that a filled in section of the array ending at n always */
103 /* has the length of at least n/4. */
104 STATIC void GC_extend_size_map(size_t i)
106 size_t orig_granule_sz = ROUNDED_UP_GRANULES(i);
108 size_t byte_sz = GRANULES_TO_BYTES(orig_granule_sz);
109 /* The size we try to preserve. */
110 /* Close to i, unless this would */
111 /* introduce too many distinct sizes. */
112 size_t smaller_than_i = byte_sz - (byte_sz >> 3);
113 size_t low_limit; /* The lowest indexed entry we initialize. */
114 size_t number_of_objs;
116 GC_ASSERT(I_HOLD_LOCK());
117 GC_ASSERT(0 == GC_size_map[i]);
118 if (0 == GC_size_map[smaller_than_i]) {
119 low_limit = byte_sz - (byte_sz >> 2); /* much smaller than i */
120 granule_sz = orig_granule_sz;
121 while (GC_size_map[low_limit] != 0)
124 low_limit = smaller_than_i + 1;
125 while (GC_size_map[low_limit] != 0)
128 granule_sz = ROUNDED_UP_GRANULES(low_limit);
129 granule_sz += granule_sz >> 3;
130 if (granule_sz < orig_granule_sz)
131 granule_sz = orig_granule_sz;
134 /* For these larger sizes, we use an even number of granules. */
135 /* This makes it easier to, e.g., construct a 16-byte-aligned */
136 /* allocator even if GRANULE_BYTES is 8. */
137 granule_sz = (granule_sz + 1) & ~1;
138 if (granule_sz > MAXOBJGRANULES)
139 granule_sz = MAXOBJGRANULES;
141 /* If we can fit the same number of larger objects in a block, do so. */
142 number_of_objs = HBLK_GRANULES / granule_sz;
143 GC_ASSERT(number_of_objs != 0);
144 granule_sz = (HBLK_GRANULES / number_of_objs) & ~1;
146 byte_sz = GRANULES_TO_BYTES(granule_sz) - EXTRA_BYTES;
147 /* We may need one extra byte; do not always */
148 /* fill in GC_size_map[byte_sz]. */
150 for (; low_limit <= byte_sz; low_limit++)
151 GC_size_map[low_limit] = granule_sz;
154 /* Allocate lb bytes for an object of kind k. */
155 /* Should not be used to directly to allocate objects */
156 /* that require special handling on allocation. */
157 GC_INNER void * GC_generic_malloc_inner(size_t lb, int k)
161 GC_ASSERT(I_HOLD_LOCK());
162 GC_ASSERT(k < MAXOBJKINDS);
164 struct obj_kind * kind = GC_obj_kinds + k;
165 size_t lg = GC_size_map[lb];
166 void ** opp = &(kind -> ok_freelist[lg]);
169 if (EXPECT(0 == op, FALSE)) {
171 if (!EXPECT(GC_is_initialized, TRUE)) {
173 UNLOCK(); /* just to unset GC_lock_holder */
176 lg = GC_size_map[lb];
179 GC_extend_size_map(lb);
180 lg = GC_size_map[lb];
184 opp = &(kind -> ok_freelist[lg]);
188 if (0 == kind -> ok_reclaim_list &&
189 !GC_alloc_reclaim_list(kind))
191 op = GC_allocobj(lg, k);
198 GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
200 op = (ptr_t)GC_alloc_large_and_clear(ADD_SLOP(lb), k, 0);
202 GC_bytes_allocd += lb;
208 #if defined(DBG_HDRS_ALL) || defined(GC_GCJ_SUPPORT) \
209 || !defined(GC_NO_FINALIZATION)
210 /* Allocate a composite object of size n bytes. The caller */
211 /* guarantees that pointers past the first page are not relevant. */
212 GC_INNER void * GC_generic_malloc_inner_ignore_off_page(size_t lb, int k)
217 GC_ASSERT(I_HOLD_LOCK());
219 return GC_generic_malloc_inner(lb, k);
220 GC_ASSERT(k < MAXOBJKINDS);
221 lb_adjusted = ADD_SLOP(lb);
222 op = GC_alloc_large_and_clear(lb_adjusted, k, IGNORE_OFF_PAGE);
224 GC_bytes_allocd += lb_adjusted;
229 #ifdef GC_COLLECT_AT_MALLOC
230 /* Parameter to force GC at every malloc of size greater or equal to */
231 /* the given value. This might be handy during debugging. */
232 # if defined(CPPCHECK)
233 size_t GC_dbg_collect_at_malloc_min_lb = 16*1024; /* e.g. */
235 size_t GC_dbg_collect_at_malloc_min_lb = (GC_COLLECT_AT_MALLOC);
239 GC_API GC_ATTR_MALLOC void * GC_CALL GC_generic_malloc(size_t lb, int k)
244 GC_ASSERT(k < MAXOBJKINDS);
245 if (EXPECT(GC_have_errors, FALSE))
246 GC_print_all_errors();
247 GC_INVOKE_FINALIZERS();
248 GC_DBG_COLLECT_AT_MALLOC(lb);
251 result = GC_generic_malloc_inner(lb, k);
259 lg = ROUNDED_UP_GRANULES(lb);
260 lb_rounded = GRANULES_TO_BYTES(lg);
261 n_blocks = OBJ_SZ_TO_BLOCKS(lb_rounded);
262 init = GC_obj_kinds[k].ok_init;
264 result = (ptr_t)GC_alloc_large(lb_rounded, k, 0);
266 if (GC_debugging_started) {
267 BZERO(result, n_blocks * HBLKSIZE);
270 /* Clear any memory that might be used for GC descriptors */
271 /* before we release the lock. */
272 ((word *)result)[0] = 0;
273 ((word *)result)[1] = 0;
274 ((word *)result)[GRANULES_TO_WORDS(lg)-1] = 0;
275 ((word *)result)[GRANULES_TO_WORDS(lg)-2] = 0;
278 GC_bytes_allocd += lb_rounded;
281 if (init && !GC_debugging_started && 0 != result) {
282 BZERO(result, n_blocks * HBLKSIZE);
286 return((*GC_get_oom_fn())(lb));
292 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_kind_global(size_t lb, int k)
294 GC_ASSERT(k < MAXOBJKINDS);
301 GC_DBG_COLLECT_AT_MALLOC(lb);
303 lg = GC_size_map[lb];
304 opp = &GC_obj_kinds[k].ok_freelist[lg];
306 if (EXPECT(op != NULL, TRUE)) {
310 GC_ASSERT(0 == obj_link(op)
311 || ((word)obj_link(op)
312 <= (word)GC_greatest_plausible_heap_addr
313 && (word)obj_link(op)
314 >= (word)GC_least_plausible_heap_addr));
318 GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
325 /* We make the GC_clear_stack() call a tail one, hoping to get more */
327 return GC_clear_stack(GC_generic_malloc(lb, k));
330 #if defined(THREADS) && !defined(THREAD_LOCAL_ALLOC)
331 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_kind(size_t lb, int k)
333 return GC_malloc_kind_global(lb, k);
337 /* Allocate lb bytes of atomic (pointer-free) data. */
338 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_atomic(size_t lb)
340 return GC_malloc_kind(lb, PTRFREE);
343 /* Allocate lb bytes of composite (pointerful) data. */
344 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc(size_t lb)
346 return GC_malloc_kind(lb, NORMAL);
349 GC_API GC_ATTR_MALLOC void * GC_CALL GC_generic_malloc_uncollectable(
355 GC_ASSERT(k < MAXOBJKINDS);
360 GC_DBG_COLLECT_AT_MALLOC(lb);
361 if (EXTRA_BYTES != 0 && lb != 0) lb--;
362 /* We don't need the extra byte, since this won't be */
363 /* collected anyway. */
365 lg = GC_size_map[lb];
366 opp = &GC_obj_kinds[k].ok_freelist[lg];
368 if (EXPECT(op != NULL, TRUE)) {
371 GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
372 /* Mark bit was already set on free list. It will be */
373 /* cleared only temporarily during a collection, as a */
374 /* result of the normal free list mark bit clearing. */
375 GC_non_gc_bytes += GRANULES_TO_BYTES((word)lg);
379 op = GC_generic_malloc(lb, k);
380 /* For small objects, the free lists are completely marked. */
382 GC_ASSERT(0 == op || GC_is_marked(op));
384 op = GC_generic_malloc(lb, k);
385 if (op /* != NULL */) { /* CPPCHECK */
386 hdr * hhdr = HDR(op);
388 GC_ASSERT(((word)op & (HBLKSIZE - 1)) == 0); /* large block */
389 /* We don't need the lock here, since we have an undisguised */
390 /* pointer. We do need to hold the lock while we adjust */
393 set_mark_bit_from_hdr(hhdr, 0); /* Only object. */
395 GC_ASSERT(hhdr -> hb_n_marks == 0);
396 /* This is not guaranteed in the multi-threaded case */
397 /* because the counter could be updated before locking. */
399 hhdr -> hb_n_marks = 1;
406 /* Allocate lb bytes of pointerful, traced, but not collectible data. */
407 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_uncollectable(size_t lb)
409 return GC_generic_malloc_uncollectable(lb, UNCOLLECTABLE);
412 #ifdef GC_ATOMIC_UNCOLLECTABLE
413 /* Allocate lb bytes of pointer-free, untraced, uncollectible data */
414 /* This is normally roughly equivalent to the system malloc. */
415 /* But it may be useful if malloc is redefined. */
416 GC_API GC_ATTR_MALLOC void * GC_CALL
417 GC_malloc_atomic_uncollectable(size_t lb)
419 return GC_generic_malloc_uncollectable(lb, AUNCOLLECTABLE);
421 #endif /* GC_ATOMIC_UNCOLLECTABLE */
423 #if defined(REDIRECT_MALLOC) && !defined(REDIRECT_MALLOC_IN_HEADER)
429 /* Avoid unnecessary nested procedure calls here, by #defining some */
430 /* malloc replacements. Otherwise we end up saving a meaningless */
431 /* return address in the object. It also speeds things up, but it is */
432 /* admittedly quite ugly. */
433 # define GC_debug_malloc_replacement(lb) GC_debug_malloc(lb, GC_DBG_EXTRAS)
435 # if defined(CPPCHECK)
436 # define REDIRECT_MALLOC_F GC_malloc /* e.g. */
438 # define REDIRECT_MALLOC_F REDIRECT_MALLOC
441 void * malloc(size_t lb)
443 /* It might help to manually inline the GC_malloc call here. */
444 /* But any decent compiler should reduce the extra procedure call */
445 /* to at most a jump instruction in this case. */
446 # if defined(I386) && defined(GC_SOLARIS_THREADS)
447 /* Thread initialization can call malloc before we are ready for. */
448 /* It is not clear that this is enough to help matters. */
449 /* The thread implementation may well call malloc at other */
450 /* inopportune times. */
451 if (!EXPECT(GC_is_initialized, TRUE)) return sbrk(lb);
453 return (void *)REDIRECT_MALLOC_F(lb);
456 # if defined(GC_LINUX_THREADS)
457 STATIC ptr_t GC_libpthread_start = 0;
458 STATIC ptr_t GC_libpthread_end = 0;
459 STATIC ptr_t GC_libld_start = 0;
460 STATIC ptr_t GC_libld_end = 0;
462 STATIC void GC_init_lib_bounds(void)
464 IF_CANCEL(int cancel_state;)
466 if (GC_libpthread_start != 0) return;
467 DISABLE_CANCEL(cancel_state);
468 GC_init(); /* if not called yet */
469 if (!GC_text_mapping("libpthread-",
470 &GC_libpthread_start, &GC_libpthread_end)) {
471 WARN("Failed to find libpthread.so text mapping: Expect crash\n", 0);
472 /* This might still work with some versions of libpthread, */
473 /* so we don't abort. Perhaps we should. */
474 /* Generate message only once: */
475 GC_libpthread_start = (ptr_t)1;
477 if (!GC_text_mapping("ld-", &GC_libld_start, &GC_libld_end)) {
478 WARN("Failed to find ld.so text mapping: Expect crash\n", 0);
480 RESTORE_CANCEL(cancel_state);
482 # endif /* GC_LINUX_THREADS */
484 void * calloc(size_t n, size_t lb)
486 if ((lb | n) > GC_SQRT_SIZE_MAX /* fast initial test */
487 && lb && n > GC_SIZE_MAX / lb)
488 return (*GC_get_oom_fn())(GC_SIZE_MAX); /* n*lb overflow */
489 # if defined(GC_LINUX_THREADS)
490 /* libpthread allocated some memory that is only pointed to by */
491 /* mmapped thread stacks. Make sure it is not collectible. */
493 static GC_bool lib_bounds_set = FALSE;
494 ptr_t caller = (ptr_t)__builtin_return_address(0);
495 /* This test does not need to ensure memory visibility, since */
496 /* the bounds will be set when/if we create another thread. */
497 if (!EXPECT(lib_bounds_set, TRUE)) {
498 GC_init_lib_bounds();
499 lib_bounds_set = TRUE;
501 if (((word)caller >= (word)GC_libpthread_start
502 && (word)caller < (word)GC_libpthread_end)
503 || ((word)caller >= (word)GC_libld_start
504 && (word)caller < (word)GC_libld_end))
505 return GC_generic_malloc_uncollectable(n * lb, UNCOLLECTABLE);
506 /* The two ranges are actually usually adjacent, so there may */
507 /* be a way to speed this up. */
510 return (void *)REDIRECT_MALLOC_F(n * lb);
514 char *strdup(const char *s)
516 size_t lb = strlen(s) + 1;
517 char *result = (char *)REDIRECT_MALLOC_F(lb);
522 BCOPY(s, result, lb);
525 # endif /* !defined(strdup) */
526 /* If strdup is macro defined, we assume that it actually calls malloc, */
527 /* and thus the right thing will happen even without overriding it. */
528 /* This seems to be true on most Linux systems. */
531 /* This is similar to strdup(). */
532 char *strndup(const char *str, size_t size)
535 size_t len = strlen(str);
538 copy = (char *)REDIRECT_MALLOC_F(len + 1);
543 if (EXPECT(len > 0, TRUE))
544 BCOPY(str, copy, len);
548 # endif /* !strndup */
550 # undef GC_debug_malloc_replacement
552 #endif /* REDIRECT_MALLOC */
554 /* Explicitly deallocate an object p. */
555 GC_API void GC_CALL GC_free(void * p)
559 size_t sz; /* In bytes */
560 size_t ngranules; /* sz in granules */
562 struct obj_kind * ok;
565 if (p /* != NULL */) {
568 /* Required by ANSI. It's not my fault ... */
573 GC_log_printf("GC_free(%p) after GC #%lu\n",
574 p, (unsigned long)GC_gc_no);
578 # if defined(REDIRECT_MALLOC) && \
579 ((defined(NEED_CALLINFO) && defined(GC_HAVE_BUILTIN_BACKTRACE)) \
580 || defined(GC_SOLARIS_THREADS) || defined(GC_LINUX_THREADS) \
582 /* This might be called indirectly by GC_print_callers to free */
583 /* the result of backtrace_symbols. */
584 /* For Solaris, we have to redirect malloc calls during */
585 /* initialization. For the others, this seems to happen */
587 /* Don't try to deallocate that memory. */
588 if (0 == hhdr) return;
590 GC_ASSERT(GC_base(p) == p);
591 sz = (size_t)hhdr->hb_sz;
592 ngranules = BYTES_TO_GRANULES(sz);
593 knd = hhdr -> hb_obj_kind;
594 ok = &GC_obj_kinds[knd];
595 if (EXPECT(ngranules <= MAXOBJGRANULES, TRUE)) {
599 GC_bytes_freed += sz;
600 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
601 /* Its unnecessary to clear the mark bit. If the */
602 /* object is reallocated, it doesn't matter. O.w. the */
603 /* collector will do it, since it's on a free list. */
604 if (ok -> ok_init && EXPECT(sz > sizeof(word), TRUE)) {
605 BZERO((word *)p + 1, sz-sizeof(word));
607 flh = &(ok -> ok_freelist[ngranules]);
612 size_t nblocks = OBJ_SZ_TO_BLOCKS(sz);
615 GC_bytes_freed += sz;
616 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
618 GC_large_allocd_bytes -= nblocks * HBLKSIZE;
625 /* Explicitly deallocate an object p when we already hold lock. */
626 /* Only used for internally allocated objects, so we can take some */
629 GC_INNER void GC_free_inner(void * p)
633 size_t sz; /* bytes */
634 size_t ngranules; /* sz in granules */
636 struct obj_kind * ok;
640 knd = hhdr -> hb_obj_kind;
641 sz = (size_t)hhdr->hb_sz;
642 ngranules = BYTES_TO_GRANULES(sz);
643 ok = &GC_obj_kinds[knd];
644 if (ngranules <= MAXOBJGRANULES) {
647 GC_bytes_freed += sz;
648 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
649 if (ok -> ok_init && EXPECT(sz > sizeof(word), TRUE)) {
650 BZERO((word *)p + 1, sz-sizeof(word));
652 flh = &(ok -> ok_freelist[ngranules]);
656 size_t nblocks = OBJ_SZ_TO_BLOCKS(sz);
657 GC_bytes_freed += sz;
658 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
660 GC_large_allocd_bytes -= nblocks * HBLKSIZE;
667 #if defined(REDIRECT_MALLOC) && !defined(REDIRECT_FREE)
668 # define REDIRECT_FREE GC_free
671 #if defined(REDIRECT_FREE) && !defined(REDIRECT_MALLOC_IN_HEADER)
673 # if defined(CPPCHECK)
674 # define REDIRECT_FREE_F GC_free /* e.g. */
676 # define REDIRECT_FREE_F REDIRECT_FREE
682 # if defined(GC_LINUX_THREADS) && !defined(USE_PROC_FOR_LIBRARIES)
683 /* Don't bother with initialization checks. If nothing */
684 /* has been initialized, the check fails, and that's safe, */
685 /* since we have not allocated uncollectible objects neither. */
686 ptr_t caller = (ptr_t)__builtin_return_address(0);
687 /* This test does not need to ensure memory visibility, since */
688 /* the bounds will be set when/if we create another thread. */
689 if (((word)caller >= (word)GC_libpthread_start
690 && (word)caller < (word)GC_libpthread_end)
691 || ((word)caller >= (word)GC_libld_start
692 && (word)caller < (word)GC_libld_end)) {
700 #endif /* REDIRECT_FREE */