source: trunk/gc6.8/misc.c@ 132

Last change on this file since 132 was 132, checked in by cinc, 19 years ago

Boehm-Demers-Weiser garbage collector. Single-threaded for OS/2.

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