]> granicus.if.org Git - gc/blob - malloc.c
Workaround 'argument to function is always 1' cppcheck false positives
[gc] / malloc.c
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.     */
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)
39 {
40     struct hblk * h;
41     word n_blocks;
42     ptr_t result;
43     GC_bool retry = FALSE;
44
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)) {
49       DCL_LOCK_STATE;
50       UNLOCK(); /* just to unset GC_lock_holder */
51       GC_init();
52       LOCK();
53     }
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);
58 #   ifdef USE_MUNMAP
59         if (0 == h) {
60             GC_merge_unmapped();
61             h = GC_allochblk(lb, k, flags);
62         }
63 #   endif
64     while (0 == h && GC_collect_or_expand(n_blocks, flags != 0, retry)) {
65         h = GC_allochblk(lb, k, flags);
66         retry = TRUE;
67     }
68     if (h == 0) {
69         result = 0;
70     } else {
71         size_t total_bytes = n_blocks * HBLKSIZE;
72         if (n_blocks > 1) {
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;
76         }
77         /* FIXME: Do we need some way to reset GC_max_large_allocd_bytes? */
78         result = h -> hb_body;
79     }
80     return result;
81 }
82
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)
86 {
87     ptr_t result;
88
89     GC_ASSERT(I_HOLD_LOCK());
90     result = GC_alloc_large(lb, k, flags);
91     if (result != NULL
92           && (GC_debugging_started || GC_obj_kinds[k].ok_init)) {
93         word n_blocks = OBJ_SZ_TO_BLOCKS(lb);
94
95         /* Clear the whole block, in case of GC_realloc call. */
96         BZERO(result, n_blocks * HBLKSIZE);
97     }
98     return result;
99 }
100
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)
105 {
106   size_t orig_granule_sz = ROUNDED_UP_GRANULES(i);
107   size_t granule_sz;
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;
115
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)
122       low_limit++;
123   } else {
124     low_limit = smaller_than_i + 1;
125     while (GC_size_map[low_limit] != 0)
126       low_limit++;
127
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;
132   }
133
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;
140
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;
145
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].                */
149
150   for (; low_limit <= byte_sz; low_limit++)
151     GC_size_map[low_limit] = granule_sz;
152 }
153
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)
158 {
159     void *op;
160
161     GC_ASSERT(I_HOLD_LOCK());
162     GC_ASSERT(k < MAXOBJKINDS);
163     if (SMALL_OBJ(lb)) {
164         struct obj_kind * kind = GC_obj_kinds + k;
165         size_t lg = GC_size_map[lb];
166         void ** opp = &(kind -> ok_freelist[lg]);
167
168         op = *opp;
169         if (EXPECT(0 == op, FALSE)) {
170           if (lg == 0) {
171             if (!EXPECT(GC_is_initialized, TRUE)) {
172               DCL_LOCK_STATE;
173               UNLOCK(); /* just to unset GC_lock_holder */
174               GC_init();
175               LOCK();
176               lg = GC_size_map[lb];
177             }
178             if (0 == lg) {
179               GC_extend_size_map(lb);
180               lg = GC_size_map[lb];
181               GC_ASSERT(lg != 0);
182             }
183             /* Retry */
184             opp = &(kind -> ok_freelist[lg]);
185             op = *opp;
186           }
187           if (0 == op) {
188             if (0 == kind -> ok_reclaim_list &&
189                 !GC_alloc_reclaim_list(kind))
190               return NULL;
191             op = GC_allocobj(lg, k);
192             if (0 == op)
193               return NULL;
194           }
195         }
196         *opp = obj_link(op);
197         obj_link(op) = 0;
198         GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
199     } else {
200         op = (ptr_t)GC_alloc_large_and_clear(ADD_SLOP(lb), k, 0);
201         if (op != NULL)
202             GC_bytes_allocd += lb;
203     }
204
205     return op;
206 }
207
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)
213   {
214     word lb_adjusted;
215     void * op;
216
217     GC_ASSERT(I_HOLD_LOCK());
218     if (lb <= HBLKSIZE)
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);
223     if (op != NULL)
224         GC_bytes_allocd += lb_adjusted;
225     return op;
226   }
227 #endif
228
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. */
234 # else
235     size_t GC_dbg_collect_at_malloc_min_lb = (GC_COLLECT_AT_MALLOC);
236 # endif
237 #endif
238
239 GC_API GC_ATTR_MALLOC void * GC_CALL GC_generic_malloc(size_t lb, int k)
240 {
241     void * result;
242     DCL_LOCK_STATE;
243
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);
249     if (SMALL_OBJ(lb)) {
250         LOCK();
251         result = GC_generic_malloc_inner(lb, k);
252         UNLOCK();
253     } else {
254         size_t lg;
255         size_t lb_rounded;
256         word n_blocks;
257         GC_bool init;
258
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;
263         LOCK();
264         result = (ptr_t)GC_alloc_large(lb_rounded, k, 0);
265         if (0 != result) {
266           if (GC_debugging_started) {
267             BZERO(result, n_blocks * HBLKSIZE);
268           } else {
269 #           ifdef THREADS
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;
276 #           endif
277           }
278           GC_bytes_allocd += lb_rounded;
279         }
280         UNLOCK();
281         if (init && !GC_debugging_started && 0 != result) {
282             BZERO(result, n_blocks * HBLKSIZE);
283         }
284     }
285     if (0 == result) {
286         return((*GC_get_oom_fn())(lb));
287     } else {
288         return(result);
289     }
290 }
291
292 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_kind_global(size_t lb, int k)
293 {
294     GC_ASSERT(k < MAXOBJKINDS);
295     if (SMALL_OBJ(lb)) {
296         void *op;
297         void **opp;
298         size_t lg;
299         DCL_LOCK_STATE;
300
301         GC_DBG_COLLECT_AT_MALLOC(lb);
302         LOCK();
303         lg = GC_size_map[lb];
304         opp = &GC_obj_kinds[k].ok_freelist[lg];
305         op = *opp;
306         if (EXPECT(op != NULL, TRUE)) {
307             if (k == PTRFREE) {
308                 *opp = obj_link(op);
309             } else {
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));
315                 *opp = obj_link(op);
316                 obj_link(op) = 0;
317             }
318             GC_bytes_allocd += GRANULES_TO_BYTES((word)lg);
319             UNLOCK();
320             return op;
321         }
322         UNLOCK();
323     }
324
325     /* We make the GC_clear_stack() call a tail one, hoping to get more */
326     /* of the stack.                                                    */
327     return GC_clear_stack(GC_generic_malloc(lb, k));
328 }
329
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)
332   {
333     return GC_malloc_kind_global(lb, k);
334   }
335 #endif
336
337 /* Allocate lb bytes of atomic (pointer-free) data.     */
338 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_atomic(size_t lb)
339 {
340     return GC_malloc_kind(lb, PTRFREE);
341 }
342
343 /* Allocate lb bytes of composite (pointerful) data.    */
344 GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc(size_t lb)
345 {
346     return GC_malloc_kind(lb, NORMAL);
347 }
348
349 GC_API GC_ATTR_MALLOC void * GC_CALL GC_generic_malloc_uncollectable(
350                                                         size_t lb, int k)
351 {
352     void *op;
353     DCL_LOCK_STATE;
354
355     GC_ASSERT(k < MAXOBJKINDS);
356     if (SMALL_OBJ(lb)) {
357         void **opp;
358         size_t lg;
359
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.                                  */
364         LOCK();
365         lg = GC_size_map[lb];
366         opp = &GC_obj_kinds[k].ok_freelist[lg];
367         op = *opp;
368         if (EXPECT(op != NULL, TRUE)) {
369             *opp = obj_link(op);
370             obj_link(op) = 0;
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);
376             UNLOCK();
377         } else {
378             UNLOCK();
379             op = GC_generic_malloc(lb, k);
380             /* For small objects, the free lists are completely marked. */
381         }
382         GC_ASSERT(0 == op || GC_is_marked(op));
383     } else {
384       op = GC_generic_malloc(lb, k);
385       if (op /* != NULL */) { /* CPPCHECK */
386         hdr * hhdr = HDR(op);
387
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        */
391         /* mark bits.                                                   */
392         LOCK();
393         set_mark_bit_from_hdr(hhdr, 0); /* Only object. */
394 #       ifndef THREADS
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. */
398 #       endif
399         hhdr -> hb_n_marks = 1;
400         UNLOCK();
401       }
402     }
403     return op;
404 }
405
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)
408 {
409   return GC_generic_malloc_uncollectable(lb, UNCOLLECTABLE);
410 }
411
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)
418   {
419     return GC_generic_malloc_uncollectable(lb, AUNCOLLECTABLE);
420   }
421 #endif /* GC_ATOMIC_UNCOLLECTABLE */
422
423 #if defined(REDIRECT_MALLOC) && !defined(REDIRECT_MALLOC_IN_HEADER)
424
425 # ifndef MSWINCE
426 #  include <errno.h>
427 # endif
428
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)
434
435 # if defined(CPPCHECK)
436 #   define REDIRECT_MALLOC_F GC_malloc /* e.g. */
437 # else
438 #   define REDIRECT_MALLOC_F REDIRECT_MALLOC
439 # endif
440
441   void * malloc(size_t lb)
442   {
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);
452 #   endif
453     return (void *)REDIRECT_MALLOC_F(lb);
454   }
455
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;
461
462     STATIC void GC_init_lib_bounds(void)
463     {
464       IF_CANCEL(int cancel_state;)
465
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;
476       }
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);
479       }
480       RESTORE_CANCEL(cancel_state);
481     }
482 # endif /* GC_LINUX_THREADS */
483
484   void * calloc(size_t n, size_t lb)
485   {
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.       */
492       {
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;
500         }
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.                                   */
508       }
509 #   endif
510     return (void *)REDIRECT_MALLOC_F(n * lb);
511   }
512
513 # ifndef strdup
514     char *strdup(const char *s)
515     {
516       size_t lb = strlen(s) + 1;
517       char *result = (char *)REDIRECT_MALLOC_F(lb);
518       if (result == 0) {
519         errno = ENOMEM;
520         return 0;
521       }
522       BCOPY(s, result, lb);
523       return result;
524     }
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.                         */
529
530 # ifndef strndup
531     /* This is similar to strdup().     */
532     char *strndup(const char *str, size_t size)
533     {
534       char *copy;
535       size_t len = strlen(str);
536       if (len > size)
537         len = size;
538       copy = (char *)REDIRECT_MALLOC_F(len + 1);
539       if (copy == NULL) {
540         errno = ENOMEM;
541         return NULL;
542       }
543       if (EXPECT(len > 0, TRUE))
544         BCOPY(str, copy, len);
545       copy[len] = '\0';
546       return copy;
547     }
548 # endif /* !strndup */
549
550 # undef GC_debug_malloc_replacement
551
552 #endif /* REDIRECT_MALLOC */
553
554 /* Explicitly deallocate an object p.                           */
555 GC_API void GC_CALL GC_free(void * p)
556 {
557     struct hblk *h;
558     hdr *hhdr;
559     size_t sz; /* In bytes */
560     size_t ngranules;   /* sz in granules */
561     int knd;
562     struct obj_kind * ok;
563     DCL_LOCK_STATE;
564
565     if (p /* != NULL */) {
566         /* CPPCHECK */
567     } else {
568         /* Required by ANSI.  It's not my fault ...     */
569         return;
570     }
571
572 #   ifdef LOG_ALLOCS
573       GC_log_printf("GC_free(%p) after GC #%lu\n",
574                     p, (unsigned long)GC_gc_no);
575 #   endif
576     h = HBLKPTR(p);
577     hhdr = HDR(h);
578 #   if defined(REDIRECT_MALLOC) && \
579         ((defined(NEED_CALLINFO) && defined(GC_HAVE_BUILTIN_BACKTRACE)) \
580          || defined(GC_SOLARIS_THREADS) || defined(GC_LINUX_THREADS) \
581          || defined(MSWIN32))
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        */
586         /* implicitly.                                                  */
587         /* Don't try to deallocate that memory.                         */
588         if (0 == hhdr) return;
589 #   endif
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)) {
596         void **flh;
597
598         LOCK();
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));
606         }
607         flh = &(ok -> ok_freelist[ngranules]);
608         obj_link(p) = *flh;
609         *flh = (ptr_t)p;
610         UNLOCK();
611     } else {
612         size_t nblocks = OBJ_SZ_TO_BLOCKS(sz);
613
614         LOCK();
615         GC_bytes_freed += sz;
616         if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
617         if (nblocks > 1) {
618           GC_large_allocd_bytes -= nblocks * HBLKSIZE;
619         }
620         GC_freehblk(h);
621         UNLOCK();
622     }
623 }
624
625 /* Explicitly deallocate an object p when we already hold lock.         */
626 /* Only used for internally allocated objects, so we can take some      */
627 /* shortcuts.                                                           */
628 #ifdef THREADS
629   GC_INNER void GC_free_inner(void * p)
630   {
631     struct hblk *h;
632     hdr *hhdr;
633     size_t sz; /* bytes */
634     size_t ngranules;  /* sz in granules */
635     int knd;
636     struct obj_kind * ok;
637
638     h = HBLKPTR(p);
639     hhdr = HDR(h);
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) {
645         void ** flh;
646
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));
651         }
652         flh = &(ok -> ok_freelist[ngranules]);
653         obj_link(p) = *flh;
654         *flh = (ptr_t)p;
655     } else {
656         size_t nblocks = OBJ_SZ_TO_BLOCKS(sz);
657         GC_bytes_freed += sz;
658         if (IS_UNCOLLECTABLE(knd)) GC_non_gc_bytes -= sz;
659         if (nblocks > 1) {
660           GC_large_allocd_bytes -= nblocks * HBLKSIZE;
661         }
662         GC_freehblk(h);
663     }
664   }
665 #endif /* THREADS */
666
667 #if defined(REDIRECT_MALLOC) && !defined(REDIRECT_FREE)
668 # define REDIRECT_FREE GC_free
669 #endif
670
671 #if defined(REDIRECT_FREE) && !defined(REDIRECT_MALLOC_IN_HEADER)
672
673 # if defined(CPPCHECK)
674 #   define REDIRECT_FREE_F GC_free /* e.g. */
675 # else
676 #   define REDIRECT_FREE_F REDIRECT_FREE
677 # endif
678
679   void free(void * p)
680   {
681 #   ifndef IGNORE_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)) {
693           GC_free(p);
694           return;
695         }
696 #     endif
697       REDIRECT_FREE_F(p);
698 #   endif
699   }
700 #endif /* REDIRECT_FREE */