Debian Codegraph @

gc/malloc.c

raw
1 /*
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.
5 *
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 *
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.
14 */
15
16 #include "private/gc_priv.h"
17 #include "gc_inline.h" /* for GC_malloc_kind */
18
19 #include <stdio.h>
20 #include <string.h>
21
22 /* Allocate reclaim list for kind: */
23 /* Return TRUE on success */
24 STATIC GC_bool GC_alloc_reclaim_list(struct obj_kind *kind)
25 {
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;
31 return(TRUE);
32 }
33
34 /* Allocate a large block of size lb bytes. The block is not cleared. */
35 /* flags argument should be 0 or IGNORE_OFF_PAGE. EXTRA_BYTES value */
36 /* was already added to lb. */
37 GC_INNER ptr_t GC_alloc_large(size_t lb, int k, unsigned flags)
38 {
39 struct hblk * h;
40 word n_blocks;
41 ptr_t result;
42 GC_bool retry = FALSE;
43
44 GC_ASSERT(I_HOLD_LOCK());
45 lb = ROUNDUP_GRANULE_SIZE(lb);
46 n_blocks = OBJ_SZ_TO_BLOCKS_CHECKED(lb);
47 if (!EXPECT(GC_is_initialized, TRUE)) {
48 DCL_LOCK_STATE;
49 UNLOCK(); /* just to unset GC_lock_holder */
50 GC_init();
51 LOCK();
52 }
53 /* Do our share of marking work */
54 if (GC_incremental && !GC_dont_gc) {
55 ENTER_GC();
56 GC_collect_a_little_inner((int)n_blocks);
57 EXIT_GC();
58 }
59 h = GC_allochblk(lb, k, flags);
60 # ifdef USE_MUNMAP
61 if (0 == h) {
62 GC_merge_unmapped();
63 h = GC_allochblk(lb, k, flags);
64 }
65 # endif
66 while (0 == h && GC_collect_or_expand(n_blocks, flags != 0, retry)) {
67 h = GC_allochblk(lb, k, flags);
68 retry = TRUE;
69 }
70 if (h == 0) {
71 result = 0;
72 } else {
73 size_t total_bytes = n_blocks * HBLKSIZE;
74 if (n_blocks > 1) {
75 GC_large_allocd_bytes += total_bytes;
76 if (GC_large_allocd_bytes > GC_max_large_allocd_bytes)
77 GC_max_large_allocd_bytes = GC_large_allocd_bytes;
78 }
79 /* FIXME: Do we need some way to reset GC_max_large_allocd_bytes? */
80 result = h -> hb_body;
81 }
82 return result;
83 }
84
85 /* Allocate a large block of size lb bytes. Clear if appropriate. */
86 /* EXTRA_BYTES were already added to lb. */
87 STATIC ptr_t GC_alloc_large_and_clear(size_t lb, int k, unsigned flags)
88 {
89 ptr_t result;
90
91 GC_ASSERT(I_HOLD_LOCK());
92 result = GC_alloc_large(lb, k, flags);
93 if (result != NULL
94 && (GC_debugging_started || GC_obj_kinds[k].ok_init)) {
95 word n_blocks = OBJ_SZ_TO_BLOCKS(lb);
96
97 /* Clear the whole block, in case of GC_realloc call. */
98 BZERO(result, n_blocks * HBLKSIZE);
99 }
100 return result;
101 }
102
103 /* Fill in additional entries in GC_size_map, including the i-th one. */
104 /* Note that a filled in section of the array ending at n always */
105 /* has the length of at least n/4. */
106 STATIC void GC_extend_size_map(size_t i)
107 {
108 size_t orig_granule_sz = ROUNDED_UP_GRANULES(i);
109 size_t granule_sz;
110 size_t byte_sz = GRANULES_TO_BYTES(orig_granule_sz);
111 /* The size we try to preserve. */
112 /* Close to i, unless this would */
113 /* introduce too many distinct sizes. */
114 size_t smaller_than_i = byte_sz - (byte_sz >> 3);
115 size_t low_limit; /* The lowest indexed entry we initialize. */
116 size_t number_of_objs;
117
118 GC_ASSERT(I_HOLD_LOCK());
119 GC_ASSERT(0 == GC_size_map[i]);
120 if (0 == GC_size_map[smaller_than_i]) {
121 low_limit = byte_sz - (byte_sz >> 2); /* much smaller than i */
122 granule_sz = orig_granule_sz;
123 while (GC_size_map[low_limit] != 0)
124 low_limit++;
125 } else {
126 low_limit = smaller_than_i + 1;
127 while (GC_size_map[low_limit] != 0)
128 low_limit++;
129
130 granule_sz = ROUNDED_UP_GRANULES(low_limit);
131 granule_sz += granule_sz >> 3;
132 if (granule_sz < orig_granule_sz)
133 granule_sz = orig_granule_sz;
134 }
135
136 /* For these larger sizes, we use an even number of granules. */
137 /* This makes it easier to, e.g., construct a 16-byte-aligned */
138 /* allocator even if GRANULE_BYTES is 8. */
139 granule_sz = (granule_sz + 1) & ~(size_t)1;
140 if (granule_sz > MAXOBJGRANULES)
141 granule_sz = MAXOBJGRANULES;
142
143 /* If we can fit the same number of larger objects in a block, do so. */
144 number_of_objs = HBLK_GRANULES / granule_sz;
145 GC_ASSERT(number_of_objs != 0);
146 granule_sz = (HBLK_GRANULES / number_of_objs) & ~(size_t)1;
147
148 byte_sz = GRANULES_TO_BYTES(granule_sz) - EXTRA_BYTES;
149 /* We may need one extra byte; do not always */
150 /* fill in GC_size_map[byte_sz]. */
151
152 for (; low_limit <= byte_sz; low_limit++)
153 GC_size_map[low_limit] = granule_sz;
154 }
155
156 /* Allocate lb bytes for an object of kind k. */
157 /* Should not be used to directly allocate objects */
158 /* that require special handling on allocation. */
159 GC_INNER void * GC_generic_malloc_inner(size_t lb, int k)
160 {
161 void *op;
162
163 GC_ASSERT(I_HOLD_LOCK());
164 GC_ASSERT(k < MAXOBJKINDS);
165 if (SMALL_OBJ(lb)) {
166 struct obj_kind * kind = GC_obj_kinds + k;
167 size_t lg = GC_size_map[lb];
168 void ** opp = &(kind -> ok_freelist[lg]);
169
170 op = *opp;
171 if (EXPECT(0 == op, FALSE)) {
172 if (lg == 0) {
173 if (!EXPECT(GC_is_initialized, TRUE)) {
174 DCL_LOCK_STATE;
175 UNLOCK(); /* just to unset GC_lock_holder */
176 GC_init();
177 LOCK();
178 lg = GC_size_map[lb];
179 }
180 if (0 == lg) {
181 GC_extend_size_map(lb);
182 lg = GC_size_map[lb];
183 GC_ASSERT(lg != 0);
184 }
185 /* Retry */
186 opp = &(kind -> ok_freelist[lg]);
187 op = *opp;
188 }
189 if (0 == op) {
190 if (0 == kind -> ok_reclaim_list &&
191 !GC_alloc_reclaim_list(kind))
192 return NULL;
193 op = GC_allocobj(lg, k);
194 if (0 == op)
195 return NULL;
196 }
197 }
198 *opp = obj_link(op);
199 obj_link(op) = 0;
200 GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
201 } else {
202 size_t lb_adjusted = ADD_SLOP(lb);
203
204 op = (ptr_t)GC_alloc_large_and_clear(lb_adjusted, k, 0 /* flags */);
205 if (op != NULL)
206 GC_bytes_allocd += lb_adjusted;
207 }
208
209 return op;
210 }
211
212 #if defined(DBG_HDRS_ALL) || defined(GC_GCJ_SUPPORT) \
213 || !defined(GC_NO_FINALIZATION)
214 /* Allocate a composite object of size n bytes. The caller */
215 /* guarantees that pointers past the first hblk are not relevant. */
216 GC_INNER void * GC_generic_malloc_inner_ignore_off_page(size_t lb, int k)
217 {
218 size_t lb_adjusted;
219 void * op;
220
221 GC_ASSERT(I_HOLD_LOCK());
222 if (lb <= HBLKSIZE)
223 return GC_generic_malloc_inner(lb, k);
224 GC_ASSERT(k < MAXOBJKINDS);
225 lb_adjusted = ADD_SLOP(lb);
226 op = GC_alloc_large_and_clear(lb_adjusted, k, IGNORE_OFF_PAGE);
227 if (op != NULL)
228 GC_bytes_allocd += lb_adjusted;
229 return op;
230 }
231 #endif
232
233 #ifdef GC_COLLECT_AT_MALLOC
234 /* Parameter to force GC at every malloc of size greater or equal to */
235 /* the given value. This might be handy during debugging. */
236 # if defined(CPPCHECK)
237 size_t GC_dbg_collect_at_malloc_min_lb = 16*1024; /* e.g. */
238 # else
239 size_t GC_dbg_collect_at_malloc_min_lb = (GC_COLLECT_AT_MALLOC);
240 # endif
241 #endif
242
243 GC_API GC_ATTR_MALLOC void * GC_CALL GC_generic_malloc(size_t lb, int k)
244 {
245 void * result;
246 DCL_LOCK_STATE;
247
248 GC_ASSERT(k < MAXOBJKINDS);
249 if (EXPECT(get_have_errors(), FALSE))
250 GC_print_all_errors();
251 GC_INVOKE_FINALIZERS();
252 GC_DBG_COLLECT_AT_MALLOC(lb);
253 if (SMALL_OBJ(lb)) {
254 LOCK();
255 result = GC_generic_malloc_inner(lb, k);
256 UNLOCK();
257 } else {
258 size_t lg;
259 size_t lb_rounded;
260 word n_blocks;
261 GC_bool init;
262
263 lg = ROUNDED_UP_GRANULES(lb);
264 lb_rounded = GRANULES_TO_BYTES(lg);
265 n_blocks = OBJ_SZ_TO_BLOCKS(lb_rounded);
266 init = GC_obj_kinds[k].ok_init;
267 LOCK();
268 result = (ptr_t)GC_alloc_large(lb_rounded, k, 0);
269 if (0 != result) {
270 if (GC_debugging_started
271 # ifndef THREADS
272 || init
273 # endif
274 ) {
275 BZERO(result, n_blocks * HBLKSIZE);
276 } else {
277 # ifdef THREADS
278 /* Clear any memory that might be used for GC descriptors */
279 /* before we release the lock. */
280 ((word *)result)[0] = 0;
281 ((word *)result)[1] = 0;
282 ((word *)result)[GRANULES_TO_WORDS(lg)-1] = 0;
283 ((word *)result)[GRANULES_TO_WORDS(lg)-2] = 0;
284 # endif
285 }
286 GC_bytes_allocd += lb_rounded;
287 }
288 UNLOCK();
289 # ifdef THREADS
290 if (init && !GC_debugging_started && result != NULL) {
291 /* Clear the rest (i.e. excluding the initial 2 words). */
292 BZERO((word *)result + 2,
293 n_blocks * HBLKSIZE - 2 * sizeof(word));
294 }
295 # endif
296 }
297 if (0 == result) {
298 return((*GC_get_oom_fn())(lb));
299 } else {
300 return(result);
301 }
302 }
303
304 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_kind_global(size_t lb, int k)
305 {
306 GC_ASSERT(k < MAXOBJKINDS);
307 if (SMALL_OBJ(lb)) {
308 void *op;
309 void **opp;
310 size_t lg;
311 DCL_LOCK_STATE;
312
313 GC_DBG_COLLECT_AT_MALLOC(lb);
314 LOCK();
315 lg = GC_size_map[lb];
316 opp = &GC_obj_kinds[k].ok_freelist[lg];
317 op = *opp;
318 if (EXPECT(op != NULL, TRUE)) {
319 if (k == PTRFREE) {
320 *opp = obj_link(op);
321 } else {
322 GC_ASSERT(0 == obj_link(op)
323 || ((word)obj_link(op)
324 <= (word)GC_greatest_plausible_heap_addr
325 && (word)obj_link(op)
326 >= (word)GC_least_plausible_heap_addr));
327 *opp = obj_link(op);
328 obj_link(op) = 0;
329 }
330 GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
331 UNLOCK();
332 return op;
333 }
334 UNLOCK();
335 }
336
337 /* We make the GC_clear_stack() call a tail one, hoping to get more */
338 /* of the stack. */
339 return GC_clear_stack(GC_generic_malloc(lb, k));
340 }
341
342 #if defined(THREADS) && !defined(THREAD_LOCAL_ALLOC)
343 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_kind(size_t lb, int k)
344 {
345 return GC_malloc_kind_global(lb, k);
346 }
347 #endif
348
349 /* Allocate lb bytes of atomic (pointer-free) data. */
350 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_atomic(size_t lb)
351 {
352 return GC_malloc_kind(lb, PTRFREE);
353 }
354
355 /* Allocate lb bytes of composite (pointerful) data. */
356 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc(size_t lb)
357 {
358 return GC_malloc_kind(lb, NORMAL);
359 }
360
361 GC_API GC_ATTR_MALLOC void * GC_CALL GC_generic_malloc_uncollectable(
362 size_t lb, int k)
363 {
364 void *op;
365 DCL_LOCK_STATE;
366
367 GC_ASSERT(k < MAXOBJKINDS);
368 if (SMALL_OBJ(lb)) {
369 void **opp;
370 size_t lg;
371
372 GC_DBG_COLLECT_AT_MALLOC(lb);
373 if (EXTRA_BYTES != 0 && lb != 0) lb--;
374 /* We don't need the extra byte, since this won't be */
375 /* collected anyway. */
376 LOCK();
377 lg = GC_size_map[lb];
378 opp = &GC_obj_kinds[k].ok_freelist[lg];
379 op = *opp;
380 if (EXPECT(op != NULL, TRUE)) {
381 *opp = obj_link(op);
382 obj_link(op) = 0;
383 GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
384 /* Mark bit was already set on free list. It will be */
385 /* cleared only temporarily during a collection, as a */
386 /* result of the normal free list mark bit clearing. */
387 GC_non_gc_bytes += GRANULES_TO_BYTES((word)lg);
388 UNLOCK();
389 } else {
390 UNLOCK();
391 op = GC_generic_malloc(lb, k);
392 /* For small objects, the free lists are completely marked. */
393 }
394 GC_ASSERT(0 == op || GC_is_marked(op));
395 } else {
396 op = GC_generic_malloc(lb, k);
397 if (op /* != NULL */) { /* CPPCHECK */
398 hdr * hhdr = HDR(op);
399
400 GC_ASSERT(((word)op & (HBLKSIZE - 1)) == 0); /* large block */
401 /* We don't need the lock here, since we have an undisguised */
402 /* pointer. We do need to hold the lock while we adjust */
403 /* mark bits. */
404 LOCK();
405 set_mark_bit_from_hdr(hhdr, 0); /* Only object. */
406 # ifndef THREADS
407 GC_ASSERT(hhdr -> hb_n_marks == 0);
408 /* This is not guaranteed in the multi-threaded case */
409 /* because the counter could be updated before locking. */
410 # endif
411 hhdr -> hb_n_marks = 1;
412 UNLOCK();
413 }
414 }
415 return op;
416 }
417
418 /* Allocate lb bytes of pointerful, traced, but not collectible data. */
419 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_uncollectable(size_t lb)
420 {
421 return GC_generic_malloc_uncollectable(lb, UNCOLLECTABLE);
422 }
423
424 #ifdef GC_ATOMIC_UNCOLLECTABLE
425 /* Allocate lb bytes of pointer-free, untraced, uncollectible data */
426 /* This is normally roughly equivalent to the system malloc. */
427 /* But it may be useful if malloc is redefined. */
428 GC_API GC_ATTR_MALLOC void * GC_CALL
429 GC_malloc_atomic_uncollectable(size_t lb)
430 {
431 return GC_generic_malloc_uncollectable(lb, AUNCOLLECTABLE);
432 }
433 #endif /* GC_ATOMIC_UNCOLLECTABLE */
434
435 #if defined(REDIRECT_MALLOC) && !defined(REDIRECT_MALLOC_IN_HEADER)
436
437 # ifndef MSWINCE
438 # include <errno.h>
439 # endif
440
441 /* Avoid unnecessary nested procedure calls here, by #defining some */
442 /* malloc replacements. Otherwise we end up saving a meaningless */
443 /* return address in the object. It also speeds things up, but it is */
444 /* admittedly quite ugly. */
445 # define GC_debug_malloc_replacement(lb) GC_debug_malloc(lb, GC_DBG_EXTRAS)
446
447 # if defined(CPPCHECK)
448 # define REDIRECT_MALLOC_F GC_malloc /* e.g. */
449 # else
450 # define REDIRECT_MALLOC_F REDIRECT_MALLOC
451 # endif
452
453 void * malloc(size_t lb)
454 {
455 /* It might help to manually inline the GC_malloc call here. */
456 /* But any decent compiler should reduce the extra procedure call */
457 /* to at most a jump instruction in this case. */
458 # if defined(I386) && defined(GC_SOLARIS_THREADS)
459 /* Thread initialization can call malloc before we are ready for. */
460 /* It is not clear that this is enough to help matters. */
461 /* The thread implementation may well call malloc at other */
462 /* inopportune times. */
463 if (!EXPECT(GC_is_initialized, TRUE)) return sbrk(lb);
464 # endif
465 return (void *)REDIRECT_MALLOC_F(lb);
466 }
467
468 # if defined(GC_LINUX_THREADS)
469 STATIC ptr_t GC_libpthread_start = 0;
470 STATIC ptr_t GC_libpthread_end = 0;
471 STATIC ptr_t GC_libld_start = 0;
472 STATIC ptr_t GC_libld_end = 0;
473 static GC_bool lib_bounds_set = FALSE;
474
475 GC_INNER void GC_init_lib_bounds(void)
476 {
477 IF_CANCEL(int cancel_state;)
478 DCL_LOCK_STATE;
479
480 /* This test does not need to ensure memory visibility, since */
481 /* the bounds will be set when/if we create another thread. */
482 if (EXPECT(lib_bounds_set, TRUE)) return;
483
484 DISABLE_CANCEL(cancel_state);
485 GC_init(); /* if not called yet */
486 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
487 LOCK(); /* just to set GC_lock_holder */
488 # endif
489 if (!GC_text_mapping("libpthread-",
490 &GC_libpthread_start, &GC_libpthread_end)) {
491 /* Some libc implementations like bionic, musl and glibc 2.34 */
492 /* do not have libpthread.so because the pthreads-related code */
493 /* is located in libc.so, thus potential calloc calls from such */
494 /* code are forwarded to real (libc) calloc without any special */
495 /* handling on the libgc side. Checking glibc version at */
496 /* compile time to turn off the warning seems to be fine. */
497 /* TODO: Remove GC_text_mapping() call for this case. */
498 # if defined(__GLIBC__) \
499 && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 34))
500 WARN("Failed to find libpthread.so text mapping: Expect crash\n", 0);
501 /* This might still work with some versions of libpthread, */
502 /* so we do not abort. */
503 # endif
504 }
505 if (!GC_text_mapping("ld-", &GC_libld_start, &GC_libld_end)) {
506 WARN("Failed to find ld.so text mapping: Expect crash\n", 0);
507 }
508 # if defined(GC_ASSERTIONS) && defined(GC_ALWAYS_MULTITHREADED)
509 UNLOCK();
510 # endif
511 RESTORE_CANCEL(cancel_state);
512 lib_bounds_set = TRUE;
513 }
514 # endif /* GC_LINUX_THREADS */
515
516 void * calloc(size_t n, size_t lb)
517 {
518 if ((lb | n) > GC_SQRT_SIZE_MAX /* fast initial test */
519 && lb && n > GC_SIZE_MAX / lb)
520 return (*GC_get_oom_fn())(GC_SIZE_MAX); /* n*lb overflow */
521 # if defined(GC_LINUX_THREADS)
522 /* libpthread allocated some memory that is only pointed to by */
523 /* mmapped thread stacks. Make sure it is not collectible. */
524 {
525 ptr_t caller = (ptr_t)__builtin_return_address(0);
526
527 GC_init_lib_bounds();
528 if (((word)caller >= (word)GC_libpthread_start
529 && (word)caller < (word)GC_libpthread_end)
530 || ((word)caller >= (word)GC_libld_start
531 && (word)caller < (word)GC_libld_end))
532 return GC_generic_malloc_uncollectable(n * lb, UNCOLLECTABLE);
533 /* The two ranges are actually usually adjacent, so there may */
534 /* be a way to speed this up. */
535 }
536 # endif
537 return (void *)REDIRECT_MALLOC_F(n * lb);
538 }
539
540 # ifndef strdup
541 char *strdup(const char *s)
542 {
543 size_t lb = strlen(s) + 1;
544 char *result = (char *)REDIRECT_MALLOC_F(lb);
545 if (result == 0) {
546 errno = ENOMEM;
547 return 0;
548 }
549 BCOPY(s, result, lb);
550 return result;
551 }
552 # endif /* !defined(strdup) */
553 /* If strdup is macro defined, we assume that it actually calls malloc, */
554 /* and thus the right thing will happen even without overriding it. */
555 /* This seems to be true on most Linux systems. */
556
557 # ifndef strndup
558 /* This is similar to strdup(). */
559 char *strndup(const char *str, size_t size)
560 {
561 char *copy;
562 size_t len = strlen(str);
563 if (len > size)
564 len = size;
565 copy = (char *)REDIRECT_MALLOC_F(len + 1);
566 if (copy == NULL) {
567 errno = ENOMEM;
568 return NULL;
569 }
570 if (EXPECT(len > 0, TRUE))
571 BCOPY(str, copy, len);
572 copy[len] = '\0';
573 return copy;
574 }
575 # endif /* !strndup */
576
577 # undef GC_debug_malloc_replacement
578
579 #endif /* REDIRECT_MALLOC */
580
581 /* Explicitly deallocate an object p. */
582 GC_API void GC_CALL GC_free(void * p)
583 {
584 struct hblk *h;
585 hdr *hhdr;
586 size_t sz; /* In bytes */
587 size_t ngranules; /* sz in granules */
588 int knd;
589 struct obj_kind * ok;
590 DCL_LOCK_STATE;
591
592 if (p /* != NULL */) {
593 /* CPPCHECK */
594 } else {
595 /* Required by ANSI. It's not my fault ... */
596 return;
597 }
598
599 # ifdef LOG_ALLOCS
600 GC_log_printf("GC_free(%p) after GC #%lu\n",
601 p, (unsigned long)GC_gc_no);
602 # endif
603 h = HBLKPTR(p);
604 hhdr = HDR(h);
605 # if defined(REDIRECT_MALLOC) && \
606 ((defined(NEED_CALLINFO) && defined(GC_HAVE_BUILTIN_BACKTRACE)) \
607 || defined(GC_SOLARIS_THREADS) || defined(GC_LINUX_THREADS) \
608 || defined(MSWIN32))
609 /* This might be called indirectly by GC_print_callers to free */
610 /* the result of backtrace_symbols. */
611 /* For Solaris, we have to redirect malloc calls during */
612 /* initialization. For the others, this seems to happen */
613 /* implicitly. */
614 /* Don't try to deallocate that memory. */
615 if (0 == hhdr) return;
616 # endif
617 GC_ASSERT(GC_base(p) == p);
618 sz = (size_t)hhdr->hb_sz;
619 ngranules = BYTES_TO_GRANULES(sz);
620 knd = hhdr -> hb_obj_kind;
621 ok = &GC_obj_kinds[knd];
622 if (EXPECT(ngranules <= MAXOBJGRANULES, TRUE)) {
623 void **flh;
624
625 LOCK();
626 GC_bytes_freed += sz;
627 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
628 /* It's unnecessary to clear the mark bit. If the */
629 /* object is reallocated, it doesn't matter. O.w. the */
630 /* collector will do it, since it's on a free list. */
631 if (ok -> ok_init && EXPECT(sz > sizeof(word), TRUE)) {
632 BZERO((word *)p + 1, sz-sizeof(word));
633 }
634 flh = &(ok -> ok_freelist[ngranules]);
635 obj_link(p) = *flh;
636 *flh = (ptr_t)p;
637 UNLOCK();
638 } else {
639 size_t nblocks = OBJ_SZ_TO_BLOCKS(sz);
640
641 LOCK();
642 GC_bytes_freed += sz;
643 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
644 if (nblocks > 1) {
645 GC_large_allocd_bytes -= nblocks * HBLKSIZE;
646 }
647 GC_freehblk(h);
648 UNLOCK();
649 }
650 }
651
652 /* Explicitly deallocate an object p when we already hold lock. */
653 /* Only used for internally allocated objects, so we can take some */
654 /* shortcuts. */
655 #ifdef THREADS
656 GC_INNER void GC_free_inner(void * p)
657 {
658 struct hblk *h;
659 hdr *hhdr;
660 size_t sz; /* bytes */
661 size_t ngranules; /* sz in granules */
662 int knd;
663 struct obj_kind * ok;
664
665 h = HBLKPTR(p);
666 hhdr = HDR(h);
667 knd = hhdr -> hb_obj_kind;
668 sz = (size_t)hhdr->hb_sz;
669 ngranules = BYTES_TO_GRANULES(sz);
670 ok = &GC_obj_kinds[knd];
671 if (ngranules <= MAXOBJGRANULES) {
672 void ** flh;
673
674 GC_bytes_freed += sz;
675 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
676 if (ok -> ok_init && EXPECT(sz > sizeof(word), TRUE)) {
677 BZERO((word *)p + 1, sz-sizeof(word));
678 }
679 flh = &(ok -> ok_freelist[ngranules]);
680 obj_link(p) = *flh;
681 *flh = (ptr_t)p;
682 } else {
683 size_t nblocks = OBJ_SZ_TO_BLOCKS(sz);
684 GC_bytes_freed += sz;
685 if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
686 if (nblocks > 1) {
687 GC_large_allocd_bytes -= nblocks * HBLKSIZE;
688 }
689 GC_freehblk(h);
690 }
691 }
692 #endif /* THREADS */
693
694 #if defined(REDIRECT_MALLOC) && !defined(REDIRECT_FREE)
695 # define REDIRECT_FREE GC_free
696 #endif
697
698 #if defined(REDIRECT_FREE) && !defined(REDIRECT_MALLOC_IN_HEADER)
699
700 # if defined(CPPCHECK)
701 # define REDIRECT_FREE_F GC_free /* e.g. */
702 # else
703 # define REDIRECT_FREE_F REDIRECT_FREE
704 # endif
705
706 void free(void * p GC_ATTR_UNUSED)
707 {
708 # ifndef IGNORE_FREE
709 # if defined(GC_LINUX_THREADS) && !defined(USE_PROC_FOR_LIBRARIES)
710 /* Don't bother with initialization checks. If nothing */
711 /* has been initialized, the check fails, and that's safe, */
712 /* since we have not allocated uncollectible objects neither. */
713 ptr_t caller = (ptr_t)__builtin_return_address(0);
714 /* This test does not need to ensure memory visibility, since */
715 /* the bounds will be set when/if we create another thread. */
716 if (((word)caller >= (word)GC_libpthread_start
717 && (word)caller < (word)GC_libpthread_end)
718 || ((word)caller >= (word)GC_libld_start
719 && (word)caller < (word)GC_libld_end)) {
720 GC_free(p);
721 return;
722 }
723 # endif
724 REDIRECT_FREE_F(p);
725 # endif
726 }
727 #endif /* REDIRECT_FREE */