misc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*
  2. * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3. * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
  4. * Copyright (c) 1999-2001 by Hewlett-Packard Company. 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. /* Boehm, July 31, 1995 5:02 pm PDT */
  16. #include <stdio.h>
  17. #include <limits.h>
  18. #ifndef _WIN32_WCE
  19. #include <signal.h>
  20. #endif
  21. #define I_HIDE_POINTERS /* To make GC_call_with_alloc_lock visible */
  22. #include "private/gc_pmark.h"
  23. #ifdef GC_SOLARIS_THREADS
  24. # include <sys/syscall.h>
  25. #endif
  26. #if defined(MSWIN32) || defined(MSWINCE)
  27. # define WIN32_LEAN_AND_MEAN
  28. # define NOSERVICE
  29. # include <windows.h>
  30. # include <tchar.h>
  31. #endif
  32. # ifdef THREADS
  33. # ifdef PCR
  34. # include "il/PCR_IL.h"
  35. PCR_Th_ML GC_allocate_ml;
  36. # else
  37. # ifdef SRC_M3
  38. /* Critical section counter is defined in the M3 runtime */
  39. /* That's all we use. */
  40. # else
  41. # ifdef GC_SOLARIS_THREADS
  42. mutex_t GC_allocate_ml; /* Implicitly initialized. */
  43. # else
  44. # if defined(GC_WIN32_THREADS)
  45. # if defined(GC_PTHREADS)
  46. pthread_mutex_t GC_allocate_ml = PTHREAD_MUTEX_INITIALIZER;
  47. # elif defined(GC_DLL)
  48. __declspec(dllexport) CRITICAL_SECTION GC_allocate_ml;
  49. # else
  50. CRITICAL_SECTION GC_allocate_ml;
  51. # endif
  52. # else
  53. # if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS)
  54. # if defined(USE_SPIN_LOCK)
  55. pthread_t GC_lock_holder = NO_THREAD;
  56. # else
  57. pthread_mutex_t GC_allocate_ml = PTHREAD_MUTEX_INITIALIZER;
  58. pthread_t GC_lock_holder = NO_THREAD;
  59. /* Used only for assertions, and to prevent */
  60. /* recursive reentry in the system call wrapper. */
  61. # endif
  62. # else
  63. --> declare allocator lock here
  64. # endif
  65. # endif
  66. # endif
  67. # endif
  68. # endif
  69. # endif
  70. #if defined(NOSYS) || defined(ECOS)
  71. #undef STACKBASE
  72. #endif
  73. /* Dont unnecessarily call GC_register_main_static_data() in case */
  74. /* dyn_load.c isn't linked in. */
  75. #ifdef DYNAMIC_LOADING
  76. # define GC_REGISTER_MAIN_STATIC_DATA() GC_register_main_static_data()
  77. #else
  78. # define GC_REGISTER_MAIN_STATIC_DATA() TRUE
  79. #endif
  80. GC_FAR struct _GC_arrays GC_arrays /* = { 0 } */;
  81. GC_bool GC_debugging_started = FALSE;
  82. /* defined here so we don't have to load debug_malloc.o */
  83. void (*GC_check_heap) GC_PROTO((void)) = (void (*) GC_PROTO((void)))0;
  84. void (*GC_print_all_smashed) GC_PROTO((void)) = (void (*) GC_PROTO((void)))0;
  85. void (*GC_start_call_back) GC_PROTO((void)) = (void (*) GC_PROTO((void)))0;
  86. ptr_t GC_stackbottom = 0;
  87. #ifdef IA64
  88. ptr_t GC_register_stackbottom = 0;
  89. #endif
  90. GC_bool GC_dont_gc = 0;
  91. GC_bool GC_dont_precollect = 0;
  92. GC_bool GC_quiet = 0;
  93. GC_bool GC_print_stats = 0;
  94. GC_bool GC_print_back_height = 0;
  95. #ifndef NO_DEBUGGING
  96. GC_bool GC_dump_regularly = 0; /* Generate regular debugging dumps. */
  97. #endif
  98. #ifdef KEEP_BACK_PTRS
  99. long GC_backtraces = 0; /* Number of random backtraces to */
  100. /* generate for each GC. */
  101. #endif
  102. #ifdef FIND_LEAK
  103. int GC_find_leak = 1;
  104. #else
  105. int GC_find_leak = 0;
  106. #endif
  107. #ifdef ALL_INTERIOR_POINTERS
  108. int GC_all_interior_pointers = 1;
  109. #else
  110. int GC_all_interior_pointers = 0;
  111. #endif
  112. long GC_large_alloc_warn_interval = 5;
  113. /* Interval between unsuppressed warnings. */
  114. long GC_large_alloc_warn_suppressed = 0;
  115. /* Number of warnings suppressed so far. */
  116. /*ARGSUSED*/
  117. GC_PTR GC_default_oom_fn GC_PROTO((size_t bytes_requested))
  118. {
  119. return(0);
  120. }
  121. GC_PTR (*GC_oom_fn) GC_PROTO((size_t bytes_requested)) = GC_default_oom_fn;
  122. extern signed_word GC_mem_found;
  123. void * GC_project2(arg1, arg2)
  124. void *arg1;
  125. void *arg2;
  126. {
  127. return arg2;
  128. }
  129. # ifdef MERGE_SIZES
  130. /* Set things up so that GC_size_map[i] >= words(i), */
  131. /* but not too much bigger */
  132. /* and so that size_map contains relatively few distinct entries */
  133. /* This is stolen from Russ Atkinson's Cedar quantization */
  134. /* alogrithm (but we precompute it). */
  135. void GC_init_size_map()
  136. {
  137. register unsigned i;
  138. /* Map size 0 to something bigger. */
  139. /* This avoids problems at lower levels. */
  140. /* One word objects don't have to be 2 word aligned, */
  141. /* unless we're using mark bytes. */
  142. for (i = 0; i < sizeof(word); i++) {
  143. GC_size_map[i] = MIN_WORDS;
  144. }
  145. # if MIN_WORDS > 1
  146. GC_size_map[sizeof(word)] = MIN_WORDS;
  147. # else
  148. GC_size_map[sizeof(word)] = ROUNDED_UP_WORDS(sizeof(word));
  149. # endif
  150. for (i = sizeof(word) + 1; i <= 8 * sizeof(word); i++) {
  151. GC_size_map[i] = ALIGNED_WORDS(i);
  152. }
  153. for (i = 8*sizeof(word) + 1; i <= 16 * sizeof(word); i++) {
  154. GC_size_map[i] = (ROUNDED_UP_WORDS(i) + 1) & (~1);
  155. }
  156. # ifdef GC_GCJ_SUPPORT
  157. /* Make all sizes up to 32 words predictable, so that a */
  158. /* compiler can statically perform the same computation, */
  159. /* or at least a computation that results in similar size */
  160. /* classes. */
  161. for (i = 16*sizeof(word) + 1; i <= 32 * sizeof(word); i++) {
  162. GC_size_map[i] = (ROUNDED_UP_WORDS(i) + 3) & (~3);
  163. }
  164. # endif
  165. /* We leave the rest of the array to be filled in on demand. */
  166. }
  167. /* Fill in additional entries in GC_size_map, including the ith one */
  168. /* We assume the ith entry is currently 0. */
  169. /* Note that a filled in section of the array ending at n always */
  170. /* has length at least n/4. */
  171. void GC_extend_size_map(i)
  172. word i;
  173. {
  174. word orig_word_sz = ROUNDED_UP_WORDS(i);
  175. word word_sz = orig_word_sz;
  176. register word byte_sz = WORDS_TO_BYTES(word_sz);
  177. /* The size we try to preserve. */
  178. /* Close to to i, unless this would */
  179. /* introduce too many distinct sizes. */
  180. word smaller_than_i = byte_sz - (byte_sz >> 3);
  181. word much_smaller_than_i = byte_sz - (byte_sz >> 2);
  182. register word low_limit; /* The lowest indexed entry we */
  183. /* initialize. */
  184. register word j;
  185. if (GC_size_map[smaller_than_i] == 0) {
  186. low_limit = much_smaller_than_i;
  187. while (GC_size_map[low_limit] != 0) low_limit++;
  188. } else {
  189. low_limit = smaller_than_i + 1;
  190. while (GC_size_map[low_limit] != 0) low_limit++;
  191. word_sz = ROUNDED_UP_WORDS(low_limit);
  192. word_sz += word_sz >> 3;
  193. if (word_sz < orig_word_sz) word_sz = orig_word_sz;
  194. }
  195. # ifdef ALIGN_DOUBLE
  196. word_sz += 1;
  197. word_sz &= ~1;
  198. # endif
  199. if (word_sz > MAXOBJSZ) {
  200. word_sz = MAXOBJSZ;
  201. }
  202. /* If we can fit the same number of larger objects in a block, */
  203. /* do so. */
  204. {
  205. size_t number_of_objs = BODY_SZ/word_sz;
  206. word_sz = BODY_SZ/number_of_objs;
  207. # ifdef ALIGN_DOUBLE
  208. word_sz &= ~1;
  209. # endif
  210. }
  211. byte_sz = WORDS_TO_BYTES(word_sz);
  212. if (GC_all_interior_pointers) {
  213. /* We need one extra byte; don't fill in GC_size_map[byte_sz] */
  214. byte_sz -= EXTRA_BYTES;
  215. }
  216. for (j = low_limit; j <= byte_sz; j++) GC_size_map[j] = word_sz;
  217. }
  218. # endif
  219. /*
  220. * The following is a gross hack to deal with a problem that can occur
  221. * on machines that are sloppy about stack frame sizes, notably SPARC.
  222. * Bogus pointers may be written to the stack and not cleared for
  223. * a LONG time, because they always fall into holes in stack frames
  224. * that are not written. We partially address this by clearing
  225. * sections of the stack whenever we get control.
  226. */
  227. word GC_stack_last_cleared = 0; /* GC_no when we last did this */
  228. # ifdef THREADS
  229. # define BIG_CLEAR_SIZE 2048 /* Clear this much now and then. */
  230. # define SMALL_CLEAR_SIZE 256 /* Clear this much every time. */
  231. # endif
  232. # define CLEAR_SIZE 213 /* Granularity for GC_clear_stack_inner */
  233. # define DEGRADE_RATE 50
  234. word GC_min_sp; /* Coolest stack pointer value from which we've */
  235. /* already cleared the stack. */
  236. word GC_high_water;
  237. /* "hottest" stack pointer value we have seen */
  238. /* recently. Degrades over time. */
  239. word GC_words_allocd_at_reset;
  240. #if defined(ASM_CLEAR_CODE)
  241. extern ptr_t GC_clear_stack_inner();
  242. #else
  243. /* Clear the stack up to about limit. Return arg. */
  244. /*ARGSUSED*/
  245. ptr_t GC_clear_stack_inner(arg, limit)
  246. ptr_t arg;
  247. word limit;
  248. {
  249. word dummy[CLEAR_SIZE];
  250. BZERO(dummy, CLEAR_SIZE*sizeof(word));
  251. if ((word)(dummy) COOLER_THAN limit) {
  252. (void) GC_clear_stack_inner(arg, limit);
  253. }
  254. /* Make sure the recursive call is not a tail call, and the bzero */
  255. /* call is not recognized as dead code. */
  256. GC_noop1((word)dummy);
  257. return(arg);
  258. }
  259. #endif
  260. /* Clear some of the inaccessible part of the stack. Returns its */
  261. /* argument, so it can be used in a tail call position, hence clearing */
  262. /* another frame. */
  263. ptr_t GC_clear_stack(arg)
  264. ptr_t arg;
  265. {
  266. register word sp = (word)GC_approx_sp(); /* Hotter than actual sp */
  267. # ifdef THREADS
  268. word dummy[SMALL_CLEAR_SIZE];
  269. static unsigned random_no = 0;
  270. /* Should be more random than it is ... */
  271. /* Used to occasionally clear a bigger */
  272. /* chunk. */
  273. # endif
  274. register word limit;
  275. # define SLOP 400
  276. /* Extra bytes we clear every time. This clears our own */
  277. /* activation record, and should cause more frequent */
  278. /* clearing near the cold end of the stack, a good thing. */
  279. # define GC_SLOP 4000
  280. /* We make GC_high_water this much hotter than we really saw */
  281. /* saw it, to cover for GC noise etc. above our current frame. */
  282. # define CLEAR_THRESHOLD 100000
  283. /* We restart the clearing process after this many bytes of */
  284. /* allocation. Otherwise very heavily recursive programs */
  285. /* with sparse stacks may result in heaps that grow almost */
  286. /* without bounds. As the heap gets larger, collection */
  287. /* frequency decreases, thus clearing frequency would decrease, */
  288. /* thus more junk remains accessible, thus the heap gets */
  289. /* larger ... */
  290. # ifdef THREADS
  291. if (++random_no % 13 == 0) {
  292. limit = sp;
  293. MAKE_HOTTER(limit, BIG_CLEAR_SIZE*sizeof(word));
  294. limit &= ~0xf; /* Make it sufficiently aligned for assembly */
  295. /* implementations of GC_clear_stack_inner. */
  296. return GC_clear_stack_inner(arg, limit);
  297. } else {
  298. BZERO(dummy, SMALL_CLEAR_SIZE*sizeof(word));
  299. return arg;
  300. }
  301. # else
  302. if (GC_gc_no > GC_stack_last_cleared) {
  303. /* Start things over, so we clear the entire stack again */
  304. if (GC_stack_last_cleared == 0) GC_high_water = (word) GC_stackbottom;
  305. GC_min_sp = GC_high_water;
  306. GC_stack_last_cleared = GC_gc_no;
  307. GC_words_allocd_at_reset = GC_words_allocd;
  308. }
  309. /* Adjust GC_high_water */
  310. MAKE_COOLER(GC_high_water, WORDS_TO_BYTES(DEGRADE_RATE) + GC_SLOP);
  311. if (sp HOTTER_THAN GC_high_water) {
  312. GC_high_water = sp;
  313. }
  314. MAKE_HOTTER(GC_high_water, GC_SLOP);
  315. limit = GC_min_sp;
  316. MAKE_HOTTER(limit, SLOP);
  317. if (sp COOLER_THAN limit) {
  318. limit &= ~0xf; /* Make it sufficiently aligned for assembly */
  319. /* implementations of GC_clear_stack_inner. */
  320. GC_min_sp = sp;
  321. return(GC_clear_stack_inner(arg, limit));
  322. } else if (WORDS_TO_BYTES(GC_words_allocd - GC_words_allocd_at_reset)
  323. > CLEAR_THRESHOLD) {
  324. /* Restart clearing process, but limit how much clearing we do. */
  325. GC_min_sp = sp;
  326. MAKE_HOTTER(GC_min_sp, CLEAR_THRESHOLD/4);
  327. if (GC_min_sp HOTTER_THAN GC_high_water) GC_min_sp = GC_high_water;
  328. GC_words_allocd_at_reset = GC_words_allocd;
  329. }
  330. return(arg);
  331. # endif
  332. }
  333. /* Return a pointer to the base address of p, given a pointer to a */
  334. /* an address within an object. Return 0 o.w. */
  335. # ifdef __STDC__
  336. GC_PTR GC_base(GC_PTR p)
  337. # else
  338. GC_PTR GC_base(p)
  339. GC_PTR p;
  340. # endif
  341. {
  342. register word r;
  343. register struct hblk *h;
  344. register bottom_index *bi;
  345. register hdr *candidate_hdr;
  346. register word limit;
  347. r = (word)p;
  348. if (!GC_is_initialized) return 0;
  349. h = HBLKPTR(r);
  350. GET_BI(r, bi);
  351. candidate_hdr = HDR_FROM_BI(bi, r);
  352. if (candidate_hdr == 0) return(0);
  353. /* If it's a pointer to the middle of a large object, move it */
  354. /* to the beginning. */
  355. while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr)) {
  356. h = FORWARDED_ADDR(h,candidate_hdr);
  357. r = (word)h;
  358. candidate_hdr = HDR(h);
  359. }
  360. if (candidate_hdr -> hb_map == GC_invalid_map) return(0);
  361. /* Make sure r points to the beginning of the object */
  362. r &= ~(WORDS_TO_BYTES(1) - 1);
  363. {
  364. register int offset = HBLKDISPL(r);
  365. register signed_word sz = candidate_hdr -> hb_sz;
  366. register signed_word map_entry;
  367. map_entry = MAP_ENTRY((candidate_hdr -> hb_map), offset);
  368. if (map_entry > CPP_MAX_OFFSET) {
  369. map_entry = (signed_word)(BYTES_TO_WORDS(offset)) % sz;
  370. }
  371. r -= WORDS_TO_BYTES(map_entry);
  372. limit = r + WORDS_TO_BYTES(sz);
  373. if (limit > (word)(h + 1)
  374. && sz <= BYTES_TO_WORDS(HBLKSIZE)) {
  375. return(0);
  376. }
  377. if ((word)p >= limit) return(0);
  378. }
  379. return((GC_PTR)r);
  380. }
  381. /* Return the size of an object, given a pointer to its base. */
  382. /* (For small obects this also happens to work from interior pointers, */
  383. /* but that shouldn't be relied upon.) */
  384. # ifdef __STDC__
  385. size_t GC_size(GC_PTR p)
  386. # else
  387. size_t GC_size(p)
  388. GC_PTR p;
  389. # endif
  390. {
  391. register int sz;
  392. register hdr * hhdr = HDR(p);
  393. sz = WORDS_TO_BYTES(hhdr -> hb_sz);
  394. return(sz);
  395. }
  396. size_t GC_get_heap_size GC_PROTO(())
  397. {
  398. return ((size_t) GC_heapsize);
  399. }
  400. size_t GC_get_free_bytes GC_PROTO(())
  401. {
  402. return ((size_t) GC_large_free_bytes);
  403. }
  404. size_t GC_get_bytes_since_gc GC_PROTO(())
  405. {
  406. return ((size_t) WORDS_TO_BYTES(GC_words_allocd));
  407. }
  408. size_t GC_get_total_bytes GC_PROTO(())
  409. {
  410. return ((size_t) WORDS_TO_BYTES(GC_words_allocd+GC_words_allocd_before_gc));
  411. }
  412. GC_bool GC_is_initialized = FALSE;
  413. void GC_init()
  414. {
  415. DCL_LOCK_STATE;
  416. DISABLE_SIGNALS();
  417. #if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
  418. if (!GC_is_initialized) {
  419. BOOL (WINAPI *pfn) (LPCRITICAL_SECTION, DWORD) = NULL;
  420. HMODULE hK32 = GetModuleHandle("kernel32.dll");
  421. if (hK32)
  422. pfn = (BOOL (WINAPI *) (LPCRITICAL_SECTION, DWORD))
  423. GetProcAddress (hK32,
  424. "InitializeCriticalSectionAndSpinCount");
  425. if (pfn)
  426. pfn(&GC_allocate_ml, 4000);
  427. else
  428. InitializeCriticalSection (&GC_allocate_ml);
  429. }
  430. #endif /* MSWIN32 */
  431. LOCK();
  432. GC_init_inner();
  433. UNLOCK();
  434. ENABLE_SIGNALS();
  435. # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
  436. /* Make sure marker threads and started and thread local */
  437. /* allocation is initialized, in case we didn't get */
  438. /* called from GC_init_parallel(); */
  439. {
  440. extern void GC_init_parallel(void);
  441. GC_init_parallel();
  442. }
  443. # endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
  444. # if defined(DYNAMIC_LOADING) && defined(DARWIN)
  445. {
  446. /* This must be called WITHOUT the allocation lock held
  447. and before any threads are created */
  448. extern void GC_init_dyld();
  449. GC_init_dyld();
  450. }
  451. # endif
  452. }
  453. #if defined(MSWIN32) || defined(MSWINCE)
  454. CRITICAL_SECTION GC_write_cs;
  455. #endif
  456. #ifdef MSWIN32
  457. extern void GC_init_win32 GC_PROTO((void));
  458. #endif
  459. extern void GC_setpagesize();
  460. #ifdef MSWIN32
  461. extern GC_bool GC_no_win32_dlls;
  462. #else
  463. # define GC_no_win32_dlls FALSE
  464. #endif
  465. void GC_exit_check GC_PROTO((void))
  466. {
  467. GC_gcollect();
  468. }
  469. #ifdef SEARCH_FOR_DATA_START
  470. extern void GC_init_linux_data_start GC_PROTO((void));
  471. #endif
  472. #ifdef UNIX_LIKE
  473. extern void GC_set_and_save_fault_handler GC_PROTO((void (*handler)(int)));
  474. static void looping_handler(sig)
  475. int sig;
  476. {
  477. GC_err_printf1("Caught signal %d: looping in handler\n", sig);
  478. for(;;);
  479. }
  480. static GC_bool installed_looping_handler = FALSE;
  481. static void maybe_install_looping_handler()
  482. {
  483. /* Install looping handler before the write fault handler, so we */
  484. /* handle write faults correctly. */
  485. if (!installed_looping_handler && 0 != GETENV("GC_LOOP_ON_ABORT")) {
  486. GC_set_and_save_fault_handler(looping_handler);
  487. installed_looping_handler = TRUE;
  488. }
  489. }
  490. #else /* !UNIX_LIKE */
  491. # define maybe_install_looping_handler()
  492. #endif
  493. void GC_init_inner()
  494. {
  495. # if !defined(THREADS) && defined(GC_ASSERTIONS)
  496. word dummy;
  497. # endif
  498. word initial_heap_sz = (word)MINHINCR;
  499. if (GC_is_initialized) return;
  500. # ifdef PRINTSTATS
  501. GC_print_stats = 1;
  502. # endif
  503. # if defined(MSWIN32) || defined(MSWINCE)
  504. InitializeCriticalSection(&GC_write_cs);
  505. # endif
  506. if (0 != GETENV("GC_PRINT_STATS")) {
  507. GC_print_stats = 1;
  508. }
  509. # ifndef NO_DEBUGGING
  510. if (0 != GETENV("GC_DUMP_REGULARLY")) {
  511. GC_dump_regularly = 1;
  512. }
  513. # endif
  514. # ifdef KEEP_BACK_PTRS
  515. {
  516. char * backtraces_string = GETENV("GC_BACKTRACES");
  517. if (0 != backtraces_string) {
  518. GC_backtraces = atol(backtraces_string);
  519. if (backtraces_string[0] == '\0') GC_backtraces = 1;
  520. }
  521. }
  522. # endif
  523. if (0 != GETENV("GC_FIND_LEAK")) {
  524. GC_find_leak = 1;
  525. # ifdef __STDC__
  526. atexit(GC_exit_check);
  527. # endif
  528. }
  529. if (0 != GETENV("GC_ALL_INTERIOR_POINTERS")) {
  530. GC_all_interior_pointers = 1;
  531. }
  532. if (0 != GETENV("GC_DONT_GC")) {
  533. GC_dont_gc = 1;
  534. }
  535. if (0 != GETENV("GC_PRINT_BACK_HEIGHT")) {
  536. GC_print_back_height = 1;
  537. }
  538. if (0 != GETENV("GC_NO_BLACKLIST_WARNING")) {
  539. GC_large_alloc_warn_interval = LONG_MAX;
  540. }
  541. {
  542. char * time_limit_string = GETENV("GC_PAUSE_TIME_TARGET");
  543. if (0 != time_limit_string) {
  544. long time_limit = atol(time_limit_string);
  545. if (time_limit < 5) {
  546. WARN("GC_PAUSE_TIME_TARGET environment variable value too small "
  547. "or bad syntax: Ignoring\n", 0);
  548. } else {
  549. GC_time_limit = time_limit;
  550. }
  551. }
  552. }
  553. {
  554. char * interval_string = GETENV("GC_LARGE_ALLOC_WARN_INTERVAL");
  555. if (0 != interval_string) {
  556. long interval = atol(interval_string);
  557. if (interval <= 0) {
  558. WARN("GC_LARGE_ALLOC_WARN_INTERVAL environment variable has "
  559. "bad value: Ignoring\n", 0);
  560. } else {
  561. GC_large_alloc_warn_interval = interval;
  562. }
  563. }
  564. }
  565. maybe_install_looping_handler();
  566. /* Adjust normal object descriptor for extra allocation. */
  567. if (ALIGNMENT > GC_DS_TAGS && EXTRA_BYTES != 0) {
  568. GC_obj_kinds[NORMAL].ok_descriptor = ((word)(-ALIGNMENT) | GC_DS_LENGTH);
  569. }
  570. GC_setpagesize();
  571. GC_exclude_static_roots(beginGC_arrays, endGC_arrays);
  572. GC_exclude_static_roots(beginGC_obj_kinds, endGC_obj_kinds);
  573. # ifdef SEPARATE_GLOBALS
  574. GC_exclude_static_roots(beginGC_objfreelist, endGC_objfreelist);
  575. GC_exclude_static_roots(beginGC_aobjfreelist, endGC_aobjfreelist);
  576. # endif
  577. # ifdef MSWIN32
  578. GC_init_win32();
  579. # endif
  580. # if defined(SEARCH_FOR_DATA_START)
  581. GC_init_linux_data_start();
  582. # endif
  583. # if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__)
  584. GC_init_netbsd_elf();
  585. # endif
  586. # if defined(GC_PTHREADS) || defined(GC_SOLARIS_THREADS) \
  587. || defined(GC_WIN32_THREADS)
  588. GC_thr_init();
  589. # endif
  590. # ifdef GC_SOLARIS_THREADS
  591. /* We need dirty bits in order to find live stack sections. */
  592. GC_dirty_init();
  593. # endif
  594. # if !defined(THREADS) || defined(GC_PTHREADS) || defined(GC_WIN32_THREADS) \
  595. || defined(GC_SOLARIS_THREADS)
  596. if (GC_stackbottom == 0) {
  597. # if defined(GC_PTHREADS) && ! defined(GC_SOLARIS_THREADS)
  598. /* Use thread_stack_base if available, as GC could be initialized from
  599. a thread that is not the "main" thread. */
  600. GC_stackbottom = GC_get_thread_stack_base();
  601. # endif
  602. if (GC_stackbottom == 0)
  603. GC_stackbottom = GC_get_stack_base();
  604. # if (defined(LINUX) || defined(HPUX)) && defined(IA64)
  605. GC_register_stackbottom = GC_get_register_stack_base();
  606. # endif
  607. } else {
  608. # if (defined(LINUX) || defined(HPUX)) && defined(IA64)
  609. if (GC_register_stackbottom == 0) {
  610. WARN("GC_register_stackbottom should be set with GC_stackbottom", 0);
  611. /* The following may fail, since we may rely on */
  612. /* alignment properties that may not hold with a user set */
  613. /* GC_stackbottom. */
  614. GC_register_stackbottom = GC_get_register_stack_base();
  615. }
  616. # endif
  617. }
  618. # endif
  619. GC_STATIC_ASSERT(sizeof (ptr_t) == sizeof(word));
  620. GC_STATIC_ASSERT(sizeof (signed_word) == sizeof(word));
  621. GC_STATIC_ASSERT(sizeof (struct hblk) == HBLKSIZE);
  622. # ifndef THREADS
  623. # if defined(STACK_GROWS_UP) && defined(STACK_GROWS_DOWN)
  624. ABORT(
  625. "Only one of STACK_GROWS_UP and STACK_GROWS_DOWN should be defd\n");
  626. # endif
  627. # if !defined(STACK_GROWS_UP) && !defined(STACK_GROWS_DOWN)
  628. ABORT(
  629. "One of STACK_GROWS_UP and STACK_GROWS_DOWN should be defd\n");
  630. # endif
  631. # ifdef STACK_GROWS_DOWN
  632. GC_ASSERT((word)(&dummy) <= (word)GC_stackbottom);
  633. # else
  634. GC_ASSERT((word)(&dummy) >= (word)GC_stackbottom);
  635. # endif
  636. # endif
  637. # if !defined(_AUX_SOURCE) || defined(__GNUC__)
  638. GC_ASSERT((word)(-1) > (word)0);
  639. /* word should be unsigned */
  640. # endif
  641. GC_ASSERT((signed_word)(-1) < (signed_word)0);
  642. /* Add initial guess of root sets. Do this first, since sbrk(0) */
  643. /* might be used. */
  644. if (GC_REGISTER_MAIN_STATIC_DATA()) GC_register_data_segments();
  645. GC_init_headers();
  646. GC_bl_init();
  647. GC_mark_init();
  648. {
  649. char * sz_str = GETENV("GC_INITIAL_HEAP_SIZE");
  650. if (sz_str != NULL) {
  651. initial_heap_sz = atoi(sz_str);
  652. if (initial_heap_sz <= MINHINCR * HBLKSIZE) {
  653. WARN("Bad initial heap size %s - ignoring it.\n",
  654. sz_str);
  655. }
  656. initial_heap_sz = divHBLKSZ(initial_heap_sz);
  657. }
  658. }
  659. {
  660. char * sz_str = GETENV("GC_MAXIMUM_HEAP_SIZE");
  661. if (sz_str != NULL) {
  662. word max_heap_sz = (word)atol(sz_str);
  663. if (max_heap_sz < initial_heap_sz * HBLKSIZE) {
  664. WARN("Bad maximum heap size %s - ignoring it.\n",
  665. sz_str);
  666. }
  667. if (0 == GC_max_retries) GC_max_retries = 2;
  668. GC_set_max_heap_size(max_heap_sz);
  669. }
  670. }
  671. if (!GC_expand_hp_inner(initial_heap_sz)) {
  672. GC_err_printf0("Can't start up: not enough memory\n");
  673. EXIT();
  674. }
  675. /* Preallocate large object map. It's otherwise inconvenient to */
  676. /* deal with failure. */
  677. if (!GC_add_map_entry((word)0)) {
  678. GC_err_printf0("Can't start up: not enough memory\n");
  679. EXIT();
  680. }
  681. GC_register_displacement_inner(0L);
  682. # ifdef MERGE_SIZES
  683. GC_init_size_map();
  684. # endif
  685. # ifdef PCR
  686. if (PCR_IL_Lock(PCR_Bool_false, PCR_allSigsBlocked, PCR_waitForever)
  687. != PCR_ERes_okay) {
  688. ABORT("Can't lock load state\n");
  689. } else if (PCR_IL_Unlock() != PCR_ERes_okay) {
  690. ABORT("Can't unlock load state\n");
  691. }
  692. PCR_IL_Unlock();
  693. GC_pcr_install();
  694. # endif
  695. # if !defined(SMALL_CONFIG)
  696. if (!GC_no_win32_dlls && 0 != GETENV("GC_ENABLE_INCREMENTAL")) {
  697. GC_ASSERT(!GC_incremental);
  698. GC_setpagesize();
  699. # ifndef GC_SOLARIS_THREADS
  700. GC_dirty_init();
  701. # endif
  702. GC_ASSERT(GC_words_allocd == 0)
  703. GC_incremental = TRUE;
  704. }
  705. # endif /* !SMALL_CONFIG */
  706. COND_DUMP;
  707. /* Get black list set up and/or incremental GC started */
  708. if (!GC_dont_precollect || GC_incremental) GC_gcollect_inner();
  709. GC_is_initialized = TRUE;
  710. # ifdef STUBBORN_ALLOC
  711. GC_stubborn_init();
  712. # endif
  713. /* Convince lint that some things are used */
  714. # ifdef LINT
  715. {
  716. extern char * GC_copyright[];
  717. extern int GC_read();
  718. extern void GC_register_finalizer_no_order();
  719. GC_noop(GC_copyright, GC_find_header,
  720. GC_push_one, GC_call_with_alloc_lock, GC_read,
  721. GC_dont_expand,
  722. # ifndef NO_DEBUGGING
  723. GC_dump,
  724. # endif
  725. GC_register_finalizer_no_order);
  726. }
  727. # endif
  728. }
  729. void GC_enable_incremental GC_PROTO(())
  730. {
  731. # if !defined(SMALL_CONFIG) && !defined(KEEP_BACK_PTRS)
  732. /* If we are keeping back pointers, the GC itself dirties all */
  733. /* pages on which objects have been marked, making */
  734. /* incremental GC pointless. */
  735. if (!GC_find_leak) {
  736. DCL_LOCK_STATE;
  737. DISABLE_SIGNALS();
  738. LOCK();
  739. if (GC_incremental) goto out;
  740. GC_setpagesize();
  741. if (GC_no_win32_dlls) goto out;
  742. # ifndef GC_SOLARIS_THREADS
  743. maybe_install_looping_handler(); /* Before write fault handler! */
  744. GC_dirty_init();
  745. # endif
  746. if (!GC_is_initialized) {
  747. GC_init_inner();
  748. }
  749. if (GC_incremental) goto out;
  750. if (GC_dont_gc) {
  751. /* Can't easily do it. */
  752. UNLOCK();
  753. ENABLE_SIGNALS();
  754. return;
  755. }
  756. if (GC_words_allocd > 0) {
  757. /* There may be unmarked reachable objects */
  758. GC_gcollect_inner();
  759. } /* else we're OK in assuming everything's */
  760. /* clean since nothing can point to an */
  761. /* unmarked object. */
  762. GC_read_dirty();
  763. GC_incremental = TRUE;
  764. out:
  765. UNLOCK();
  766. ENABLE_SIGNALS();
  767. }
  768. # endif
  769. }
  770. #if defined(MSWIN32) || defined(MSWINCE)
  771. # define LOG_FILE _T("gc.log")
  772. HANDLE GC_stdout = 0;
  773. void GC_deinit()
  774. {
  775. if (GC_is_initialized) {
  776. DeleteCriticalSection(&GC_write_cs);
  777. }
  778. }
  779. int GC_write(buf, len)
  780. GC_CONST char * buf;
  781. size_t len;
  782. {
  783. BOOL tmp;
  784. DWORD written;
  785. if (len == 0)
  786. return 0;
  787. EnterCriticalSection(&GC_write_cs);
  788. if (GC_stdout == INVALID_HANDLE_VALUE) {
  789. return -1;
  790. } else if (GC_stdout == 0) {
  791. GC_stdout = CreateFile(LOG_FILE, GENERIC_WRITE,
  792. FILE_SHARE_READ | FILE_SHARE_WRITE,
  793. NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH,
  794. NULL);
  795. if (GC_stdout == INVALID_HANDLE_VALUE) ABORT("Open of log file failed");
  796. }
  797. tmp = WriteFile(GC_stdout, buf, len, &written, NULL);
  798. if (!tmp)
  799. DebugBreak();
  800. LeaveCriticalSection(&GC_write_cs);
  801. return tmp ? (int)written : -1;
  802. }
  803. #endif
  804. #if defined(OS2) || defined(MACOS)
  805. FILE * GC_stdout = NULL;
  806. FILE * GC_stderr = NULL;
  807. int GC_tmp; /* Should really be local ... */
  808. void GC_set_files()
  809. {
  810. if (GC_stdout == NULL) {
  811. GC_stdout = stdout;
  812. }
  813. if (GC_stderr == NULL) {
  814. GC_stderr = stderr;
  815. }
  816. }
  817. #endif
  818. #if !defined(OS2) && !defined(MACOS) && !defined(MSWIN32) && !defined(MSWINCE)
  819. int GC_stdout = 1;
  820. int GC_stderr = 2;
  821. # if !defined(AMIGA)
  822. # include <unistd.h>
  823. # endif
  824. #endif
  825. #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(OS2) \
  826. && !defined(MACOS) && !defined(ECOS) && !defined(NOSYS)
  827. int GC_write(fd, buf, len)
  828. int fd;
  829. GC_CONST char *buf;
  830. size_t len;
  831. {
  832. register int bytes_written = 0;
  833. register int result;
  834. while (bytes_written < len) {
  835. # ifdef GC_SOLARIS_THREADS
  836. result = syscall(SYS_write, fd, buf + bytes_written,
  837. len - bytes_written);
  838. # else
  839. result = write(fd, buf + bytes_written, len - bytes_written);
  840. # endif
  841. if (-1 == result) return(result);
  842. bytes_written += result;
  843. }
  844. return(bytes_written);
  845. }
  846. #endif /* UN*X */
  847. #ifdef ECOS
  848. int GC_write(fd, buf, len)
  849. {
  850. _Jv_diag_write (buf, len);
  851. return len;
  852. }
  853. #endif
  854. #ifdef NOSYS
  855. int GC_write(fd, buf, len)
  856. {
  857. /* No writing. */
  858. return len;
  859. }
  860. #endif
  861. #if defined(MSWIN32) || defined(MSWINCE)
  862. # define WRITE(f, buf, len) GC_write(buf, len)
  863. #else
  864. # if defined(OS2) || defined(MACOS)
  865. # define WRITE(f, buf, len) (GC_set_files(), \
  866. GC_tmp = fwrite((buf), 1, (len), (f)), \
  867. fflush(f), GC_tmp)
  868. # else
  869. # define WRITE(f, buf, len) GC_write((f), (buf), (len))
  870. # endif
  871. #endif
  872. /* A version of printf that is unlikely to call malloc, and is thus safer */
  873. /* to call from the collector in case malloc has been bound to GC_malloc. */
  874. /* Assumes that no more than 1023 characters are written at once. */
  875. /* Assumes that all arguments have been converted to something of the */
  876. /* same size as long, and that the format conversions expect something */
  877. /* of that size. */
  878. void GC_printf(format, a, b, c, d, e, f)
  879. GC_CONST char * format;
  880. long a, b, c, d, e, f;
  881. {
  882. char buf[1025];
  883. if (GC_quiet) return;
  884. buf[1024] = 0x15;
  885. (void) sprintf(buf, format, a, b, c, d, e, f);
  886. if (buf[1024] != 0x15) ABORT("GC_printf clobbered stack");
  887. if (WRITE(GC_stdout, buf, strlen(buf)) < 0) ABORT("write to stdout failed");
  888. }
  889. void GC_err_printf(format, a, b, c, d, e, f)
  890. GC_CONST char * format;
  891. long a, b, c, d, e, f;
  892. {
  893. char buf[1025];
  894. buf[1024] = 0x15;
  895. (void) sprintf(buf, format, a, b, c, d, e, f);
  896. if (buf[1024] != 0x15) ABORT("GC_err_printf clobbered stack");
  897. if (WRITE(GC_stderr, buf, strlen(buf)) < 0) ABORT("write to stderr failed");
  898. }
  899. void GC_err_puts(s)
  900. GC_CONST char *s;
  901. {
  902. if (WRITE(GC_stderr, s, strlen(s)) < 0) ABORT("write to stderr failed");
  903. }
  904. #if defined(LINUX) && !defined(SMALL_CONFIG)
  905. void GC_err_write(buf, len)
  906. GC_CONST char *buf;
  907. size_t len;
  908. {
  909. if (WRITE(GC_stderr, buf, len) < 0) ABORT("write to stderr failed");
  910. }
  911. #endif
  912. # if defined(__STDC__) || defined(__cplusplus)
  913. void GC_default_warn_proc(char *msg, GC_word arg)
  914. # else
  915. void GC_default_warn_proc(msg, arg)
  916. char *msg;
  917. GC_word arg;
  918. # endif
  919. {
  920. GC_err_printf1(msg, (unsigned long)arg);
  921. }
  922. GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;
  923. # if defined(__STDC__) || defined(__cplusplus)
  924. GC_warn_proc GC_set_warn_proc(GC_warn_proc p)
  925. # else
  926. GC_warn_proc GC_set_warn_proc(p)
  927. GC_warn_proc p;
  928. # endif
  929. {
  930. GC_warn_proc result;
  931. # ifdef GC_WIN32_THREADS
  932. GC_ASSERT(GC_is_initialized);
  933. # endif
  934. LOCK();
  935. result = GC_current_warn_proc;
  936. GC_current_warn_proc = p;
  937. UNLOCK();
  938. return(result);
  939. }
  940. # if defined(__STDC__) || defined(__cplusplus)
  941. GC_word GC_set_free_space_divisor (GC_word value)
  942. # else
  943. GC_word GC_set_free_space_divisor (value)
  944. GC_word value;
  945. # endif
  946. {
  947. GC_word old = GC_free_space_divisor;
  948. GC_free_space_divisor = value;
  949. return old;
  950. }
  951. #ifndef PCR
  952. void GC_abort(msg)
  953. GC_CONST char * msg;
  954. {
  955. # if defined(MSWIN32)
  956. (void) MessageBoxA(NULL, msg, "Fatal error in gc", MB_ICONERROR|MB_OK);
  957. # else
  958. GC_err_printf1("%s\n", msg);
  959. # endif
  960. if (GETENV("GC_LOOP_ON_ABORT") != NULL) {
  961. /* In many cases it's easier to debug a running process. */
  962. /* It's arguably nicer to sleep, but that makes it harder */
  963. /* to look at the thread if the debugger doesn't know much */
  964. /* about threads. */
  965. for(;;) {}
  966. }
  967. # if defined(MSWIN32) || defined(MSWINCE)
  968. DebugBreak();
  969. # else
  970. (void) abort();
  971. # endif
  972. }
  973. #endif
  974. void GC_enable()
  975. {
  976. LOCK();
  977. GC_dont_gc--;
  978. UNLOCK();
  979. }
  980. void GC_disable()
  981. {
  982. LOCK();
  983. GC_dont_gc++;
  984. UNLOCK();
  985. }
  986. /* Helper procedures for new kind creation. */
  987. void ** GC_new_free_list_inner()
  988. {
  989. void *result = GC_INTERNAL_MALLOC((MAXOBJSZ+1)*sizeof(ptr_t), PTRFREE);
  990. if (result == 0) ABORT("Failed to allocate freelist for new kind");
  991. BZERO(result, (MAXOBJSZ+1)*sizeof(ptr_t));
  992. return result;
  993. }
  994. void ** GC_new_free_list()
  995. {
  996. void *result;
  997. LOCK(); DISABLE_SIGNALS();
  998. result = GC_new_free_list_inner();
  999. UNLOCK(); ENABLE_SIGNALS();
  1000. return result;
  1001. }
  1002. int GC_new_kind_inner(fl, descr, adjust, clear)
  1003. void **fl;
  1004. GC_word descr;
  1005. int adjust;
  1006. int clear;
  1007. {
  1008. int result = GC_n_kinds++;
  1009. if (GC_n_kinds > MAXOBJKINDS) ABORT("Too many kinds");
  1010. GC_obj_kinds[result].ok_freelist = (ptr_t *)fl;
  1011. GC_obj_kinds[result].ok_reclaim_list = 0;
  1012. GC_obj_kinds[result].ok_descriptor = descr;
  1013. GC_obj_kinds[result].ok_relocate_descr = adjust;
  1014. GC_obj_kinds[result].ok_init = clear;
  1015. return result;
  1016. }
  1017. int GC_new_kind(fl, descr, adjust, clear)
  1018. void **fl;
  1019. GC_word descr;
  1020. int adjust;
  1021. int clear;
  1022. {
  1023. int result;
  1024. LOCK(); DISABLE_SIGNALS();
  1025. result = GC_new_kind_inner(fl, descr, adjust, clear);
  1026. UNLOCK(); ENABLE_SIGNALS();
  1027. return result;
  1028. }
  1029. int GC_new_proc_inner(proc)
  1030. GC_mark_proc proc;
  1031. {
  1032. int result = GC_n_mark_procs++;
  1033. if (GC_n_mark_procs > MAX_MARK_PROCS) ABORT("Too many mark procedures");
  1034. GC_mark_procs[result] = proc;
  1035. return result;
  1036. }
  1037. int GC_new_proc(proc)
  1038. GC_mark_proc proc;
  1039. {
  1040. int result;
  1041. LOCK(); DISABLE_SIGNALS();
  1042. result = GC_new_proc_inner(proc);
  1043. UNLOCK(); ENABLE_SIGNALS();
  1044. return result;
  1045. }
  1046. #if !defined(NO_DEBUGGING)
  1047. void GC_dump()
  1048. {
  1049. GC_printf0("***Static roots:\n");
  1050. GC_print_static_roots();
  1051. GC_printf0("\n***Heap sections:\n");
  1052. GC_print_heap_sects();
  1053. GC_printf0("\n***Free blocks:\n");
  1054. GC_print_hblkfreelist();
  1055. GC_printf0("\n***Blocks in use:\n");
  1056. GC_print_block_list();
  1057. GC_printf0("\n***Finalization statistics:\n");
  1058. GC_print_finalization_stats();
  1059. }
  1060. #endif /* NO_DEBUGGING */