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