2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
14 /* Boehm, April 18, 1995 3:29 pm PDT */
17 /* Do we want to and know how to save the call stack at the time of */
18 /* an allocation? How much space do we want to use in each object? */
20 # define START_FLAG ((word)0xfedcedcb)
21 # define END_FLAG ((word)0xbcdecdef)
22 /* Stored both one past the end of user object, and one before */
23 /* the end of the object as seen by the allocator. */
28 char * oh_string; /* object descriptor string */
29 word oh_int; /* object descriptor integers */
30 # ifdef SAVE_CALL_CHAIN
31 struct callinfo oh_ci[NFRAMES];
33 word oh_sz; /* Original malloc arg. */
34 word oh_sf; /* start flag */
36 /* The size of the above structure is assumed not to dealign things, */
37 /* and to be a multiple of the word length. */
39 #define DEBUG_BYTES (sizeof (oh) + sizeof (word))
40 #undef ROUNDED_UP_WORDS
41 #define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + WORDS_TO_BYTES(1) - 1)
44 #ifdef SAVE_CALL_CHAIN
45 # define ADD_CALL_CHAIN(base) GC_save_callers(((oh *)(base)) -> oh_ci)
46 # define PRINT_CALL_CHAIN(base) GC_print_callers(((oh *)(base)) -> oh_ci)
48 # define ADD_CALL_CHAIN(base)
49 # define PRINT_CALL_CHAIN(base)
52 /* Check whether object with base pointer p has debugging info */
53 /* p is assumed to point to a legitimate object in our part */
55 bool GC_has_debug_info(p)
58 register oh * ohdr = (oh *)p;
59 register ptr_t body = (ptr_t)(ohdr + 1);
60 register word sz = GC_size((ptr_t) ohdr);
62 if (HBLKPTR((ptr_t)ohdr) != HBLKPTR((ptr_t)body)
63 || sz < sizeof (oh)) {
66 if (ohdr -> oh_sz == sz) {
67 /* Object may have had debug info, but has been deallocated */
70 if (ohdr -> oh_sf == (START_FLAG ^ (word)body)) return(TRUE);
71 if (((word *)ohdr)[BYTES_TO_WORDS(sz)-1] == (END_FLAG ^ (word)body)) {
77 /* Store debugging info into p. Return displaced pointer. */
78 /* Assumes we don't hold allocation lock. */
79 ptr_t GC_store_debug_info(p, sz, string, integer)
80 register ptr_t p; /* base pointer */
85 register word * result = (word *)((oh *)p + 1);
88 /* There is some argument that we should dissble signals here. */
89 /* But that's expensive. And this way things should only appear */
90 /* inconsistent while we're in the handler. */
92 ((oh *)p) -> oh_string = string;
93 ((oh *)p) -> oh_int = integer;
94 ((oh *)p) -> oh_sz = sz;
95 ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
96 ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
97 result[ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
99 return((ptr_t)result);
102 /* Check the object with debugging info at p */
103 /* return NIL if it's OK. Else return clobbered */
105 ptr_t GC_check_annotated_obj(ohdr)
108 register ptr_t body = (ptr_t)(ohdr + 1);
109 register word gc_sz = GC_size((ptr_t)ohdr);
110 if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
111 return((ptr_t)(&(ohdr -> oh_sz)));
113 if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
114 return((ptr_t)(&(ohdr -> oh_sf)));
116 if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
117 return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
119 if (((word *)body)[ROUNDED_UP_WORDS(ohdr -> oh_sz)]
120 != (END_FLAG ^ (word)body)) {
121 return((ptr_t)((word *)body + ROUNDED_UP_WORDS(ohdr -> oh_sz)));
129 register oh * ohdr = (oh *)GC_base(p);
131 GC_err_printf1("0x%lx (", (unsigned long)ohdr + sizeof(oh));
132 GC_err_puts(ohdr -> oh_string);
133 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
134 (unsigned long)(ohdr -> oh_sz));
135 PRINT_CALL_CHAIN(ohdr);
137 void GC_print_smashed_obj(p, clobbered_addr)
138 ptr_t p, clobbered_addr;
140 register oh * ohdr = (oh *)GC_base(p);
142 GC_err_printf2("0x%lx in object at 0x%lx(", (unsigned long)clobbered_addr,
144 if (clobbered_addr <= (ptr_t)(&(ohdr -> oh_sz))
145 || ohdr -> oh_string == 0) {
146 GC_err_printf1("<smashed>, appr. sz = %ld)\n",
147 GC_size((ptr_t)ohdr) - DEBUG_BYTES);
149 if (ohdr -> oh_string[0] == '\0') {
150 GC_err_puts("EMPTY(smashed?)");
152 GC_err_puts(ohdr -> oh_string);
154 GC_err_printf2(":%ld, sz=%ld)\n", (unsigned long)(ohdr -> oh_int),
155 (unsigned long)(ohdr -> oh_sz));
156 PRINT_CALL_CHAIN(ohdr);
160 void GC_check_heap_proc();
162 void GC_start_debugging()
164 GC_check_heap = GC_check_heap_proc;
165 GC_debugging_started = TRUE;
166 GC_register_displacement((word)sizeof(oh));
169 void GC_debug_register_displacement(n)
172 GC_register_displacement(n);
173 GC_register_displacement((word)sizeof(oh) + n);
177 extern_ptr_t GC_debug_malloc(size_t lb, char * s, int i)
179 extern_ptr_t GC_debug_malloc(lb, s, i)
185 extern_ptr_t result = GC_malloc(lb + DEBUG_BYTES);
188 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
191 GC_err_printf1(":%ld)\n", (unsigned long)i);
194 if (!GC_debugging_started) {
195 GC_start_debugging();
197 ADD_CALL_CHAIN(result);
198 return (GC_store_debug_info(result, (word)lb, s, (word)i));
201 #ifdef STUBBORN_ALLOC
203 extern_ptr_t GC_debug_malloc_stubborn(size_t lb, char * s, int i)
205 extern_ptr_t GC_debug_malloc_stubborn(lb, s, i)
211 extern_ptr_t result = GC_malloc_stubborn(lb + DEBUG_BYTES);
214 GC_err_printf1("GC_debug_malloc(%ld) returning NIL (",
217 GC_err_printf1(":%ld)\n", (unsigned long)i);
220 if (!GC_debugging_started) {
221 GC_start_debugging();
223 ADD_CALL_CHAIN(result);
224 return (GC_store_debug_info(result, (word)lb, s, (word)i));
227 void GC_debug_change_stubborn(p)
230 register extern_ptr_t q = GC_base(p);
234 GC_err_printf1("Bad argument: 0x%lx to GC_debug_change_stubborn\n",
236 ABORT("GC_debug_change_stubborn: bad arg");
239 if (hhdr -> hb_obj_kind != STUBBORN) {
240 GC_err_printf1("GC_debug_change_stubborn arg not stubborn: 0x%lx\n",
242 ABORT("GC_debug_change_stubborn: arg not stubborn");
244 GC_change_stubborn(q);
247 void GC_debug_end_stubborn_change(p)
250 register extern_ptr_t q = GC_base(p);
254 GC_err_printf1("Bad argument: 0x%lx to GC_debug_end_stubborn_change\n",
256 ABORT("GC_debug_end_stubborn_change: bad arg");
259 if (hhdr -> hb_obj_kind != STUBBORN) {
260 GC_err_printf1("debug_end_stubborn_change arg not stubborn: 0x%lx\n",
262 ABORT("GC_debug_end_stubborn_change: arg not stubborn");
264 GC_end_stubborn_change(q);
267 #endif /* STUBBORN_ALLOC */
270 extern_ptr_t GC_debug_malloc_atomic(size_t lb, char * s, int i)
272 extern_ptr_t GC_debug_malloc_atomic(lb, s, i)
278 extern_ptr_t result = GC_malloc_atomic(lb + DEBUG_BYTES);
281 GC_err_printf1("GC_debug_malloc_atomic(%ld) returning NIL (",
284 GC_err_printf1(":%ld)\n", (unsigned long)i);
287 if (!GC_debugging_started) {
288 GC_start_debugging();
290 ADD_CALL_CHAIN(result);
291 return (GC_store_debug_info(result, (word)lb, s, (word)i));
295 extern_ptr_t GC_debug_malloc_uncollectable(size_t lb, char * s, int i)
297 extern_ptr_t GC_debug_malloc_uncollectable(lb, s, i)
303 extern_ptr_t result = GC_malloc_uncollectable(lb + DEBUG_BYTES);
306 GC_err_printf1("GC_debug_malloc_uncollectable(%ld) returning NIL (",
309 GC_err_printf1(":%ld)\n", (unsigned long)i);
312 if (!GC_debugging_started) {
313 GC_start_debugging();
315 ADD_CALL_CHAIN(result);
316 return (GC_store_debug_info(result, (word)lb, s, (word)i));
321 void GC_debug_free(extern_ptr_t p)
323 void GC_debug_free(p)
327 register extern_ptr_t base = GC_base(p);
328 register ptr_t clobbered;
331 GC_err_printf1("Attempt to free invalid pointer %lx\n",
333 if (p != 0) ABORT("free(invalid pointer)");
335 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
337 "GC_debug_free called on pointer %lx wo debugging info\n",
340 clobbered = GC_check_annotated_obj((oh *)base);
341 if (clobbered != 0) {
342 if (((oh *)base) -> oh_sz == GC_size(base)) {
344 "GC_debug_free: found previously deallocated (?) object at ");
346 GC_err_printf0("GC_debug_free: found smashed object at ");
348 GC_print_smashed_obj(p, clobbered);
350 /* Invalidate size */
351 ((oh *)base) -> oh_sz = GC_size(base);
359 extern_ptr_t GC_debug_realloc(extern_ptr_t p, size_t lb, char *s, int i)
361 extern_ptr_t GC_debug_realloc(p, lb, s, i)
368 register extern_ptr_t base = GC_base(p);
369 register ptr_t clobbered;
370 register extern_ptr_t result = GC_debug_malloc(lb, s, i);
371 register size_t copy_sz = lb;
372 register size_t old_sz;
375 if (p == 0) return(GC_debug_malloc(lb, s, i));
378 "Attempt to free invalid pointer %lx\n", (unsigned long)p);
379 ABORT("realloc(invalid pointer)");
381 if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
383 "GC_debug_realloc called on pointer %lx wo debugging info\n",
385 return(GC_realloc(p, lb));
388 switch (hhdr -> hb_obj_kind) {
389 # ifdef STUBBORN_ALLOC
391 result = GC_debug_malloc_stubborn(lb, s, i);
395 result = GC_debug_malloc(lb, s, i);
398 result = GC_debug_malloc_atomic(lb, s, i);
401 GC_err_printf0("GC_debug_realloc: encountered bad kind\n");
404 clobbered = GC_check_annotated_obj((oh *)base);
405 if (clobbered != 0) {
406 GC_err_printf0("GC_debug_realloc: found smashed object at ");
407 GC_print_smashed_obj(p, clobbered);
409 old_sz = ((oh *)base) -> oh_sz;
410 if (old_sz < copy_sz) copy_sz = old_sz;
411 if (result == 0) return(0);
412 BCOPY(p, result, copy_sz);
416 /* Check all marked objects in the given block for validity */
418 void GC_check_heap_block(hbp, dummy)
419 register struct hblk *hbp; /* ptr to current heap block */
422 register struct hblkhdr * hhdr = HDR(hbp);
423 register word sz = hhdr -> hb_sz;
424 register int word_no;
425 register word *p, *plim;
427 p = (word *)(hbp->hb_body);
429 plim = (word *)((((word)hbp) + HBLKSIZE)
430 - WORDS_TO_BYTES(sz));
432 /* go through all words in block */
434 if( mark_bit_from_hdr(hhdr, word_no)
435 && GC_has_debug_info((ptr_t)p)) {
436 ptr_t clobbered = GC_check_annotated_obj((oh *)p);
438 if (clobbered != 0) {
440 "GC_check_heap_block: found smashed object at ");
441 GC_print_smashed_obj((ptr_t)p, clobbered);
446 } while( p <= plim );
450 /* This assumes that all accessible objects are marked, and that */
451 /* I hold the allocation lock. Normally called by collector. */
452 void GC_check_heap_proc()
454 # ifndef SMALL_CONFIG
455 if (sizeof(oh) & (2 * sizeof(word) - 1) != 0) {
456 ABORT("Alignment problem: object header has inappropriate size\n");
459 GC_apply_to_all_blocks(GC_check_heap_block, (word)0);
463 GC_finalization_proc cl_fn;
464 extern_ptr_t cl_data;
468 void * GC_make_closure(GC_finalization_proc fn, void * data)
470 extern_ptr_t GC_make_closure(fn, data)
471 GC_finalization_proc fn;
475 struct closure * result =
476 (struct closure *) GC_malloc(sizeof (struct closure));
478 result -> cl_fn = fn;
479 result -> cl_data = data;
480 return((extern_ptr_t)result);
484 void GC_debug_invoke_finalizer(void * obj, void * data)
486 void GC_debug_invoke_finalizer(obj, data)
491 register struct closure * cl = (struct closure *) data;
493 (*(cl -> cl_fn))((extern_ptr_t)((char *)obj + sizeof(oh)), cl -> cl_data);