dbg_mlc.c 38 KB

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