2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
14 /* Boehm, July 31, 1995 5:02 pm PDT */
20 #define I_HIDE_POINTERS /* To make GC_call_with_alloc_lock visible */
23 #ifdef SOLARIS_THREADS
24 # include <sys/syscall.h>
32 # include "il/PCR_IL.h"
33 PCR_Th_ML GC_allocate_ml;
36 /* Critical section counter is defined in the M3 runtime */
37 /* That's all we use. */
39 # ifdef SOLARIS_THREADS
40 mutex_t GC_allocate_ml; /* Implicitly initialized. */
43 GC_API CRITICAL_SECTION GC_allocate_ml;
45 # if defined(IRIX_THREADS) || defined(LINUX_THREADS) \
46 || defined(IRIX_JDK_THREADS)
48 pthread_mutex_t GC_allocate_ml = PTHREAD_MUTEX_INITIALIZER;
50 pthread_t GC_lock_holder = NO_THREAD;
52 --> declare allocator lock here
60 GC_FAR struct _GC_arrays GC_arrays /* = { 0 } */;
63 GC_bool GC_debugging_started = FALSE;
64 /* defined here so we don't have to load debug_malloc.o */
66 void (*GC_check_heap)() = (void (*)())0;
68 void (*GC_start_call_back)() = (void (*)())0;
70 ptr_t GC_stackbottom = 0;
72 GC_bool GC_dont_gc = 0;
83 GC_PTR GC_default_oom_fn GC_PROTO((size_t bytes_requested))
88 GC_PTR (*GC_oom_fn) GC_PROTO((size_t bytes_requested)) = GC_default_oom_fn;
90 extern signed_word GC_mem_found;
93 /* Set things up so that GC_size_map[i] >= words(i), */
94 /* but not too much bigger */
95 /* and so that size_map contains relatively few distinct entries */
96 /* This is stolen from Russ Atkinson's Cedar quantization */
97 /* alogrithm (but we precompute it). */
100 void GC_init_size_map()
104 /* Map size 0 to 1. This avoids problems at lower levels. */
106 /* One word objects don't have to be 2 word aligned. */
107 for (i = 1; i < sizeof(word); i++) {
110 GC_size_map[sizeof(word)] = ROUNDED_UP_WORDS(sizeof(word));
111 for (i = sizeof(word) + 1; i <= 8 * sizeof(word); i++) {
113 GC_size_map[i] = (ROUNDED_UP_WORDS(i) + 1) & (~1);
115 GC_size_map[i] = ROUNDED_UP_WORDS(i);
118 for (i = 8*sizeof(word) + 1; i <= 16 * sizeof(word); i++) {
119 GC_size_map[i] = (ROUNDED_UP_WORDS(i) + 1) & (~1);
121 /* We leave the rest of the array to be filled in on demand. */
124 /* Fill in additional entries in GC_size_map, including the ith one */
125 /* We assume the ith entry is currently 0. */
126 /* Note that a filled in section of the array ending at n always */
127 /* has length at least n/4. */
128 void GC_extend_size_map(i)
131 word orig_word_sz = ROUNDED_UP_WORDS(i);
132 word word_sz = orig_word_sz;
133 register word byte_sz = WORDS_TO_BYTES(word_sz);
134 /* The size we try to preserve. */
135 /* Close to to i, unless this would */
136 /* introduce too many distinct sizes. */
137 word smaller_than_i = byte_sz - (byte_sz >> 3);
138 word much_smaller_than_i = byte_sz - (byte_sz >> 2);
139 register word low_limit; /* The lowest indexed entry we */
143 if (GC_size_map[smaller_than_i] == 0) {
144 low_limit = much_smaller_than_i;
145 while (GC_size_map[low_limit] != 0) low_limit++;
147 low_limit = smaller_than_i + 1;
148 while (GC_size_map[low_limit] != 0) low_limit++;
149 word_sz = ROUNDED_UP_WORDS(low_limit);
150 word_sz += word_sz >> 3;
151 if (word_sz < orig_word_sz) word_sz = orig_word_sz;
157 if (word_sz > MAXOBJSZ) {
160 /* If we can fit the same number of larger objects in a block, */
163 size_t number_of_objs = BODY_SZ/word_sz;
164 word_sz = BODY_SZ/number_of_objs;
169 byte_sz = WORDS_TO_BYTES(word_sz);
170 # ifdef ADD_BYTE_AT_END
171 /* We need one extra byte; don't fill in GC_size_map[byte_sz] */
175 for (j = low_limit; j <= byte_sz; j++) GC_size_map[j] = word_sz;
181 * The following is a gross hack to deal with a problem that can occur
182 * on machines that are sloppy about stack frame sizes, notably SPARC.
183 * Bogus pointers may be written to the stack and not cleared for
184 * a LONG time, because they always fall into holes in stack frames
185 * that are not written. We partially address this by clearing
186 * sections of the stack whenever we get control.
188 word GC_stack_last_cleared = 0; /* GC_no when we last did this */
190 # define CLEAR_SIZE 2048
192 # define CLEAR_SIZE 213
194 # define DEGRADE_RATE 50
196 word GC_min_sp; /* Coolest stack pointer value from which we've */
197 /* already cleared the stack. */
199 # ifdef STACK_GROWS_DOWN
200 # define COOLER_THAN >
201 # define HOTTER_THAN <
202 # define MAKE_COOLER(x,y) if ((word)(x)+(y) > (word)(x)) {(x) += (y);} \
203 else {(x) = (word)ONES;}
204 # define MAKE_HOTTER(x,y) (x) -= (y)
206 # define COOLER_THAN <
207 # define HOTTER_THAN >
208 # define MAKE_COOLER(x,y) if ((word)(x)-(y) < (word)(x)) {(x) -= (y);} else {(x) = 0;}
209 # define MAKE_HOTTER(x,y) (x) += (y)
213 /* "hottest" stack pointer value we have seen */
214 /* recently. Degrades over time. */
216 word GC_words_allocd_at_reset;
218 #if defined(ASM_CLEAR_CODE) && !defined(THREADS)
219 extern ptr_t GC_clear_stack_inner();
222 #if !defined(ASM_CLEAR_CODE) && !defined(THREADS)
223 /* Clear the stack up to about limit. Return arg. */
225 ptr_t GC_clear_stack_inner(arg, limit)
229 word dummy[CLEAR_SIZE];
231 BZERO(dummy, CLEAR_SIZE*sizeof(word));
232 if ((word)(dummy) COOLER_THAN limit) {
233 (void) GC_clear_stack_inner(arg, limit);
235 /* Make sure the recursive call is not a tail call, and the bzero */
236 /* call is not recognized as dead code. */
237 GC_noop1((word)dummy);
242 /* Clear some of the inaccessible part of the stack. Returns its */
243 /* argument, so it can be used in a tail call position, hence clearing */
245 ptr_t GC_clear_stack(arg)
248 register word sp = (word)GC_approx_sp(); /* Hotter than actual sp */
250 word dummy[CLEAR_SIZE];
256 /* Extra bytes we clear every time. This clears our own */
257 /* activation record, and should cause more frequent */
258 /* clearing near the cold end of the stack, a good thing. */
259 # define GC_SLOP 4000
260 /* We make GC_high_water this much hotter than we really saw */
261 /* saw it, to cover for GC noise etc. above our current frame. */
262 # define CLEAR_THRESHOLD 100000
263 /* We restart the clearing process after this many bytes of */
264 /* allocation. Otherwise very heavily recursive programs */
265 /* with sparse stacks may result in heaps that grow almost */
266 /* without bounds. As the heap gets larger, collection */
267 /* frequency decreases, thus clearing frequency would decrease, */
268 /* thus more junk remains accessible, thus the heap gets */
271 BZERO(dummy, CLEAR_SIZE*sizeof(word));
273 if (GC_gc_no > GC_stack_last_cleared) {
274 /* Start things over, so we clear the entire stack again */
275 if (GC_stack_last_cleared == 0) GC_high_water = (word) GC_stackbottom;
276 GC_min_sp = GC_high_water;
277 GC_stack_last_cleared = GC_gc_no;
278 GC_words_allocd_at_reset = GC_words_allocd;
280 /* Adjust GC_high_water */
281 MAKE_COOLER(GC_high_water, WORDS_TO_BYTES(DEGRADE_RATE) + GC_SLOP);
282 if (sp HOTTER_THAN GC_high_water) {
285 MAKE_HOTTER(GC_high_water, GC_SLOP);
287 MAKE_HOTTER(limit, SLOP);
288 if (sp COOLER_THAN limit) {
289 limit &= ~0xf; /* Make it sufficiently aligned for assembly */
290 /* implementations of GC_clear_stack_inner. */
292 return(GC_clear_stack_inner(arg, limit));
293 } else if (WORDS_TO_BYTES(GC_words_allocd - GC_words_allocd_at_reset)
295 /* Restart clearing process, but limit how much clearing we do. */
297 MAKE_HOTTER(GC_min_sp, CLEAR_THRESHOLD/4);
298 if (GC_min_sp HOTTER_THAN GC_high_water) GC_min_sp = GC_high_water;
299 GC_words_allocd_at_reset = GC_words_allocd;
306 /* Return a pointer to the base address of p, given a pointer to a */
307 /* an address within an object. Return 0 o.w. */
309 GC_PTR GC_base(GC_PTR p)
316 register struct hblk *h;
317 register bottom_index *bi;
318 register hdr *candidate_hdr;
322 if (!GC_is_initialized) return 0;
325 candidate_hdr = HDR_FROM_BI(bi, r);
326 if (candidate_hdr == 0) return(0);
327 /* If it's a pointer to the middle of a large object, move it */
328 /* to the beginning. */
329 while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr)) {
330 h = FORWARDED_ADDR(h,candidate_hdr);
331 r = (word)h + HDR_BYTES;
332 candidate_hdr = HDR(h);
334 if (candidate_hdr -> hb_map == GC_invalid_map) return(0);
335 /* Make sure r points to the beginning of the object */
336 r &= ~(WORDS_TO_BYTES(1) - 1);
338 register int offset = (char *)r - (char *)(HBLKPTR(r));
339 register signed_word sz = candidate_hdr -> hb_sz;
341 # ifdef ALL_INTERIOR_POINTERS
342 register map_entry_type map_entry;
344 map_entry = MAP_ENTRY((candidate_hdr -> hb_map), offset);
345 if (map_entry == OBJ_INVALID) {
348 r -= WORDS_TO_BYTES(map_entry);
349 limit = r + WORDS_TO_BYTES(sz);
351 register int correction;
353 offset = BYTES_TO_WORDS(offset - HDR_BYTES);
354 correction = offset % sz;
355 r -= (WORDS_TO_BYTES(correction));
356 limit = r + WORDS_TO_BYTES(sz);
357 if (limit > (word)(h + 1)
358 && sz <= BYTES_TO_WORDS(HBLKSIZE) - HDR_WORDS) {
362 if ((word)p >= limit) return(0);
368 /* Return the size of an object, given a pointer to its base. */
369 /* (For small obects this also happens to work from interior pointers, */
370 /* but that shouldn't be relied upon.) */
372 size_t GC_size(GC_PTR p)
379 register hdr * hhdr = HDR(p);
381 sz = WORDS_TO_BYTES(hhdr -> hb_sz);
389 size_t GC_get_heap_size GC_PROTO(())
391 return ((size_t) GC_heapsize);
394 size_t GC_get_bytes_since_gc GC_PROTO(())
396 return ((size_t) WORDS_TO_BYTES(GC_words_allocd));
399 GC_bool GC_is_initialized = FALSE;
414 extern void GC_init_win32();
417 extern void GC_setpagesize();
425 if (GC_is_initialized) return;
427 GC_exclude_static_roots(beginGC_arrays, end_gc_area);
429 if ((ptr_t)endGC_arrays != (ptr_t)(&GC_obj_kinds)) {
430 GC_printf0("Reordering linker, didn't exclude obj_kinds\n");
436 # if defined(LINUX) && (defined(POWERPC) || defined(ALPHA) || defined(SPARC))
437 GC_init_linux_data_start();
439 # ifdef SOLARIS_THREADS
441 /* We need dirty bits in order to find live stack sections. */
444 # if defined(IRIX_THREADS) || defined(LINUX_THREADS) \
445 || defined(IRIX_JDK_THREADS)
448 # if !defined(THREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) \
449 || defined(IRIX_THREADS) || defined(LINUX_THREADS)
450 if (GC_stackbottom == 0) {
451 GC_stackbottom = GC_get_stack_base();
454 if (sizeof (ptr_t) != sizeof(word)) {
455 ABORT("sizeof (ptr_t) != sizeof(word)\n");
457 if (sizeof (signed_word) != sizeof(word)) {
458 ABORT("sizeof (signed_word) != sizeof(word)\n");
460 if (sizeof (struct hblk) != HBLKSIZE) {
461 ABORT("sizeof (struct hblk) != HBLKSIZE\n");
464 # if defined(STACK_GROWS_UP) && defined(STACK_GROWS_DOWN)
466 "Only one of STACK_GROWS_UP and STACK_GROWS_DOWN should be defd\n");
468 # if !defined(STACK_GROWS_UP) && !defined(STACK_GROWS_DOWN)
470 "One of STACK_GROWS_UP and STACK_GROWS_DOWN should be defd\n");
472 # ifdef STACK_GROWS_DOWN
473 if ((word)(&dummy) > (word)GC_stackbottom) {
475 "STACK_GROWS_DOWN is defd, but stack appears to grow up\n");
476 # ifndef UTS4 /* Compiler bug workaround */
477 GC_err_printf2("sp = 0x%lx, GC_stackbottom = 0x%lx\n",
478 (unsigned long) (&dummy),
479 (unsigned long) GC_stackbottom);
481 ABORT("stack direction 3\n");
484 if ((word)(&dummy) < (word)GC_stackbottom) {
486 "STACK_GROWS_UP is defd, but stack appears to grow down\n");
487 GC_err_printf2("sp = 0x%lx, GC_stackbottom = 0x%lx\n",
488 (unsigned long) (&dummy),
489 (unsigned long) GC_stackbottom);
490 ABORT("stack direction 4");
494 # if !defined(_AUX_SOURCE) || defined(__GNUC__)
495 if ((word)(-1) < (word)0) {
496 GC_err_printf0("The type word should be an unsigned integer type\n");
497 GC_err_printf0("It appears to be signed\n");
501 if ((signed_word)(-1) >= (signed_word)0) {
503 "The type signed_word should be a signed integer type\n");
504 GC_err_printf0("It appears to be unsigned\n");
505 ABORT("signed_word");
508 /* Add initial guess of root sets. Do this first, since sbrk(0) */
510 GC_register_data_segments();
514 if (!GC_expand_hp_inner((word)MINHINCR)) {
515 GC_err_printf0("Can't start up: not enough memory\n");
518 /* Preallocate large object map. It's otherwise inconvenient to */
519 /* deal with failure. */
520 if (!GC_add_map_entry((word)0)) {
521 GC_err_printf0("Can't start up: not enough memory\n");
524 GC_register_displacement_inner(0L);
529 if (PCR_IL_Lock(PCR_Bool_false, PCR_allSigsBlocked, PCR_waitForever)
531 ABORT("Can't lock load state\n");
532 } else if (PCR_IL_Unlock() != PCR_ERes_okay) {
533 ABORT("Can't unlock load state\n");
538 /* Get black list set up */
540 # ifdef STUBBORN_ALLOC
543 GC_is_initialized = TRUE;
544 /* Convince lint that some things are used */
547 extern char * GC_copyright[];
548 extern int GC_read();
549 extern void GC_register_finalizer_no_order();
551 GC_noop(GC_copyright, GC_find_header,
552 GC_push_one, GC_call_with_alloc_lock, GC_read,
554 # ifndef NO_DEBUGGING
557 GC_register_finalizer_no_order);
562 void GC_enable_incremental GC_PROTO(())
564 # if !defined(SMALL_CONFIG)
570 if (GC_incremental) goto out;
574 extern GC_bool GC_is_win32s();
576 /* VirtualProtect is not functional under win32s. */
577 if (GC_is_win32s()) goto out;
579 # endif /* MSWIN32 */
580 # ifndef SOLARIS_THREADS
583 if (!GC_is_initialized) {
587 /* Can't easily do it. */
592 if (GC_words_allocd > 0) {
593 /* There may be unmarked reachable objects */
595 } /* else we're OK in assuming everything's */
596 /* clean since nothing can point to an */
597 /* unmarked object. */
599 GC_incremental = TRUE;
609 # define LOG_FILE "gc.log"
611 HANDLE GC_stdout = 0, GC_stderr;
618 GC_stdout = CreateFile(LOG_FILE, GENERIC_WRITE,
619 FILE_SHARE_READ | FILE_SHARE_WRITE,
620 NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH,
622 if (INVALID_HANDLE_VALUE == GC_stdout) ABORT("Open of log file failed");
624 if (GC_stderr == 0) {
625 GC_stderr = GC_stdout;
631 #if defined(OS2) || defined(MACOS)
632 FILE * GC_stdout = NULL;
633 FILE * GC_stderr = NULL;
634 int GC_tmp; /* Should really be local ... */
638 if (GC_stdout == NULL) {
641 if (GC_stderr == NULL) {
647 #if !defined(OS2) && !defined(MACOS) && !defined(MSWIN32)
655 #if !defined(MSWIN32) && !defined(OS2) && !defined(MACOS)
656 int GC_write(fd, buf, len)
661 register int bytes_written = 0;
664 while (bytes_written < len) {
665 # ifdef SOLARIS_THREADS
666 result = syscall(SYS_write, fd, buf + bytes_written,
667 len - bytes_written);
669 result = write(fd, buf + bytes_written, len - bytes_written);
671 if (-1 == result) return(result);
672 bytes_written += result;
674 return(bytes_written);
679 # define WRITE(f, buf, len) (GC_set_files(), \
680 GC_tmp = WriteFile((f), (buf), \
681 (len), &GC_junk, NULL),\
684 # if defined(OS2) || defined(MACOS)
685 # define WRITE(f, buf, len) (GC_set_files(), \
686 GC_tmp = fwrite((buf), 1, (len), (f)), \
689 # define WRITE(f, buf, len) GC_write((f), (buf), (len))
693 /* A version of printf that is unlikely to call malloc, and is thus safer */
694 /* to call from the collector in case malloc has been bound to GC_malloc. */
695 /* Assumes that no more than 1023 characters are written at once. */
696 /* Assumes that all arguments have been converted to something of the */
697 /* same size as long, and that the format conversions expect something */
699 void GC_printf(format, a, b, c, d, e, f)
701 long a, b, c, d, e, f;
705 if (GC_quiet) return;
707 (void) sprintf(buf, format, a, b, c, d, e, f);
708 if (buf[1024] != 0x15) ABORT("GC_printf clobbered stack");
709 if (WRITE(GC_stdout, buf, strlen(buf)) < 0) ABORT("write to stdout failed");
712 void GC_err_printf(format, a, b, c, d, e, f)
714 long a, b, c, d, e, f;
719 (void) sprintf(buf, format, a, b, c, d, e, f);
720 if (buf[1024] != 0x15) ABORT("GC_err_printf clobbered stack");
721 if (WRITE(GC_stderr, buf, strlen(buf)) < 0) ABORT("write to stderr failed");
727 if (WRITE(GC_stderr, s, strlen(s)) < 0) ABORT("write to stderr failed");
730 # if defined(__STDC__) || defined(__cplusplus)
731 void GC_default_warn_proc(char *msg, GC_word arg)
733 void GC_default_warn_proc(msg, arg)
738 GC_err_printf1(msg, (unsigned long)arg);
741 GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;
743 # if defined(__STDC__) || defined(__cplusplus)
744 GC_warn_proc GC_set_warn_proc(GC_warn_proc p)
746 GC_warn_proc GC_set_warn_proc(p)
753 result = GC_current_warn_proc;
754 GC_current_warn_proc = p;
764 GC_err_printf1("%s\n", msg);
771 void GC_print_callers (info)
772 struct callinfo info[NFRAMES];
777 GC_err_printf0("\tCaller at allocation:\n");
779 GC_err_printf0("\tCall chain at allocation:\n");
781 for (i = 0; i < NFRAMES; i++) {
782 if (info[i].ci_pc == 0) break;
787 GC_err_printf0("\t\targs: ");
788 for (j = 0; j < NARGS; j++) {
789 if (j != 0) GC_err_printf0(", ");
790 GC_err_printf2("%d (0x%X)", ~(info[i].ci_arg[j]),
791 ~(info[i].ci_arg[j]));
793 GC_err_printf0("\n");
796 GC_err_printf1("\t\t##PC##= 0x%X\n", info[i].ci_pc);
800 #endif /* SAVE_CALL_CHAIN */
814 #if !defined(NO_DEBUGGING)
818 GC_printf0("***Static roots:\n");
819 GC_print_static_roots();
820 GC_printf0("\n***Heap sections:\n");
821 GC_print_heap_sects();
822 GC_printf0("\n***Free blocks:\n");
823 GC_print_hblkfreelist();
824 GC_printf0("\n***Blocks in use:\n");
825 GC_print_block_list();
828 # endif /* NO_DEBUGGING */