gc.h (dlopen): Define as GC_dlopen on Linux.
[platform/upstream/gcc.git] / boehm-gc / 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  *
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 # define I_HIDE_POINTERS
16 # include "gc_priv.h"
17 # ifdef KEEP_BACK_PTRS
18 #   include "backptr.h"
19 # endif
20
21 void GC_default_print_heap_obj_proc();
22 GC_API void GC_register_finalizer_no_order
23         GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
24                   GC_finalization_proc *ofn, GC_PTR *ocd));
25
26 /* Do we want to and know how to save the call stack at the time of     */
27 /* an allocation?  How much space do we want to use in each object?     */
28
29 # define START_FLAG ((word)0xfedcedcb)
30 # define END_FLAG ((word)0xbcdecdef)
31         /* Stored both one past the end of user object, and one before  */
32         /* the end of the object as seen by the allocator.              */
33
34
35 /* Object header */
36 typedef struct {
37 #   ifdef KEEP_BACK_PTRS
38         ptr_t oh_back_ptr;
39 #       define MARKED_FOR_FINALIZATION (ptr_t)(-1)
40             /* Object was marked because it is finalizable. */
41 #       ifdef ALIGN_DOUBLE
42           word oh_dummy;
43 #       endif
44 #   endif
45     char * oh_string;           /* object descriptor string     */
46     word oh_int;                /* object descriptor integers   */
47 #   ifdef NEED_CALLINFO
48       struct callinfo oh_ci[NFRAMES];
49 #   endif
50     word oh_sz;                 /* Original malloc arg.         */
51     word oh_sf;                 /* start flag */
52 } oh;
53 /* The size of the above structure is assumed not to dealign things,    */
54 /* and to be a multiple of the word length.                             */
55
56 #define DEBUG_BYTES (sizeof (oh) + sizeof (word))
57 #undef ROUNDED_UP_WORDS
58 #define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + WORDS_TO_BYTES(1) - 1)
59
60
61 #ifdef SAVE_CALL_CHAIN
62 #   define ADD_CALL_CHAIN(base, ra) GC_save_callers(((oh *)(base)) -> oh_ci)
63 #   define PRINT_CALL_CHAIN(base) GC_print_callers(((oh *)(base)) -> oh_ci)
64 #else
65 # ifdef GC_ADD_CALLER
66 #   define ADD_CALL_CHAIN(base, ra) ((oh *)(base)) -> oh_ci[0].ci_pc = (ra)
67 #   define PRINT_CALL_CHAIN(base) GC_print_callers(((oh *)(base)) -> oh_ci)
68 # else
69 #   define ADD_CALL_CHAIN(base, ra)
70 #   define PRINT_CALL_CHAIN(base)
71 # endif
72 #endif
73
74 /* Check whether object with base pointer p has debugging info  */ 
75 /* p is assumed to point to a legitimate object in our part     */
76 /* of the heap.                                                 */
77 GC_bool GC_has_debug_info(p)
78 ptr_t p;
79 {
80     register oh * ohdr = (oh *)p;
81     register ptr_t body = (ptr_t)(ohdr + 1);
82     register word sz = GC_size((ptr_t) ohdr);
83     
84     if (HBLKPTR((ptr_t)ohdr) != HBLKPTR((ptr_t)body)
85         || sz < sizeof (oh)) {
86         return(FALSE);
87     }
88     if (ohdr -> oh_sz == sz) {
89         /* Object may have had debug info, but has been deallocated     */
90         return(FALSE);
91     }
92     if (ohdr -> oh_sf == (START_FLAG ^ (word)body)) return(TRUE);
93     if (((word *)ohdr)[BYTES_TO_WORDS(sz)-1] == (END_FLAG ^ (word)body)) {
94         return(TRUE);
95     }
96     return(FALSE);
97 }
98
99 #ifdef KEEP_BACK_PTRS
100   /* Store back pointer to source in dest, if that appears to be possible. */
101   /* This is not completely safe, since we may mistakenly conclude that    */
102   /* dest has a debugging wrapper.  But the error probability is very      */
103   /* small, and this shouldn't be used in production code.                 */
104   /* We assume that dest is the real base pointer.  Source will usually    */
105   /* be a pointer to the interior of an object.                            */
106   void GC_store_back_pointer(ptr_t source, ptr_t dest)
107   {
108     if (GC_has_debug_info(dest)) {
109       ((oh *)dest) -> oh_back_ptr = (ptr_t)HIDE_POINTER(source);
110     }
111   }
112
113   void GC_marked_for_finalization(ptr_t dest) {
114     GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
115   }
116
117   /* Store information about the object referencing dest in *base_p     */
118   /* and *offset_p.                                                     */
119   /*   source is root ==> *base_p = 0, *offset_p = address              */
120   /*   source is heap object ==> *base_p != 0, *offset_p = offset       */
121   /*   Returns 1 on success, 0 if source couldn't be determined.        */
122   /* Dest can be any address within a heap object.                      */
123   GC_ref_kind GC_get_back_ptr_info(void *dest, void **base_p, size_t *offset_p)
124   {
125     oh * hdr = (oh *)GC_base(dest);
126     ptr_t bp;
127     ptr_t bp_base;
128     if (!GC_has_debug_info((ptr_t) hdr)) return GC_NO_SPACE;
129     bp = hdr -> oh_back_ptr;
130     if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
131     if (0 == bp) return GC_UNREFERENCED;
132     bp = REVEAL_POINTER(bp);
133     bp_base = GC_base(bp);
134     if (0 == bp_base) {
135       *base_p = bp;
136       *offset_p = 0;
137       return GC_REFD_FROM_ROOT;
138     } else {
139       if (GC_has_debug_info(bp_base)) bp_base += sizeof(oh);
140       *base_p = bp_base;
141       *offset_p = bp - bp_base;
142       return GC_REFD_FROM_HEAP;
143     }
144   }
145
146   /* Generate a random heap address.            */
147   /* The resulting address is in the heap, but  */
148   /* not necessarily inside a valid object.     */
149   void *GC_generate_random_heap_address(void)
150   {
151     int i;
152     int heap_offset = random() % GC_heapsize;
153     for (i = 0; i < GC_n_heap_sects; ++ i) {
154         int size = GC_heap_sects[i].hs_bytes;
155         if (heap_offset < size) {
156             return GC_heap_sects[i].hs_start + heap_offset;
157         } else {
158             heap_offset -= size;
159         }
160     }
161     ABORT("GC_generate_random_heap_address: size inconsistency");
162     /*NOTREACHED*/
163     return 0;
164   }
165
166   /* Generate a random address inside a valid marked heap object. */
167   void *GC_generate_random_valid_address(void)
168   {
169     ptr_t result;
170     ptr_t base;
171     for (;;) {
172         result = GC_generate_random_heap_address();
173         base = GC_base(result);
174         if (0 == base) continue;
175         if (!GC_is_marked(base)) continue;
176         return result;
177     }
178   }
179
180   /* Force a garbage collection and generate a backtrace from a */
181   /* random heap address.                                       */
182   void GC_generate_random_backtrace(void)
183   {
184     void * current;
185     int i;
186     void * base;
187     size_t offset;
188     GC_ref_kind source;
189     GC_gcollect();
190     current = GC_generate_random_valid_address();
191     GC_printf1("Chose address 0x%lx in object\n", (unsigned long)current);
192     GC_print_heap_obj(GC_base(current));
193     GC_err_printf0("\n");
194     for (i = 0; ; ++i) {
195       source = GC_get_back_ptr_info(current, &base, &offset);
196       if (GC_UNREFERENCED == source) {
197         GC_err_printf0("Reference could not be found\n");
198         goto out;
199       }
200       if (GC_NO_SPACE == source) {
201         GC_err_printf0("No debug info in object: Can't find reference\n");
202         goto out;
203       }
204       GC_err_printf1("Reachable via %d levels of pointers from ",
205                  (unsigned long)i);
206       switch(source) {
207         case GC_REFD_FROM_ROOT:
208           GC_err_printf1("root at 0x%lx\n", (unsigned long)base);
209           goto out;
210         case GC_FINALIZER_REFD:
211           GC_err_printf0("list of finalizable objects\n");
212           goto out;
213         case GC_REFD_FROM_HEAP:
214           GC_err_printf1("offset %ld in object:\n", (unsigned long)offset);
215           /* Take GC_base(base) to get real base, i.e. header. */
216           GC_print_heap_obj(GC_base(base));
217           GC_err_printf0("\n");
218           break;
219       }
220       current = base;
221     }
222     out:;
223   }
224     
225 #endif /* KEEP_BACK_PTRS */
226
227 /* Store debugging info into p.  Return displaced pointer. */
228 /* Assumes we don't hold allocation lock.                  */
229 ptr_t GC_store_debug_info(p, sz, string, integer)
230 register ptr_t p;       /* base pointer */
231 word sz;        /* bytes */
232 char * string;
233 word integer;
234 {
235     register word * result = (word *)((oh *)p + 1);
236     DCL_LOCK_STATE;
237     
238     /* There is some argument that we should dissble signals here.      */
239     /* But that's expensive.  And this way things should only appear    */
240     /* inconsistent while we're in the handler.                         */
241     LOCK();
242 #   ifdef KEEP_BACK_PTRS
243       ((oh *)p) -> oh_back_ptr = 0;
244 #   endif
245     ((oh *)p) -> oh_string = string;
246     ((oh *)p) -> oh_int = integer;
247     ((oh *)p) -> oh_sz = sz;
248     ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
249     ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
250          result[ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
251     UNLOCK();
252     return((ptr_t)result);
253 }
254
255 /* Check the object with debugging info at ohdr         */
256 /* return NIL if it's OK.  Else return clobbered        */
257 /* address.                                             */
258 ptr_t GC_check_annotated_obj(ohdr)
259 register oh * ohdr;
260 {
261     register ptr_t body = (ptr_t)(ohdr + 1);
262     register word gc_sz = GC_size((ptr_t)ohdr);
263     if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
264         return((ptr_t)(&(ohdr -> oh_sz)));
265     }
266     if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
267         return((ptr_t)(&(ohdr -> oh_sf)));
268     }
269     if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
270         return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
271     }
272     if (((word *)body)[ROUNDED_UP_WORDS(ohdr -> oh_sz)]
273         != (END_FLAG ^ (word)body)) {
274         return((ptr_t)((word *)body + ROUNDED_UP_WORDS(ohdr -> oh_sz)));
275     }
276     return(0);
277 }
278
279 void GC_print_obj(p)
280 ptr_t p;
281 {
282     register oh * ohdr = (oh *)GC_base(p);
283     
284     GC_err_printf1("0x%lx (", ((unsigned long)ohdr + sizeof(oh)));
285     GC_err_puts(ohdr -> oh_string);
286     GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
287                                       (unsigned long)(ohdr -> oh_sz));
288     PRINT_CALL_CHAIN(ohdr);
289 }
290
291 void GC_debug_print_heap_obj_proc(p)
292 ptr_t p;
293 {
294     if (GC_has_debug_info(p)) {
295         GC_print_obj(p);
296     } else {
297         GC_default_print_heap_obj_proc(p);
298     }
299 }
300
301 void GC_print_smashed_obj(p, clobbered_addr)
302 ptr_t p, clobbered_addr;
303 {
304     register oh * ohdr = (oh *)GC_base(p);
305     
306     GC_err_printf2("0x%lx in object at 0x%lx(", (unsigned long)clobbered_addr,
307                                                 (unsigned long)p);
308     if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
309         || ohdr -> oh_string == 0) {
310         GC_err_printf1("<smashed>, appr. sz = %ld)\n",
311                        (GC_size((ptr_t)ohdr) - DEBUG_BYTES));
312     } else {
313         if (ohdr -> oh_string[0] == '\0') {
314             GC_err_puts("EMPTY(smashed?)");
315         } else {
316             GC_err_puts(ohdr -> oh_string);
317         }
318         GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
319                                           (unsigned long)(ohdr -> oh_sz));
320         PRINT_CALL_CHAIN(ohdr);
321     }
322 }
323
324 void GC_check_heap_proc();
325
326 void GC_start_debugging()
327 {
328     GC_check_heap = GC_check_heap_proc;
329     GC_print_heap_obj = GC_debug_print_heap_obj_proc;
330     GC_debugging_started = TRUE;
331     GC_register_displacement((word)sizeof(oh));
332 }
333
334 # if defined(__STDC__) || defined(__cplusplus)
335     void GC_debug_register_displacement(GC_word offset)
336 # else
337     void GC_debug_register_displacement(offset) 
338     GC_word offset;
339 # endif
340 {
341     GC_register_displacement(offset);
342     GC_register_displacement((word)sizeof(oh) + offset);
343 }
344
345 # ifdef GC_ADD_CALLER
346 #   define EXTRA_ARGS word ra, CONST char * s, int i
347 #   define OPT_RA ra,
348 # else
349 #   define EXTRA_ARGS CONST char * s, int i
350 #   define OPT_RA
351 # endif
352
353 # ifdef __STDC__
354     GC_PTR GC_debug_malloc(size_t lb, EXTRA_ARGS)
355 # else
356     GC_PTR GC_debug_malloc(lb, s, i)
357     size_t lb;
358     char * s;
359     int i;
360 #   ifdef GC_ADD_CALLER
361         --> GC_ADD_CALLER not implemented for K&R C
362 #   endif
363 # endif
364 {
365     GC_PTR result = GC_malloc(lb + DEBUG_BYTES);
366     
367     if (result == 0) {
368         GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
369                        (unsigned long) lb);
370         GC_err_puts(s);
371         GC_err_printf1(":%ld)\n", (unsigned long)i);
372         return(0);
373     }
374     if (!GC_debugging_started) {
375         GC_start_debugging();
376     }
377     ADD_CALL_CHAIN(result, ra);
378     return (GC_store_debug_info(result, (word)lb, s, (word)i));
379 }
380
381 # ifdef __STDC__
382     GC_PTR GC_debug_generic_malloc(size_t lb, int k, EXTRA_ARGS)
383 # else
384     GC_PTR GC_debug_malloc(lb, k, s, i)
385     size_t lb;
386     int k;
387     char * s;
388     int i;
389 #   ifdef GC_ADD_CALLER
390         --> GC_ADD_CALLER not implemented for K&R C
391 #   endif
392 # endif
393 {
394     GC_PTR result = GC_generic_malloc(lb + DEBUG_BYTES, k);
395     
396     if (result == 0) {
397         GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
398                        (unsigned long) lb);
399         GC_err_puts(s);
400         GC_err_printf1(":%ld)\n", (unsigned long)i);
401         return(0);
402     }
403     if (!GC_debugging_started) {
404         GC_start_debugging();
405     }
406     ADD_CALL_CHAIN(result, ra);
407     return (GC_store_debug_info(result, (word)lb, s, (word)i));
408 }
409
410 #ifdef STUBBORN_ALLOC
411 # ifdef __STDC__
412     GC_PTR GC_debug_malloc_stubborn(size_t lb, EXTRA_ARGS)
413 # else
414     GC_PTR GC_debug_malloc_stubborn(lb, s, i)
415     size_t lb;
416     char * s;
417     int i;
418 # endif
419 {
420     GC_PTR result = GC_malloc_stubborn(lb + DEBUG_BYTES);
421     
422     if (result == 0) {
423         GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
424                        (unsigned long) lb);
425         GC_err_puts(s);
426         GC_err_printf1(":%ld)\n", (unsigned long)i);
427         return(0);
428     }
429     if (!GC_debugging_started) {
430         GC_start_debugging();
431     }
432     ADD_CALL_CHAIN(result, ra);
433     return (GC_store_debug_info(result, (word)lb, s, (word)i));
434 }
435
436 void GC_debug_change_stubborn(p)
437 GC_PTR p;
438 {
439     register GC_PTR q = GC_base(p);
440     register hdr * hhdr;
441     
442     if (q == 0) {
443         GC_err_printf1("Bad argument: 0x%lx to GC_debug_change_stubborn\n",
444                        (unsigned long) p);
445         ABORT("GC_debug_change_stubborn: bad arg");
446     }
447     hhdr = HDR(q);
448     if (hhdr -> hb_obj_kind != STUBBORN) {
449         GC_err_printf1("GC_debug_change_stubborn arg not stubborn: 0x%lx\n",
450                        (unsigned long) p);
451         ABORT("GC_debug_change_stubborn: arg not stubborn");
452     }
453     GC_change_stubborn(q);
454 }
455
456 void GC_debug_end_stubborn_change(p)
457 GC_PTR p;
458 {
459     register GC_PTR q = GC_base(p);
460     register hdr * hhdr;
461     
462     if (q == 0) {
463         GC_err_printf1("Bad argument: 0x%lx to GC_debug_end_stubborn_change\n",
464                        (unsigned long) p);
465         ABORT("GC_debug_end_stubborn_change: bad arg");
466     }
467     hhdr = HDR(q);
468     if (hhdr -> hb_obj_kind != STUBBORN) {
469         GC_err_printf1("debug_end_stubborn_change arg not stubborn: 0x%lx\n",
470                        (unsigned long) p);
471         ABORT("GC_debug_end_stubborn_change: arg not stubborn");
472     }
473     GC_end_stubborn_change(q);
474 }
475
476 #endif /* STUBBORN_ALLOC */
477
478 # ifdef __STDC__
479     GC_PTR GC_debug_malloc_atomic(size_t lb, EXTRA_ARGS)
480 # else
481     GC_PTR GC_debug_malloc_atomic(lb, s, i)
482     size_t lb;
483     char * s;
484     int i;
485 # endif
486 {
487     GC_PTR result = GC_malloc_atomic(lb + DEBUG_BYTES);
488     
489     if (result == 0) {
490         GC_err_printf1("GC_debug_malloc_atomic(%ld) returning NIL (",
491                       (unsigned long) lb);
492         GC_err_puts(s);
493         GC_err_printf1(":%ld)\n", (unsigned long)i);
494         return(0);
495     }
496     if (!GC_debugging_started) {
497         GC_start_debugging();
498     }
499     ADD_CALL_CHAIN(result, ra);
500     return (GC_store_debug_info(result, (word)lb, s, (word)i));
501 }
502
503 # ifdef __STDC__
504     GC_PTR GC_debug_malloc_uncollectable(size_t lb, EXTRA_ARGS)
505 # else
506     GC_PTR GC_debug_malloc_uncollectable(lb, s, i)
507     size_t lb;
508     char * s;
509     int i;
510 # endif
511 {
512     GC_PTR result = GC_malloc_uncollectable(lb + DEBUG_BYTES);
513     
514     if (result == 0) {
515         GC_err_printf1("GC_debug_malloc_uncollectable(%ld) returning NIL (",
516                       (unsigned long) lb);
517         GC_err_puts(s);
518         GC_err_printf1(":%ld)\n", (unsigned long)i);
519         return(0);
520     }
521     if (!GC_debugging_started) {
522         GC_start_debugging();
523     }
524     ADD_CALL_CHAIN(result, ra);
525     return (GC_store_debug_info(result, (word)lb, s, (word)i));
526 }
527
528 #ifdef ATOMIC_UNCOLLECTABLE
529 # ifdef __STDC__
530     GC_PTR GC_debug_malloc_atomic_uncollectable(size_t lb, EXTRA_ARGS)
531 # else
532     GC_PTR GC_debug_malloc_atomic_uncollectable(lb, s, i)
533     size_t lb;
534     char * s;
535     int i;
536 # endif
537 {
538     GC_PTR result = GC_malloc_atomic_uncollectable(lb + DEBUG_BYTES);
539     
540     if (result == 0) {
541         GC_err_printf1(
542                 "GC_debug_malloc_atomic_uncollectable(%ld) returning NIL (",
543                 (unsigned long) lb);
544         GC_err_puts(s);
545         GC_err_printf1(":%ld)\n", (unsigned long)i);
546         return(0);
547     }
548     if (!GC_debugging_started) {
549         GC_start_debugging();
550     }
551     ADD_CALL_CHAIN(result, ra);
552     return (GC_store_debug_info(result, (word)lb, s, (word)i));
553 }
554 #endif /* ATOMIC_UNCOLLECTABLE */
555
556 # ifdef __STDC__
557     void GC_debug_free(GC_PTR p)
558 # else
559     void GC_debug_free(p)
560     GC_PTR p;
561 # endif
562 {
563     register GC_PTR base;
564     register ptr_t clobbered;
565     
566     if (0 == p) return;
567     base = GC_base(p);
568     if (base == 0) {
569         GC_err_printf1("Attempt to free invalid pointer %lx\n",
570                        (unsigned long)p);
571         ABORT("free(invalid pointer)");
572     }
573     if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
574         GC_err_printf1(
575                   "GC_debug_free called on pointer %lx wo debugging info\n",
576                   (unsigned long)p);
577     } else {
578       clobbered = GC_check_annotated_obj((oh *)base);
579       if (clobbered != 0) {
580         if (((oh *)base) -> oh_sz == GC_size(base)) {
581             GC_err_printf0(
582                   "GC_debug_free: found previously deallocated (?) object at ");
583         } else {
584             GC_err_printf0("GC_debug_free: found smashed location at ");
585         }
586         GC_print_smashed_obj(p, clobbered);
587       }
588       /* Invalidate size */
589       ((oh *)base) -> oh_sz = GC_size(base);
590     }
591     if (GC_find_leak) {
592         GC_free(base);
593     } else {
594         register hdr * hhdr = HDR(p);
595         GC_bool uncollectable = FALSE;
596
597         if (hhdr ->  hb_obj_kind == UNCOLLECTABLE) {
598             uncollectable = TRUE;
599         }
600 #       ifdef ATOMIC_UNCOLLECTABLE
601             if (hhdr ->  hb_obj_kind == AUNCOLLECTABLE) {
602                     uncollectable = TRUE;
603             }
604 #       endif
605         if (uncollectable) GC_free(base);
606     } /* !GC_find_leak */
607 }
608
609 # ifdef __STDC__
610     GC_PTR GC_debug_realloc(GC_PTR p, size_t lb, EXTRA_ARGS)
611 # else
612     GC_PTR GC_debug_realloc(p, lb, s, i)
613     GC_PTR p;
614     size_t lb;
615     char *s;
616     int i;
617 # endif
618 {
619     register GC_PTR base = GC_base(p);
620     register ptr_t clobbered;
621     register GC_PTR result;
622     register size_t copy_sz = lb;
623     register size_t old_sz;
624     register hdr * hhdr;
625     
626     if (p == 0) return(GC_debug_malloc(lb, OPT_RA s, i));
627     if (base == 0) {
628         GC_err_printf1(
629               "Attempt to reallocate invalid pointer %lx\n", (unsigned long)p);
630         ABORT("realloc(invalid pointer)");
631     }
632     if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
633         GC_err_printf1(
634                 "GC_debug_realloc called on pointer %lx wo debugging info\n",
635                 (unsigned long)p);
636         return(GC_realloc(p, lb));
637     }
638     hhdr = HDR(base);
639     switch (hhdr -> hb_obj_kind) {
640 #    ifdef STUBBORN_ALLOC
641       case STUBBORN:
642         result = GC_debug_malloc_stubborn(lb, OPT_RA s, i);
643         break;
644 #    endif
645       case NORMAL:
646         result = GC_debug_malloc(lb, OPT_RA s, i);
647         break;
648       case PTRFREE:
649         result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
650         break;
651       case UNCOLLECTABLE:
652         result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
653         break;
654 #    ifdef ATOMIC_UNCOLLECTABLE
655       case AUNCOLLECTABLE:
656         result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
657         break;
658 #    endif
659       default:
660         GC_err_printf0("GC_debug_realloc: encountered bad kind\n");
661         ABORT("bad kind");
662     }
663     clobbered = GC_check_annotated_obj((oh *)base);
664     if (clobbered != 0) {
665         GC_err_printf0("GC_debug_realloc: found smashed location at ");
666         GC_print_smashed_obj(p, clobbered);
667     }
668     old_sz = ((oh *)base) -> oh_sz;
669     if (old_sz < copy_sz) copy_sz = old_sz;
670     if (result == 0) return(0);
671     BCOPY(p, result,  copy_sz);
672     GC_debug_free(p);
673     return(result);
674 }
675
676 /* Check all marked objects in the given block for validity */
677 /*ARGSUSED*/
678 void GC_check_heap_block(hbp, dummy)
679 register struct hblk *hbp;      /* ptr to current heap block            */
680 word dummy;
681 {
682     register struct hblkhdr * hhdr = HDR(hbp);
683     register word sz = hhdr -> hb_sz;
684     register int word_no;
685     register word *p, *plim;
686     
687     p = (word *)(hbp->hb_body);
688     word_no = HDR_WORDS;
689     if (sz > MAXOBJSZ) {
690         plim = p;
691     } else {
692         plim = (word *)((((word)hbp) + HBLKSIZE) - WORDS_TO_BYTES(sz));
693     }
694     /* go through all words in block */
695         while( p <= plim ) {
696             if( mark_bit_from_hdr(hhdr, word_no)
697                 && GC_has_debug_info((ptr_t)p)) {
698                 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
699                 
700                 if (clobbered != 0) {
701                     GC_err_printf0(
702                         "GC_check_heap_block: found smashed location at ");
703                     GC_print_smashed_obj((ptr_t)p, clobbered);
704                 }
705             }
706             word_no += sz;
707             p += sz;
708         }
709 }
710
711
712 /* This assumes that all accessible objects are marked, and that        */
713 /* I hold the allocation lock.  Normally called by collector.           */
714 void GC_check_heap_proc()
715 {
716 #   ifndef SMALL_CONFIG
717         if (sizeof(oh) & (2 * sizeof(word) - 1) != 0) {
718             ABORT("Alignment problem: object header has inappropriate size\n");
719         }
720 #   endif
721     GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
722 }
723
724 struct closure {
725     GC_finalization_proc cl_fn;
726     GC_PTR cl_data;
727 };
728
729 # ifdef __STDC__
730     void * GC_make_closure(GC_finalization_proc fn, void * data)
731 # else
732     GC_PTR GC_make_closure(fn, data)
733     GC_finalization_proc fn;
734     GC_PTR data;
735 # endif
736 {
737     struct closure * result =
738                 (struct closure *) GC_malloc(sizeof (struct closure));
739     
740     result -> cl_fn = fn;
741     result -> cl_data = data;
742     return((GC_PTR)result);
743 }
744
745 # ifdef __STDC__
746     void GC_debug_invoke_finalizer(void * obj, void * data)
747 # else
748     void GC_debug_invoke_finalizer(obj, data)
749     char * obj;
750     char * data;
751 # endif
752 {
753     register struct closure * cl = (struct closure *) data;
754     
755     (*(cl -> cl_fn))((GC_PTR)((char *)obj + sizeof(oh)), cl -> cl_data);
756
757
758
759 # ifdef __STDC__
760     void GC_debug_register_finalizer(GC_PTR obj, GC_finalization_proc fn,
761                                      GC_PTR cd, GC_finalization_proc *ofn,
762                                      GC_PTR *ocd)
763 # else
764     void GC_debug_register_finalizer(obj, fn, cd, ofn, ocd)
765     GC_PTR obj;
766     GC_finalization_proc fn;
767     GC_PTR cd;
768     GC_finalization_proc *ofn;
769     GC_PTR *ocd;
770 # endif
771 {
772     ptr_t base = GC_base(obj);
773     if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
774         GC_err_printf1(
775             "GC_register_finalizer called with non-base-pointer 0x%lx\n",
776             obj);
777     }
778     GC_register_finalizer(base, GC_debug_invoke_finalizer,
779                           GC_make_closure(fn,cd), ofn, ocd);
780 }
781
782 # ifdef __STDC__
783     void GC_debug_register_finalizer_no_order
784                                     (GC_PTR obj, GC_finalization_proc fn,
785                                      GC_PTR cd, GC_finalization_proc *ofn,
786                                      GC_PTR *ocd)
787 # else
788     void GC_debug_register_finalizer_no_order
789                                     (obj, fn, cd, ofn, ocd)
790     GC_PTR obj;
791     GC_finalization_proc fn;
792     GC_PTR cd;
793     GC_finalization_proc *ofn;
794     GC_PTR *ocd;
795 # endif
796 {
797     ptr_t base = GC_base(obj);
798     if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
799         GC_err_printf1(
800           "GC_register_finalizer_no_order called with non-base-pointer 0x%lx\n",
801           obj);
802     }
803     GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
804                                       GC_make_closure(fn,cd), ofn, ocd);
805  }
806
807 # ifdef __STDC__
808     void GC_debug_register_finalizer_ignore_self
809                                     (GC_PTR obj, GC_finalization_proc fn,
810                                      GC_PTR cd, GC_finalization_proc *ofn,
811                                      GC_PTR *ocd)
812 # else
813     void GC_debug_register_finalizer_no_order
814                                     (obj, fn, cd, ofn, ocd)
815     GC_PTR obj;
816     GC_finalization_proc fn;
817     GC_PTR cd;
818     GC_finalization_proc *ofn;
819     GC_PTR *ocd;
820 # endif
821 {
822     ptr_t base = GC_base(obj);
823     if (0 == base || (ptr_t)obj - base != sizeof(oh)) {
824         GC_err_printf1(
825             "GC_register_finalizer_no_order called with non-base-pointer 0x%lx\n",
826             obj);
827     }
828     GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
829                                       GC_make_closure(fn,cd), ofn, ocd);
830 }