Fix 'void pointers in calculations: behavior undefined' cppcheck warning
[platform/upstream/libgc.git] / dbg_mlc.c
1 /*
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1995 by Xerox Corporation.  All rights reserved.
4  * Copyright (c) 1997 by Silicon Graphics.  All rights reserved.
5  * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
6  * Copyright (C) 2007 Free Software Foundation, Inc
7  *
8  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
10  *
11  * Permission is hereby granted to use or copy this program
12  * for any purpose,  provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  */
17
18 #include "private/dbg_mlc.h"
19
20 #ifndef MSWINCE
21 # include <errno.h>
22 #endif
23 #include <string.h>
24
25 #ifndef SHORT_DBG_HDRS
26   /* Check whether object with base pointer p has debugging info. */
27   /* p is assumed to point to a legitimate object in our part     */
28   /* of the heap.                                                 */
29   /* This excludes the check as to whether the back pointer is    */
30   /* odd, which is added by the GC_HAS_DEBUG_INFO macro.          */
31   /* Note that if DBG_HDRS_ALL is set, uncollectible objects      */
32   /* on free lists may not have debug information set.  Thus it's */
33   /* not always safe to return TRUE (1), even if the client does  */
34   /* its part.  Return -1 if the object with debug info has been  */
35   /* marked as deallocated.                                       */
36   GC_INNER int GC_has_other_debug_info(ptr_t p)
37   {
38     ptr_t body = (ptr_t)((oh *)p + 1);
39     word sz = GC_size(p);
40
41     if (HBLKPTR(p) != HBLKPTR((ptr_t)body)
42         || sz < DEBUG_BYTES + EXTRA_BYTES) {
43       return 0;
44     }
45     if (((oh *)p) -> oh_sf != (START_FLAG ^ (word)body)
46         && ((word *)p)[BYTES_TO_WORDS(sz)-1] != (END_FLAG ^ (word)body)) {
47       return 0;
48     }
49     if (((oh *)p)->oh_sz == sz) {
50       /* Object may have had debug info, but has been deallocated     */
51       return -1;
52     }
53     return 1;
54   }
55 #endif /* !SHORT_DBG_HDRS */
56
57 #ifdef KEEP_BACK_PTRS
58
59 # include <stdlib.h>
60
61 # if defined(__GLIBC__) || defined(SOLARIS) \
62      || defined(HPUX) || defined(IRIX5) || defined(OSF1)
63 #   define RANDOM() random()
64 # else
65 #   define RANDOM() (long)rand()
66 # endif
67
68   /* Store back pointer to source in dest, if that appears to be possible. */
69   /* This is not completely safe, since we may mistakenly conclude that    */
70   /* dest has a debugging wrapper.  But the error probability is very      */
71   /* small, and this shouldn't be used in production code.                 */
72   /* We assume that dest is the real base pointer.  Source will usually    */
73   /* be a pointer to the interior of an object.                            */
74   GC_INNER void GC_store_back_pointer(ptr_t source, ptr_t dest)
75   {
76     if (GC_HAS_DEBUG_INFO(dest)) {
77       ((oh *)dest) -> oh_back_ptr = HIDE_BACK_PTR(source);
78     }
79   }
80
81   GC_INNER void GC_marked_for_finalization(ptr_t dest)
82   {
83     GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
84   }
85
86   /* Store information about the object referencing dest in *base_p     */
87   /* and *offset_p.                                                     */
88   /*   source is root ==> *base_p = address, *offset_p = 0              */
89   /*   source is heap object ==> *base_p != 0, *offset_p = offset       */
90   /*   Returns 1 on success, 0 if source couldn't be determined.        */
91   /* Dest can be any address within a heap object.                      */
92   GC_API GC_ref_kind GC_CALL GC_get_back_ptr_info(void *dest, void **base_p,
93                                                   size_t *offset_p)
94   {
95     oh * hdr = (oh *)GC_base(dest);
96     ptr_t bp;
97     ptr_t bp_base;
98
99 #   ifdef LINT2
100       /* Explicitly instruct the code analysis tool that                */
101       /* GC_get_back_ptr_info is not expected to be called with an      */
102       /* incorrect "dest" value.                                        */
103       if (!hdr) ABORT("Invalid GC_get_back_ptr_info argument");
104 #   endif
105     if (!GC_HAS_DEBUG_INFO((ptr_t) hdr)) return GC_NO_SPACE;
106     bp = GC_REVEAL_POINTER(hdr -> oh_back_ptr);
107     if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
108     if (MARKED_FROM_REGISTER == bp) return GC_REFD_FROM_REG;
109     if (NOT_MARKED == bp) return GC_UNREFERENCED;
110 #   if ALIGNMENT == 1
111       /* Heuristically try to fix off by 1 errors we introduced by      */
112       /* insisting on even addresses.                                   */
113       {
114         ptr_t alternate_ptr = bp + 1;
115         ptr_t target = *(ptr_t *)bp;
116         ptr_t alternate_target = *(ptr_t *)alternate_ptr;
117
118         if ((word)alternate_target >= (word)GC_least_plausible_heap_addr
119             && (word)alternate_target <= (word)GC_greatest_plausible_heap_addr
120             && ((word)target < (word)GC_least_plausible_heap_addr
121                 || (word)target > (word)GC_greatest_plausible_heap_addr)) {
122             bp = alternate_ptr;
123         }
124       }
125 #   endif
126     bp_base = GC_base(bp);
127     if (0 == bp_base) {
128       *base_p = bp;
129       *offset_p = 0;
130       return GC_REFD_FROM_ROOT;
131     } else {
132       if (GC_HAS_DEBUG_INFO(bp_base)) bp_base += sizeof(oh);
133       *base_p = bp_base;
134       *offset_p = bp - bp_base;
135       return GC_REFD_FROM_HEAP;
136     }
137   }
138
139   /* Generate a random heap address.            */
140   /* The resulting address is in the heap, but  */
141   /* not necessarily inside a valid object.     */
142   GC_API void * GC_CALL GC_generate_random_heap_address(void)
143   {
144     size_t i;
145     word heap_offset = RANDOM();
146
147     if (GC_heapsize > RAND_MAX) {
148         heap_offset *= RAND_MAX;
149         heap_offset += RANDOM();
150     }
151     heap_offset %= GC_heapsize;
152         /* This doesn't yield a uniform distribution, especially if     */
153         /* e.g. RAND_MAX = 1.5* GC_heapsize.  But for typical cases,    */
154         /* it's not too bad.                                            */
155     for (i = 0;; ++i) {
156         size_t size;
157
158         if (i >= GC_n_heap_sects)
159           ABORT("GC_generate_random_heap_address: size inconsistency");
160
161         size = GC_heap_sects[i].hs_bytes;
162         if (heap_offset < size) {
163             break;
164         } else {
165             heap_offset -= size;
166         }
167     }
168     return GC_heap_sects[i].hs_start + heap_offset;
169   }
170
171   /* Generate a random address inside a valid marked heap object. */
172   GC_API void * GC_CALL GC_generate_random_valid_address(void)
173   {
174     ptr_t result;
175     ptr_t base;
176     do {
177       result = GC_generate_random_heap_address();
178       base = GC_base(result);
179     } while (base == 0 || !GC_is_marked(base));
180     return result;
181   }
182
183   /* Print back trace for p */
184   GC_API void GC_CALL GC_print_backtrace(void *p)
185   {
186     void *current = p;
187     int i;
188     GC_ref_kind source;
189     size_t offset;
190     void *base;
191
192     GC_print_heap_obj(GC_base(current));
193
194     for (i = 0; ; ++i) {
195       source = GC_get_back_ptr_info(current, &base, &offset);
196       if (GC_UNREFERENCED == source) {
197         GC_err_printf("Reference could not be found\n");
198         goto out;
199       }
200       if (GC_NO_SPACE == source) {
201         GC_err_printf("No debug info in object: Can't find reference\n");
202         goto out;
203       }
204       GC_err_printf("Reachable via %d levels of pointers from ", i);
205       switch(source) {
206         case GC_REFD_FROM_ROOT:
207           GC_err_printf("root at %p\n\n", base);
208           goto out;
209         case GC_REFD_FROM_REG:
210           GC_err_printf("root in register\n\n");
211           goto out;
212         case GC_FINALIZER_REFD:
213           GC_err_printf("list of finalizable objects\n\n");
214           goto out;
215         case GC_REFD_FROM_HEAP:
216           GC_err_printf("offset %ld in object:\n", (long)offset);
217           /* Take GC_base(base) to get real base, i.e. header. */
218           GC_print_heap_obj(GC_base(base));
219           break;
220         default:
221           GC_err_printf("INTERNAL ERROR: UNEXPECTED SOURCE!!!!\n");
222           goto out;
223       }
224       current = base;
225     }
226     out:;
227   }
228
229   /* Force a garbage collection and generate/print a backtrace  */
230   /* from a random heap address.                                */
231   GC_INNER void GC_generate_random_backtrace_no_gc(void)
232   {
233     void * current;
234     current = GC_generate_random_valid_address();
235     GC_printf("\n****Chosen address %p in object\n", current);
236     GC_print_backtrace(current);
237   }
238
239   GC_API void GC_CALL GC_generate_random_backtrace(void)
240   {
241     if (GC_try_to_collect(GC_never_stop_func) == 0) {
242       GC_err_printf("Cannot generate a backtrace: "
243                     "garbage collection is disabled!\n");
244       return;
245     }
246     GC_generate_random_backtrace_no_gc();
247   }
248
249 #endif /* KEEP_BACK_PTRS */
250
251 # define CROSSES_HBLK(p, sz) \
252         (((word)((p) + sizeof(oh) + (sz) - 1) ^ (word)(p)) >= HBLKSIZE)
253
254 /* Store debugging info into p.  Return displaced pointer.         */
255 /* This version assumes we do hold the allocation lock.            */
256 STATIC ptr_t GC_store_debug_info_inner(ptr_t p, word sz GC_ATTR_UNUSED,
257                                        const char *string, int linenum)
258 {
259     word * result = (word *)((oh *)p + 1);
260
261     GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
262     GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK(p, sz)));
263 #   ifdef KEEP_BACK_PTRS
264       ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
265 #   endif
266 #   ifdef MAKE_BACK_GRAPH
267       ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
268 #   endif
269     ((oh *)p) -> oh_string = string;
270     ((oh *)p) -> oh_int = (word)linenum;
271 #   ifndef SHORT_DBG_HDRS
272       ((oh *)p) -> oh_sz = sz;
273       ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
274       ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
275          result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
276 #   endif
277     return((ptr_t)result);
278 }
279
280 GC_INNER ptr_t GC_store_debug_info(ptr_t p, word sz, const char *string,
281                                    int linenum)
282 {
283     ptr_t result;
284     DCL_LOCK_STATE;
285
286     LOCK();
287     result = GC_store_debug_info_inner(p, sz, string, linenum);
288     UNLOCK();
289     return result;
290 }
291
292 #ifndef SHORT_DBG_HDRS
293   /* Check the object with debugging info at ohdr.      */
294   /* Return NULL if it's OK.  Else return clobbered     */
295   /* address.                                           */
296   STATIC ptr_t GC_check_annotated_obj(oh *ohdr)
297   {
298     ptr_t body = (ptr_t)(ohdr + 1);
299     word gc_sz = GC_size((ptr_t)ohdr);
300     if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
301         return((ptr_t)(&(ohdr -> oh_sz)));
302     }
303     if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
304         return((ptr_t)(&(ohdr -> oh_sf)));
305     }
306     if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
307         return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
308     }
309     if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
310         != (END_FLAG ^ (word)body)) {
311         return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr->oh_sz)));
312     }
313     return(0);
314   }
315 #endif /* !SHORT_DBG_HDRS */
316
317 STATIC GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
318
319 GC_API void GC_CALL GC_register_describe_type_fn(int kind,
320                                                  GC_describe_type_fn fn)
321 {
322   GC_describe_type_fns[kind] = fn;
323 }
324
325 #define GET_OH_LINENUM(ohdr) ((int)(ohdr)->oh_int)
326
327 #ifndef SHORT_DBG_HDRS
328 # define IF_NOT_SHORTDBG_HDRS(x) x
329 # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* comma */, x
330 #else
331 # define IF_NOT_SHORTDBG_HDRS(x) /* empty */
332 # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* empty */
333 #endif
334
335 /* Print a human-readable description of the object to stderr.          */
336 /* p points to somewhere inside an object with the debugging info.      */
337 STATIC void GC_print_obj(ptr_t p)
338 {
339     oh * ohdr = (oh *)GC_base(p);
340     ptr_t q;
341     hdr * hhdr;
342     int kind;
343     char *kind_str;
344     char buffer[GC_TYPE_DESCR_LEN + 1];
345
346     GC_ASSERT(I_DONT_HOLD_LOCK());
347 #   ifdef LINT2
348       if (!ohdr) ABORT("Invalid GC_print_obj argument");
349 #   endif
350
351     q = (ptr_t)(ohdr + 1);
352     /* Print a type description for the object whose client-visible     */
353     /* address is q.                                                    */
354     hhdr = GC_find_header(q);
355     kind = hhdr -> hb_obj_kind;
356     if (0 != GC_describe_type_fns[kind] && GC_is_marked(ohdr)) {
357         /* This should preclude free list objects except with   */
358         /* thread-local allocation.                             */
359         buffer[GC_TYPE_DESCR_LEN] = 0;
360         (GC_describe_type_fns[kind])(q, buffer);
361         GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
362         kind_str = buffer;
363     } else {
364         switch(kind) {
365           case PTRFREE:
366             kind_str = "PTRFREE";
367             break;
368           case NORMAL:
369             kind_str = "NORMAL";
370             break;
371           case UNCOLLECTABLE:
372             kind_str = "UNCOLLECTABLE";
373             break;
374 #         ifdef GC_ATOMIC_UNCOLLECTABLE
375             case AUNCOLLECTABLE:
376               kind_str = "ATOMIC_UNCOLLECTABLE";
377               break;
378 #         endif
379           case STUBBORN:
380             kind_str = "STUBBORN";
381             break;
382           default:
383             kind_str = NULL;
384                 /* The alternative is to use snprintf(buffer) but it is */
385                 /* not quite portable (see vsnprintf in misc.c).        */
386         }
387     }
388
389     if (NULL != kind_str) {
390         GC_err_printf("%p (%s:%d," IF_NOT_SHORTDBG_HDRS(" sz=%lu,") " %s)\n",
391                       (ptr_t)ohdr + sizeof(oh),
392                       ohdr->oh_string, GET_OH_LINENUM(ohdr) /*, */
393                       COMMA_IFNOT_SHORTDBG_HDRS((unsigned long)ohdr->oh_sz),
394                       kind_str);
395     } else {
396         GC_err_printf("%p (%s:%d," IF_NOT_SHORTDBG_HDRS(" sz=%lu,")
397                       " kind=%d descr=0x%lx)\n", (ptr_t)ohdr + sizeof(oh),
398                       ohdr->oh_string, GET_OH_LINENUM(ohdr) /*, */
399                       COMMA_IFNOT_SHORTDBG_HDRS((unsigned long)ohdr->oh_sz),
400                       kind, (unsigned long)hhdr->hb_descr);
401     }
402     PRINT_CALL_CHAIN(ohdr);
403 }
404
405 STATIC void GC_debug_print_heap_obj_proc(ptr_t p)
406 {
407     GC_ASSERT(I_DONT_HOLD_LOCK());
408     if (GC_HAS_DEBUG_INFO(p)) {
409         GC_print_obj(p);
410     } else {
411         GC_default_print_heap_obj_proc(p);
412     }
413 }
414
415 #ifndef SHORT_DBG_HDRS
416   /* Use GC_err_printf and friends to print a description of the object */
417   /* whose client-visible address is p, and which was smashed at        */
418   /* clobbered_addr.                                                    */
419   STATIC void GC_print_smashed_obj(const char *msg, ptr_t p,
420                                    ptr_t clobbered_addr)
421   {
422     oh * ohdr = (oh *)GC_base(p);
423
424     GC_ASSERT(I_DONT_HOLD_LOCK());
425 #   ifdef LINT2
426       if (!ohdr) ABORT("Invalid GC_print_smashed_obj argument");
427 #   endif
428     if ((word)clobbered_addr <= (word)(&ohdr->oh_sz)
429         || ohdr -> oh_string == 0) {
430         GC_err_printf(
431                 "%s %p in or near object at %p(<smashed>, appr. sz = %lu)\n",
432                 msg, clobbered_addr, p,
433                 (unsigned long)(GC_size((ptr_t)ohdr) - DEBUG_BYTES));
434     } else {
435         GC_err_printf("%s %p in or near object at %p (%s:%d, sz=%lu)\n",
436                 msg, clobbered_addr, p,
437                 (word)(ohdr -> oh_string) < HBLKSIZE ? "(smashed string)" :
438                 ohdr -> oh_string[0] == '\0' ? "EMPTY(smashed?)" :
439                                                 ohdr -> oh_string,
440                 GET_OH_LINENUM(ohdr), (unsigned long)(ohdr -> oh_sz));
441         PRINT_CALL_CHAIN(ohdr);
442     }
443   }
444 #endif
445
446 #ifndef SHORT_DBG_HDRS
447   STATIC void GC_check_heap_proc (void);
448   STATIC void GC_print_all_smashed_proc (void);
449 #else
450   STATIC void GC_do_nothing(void) {}
451 #endif
452
453 STATIC void GC_start_debugging_inner(void)
454 {
455   GC_ASSERT(I_HOLD_LOCK());
456 # ifndef SHORT_DBG_HDRS
457     GC_check_heap = GC_check_heap_proc;
458     GC_print_all_smashed = GC_print_all_smashed_proc;
459 # else
460     GC_check_heap = GC_do_nothing;
461     GC_print_all_smashed = GC_do_nothing;
462 # endif
463   GC_print_heap_obj = GC_debug_print_heap_obj_proc;
464   GC_debugging_started = TRUE;
465   GC_register_displacement_inner((word)sizeof(oh));
466 }
467
468 GC_INNER void GC_start_debugging(void)
469 {
470   DCL_LOCK_STATE;
471
472   LOCK();
473   GC_start_debugging_inner();
474   UNLOCK();
475 }
476
477 size_t GC_debug_header_size = sizeof(oh);
478
479 GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
480 {
481   DCL_LOCK_STATE;
482
483   LOCK();
484   GC_register_displacement_inner(offset);
485   GC_register_displacement_inner((word)sizeof(oh) + offset);
486   UNLOCK();
487 }
488
489 #ifdef GC_ADD_CALLER
490 # if defined(HAVE_DLADDR) && defined(GC_RETURN_ADDR_PARENT)
491 #   include <dlfcn.h>
492
493     STATIC void GC_caller_func_offset(word ad, const char **symp, int *offp)
494     {
495       Dl_info caller;
496
497       if (ad && dladdr((void *)ad, &caller) && caller.dli_sname != NULL) {
498         *symp = caller.dli_sname;
499         *offp = (int)((char *)ad - (char *)caller.dli_saddr);
500       }
501       if (NULL == *symp) {
502         *symp = "unknown";
503       }
504     }
505 # else
506 #   define GC_caller_func_offset(ad, symp, offp) (void)(*(symp) = "unknown")
507 # endif
508 #endif /* GC_ADD_CALLER */
509
510 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc(size_t lb,
511                                                      GC_EXTRA_PARAMS)
512 {
513     void * result;
514
515     /* Note that according to malloc() specification, if size is 0 then */
516     /* malloc() returns either NULL, or a unique pointer value that can */
517     /* later be successfully passed to free(). We always do the latter. */
518     result = GC_malloc(lb + DEBUG_BYTES);
519 #   ifdef GC_ADD_CALLER
520       if (s == NULL) {
521         GC_caller_func_offset(ra, &s, &i);
522       }
523 #   endif
524     if (result == 0) {
525         GC_err_printf("GC_debug_malloc(%lu) returning NULL (%s:%d)\n",
526                       (unsigned long)lb, s, i);
527         return(0);
528     }
529     if (!GC_debugging_started) {
530         GC_start_debugging();
531     }
532     ADD_CALL_CHAIN(result, ra);
533     return (GC_store_debug_info(result, (word)lb, s, i));
534 }
535
536 GC_API GC_ATTR_MALLOC void * GC_CALL
537     GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
538 {
539     void * result = GC_malloc_ignore_off_page(lb + DEBUG_BYTES);
540
541     if (result == 0) {
542         GC_err_printf("GC_debug_malloc_ignore_off_page(%lu)"
543                       " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
544         return(0);
545     }
546     if (!GC_debugging_started) {
547         GC_start_debugging();
548     }
549     ADD_CALL_CHAIN(result, ra);
550     return (GC_store_debug_info(result, (word)lb, s, i));
551 }
552
553 GC_API GC_ATTR_MALLOC void * GC_CALL
554     GC_debug_malloc_atomic_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
555 {
556     void * result = GC_malloc_atomic_ignore_off_page(lb + DEBUG_BYTES);
557
558     if (result == 0) {
559         GC_err_printf("GC_debug_malloc_atomic_ignore_off_page(%lu)"
560                       " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
561         return(0);
562     }
563     if (!GC_debugging_started) {
564         GC_start_debugging();
565     }
566     ADD_CALL_CHAIN(result, ra);
567     return (GC_store_debug_info(result, (word)lb, s, i));
568 }
569
570 STATIC void * GC_debug_generic_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
571 {
572     void * result = GC_generic_malloc(lb + DEBUG_BYTES, knd);
573
574     if (NULL == result) {
575         GC_err_printf(
576                 "GC_debug_generic_malloc(%lu, %d) returning NULL (%s:%d)\n",
577                 (unsigned long)lb, knd, s, i);
578         return NULL;
579     }
580     if (!GC_debugging_started) {
581         GC_start_debugging();
582     }
583     ADD_CALL_CHAIN(result, ra);
584     return GC_store_debug_info(result, (word)lb, s, i);
585 }
586
587 #ifdef DBG_HDRS_ALL
588   /* An allocation function for internal use.  Normally internally      */
589   /* allocated objects do not have debug information.  But in this      */
590   /* case, we need to make sure that all objects have debug headers.    */
591   /* We assume debugging was started in collector initialization, and   */
592   /* we already hold the GC lock.                                       */
593   GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
594   {
595     void * result = GC_generic_malloc_inner(lb + DEBUG_BYTES, k);
596
597     if (result == 0) {
598         GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
599                        (unsigned long) lb);
600         return(0);
601     }
602     if (!GC_debugging_started) {
603         GC_start_debugging_inner();
604     }
605     ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
606     return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
607   }
608
609   GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
610                                                                 int k)
611   {
612     void * result = GC_generic_malloc_inner_ignore_off_page(
613                                                 lb + DEBUG_BYTES, k);
614
615     if (result == 0) {
616         GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
617                        (unsigned long) lb);
618         return(0);
619     }
620     if (!GC_debugging_started) {
621         GC_start_debugging_inner();
622     }
623     ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
624     return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
625   }
626 #endif /* DBG_HDRS_ALL */
627
628 #ifdef STUBBORN_ALLOC
629   GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_stubborn(size_t lb,
630                                                         GC_EXTRA_PARAMS)
631   {
632     void * result = GC_malloc_stubborn(lb + DEBUG_BYTES);
633
634     if (result == 0) {
635         GC_err_printf("GC_debug_malloc_stubborn(%lu)"
636                       " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
637         return(0);
638     }
639     if (!GC_debugging_started) {
640         GC_start_debugging();
641     }
642     ADD_CALL_CHAIN(result, ra);
643     return (GC_store_debug_info(result, (word)lb, s, i));
644   }
645
646   GC_API void GC_CALL GC_debug_change_stubborn(const void *p)
647   {
648     const void * q = GC_base_C(p);
649     hdr * hhdr;
650
651     if (q == 0) {
652         ABORT_ARG1("GC_debug_change_stubborn: bad arg", ": %p", p);
653     }
654     hhdr = HDR(q);
655     if (hhdr -> hb_obj_kind != STUBBORN) {
656         ABORT_ARG1("GC_debug_change_stubborn: arg not stubborn", ": %p", p);
657     }
658     GC_change_stubborn(q);
659   }
660
661   GC_API void GC_CALL GC_debug_end_stubborn_change(const void *p)
662   {
663     const void * q = GC_base_C(p);
664     hdr * hhdr;
665
666     if (q == 0) {
667         ABORT_ARG1("GC_debug_end_stubborn_change: bad arg", ": %p", p);
668     }
669     hhdr = HDR(q);
670     if (hhdr -> hb_obj_kind != STUBBORN) {
671         ABORT_ARG1("GC_debug_end_stubborn_change: arg not stubborn",
672                    ": %p", p);
673     }
674     GC_end_stubborn_change(q);
675   }
676
677 #else /* !STUBBORN_ALLOC */
678
679   GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_stubborn(size_t lb,
680                                                         GC_EXTRA_PARAMS)
681   {
682     return GC_debug_malloc(lb, OPT_RA s, i);
683   }
684
685   GC_API void GC_CALL GC_debug_change_stubborn(
686                                 const void * p GC_ATTR_UNUSED) {}
687
688   GC_API void GC_CALL GC_debug_end_stubborn_change(
689                                 const void * p GC_ATTR_UNUSED) {}
690 #endif /* !STUBBORN_ALLOC */
691
692 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_atomic(size_t lb,
693                                                             GC_EXTRA_PARAMS)
694 {
695     void * result = GC_malloc_atomic(lb + DEBUG_BYTES);
696
697     if (result == 0) {
698         GC_err_printf("GC_debug_malloc_atomic(%lu) returning NULL (%s:%d)\n",
699                       (unsigned long)lb, s, i);
700         return(0);
701     }
702     if (!GC_debugging_started) {
703         GC_start_debugging();
704     }
705     ADD_CALL_CHAIN(result, ra);
706     return (GC_store_debug_info(result, (word)lb, s, i));
707 }
708
709 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strdup(const char *str,
710                                                      GC_EXTRA_PARAMS)
711 {
712   char *copy;
713   size_t lb;
714   if (str == NULL) {
715     if (GC_find_leak)
716       GC_err_printf("strdup(NULL) behavior is undefined\n");
717     return NULL;
718   }
719
720   lb = strlen(str) + 1;
721   copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
722   if (copy == NULL) {
723 #   ifndef MSWINCE
724       errno = ENOMEM;
725 #   endif
726     return NULL;
727   }
728   BCOPY(str, copy, lb);
729   return copy;
730 }
731
732 GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strndup(const char *str,
733                                                 size_t size, GC_EXTRA_PARAMS)
734 {
735   char *copy;
736   size_t len = strlen(str); /* str is expected to be non-NULL  */
737   if (len > size)
738     len = size;
739   copy = GC_debug_malloc_atomic(len + 1, OPT_RA s, i);
740   if (copy == NULL) {
741 #   ifndef MSWINCE
742       errno = ENOMEM;
743 #   endif
744     return NULL;
745   }
746   BCOPY(str, copy, len);
747   copy[len] = '\0';
748   return copy;
749 }
750
751 #ifdef GC_REQUIRE_WCSDUP
752 # include <wchar.h> /* for wcslen() */
753
754   GC_API GC_ATTR_MALLOC wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *str,
755                                                           GC_EXTRA_PARAMS)
756   {
757     size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
758     wchar_t *copy = GC_debug_malloc_atomic(lb, OPT_RA s, i);
759     if (copy == NULL) {
760 #     ifndef MSWINCE
761         errno = ENOMEM;
762 #     endif
763       return NULL;
764     }
765     BCOPY(str, copy, lb);
766     return copy;
767   }
768 #endif /* GC_REQUIRE_WCSDUP */
769
770 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
771                                                         GC_EXTRA_PARAMS)
772 {
773     void * result = GC_malloc_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
774
775     if (result == 0) {
776         GC_err_printf("GC_debug_malloc_uncollectable(%lu)"
777                       " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
778         return(0);
779     }
780     if (!GC_debugging_started) {
781         GC_start_debugging();
782     }
783     ADD_CALL_CHAIN(result, ra);
784     return (GC_store_debug_info(result, (word)lb, s, i));
785 }
786
787 #ifdef GC_ATOMIC_UNCOLLECTABLE
788   GC_API GC_ATTR_MALLOC void * GC_CALL
789         GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
790   {
791     void * result =
792         GC_malloc_atomic_uncollectable(lb + UNCOLLECTABLE_DEBUG_BYTES);
793
794     if (result == 0) {
795         GC_err_printf("GC_debug_malloc_atomic_uncollectable(%lu)"
796                       " returning NULL (%s:%d)\n", (unsigned long)lb, s, i);
797         return(0);
798     }
799     if (!GC_debugging_started) {
800         GC_start_debugging();
801     }
802     ADD_CALL_CHAIN(result, ra);
803     return (GC_store_debug_info(result, (word)lb, s, i));
804   }
805 #endif /* GC_ATOMIC_UNCOLLECTABLE */
806
807 #ifndef GC_FREED_MEM_MARKER
808 # if CPP_WORDSZ == 32
809 #   define GC_FREED_MEM_MARKER 0xdeadbeef
810 # else
811 #   define GC_FREED_MEM_MARKER GC_WORD_C(0xEFBEADDEdeadbeef)
812 # endif
813 #endif
814
815 GC_API void GC_CALL GC_debug_free(void * p)
816 {
817     ptr_t base;
818     if (0 == p) return;
819
820     base = GC_base(p);
821     if (base == 0) {
822       ABORT_ARG1("Invalid pointer passed to free()", ": %p", p);
823     }
824     if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
825       GC_err_printf(
826                "GC_debug_free called on pointer %p w/o debugging info\n", p);
827     } else {
828 #     ifndef SHORT_DBG_HDRS
829         ptr_t clobbered = GC_check_annotated_obj((oh *)base);
830         word sz = GC_size(base);
831         if (clobbered != 0) {
832           GC_have_errors = TRUE;
833           if (((oh *)base) -> oh_sz == sz) {
834             GC_print_smashed_obj(
835                   "GC_debug_free: found previously deallocated (?) object at",
836                   p, clobbered);
837             return; /* ignore double free */
838           } else {
839             GC_print_smashed_obj("GC_debug_free: found smashed location at",
840                                  p, clobbered);
841           }
842         }
843         /* Invalidate size (mark the object as deallocated) */
844         ((oh *)base) -> oh_sz = sz;
845 #     endif /* SHORT_DBG_HDRS */
846     }
847     if (GC_find_leak
848 #       ifndef SHORT_DBG_HDRS
849           && ((ptr_t)p - (ptr_t)base != sizeof(oh) || !GC_findleak_delay_free)
850 #       endif
851         ) {
852       GC_free(base);
853     } else {
854       hdr * hhdr = HDR(p);
855       if (hhdr -> hb_obj_kind == UNCOLLECTABLE
856 #         ifdef GC_ATOMIC_UNCOLLECTABLE
857             || hhdr -> hb_obj_kind == AUNCOLLECTABLE
858 #         endif
859           ) {
860         GC_free(base);
861       } else {
862         size_t i;
863         size_t obj_sz = BYTES_TO_WORDS(hhdr -> hb_sz - sizeof(oh));
864
865         for (i = 0; i < obj_sz; ++i)
866           ((word *)p)[i] = GC_FREED_MEM_MARKER;
867         GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
868       }
869     } /* !GC_find_leak */
870 }
871
872 #if defined(THREADS) && defined(DBG_HDRS_ALL)
873   /* Used internally; we assume it's called correctly.    */
874   GC_INNER void GC_debug_free_inner(void * p)
875   {
876     ptr_t base = GC_base(p);
877     GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
878 #   ifdef LINT2
879       if (!base) ABORT("Invalid GC_debug_free_inner argument");
880 #   endif
881 #   ifndef SHORT_DBG_HDRS
882       /* Invalidate size */
883       ((oh *)base) -> oh_sz = GC_size(base);
884 #   endif
885     GC_free_inner(base);
886   }
887 #endif
888
889 GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
890 {
891     void * base;
892     void * result;
893     hdr * hhdr;
894
895     if (p == 0) {
896       return GC_debug_malloc(lb, OPT_RA s, i);
897     }
898     if (0 == lb) /* and p != NULL */ {
899       GC_debug_free(p);
900       return NULL;
901     }
902
903 #   ifdef GC_ADD_CALLER
904       if (s == NULL) {
905         GC_caller_func_offset(ra, &s, &i);
906       }
907 #   endif
908     base = GC_base(p);
909     if (base == 0) {
910         ABORT_ARG1("Invalid pointer passed to realloc()", ": %p", p);
911     }
912     if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
913         GC_err_printf(
914               "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
915         return(GC_realloc(p, lb));
916     }
917     hhdr = HDR(base);
918     switch (hhdr -> hb_obj_kind) {
919 #    ifdef STUBBORN_ALLOC
920       case STUBBORN:
921         result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
922         break;
923 #    endif
924       case NORMAL:
925         result = GC_debug_malloc(lb, OPT_RA s, i);
926         break;
927       case PTRFREE:
928         result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
929         break;
930       case UNCOLLECTABLE:
931         result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
932         break;
933 #    ifdef GC_ATOMIC_UNCOLLECTABLE
934       case AUNCOLLECTABLE:
935         result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
936         break;
937 #    endif
938       default:
939         result = NULL; /* initialized to prevent warning. */
940         ABORT_RET("GC_debug_realloc: encountered bad kind");
941     }
942
943     if (result != NULL) {
944       size_t old_sz;
945 #     ifdef SHORT_DBG_HDRS
946         old_sz = GC_size(base) - sizeof(oh);
947 #     else
948         old_sz = ((oh *)base) -> oh_sz;
949 #     endif
950       BCOPY(p, result, old_sz < lb ? old_sz : lb);
951       GC_debug_free(p);
952     }
953     return(result);
954 }
955
956 GC_API GC_ATTR_MALLOC void * GC_CALL
957     GC_debug_generic_or_special_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
958 {
959     switch (knd) {
960 #     ifdef STUBBORN_ALLOC
961         case STUBBORN:
962             return GC_debug_malloc_stubborn(lb, OPT_RA s, i);
963 #     endif
964         case PTRFREE:
965             return GC_debug_malloc_atomic(lb, OPT_RA s, i);
966         case NORMAL:
967             return GC_debug_malloc(lb, OPT_RA s, i);
968         case UNCOLLECTABLE:
969             return GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
970 #     ifdef GC_ATOMIC_UNCOLLECTABLE
971         case AUNCOLLECTABLE:
972             return GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
973 #     endif
974         default:
975             return GC_debug_generic_malloc(lb, knd, OPT_RA s, i);
976     }
977 }
978
979 #ifndef SHORT_DBG_HDRS
980
981 /* List of smashed (clobbered) locations.  We defer printing these,     */
982 /* since we can't always print them nicely with the allocation lock     */
983 /* held.  We put them here instead of in GC_arrays, since it may be     */
984 /* useful to be able to look at them with the debugger.                 */
985 #ifndef MAX_SMASHED
986 # define MAX_SMASHED 20
987 #endif
988 STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
989 STATIC unsigned GC_n_smashed = 0;
990
991 STATIC void GC_add_smashed(ptr_t smashed)
992 {
993     GC_ASSERT(GC_is_marked(GC_base(smashed)));
994     /* FIXME: Prevent adding an object while printing smashed list.     */
995     GC_smashed[GC_n_smashed] = smashed;
996     if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
997       /* In case of overflow, we keep the first MAX_SMASHED-1   */
998       /* entries plus the last one.                             */
999     GC_have_errors = TRUE;
1000 }
1001
1002 /* Print all objects on the list.  Clear the list.      */
1003 STATIC void GC_print_all_smashed_proc(void)
1004 {
1005     unsigned i;
1006
1007     GC_ASSERT(I_DONT_HOLD_LOCK());
1008     if (GC_n_smashed == 0) return;
1009     GC_err_printf("GC_check_heap_block: found %u smashed heap objects:\n",
1010                   GC_n_smashed);
1011     for (i = 0; i < GC_n_smashed; ++i) {
1012         ptr_t base = (ptr_t)GC_base(GC_smashed[i]);
1013
1014 #       ifdef LINT2
1015           if (!base) ABORT("Invalid GC_smashed element");
1016 #       endif
1017         GC_print_smashed_obj("", base + sizeof(oh), GC_smashed[i]);
1018         GC_smashed[i] = 0;
1019     }
1020     GC_n_smashed = 0;
1021 }
1022
1023 /* Check all marked objects in the given block for validity     */
1024 /* Avoid GC_apply_to_each_object for performance reasons.       */
1025 STATIC void GC_check_heap_block(struct hblk *hbp, word dummy GC_ATTR_UNUSED)
1026 {
1027     struct hblkhdr * hhdr = HDR(hbp);
1028     size_t sz = hhdr -> hb_sz;
1029     size_t bit_no;
1030     char *p, *plim;
1031
1032     p = hbp->hb_body;
1033     if (sz > MAXOBJBYTES) {
1034       plim = p;
1035     } else {
1036       plim = hbp->hb_body + HBLKSIZE - sz;
1037     }
1038     /* go through all words in block */
1039     for (bit_no = 0; (word)p <= (word)plim;
1040          bit_no += MARK_BIT_OFFSET(sz), p += sz) {
1041       if (mark_bit_from_hdr(hhdr, bit_no) && GC_HAS_DEBUG_INFO((ptr_t)p)) {
1042         ptr_t clobbered = GC_check_annotated_obj((oh *)p);
1043         if (clobbered != 0)
1044           GC_add_smashed(clobbered);
1045       }
1046     }
1047 }
1048
1049 /* This assumes that all accessible objects are marked, and that        */
1050 /* I hold the allocation lock.  Normally called by collector.           */
1051 STATIC void GC_check_heap_proc(void)
1052 {
1053   GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
1054   /* FIXME: Should we check for twice that alignment?   */
1055   GC_apply_to_all_blocks(GC_check_heap_block, 0);
1056 }
1057
1058 GC_INNER GC_bool GC_check_leaked(ptr_t base)
1059 {
1060   size_t i;
1061   size_t obj_sz;
1062   word *p;
1063
1064   if (
1065 #     if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1066         (*(word *)base & 1) != 0 &&
1067 #     endif
1068       GC_has_other_debug_info(base) >= 0)
1069     return TRUE; /* object has leaked */
1070
1071   /* Validate freed object's content. */
1072   p = (word *)(base + sizeof(oh));
1073   obj_sz = BYTES_TO_WORDS(HDR(base)->hb_sz - sizeof(oh));
1074   for (i = 0; i < obj_sz; ++i)
1075     if (p[i] != GC_FREED_MEM_MARKER) {
1076         GC_set_mark_bit(base); /* do not reclaim it in this cycle */
1077         GC_add_smashed((ptr_t)(&p[i])); /* alter-after-free detected */
1078         break; /* don't report any other smashed locations in the object */
1079     }
1080
1081   return FALSE; /* GC_debug_free() has been called */
1082 }
1083
1084 #endif /* !SHORT_DBG_HDRS */
1085
1086 #ifndef GC_NO_FINALIZATION
1087
1088 struct closure {
1089     GC_finalization_proc cl_fn;
1090     void * cl_data;
1091 };
1092
1093 STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
1094 {
1095     struct closure * result =
1096 #   ifdef DBG_HDRS_ALL
1097       (struct closure *) GC_debug_malloc(sizeof (struct closure),
1098                                          GC_EXTRAS);
1099 #   else
1100       (struct closure *) GC_malloc(sizeof (struct closure));
1101 #   endif
1102     if (result != 0) {
1103       result -> cl_fn = fn;
1104       result -> cl_data = data;
1105     }
1106     return((void *)result);
1107 }
1108
1109 /* An auxiliary fns to make finalization work correctly with displaced  */
1110 /* pointers introduced by the debugging allocators.                     */
1111 STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
1112 {
1113     struct closure * cl = (struct closure *) data;
1114     (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
1115 }
1116
1117 /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
1118 #define OFN_UNSET ((GC_finalization_proc)~(signed_word)0)
1119
1120 /* Set ofn and ocd to reflect the values we got back.   */
1121 static void store_old(void *obj, GC_finalization_proc my_old_fn,
1122                       struct closure *my_old_cd, GC_finalization_proc *ofn,
1123                       void **ocd)
1124 {
1125     if (0 != my_old_fn) {
1126       if (my_old_fn == OFN_UNSET) {
1127         /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
1128         return;
1129       }
1130       if (my_old_fn != GC_debug_invoke_finalizer) {
1131         GC_err_printf("Debuggable object at %p had a non-debug finalizer\n",
1132                       obj);
1133         /* This should probably be fatal. */
1134       } else {
1135         if (ofn) *ofn = my_old_cd -> cl_fn;
1136         if (ocd) *ocd = my_old_cd -> cl_data;
1137       }
1138     } else {
1139       if (ofn) *ofn = 0;
1140       if (ocd) *ocd = 0;
1141     }
1142 }
1143
1144 GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
1145                                         GC_finalization_proc fn,
1146                                         void * cd, GC_finalization_proc *ofn,
1147                                         void * *ocd)
1148 {
1149     GC_finalization_proc my_old_fn = OFN_UNSET;
1150     void * my_old_cd;
1151     ptr_t base = GC_base(obj);
1152     if (0 == base) {
1153         /* We won't collect it, hence finalizer wouldn't be run. */
1154         if (ocd) *ocd = 0;
1155         if (ofn) *ofn = 0;
1156         return;
1157     }
1158     if ((ptr_t)obj - base != sizeof(oh)) {
1159         GC_err_printf("GC_debug_register_finalizer called with"
1160                       " non-base-pointer %p\n", obj);
1161     }
1162     if (0 == fn) {
1163       GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
1164     } else {
1165       cd = GC_make_closure(fn, cd);
1166       if (cd == 0) return; /* out of memory */
1167       GC_register_finalizer(base, GC_debug_invoke_finalizer,
1168                             cd, &my_old_fn, &my_old_cd);
1169     }
1170     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1171 }
1172
1173 GC_API void GC_CALL GC_debug_register_finalizer_no_order
1174                                     (void * obj, GC_finalization_proc fn,
1175                                      void * cd, GC_finalization_proc *ofn,
1176                                      void * *ocd)
1177 {
1178     GC_finalization_proc my_old_fn = OFN_UNSET;
1179     void * my_old_cd;
1180     ptr_t base = GC_base(obj);
1181     if (0 == base) {
1182         /* We won't collect it, hence finalizer wouldn't be run. */
1183         if (ocd) *ocd = 0;
1184         if (ofn) *ofn = 0;
1185         return;
1186     }
1187     if ((ptr_t)obj - base != sizeof(oh)) {
1188         GC_err_printf("GC_debug_register_finalizer_no_order called with"
1189                       " non-base-pointer %p\n", obj);
1190     }
1191     if (0 == fn) {
1192       GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
1193     } else {
1194       cd = GC_make_closure(fn, cd);
1195       if (cd == 0) return; /* out of memory */
1196       GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
1197                                      cd, &my_old_fn, &my_old_cd);
1198     }
1199     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1200 }
1201
1202 GC_API void GC_CALL GC_debug_register_finalizer_unreachable
1203                                     (void * obj, GC_finalization_proc fn,
1204                                      void * cd, GC_finalization_proc *ofn,
1205                                      void * *ocd)
1206 {
1207     GC_finalization_proc my_old_fn = OFN_UNSET;
1208     void * my_old_cd;
1209     ptr_t base = GC_base(obj);
1210     if (0 == base) {
1211         /* We won't collect it, hence finalizer wouldn't be run. */
1212         if (ocd) *ocd = 0;
1213         if (ofn) *ofn = 0;
1214         return;
1215     }
1216     if ((ptr_t)obj - base != sizeof(oh)) {
1217         GC_err_printf("GC_debug_register_finalizer_unreachable called with"
1218                       " non-base-pointer %p\n", obj);
1219     }
1220     if (0 == fn) {
1221       GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
1222     } else {
1223       cd = GC_make_closure(fn, cd);
1224       if (cd == 0) return; /* out of memory */
1225       GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
1226                                         cd, &my_old_fn, &my_old_cd);
1227     }
1228     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1229 }
1230
1231 GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
1232                                     (void * obj, GC_finalization_proc fn,
1233                                      void * cd, GC_finalization_proc *ofn,
1234                                      void * *ocd)
1235 {
1236     GC_finalization_proc my_old_fn = OFN_UNSET;
1237     void * my_old_cd;
1238     ptr_t base = GC_base(obj);
1239     if (0 == base) {
1240         /* We won't collect it, hence finalizer wouldn't be run. */
1241         if (ocd) *ocd = 0;
1242         if (ofn) *ofn = 0;
1243         return;
1244     }
1245     if ((ptr_t)obj - base != sizeof(oh)) {
1246         GC_err_printf("GC_debug_register_finalizer_ignore_self called with"
1247                       " non-base-pointer %p\n", obj);
1248     }
1249     if (0 == fn) {
1250       GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
1251     } else {
1252       cd = GC_make_closure(fn, cd);
1253       if (cd == 0) return; /* out of memory */
1254       GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
1255                                         cd, &my_old_fn, &my_old_cd);
1256     }
1257     store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
1258 }
1259
1260 #endif /* !GC_NO_FINALIZATION */
1261
1262 GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_replacement(size_t lb)
1263 {
1264     return GC_debug_malloc(lb, GC_DBG_EXTRAS);
1265 }
1266
1267 GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
1268 {
1269     return GC_debug_realloc(p, lb, GC_DBG_EXTRAS);
1270 }