* gcc.c (default_compilers): Add missing initializers.
[official-gcc.git] / boehm-gc / alloc.c
blobd2b874fc7704b352556cd934af1933645820bf9a
1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1996 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1998 by Silicon Graphics. All rights reserved.
5 * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
19 # include "private/gc_priv.h"
21 # include <stdio.h>
22 # if !defined(MACOS) && !defined(MSWINCE)
23 # include <signal.h>
24 # include <sys/types.h>
25 # endif
28 * Separate free lists are maintained for different sized objects
29 * up to MAXOBJSZ.
30 * The call GC_allocobj(i,k) ensures that the freelist for
31 * kind k objects of size i points to a non-empty
32 * free list. It returns a pointer to the first entry on the free list.
33 * In a single-threaded world, GC_allocobj may be called to allocate
34 * an object of (small) size i as follows:
36 * opp = &(GC_objfreelist[i]);
37 * if (*opp == 0) GC_allocobj(i, NORMAL);
38 * ptr = *opp;
39 * *opp = obj_link(ptr);
41 * Note that this is very fast if the free list is non-empty; it should
42 * only involve the execution of 4 or 5 simple instructions.
43 * All composite objects on freelists are cleared, except for
44 * their first word.
48 * The allocator uses GC_allochblk to allocate large chunks of objects.
49 * These chunks all start on addresses which are multiples of
50 * HBLKSZ. Each allocated chunk has an associated header,
51 * which can be located quickly based on the address of the chunk.
52 * (See headers.c for details.)
53 * This makes it possible to check quickly whether an
54 * arbitrary address corresponds to an object administered by the
55 * allocator.
58 word GC_non_gc_bytes = 0; /* Number of bytes not intended to be collected */
60 word GC_gc_no = 0;
62 #ifndef SMALL_CONFIG
63 int GC_incremental = 0; /* By default, stop the world. */
64 #endif
66 int GC_parallel = FALSE; /* By default, parallel GC is off. */
68 int GC_full_freq = 19; /* Every 20th collection is a full */
69 /* collection, whether we need it */
70 /* or not. */
72 GC_bool GC_need_full_gc = FALSE;
73 /* Need full GC do to heap growth. */
75 #ifdef THREADS
76 GC_bool GC_world_stopped = FALSE;
77 # define IF_THREADS(x) x
78 #else
79 # define IF_THREADS(x)
80 #endif
82 word GC_used_heap_size_after_full = 0;
84 char * GC_copyright[] =
85 {"Copyright 1988,1989 Hans-J. Boehm and Alan J. Demers ",
86 "Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved. ",
87 "Copyright (c) 1996-1998 by Silicon Graphics. All rights reserved. ",
88 "Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved. ",
89 "THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY",
90 " EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.",
91 "See source code for details." };
93 # include "version.h"
95 /* some more variables */
97 extern signed_word GC_mem_found; /* Number of reclaimed longwords */
98 /* after garbage collection */
100 GC_bool GC_dont_expand = 0;
102 word GC_free_space_divisor = 3;
104 extern GC_bool GC_collection_in_progress();
105 /* Collection is in progress, or was abandoned. */
107 extern GC_bool GC_print_back_height;
109 int GC_never_stop_func GC_PROTO((void)) { return(0); }
111 unsigned long GC_time_limit = TIME_LIMIT;
113 CLOCK_TYPE GC_start_time; /* Time at which we stopped world. */
114 /* used only in GC_timeout_stop_func. */
116 int GC_n_attempts = 0; /* Number of attempts at finishing */
117 /* collection within GC_time_limit. */
119 #if defined(SMALL_CONFIG) || defined(NO_CLOCK)
120 # define GC_timeout_stop_func GC_never_stop_func
121 #else
122 int GC_timeout_stop_func GC_PROTO((void))
124 CLOCK_TYPE current_time;
125 static unsigned count = 0;
126 unsigned long time_diff;
128 if ((count++ & 3) != 0) return(0);
129 GET_TIME(current_time);
130 time_diff = MS_TIME_DIFF(current_time,GC_start_time);
131 if (time_diff >= GC_time_limit) {
132 # ifdef CONDPRINT
133 if (GC_print_stats) {
134 GC_printf0("Abandoning stopped marking after ");
135 GC_printf1("%lu msecs", (unsigned long)time_diff);
136 GC_printf1("(attempt %d)\n", (unsigned long) GC_n_attempts);
138 # endif
139 return(1);
141 return(0);
143 #endif /* !SMALL_CONFIG */
145 /* Return the minimum number of words that must be allocated between */
146 /* collections to amortize the collection cost. */
147 static word min_words_allocd()
149 # ifdef THREADS
150 /* We punt, for now. */
151 register signed_word stack_size = 10000;
152 # else
153 int dummy;
154 register signed_word stack_size = (ptr_t)(&dummy) - GC_stackbottom;
155 # endif
156 word total_root_size; /* includes double stack size, */
157 /* since the stack is expensive */
158 /* to scan. */
159 word scan_size; /* Estimate of memory to be scanned */
160 /* during normal GC. */
162 if (stack_size < 0) stack_size = -stack_size;
163 total_root_size = 2 * stack_size + GC_root_size;
164 scan_size = BYTES_TO_WORDS(GC_heapsize - GC_large_free_bytes
165 + (GC_large_free_bytes >> 2)
166 /* use a bit more of large empty heap */
167 + total_root_size);
168 if (TRUE_INCREMENTAL) {
169 return scan_size / (2 * GC_free_space_divisor);
170 } else {
171 return scan_size / GC_free_space_divisor;
175 /* Return the number of words allocated, adjusted for explicit storage */
176 /* management, etc.. This number is used in deciding when to trigger */
177 /* collections. */
178 word GC_adj_words_allocd()
180 register signed_word result;
181 register signed_word expl_managed =
182 BYTES_TO_WORDS((long)GC_non_gc_bytes
183 - (long)GC_non_gc_bytes_at_gc);
185 /* Don't count what was explicitly freed, or newly allocated for */
186 /* explicit management. Note that deallocating an explicitly */
187 /* managed object should not alter result, assuming the client */
188 /* is playing by the rules. */
189 result = (signed_word)GC_words_allocd
190 - (signed_word)GC_mem_freed
191 + (signed_word)GC_finalizer_mem_freed - expl_managed;
192 if (result > (signed_word)GC_words_allocd) {
193 result = GC_words_allocd;
194 /* probably client bug or unfortunate scheduling */
196 result += GC_words_finalized;
197 /* We count objects enqueued for finalization as though they */
198 /* had been reallocated this round. Finalization is user */
199 /* visible progress. And if we don't count this, we have */
200 /* stability problems for programs that finalize all objects. */
201 result += GC_words_wasted;
202 /* This doesn't reflect useful work. But if there is lots of */
203 /* new fragmentation, the same is probably true of the heap, */
204 /* and the collection will be correspondingly cheaper. */
205 if (result < (signed_word)(GC_words_allocd >> 3)) {
206 /* Always count at least 1/8 of the allocations. We don't want */
207 /* to collect too infrequently, since that would inhibit */
208 /* coalescing of free storage blocks. */
209 /* This also makes us partially robust against client bugs. */
210 return(GC_words_allocd >> 3);
211 } else {
212 return(result);
217 /* Clear up a few frames worth of garbage left at the top of the stack. */
218 /* This is used to prevent us from accidentally treating garbade left */
219 /* on the stack by other parts of the collector as roots. This */
220 /* differs from the code in misc.c, which actually tries to keep the */
221 /* stack clear of long-lived, client-generated garbage. */
222 void GC_clear_a_few_frames()
224 # define NWORDS 64
225 word frames[NWORDS];
226 register int i;
228 for (i = 0; i < NWORDS; i++) frames[i] = 0;
231 /* Have we allocated enough to amortize a collection? */
232 GC_bool GC_should_collect()
234 return(GC_adj_words_allocd() >= min_words_allocd());
238 void GC_notify_full_gc()
240 if (GC_start_call_back != (void (*) GC_PROTO((void)))0) {
241 (*GC_start_call_back)();
245 GC_bool GC_is_full_gc = FALSE;
248 * Initiate a garbage collection if appropriate.
249 * Choose judiciously
250 * between partial, full, and stop-world collections.
251 * Assumes lock held, signals disabled.
253 void GC_maybe_gc()
255 static int n_partial_gcs = 0;
257 if (GC_should_collect()) {
258 if (!GC_incremental) {
259 GC_gcollect_inner();
260 n_partial_gcs = 0;
261 return;
262 } else {
263 # ifdef PARALLEL_MARK
264 GC_wait_for_reclaim();
265 # endif
266 if (GC_need_full_gc || n_partial_gcs >= GC_full_freq) {
267 # ifdef CONDPRINT
268 if (GC_print_stats) {
269 GC_printf2(
270 "***>Full mark for collection %lu after %ld allocd bytes\n",
271 (unsigned long) GC_gc_no+1,
272 (long)WORDS_TO_BYTES(GC_words_allocd));
274 # endif
275 GC_promote_black_lists();
276 (void)GC_reclaim_all((GC_stop_func)0, TRUE);
277 GC_clear_marks();
278 n_partial_gcs = 0;
279 GC_notify_full_gc();
280 GC_is_full_gc = TRUE;
281 } else {
282 n_partial_gcs++;
285 /* We try to mark with the world stopped. */
286 /* If we run out of time, this turns into */
287 /* incremental marking. */
288 # ifndef NO_CLOCK
289 if (GC_time_limit != GC_TIME_UNLIMITED) { GET_TIME(GC_start_time); }
290 # endif
291 if (GC_stopped_mark(GC_time_limit == GC_TIME_UNLIMITED?
292 GC_never_stop_func : GC_timeout_stop_func)) {
293 # ifdef SAVE_CALL_CHAIN
294 GC_save_callers(GC_last_stack);
295 # endif
296 GC_finish_collection();
297 } else {
298 if (!GC_is_full_gc) {
299 /* Count this as the first attempt */
300 GC_n_attempts++;
308 * Stop the world garbage collection. Assumes lock held, signals disabled.
309 * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
310 * Return TRUE if we successfully completed the collection.
312 GC_bool GC_try_to_collect_inner(stop_func)
313 GC_stop_func stop_func;
315 # ifdef CONDPRINT
316 CLOCK_TYPE start_time, current_time;
317 # endif
318 if (GC_dont_gc) return FALSE;
319 if (GC_incremental && GC_collection_in_progress()) {
320 # ifdef CONDPRINT
321 if (GC_print_stats) {
322 GC_printf0(
323 "GC_try_to_collect_inner: finishing collection in progress\n");
325 # endif /* CONDPRINT */
326 /* Just finish collection already in progress. */
327 while(GC_collection_in_progress()) {
328 if (stop_func()) return(FALSE);
329 GC_collect_a_little_inner(1);
332 if (stop_func == GC_never_stop_func) GC_notify_full_gc();
333 # ifdef CONDPRINT
334 if (GC_print_stats) {
335 if (GC_print_stats) GET_TIME(start_time);
336 GC_printf2(
337 "Initiating full world-stop collection %lu after %ld allocd bytes\n",
338 (unsigned long) GC_gc_no+1,
339 (long)WORDS_TO_BYTES(GC_words_allocd));
341 # endif
342 GC_promote_black_lists();
343 /* Make sure all blocks have been reclaimed, so sweep routines */
344 /* don't see cleared mark bits. */
345 /* If we're guaranteed to finish, then this is unnecessary. */
346 /* In the find_leak case, we have to finish to guarantee that */
347 /* previously unmarked objects are not reported as leaks. */
348 # ifdef PARALLEL_MARK
349 GC_wait_for_reclaim();
350 # endif
351 if ((GC_find_leak || stop_func != GC_never_stop_func)
352 && !GC_reclaim_all(stop_func, FALSE)) {
353 /* Aborted. So far everything is still consistent. */
354 return(FALSE);
356 GC_invalidate_mark_state(); /* Flush mark stack. */
357 GC_clear_marks();
358 # ifdef SAVE_CALL_CHAIN
359 GC_save_callers(GC_last_stack);
360 # endif
361 GC_is_full_gc = TRUE;
362 if (!GC_stopped_mark(stop_func)) {
363 if (!GC_incremental) {
364 /* We're partially done and have no way to complete or use */
365 /* current work. Reestablish invariants as cheaply as */
366 /* possible. */
367 GC_invalidate_mark_state();
368 GC_unpromote_black_lists();
369 } /* else we claim the world is already still consistent. We'll */
370 /* finish incrementally. */
371 return(FALSE);
373 GC_finish_collection();
374 # if defined(CONDPRINT)
375 if (GC_print_stats) {
376 GET_TIME(current_time);
377 GC_printf1("Complete collection took %lu msecs\n",
378 MS_TIME_DIFF(current_time,start_time));
380 # endif
381 return(TRUE);
387 * Perform n units of garbage collection work. A unit is intended to touch
388 * roughly GC_RATE pages. Every once in a while, we do more than that.
389 * This needa to be a fairly large number with our current incremental
390 * GC strategy, since otherwise we allocate too much during GC, and the
391 * cleanup gets expensive.
393 # define GC_RATE 10
394 # define MAX_PRIOR_ATTEMPTS 1
395 /* Maximum number of prior attempts at world stop marking */
396 /* A value of 1 means that we finish the second time, no matter */
397 /* how long it takes. Doesn't count the initial root scan */
398 /* for a full GC. */
400 int GC_deficit = 0; /* The number of extra calls to GC_mark_some */
401 /* that we have made. */
403 void GC_collect_a_little_inner(n)
404 int n;
406 register int i;
408 if (GC_dont_gc) return;
409 if (GC_incremental && GC_collection_in_progress()) {
410 for (i = GC_deficit; i < GC_RATE*n; i++) {
411 if (GC_mark_some((ptr_t)0)) {
412 /* Need to finish a collection */
413 # ifdef SAVE_CALL_CHAIN
414 GC_save_callers(GC_last_stack);
415 # endif
416 # ifdef PARALLEL_MARK
417 GC_wait_for_reclaim();
418 # endif
419 if (GC_n_attempts < MAX_PRIOR_ATTEMPTS
420 && GC_time_limit != GC_TIME_UNLIMITED) {
421 GET_TIME(GC_start_time);
422 if (!GC_stopped_mark(GC_timeout_stop_func)) {
423 GC_n_attempts++;
424 break;
426 } else {
427 (void)GC_stopped_mark(GC_never_stop_func);
429 GC_finish_collection();
430 break;
433 if (GC_deficit > 0) GC_deficit -= GC_RATE*n;
434 if (GC_deficit < 0) GC_deficit = 0;
435 } else {
436 GC_maybe_gc();
440 int GC_collect_a_little GC_PROTO(())
442 int result;
443 DCL_LOCK_STATE;
445 DISABLE_SIGNALS();
446 LOCK();
447 GC_collect_a_little_inner(1);
448 result = (int)GC_collection_in_progress();
449 UNLOCK();
450 ENABLE_SIGNALS();
451 if (!result && GC_debugging_started) GC_print_all_smashed();
452 return(result);
456 * Assumes lock is held, signals are disabled.
457 * We stop the world.
458 * If stop_func() ever returns TRUE, we may fail and return FALSE.
459 * Increment GC_gc_no if we succeed.
461 GC_bool GC_stopped_mark(stop_func)
462 GC_stop_func stop_func;
464 register int i;
465 int dummy;
466 # if defined(PRINTTIMES) || defined(CONDPRINT)
467 CLOCK_TYPE start_time, current_time;
468 # endif
470 # ifdef PRINTTIMES
471 GET_TIME(start_time);
472 # endif
473 # if defined(CONDPRINT) && !defined(PRINTTIMES)
474 if (GC_print_stats) GET_TIME(start_time);
475 # endif
476 # if defined(REGISTER_LIBRARIES_EARLY)
477 GC_cond_register_dynamic_libraries();
478 # endif
479 STOP_WORLD();
480 IF_THREADS(GC_world_stopped = TRUE);
481 # ifdef CONDPRINT
482 if (GC_print_stats) {
483 GC_printf1("--> Marking for collection %lu ",
484 (unsigned long) GC_gc_no + 1);
485 GC_printf2("after %lu allocd bytes + %lu wasted bytes\n",
486 (unsigned long) WORDS_TO_BYTES(GC_words_allocd),
487 (unsigned long) WORDS_TO_BYTES(GC_words_wasted));
489 # endif
490 # ifdef MAKE_BACK_GRAPH
491 if (GC_print_back_height) {
492 GC_build_back_graph();
494 # endif
496 /* Mark from all roots. */
497 /* Minimize junk left in my registers and on the stack */
498 GC_clear_a_few_frames();
499 GC_noop(0,0,0,0,0,0);
500 GC_initiate_gc();
501 for(i = 0;;i++) {
502 if ((*stop_func)()) {
503 # ifdef CONDPRINT
504 if (GC_print_stats) {
505 GC_printf0("Abandoned stopped marking after ");
506 GC_printf1("%lu iterations\n",
507 (unsigned long)i);
509 # endif
510 GC_deficit = i; /* Give the mutator a chance. */
511 IF_THREADS(GC_world_stopped = FALSE);
512 START_WORLD();
513 return(FALSE);
515 if (GC_mark_some((ptr_t)(&dummy))) break;
518 GC_gc_no++;
519 # ifdef PRINTSTATS
520 GC_printf2("Collection %lu reclaimed %ld bytes",
521 (unsigned long) GC_gc_no - 1,
522 (long)WORDS_TO_BYTES(GC_mem_found));
523 # else
524 # ifdef CONDPRINT
525 if (GC_print_stats) {
526 GC_printf1("Collection %lu finished", (unsigned long) GC_gc_no - 1);
528 # endif
529 # endif /* !PRINTSTATS */
530 # ifdef CONDPRINT
531 if (GC_print_stats) {
532 GC_printf1(" ---> heapsize = %lu bytes\n",
533 (unsigned long) GC_heapsize);
534 /* Printf arguments may be pushed in funny places. Clear the */
535 /* space. */
536 GC_printf0("");
538 # endif /* CONDPRINT */
540 /* Check all debugged objects for consistency */
541 if (GC_debugging_started) {
542 (*GC_check_heap)();
545 IF_THREADS(GC_world_stopped = FALSE);
546 START_WORLD();
547 # ifdef PRINTTIMES
548 GET_TIME(current_time);
549 GC_printf1("World-stopped marking took %lu msecs\n",
550 MS_TIME_DIFF(current_time,start_time));
551 # else
552 # ifdef CONDPRINT
553 if (GC_print_stats) {
554 GET_TIME(current_time);
555 GC_printf1("World-stopped marking took %lu msecs\n",
556 MS_TIME_DIFF(current_time,start_time));
558 # endif
559 # endif
560 return(TRUE);
563 /* Set all mark bits for the free list whose first entry is q */
564 #ifdef __STDC__
565 void GC_set_fl_marks(ptr_t q)
566 #else
567 void GC_set_fl_marks(q)
568 ptr_t q;
569 #endif
571 ptr_t p;
572 struct hblk * h, * last_h = 0;
573 hdr *hhdr;
574 int word_no;
576 for (p = q; p != 0; p = obj_link(p)){
577 h = HBLKPTR(p);
578 if (h != last_h) {
579 last_h = h;
580 hhdr = HDR(h);
582 word_no = (((word *)p) - ((word *)h));
583 set_mark_bit_from_hdr(hhdr, word_no);
587 /* Clear all mark bits for the free list whose first entry is q */
588 /* Decrement GC_mem_found by number of words on free list. */
589 #ifdef __STDC__
590 void GC_clear_fl_marks(ptr_t q)
591 #else
592 void GC_clear_fl_marks(q)
593 ptr_t q;
594 #endif
596 ptr_t p;
597 struct hblk * h, * last_h = 0;
598 hdr *hhdr;
599 int word_no;
601 for (p = q; p != 0; p = obj_link(p)){
602 h = HBLKPTR(p);
603 if (h != last_h) {
604 last_h = h;
605 hhdr = HDR(h);
607 word_no = (((word *)p) - ((word *)h));
608 clear_mark_bit_from_hdr(hhdr, word_no);
609 # ifdef GATHERSTATS
610 GC_mem_found -= hhdr -> hb_sz;
611 # endif
615 /* Finish up a collection. Assumes lock is held, signals are disabled, */
616 /* but the world is otherwise running. */
617 void GC_finish_collection()
619 # ifdef PRINTTIMES
620 CLOCK_TYPE start_time;
621 CLOCK_TYPE finalize_time;
622 CLOCK_TYPE done_time;
624 GET_TIME(start_time);
625 finalize_time = start_time;
626 # endif
628 # ifdef GATHERSTATS
629 GC_mem_found = 0;
630 # endif
631 # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
632 if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
633 GC_print_address_map();
635 # endif
636 COND_DUMP;
637 if (GC_find_leak) {
638 /* Mark all objects on the free list. All objects should be */
639 /* marked when we're done. */
641 register word size; /* current object size */
642 int kind;
643 ptr_t q;
645 for (kind = 0; kind < GC_n_kinds; kind++) {
646 for (size = 1; size <= MAXOBJSZ; size++) {
647 q = GC_obj_kinds[kind].ok_freelist[size];
648 if (q != 0) GC_set_fl_marks(q);
652 GC_start_reclaim(TRUE);
653 /* The above just checks; it doesn't really reclaim anything. */
656 GC_finalize();
657 # ifdef STUBBORN_ALLOC
658 GC_clean_changing_list();
659 # endif
661 # ifdef PRINTTIMES
662 GET_TIME(finalize_time);
663 # endif
665 if (GC_print_back_height) {
666 # ifdef MAKE_BACK_GRAPH
667 GC_traverse_back_graph();
668 # else
669 # ifndef SMALL_CONFIG
670 GC_err_printf0("Back height not available: "
671 "Rebuild collector with -DMAKE_BACK_GRAPH\n");
672 # endif
673 # endif
676 /* Clear free list mark bits, in case they got accidentally marked */
677 /* (or GC_find_leak is set and they were intentionally marked). */
678 /* Also subtract memory remaining from GC_mem_found count. */
679 /* Note that composite objects on free list are cleared. */
680 /* Thus accidentally marking a free list is not a problem; only */
681 /* objects on the list itself will be marked, and that's fixed here. */
683 register word size; /* current object size */
684 register ptr_t q; /* pointer to current object */
685 int kind;
687 for (kind = 0; kind < GC_n_kinds; kind++) {
688 for (size = 1; size <= MAXOBJSZ; size++) {
689 q = GC_obj_kinds[kind].ok_freelist[size];
690 if (q != 0) GC_clear_fl_marks(q);
696 # ifdef PRINTSTATS
697 GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
698 (long)WORDS_TO_BYTES(GC_mem_found));
699 # endif
700 /* Reconstruct free lists to contain everything not marked */
701 GC_start_reclaim(FALSE);
702 if (GC_is_full_gc) {
703 GC_used_heap_size_after_full = USED_HEAP_SIZE;
704 GC_need_full_gc = FALSE;
705 } else {
706 GC_need_full_gc =
707 BYTES_TO_WORDS(USED_HEAP_SIZE - GC_used_heap_size_after_full)
708 > min_words_allocd();
711 # ifdef PRINTSTATS
712 GC_printf2(
713 "Immediately reclaimed %ld bytes in heap of size %lu bytes",
714 (long)WORDS_TO_BYTES(GC_mem_found),
715 (unsigned long)GC_heapsize);
716 # ifdef USE_MUNMAP
717 GC_printf1("(%lu unmapped)", GC_unmapped_bytes);
718 # endif
719 GC_printf2(
720 "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
721 (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use),
722 (unsigned long)WORDS_TO_BYTES(GC_composite_in_use));
723 # endif
725 GC_n_attempts = 0;
726 GC_is_full_gc = FALSE;
727 /* Reset or increment counters for next cycle */
728 GC_words_allocd_before_gc += GC_words_allocd;
729 GC_non_gc_bytes_at_gc = GC_non_gc_bytes;
730 GC_words_allocd = 0;
731 GC_words_wasted = 0;
732 GC_mem_freed = 0;
733 GC_finalizer_mem_freed = 0;
735 # ifdef USE_MUNMAP
736 GC_unmap_old();
737 # endif
738 # ifdef PRINTTIMES
739 GET_TIME(done_time);
740 GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
741 MS_TIME_DIFF(finalize_time,start_time),
742 MS_TIME_DIFF(done_time,finalize_time));
743 # endif
746 /* Externally callable routine to invoke full, stop-world collection */
747 # if defined(__STDC__) || defined(__cplusplus)
748 int GC_try_to_collect(GC_stop_func stop_func)
749 # else
750 int GC_try_to_collect(stop_func)
751 GC_stop_func stop_func;
752 # endif
754 int result;
755 DCL_LOCK_STATE;
757 if (GC_debugging_started) GC_print_all_smashed();
758 GC_INVOKE_FINALIZERS();
759 DISABLE_SIGNALS();
760 LOCK();
761 ENTER_GC();
762 if (!GC_is_initialized) GC_init_inner();
763 /* Minimize junk left in my registers */
764 GC_noop(0,0,0,0,0,0);
765 result = (int)GC_try_to_collect_inner(stop_func);
766 EXIT_GC();
767 UNLOCK();
768 ENABLE_SIGNALS();
769 if(result) {
770 if (GC_debugging_started) GC_print_all_smashed();
771 GC_INVOKE_FINALIZERS();
773 return(result);
776 void GC_gcollect GC_PROTO(())
778 (void)GC_try_to_collect(GC_never_stop_func);
779 if (GC_have_errors) GC_print_all_errors();
782 word GC_n_heap_sects = 0; /* Number of sections currently in heap. */
785 * Use the chunk of memory starting at p of size bytes as part of the heap.
786 * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
788 void GC_add_to_heap(p, bytes)
789 struct hblk *p;
790 word bytes;
792 word words;
793 hdr * phdr;
795 if (GC_n_heap_sects >= MAX_HEAP_SECTS) {
796 ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
798 phdr = GC_install_header(p);
799 if (0 == phdr) {
800 /* This is extremely unlikely. Can't add it. This will */
801 /* almost certainly result in a 0 return from the allocator, */
802 /* which is entirely appropriate. */
803 return;
805 GC_heap_sects[GC_n_heap_sects].hs_start = (ptr_t)p;
806 GC_heap_sects[GC_n_heap_sects].hs_bytes = bytes;
807 GC_n_heap_sects++;
808 words = BYTES_TO_WORDS(bytes);
809 phdr -> hb_sz = words;
810 phdr -> hb_map = (unsigned char *)1; /* A value != GC_invalid_map */
811 phdr -> hb_flags = 0;
812 GC_freehblk(p);
813 GC_heapsize += bytes;
814 if ((ptr_t)p <= (ptr_t)GC_least_plausible_heap_addr
815 || GC_least_plausible_heap_addr == 0) {
816 GC_least_plausible_heap_addr = (GC_PTR)((ptr_t)p - sizeof(word));
817 /* Making it a little smaller than necessary prevents */
818 /* us from getting a false hit from the variable */
819 /* itself. There's some unintentional reflection */
820 /* here. */
822 if ((ptr_t)p + bytes >= (ptr_t)GC_greatest_plausible_heap_addr) {
823 GC_greatest_plausible_heap_addr = (GC_PTR)((ptr_t)p + bytes);
827 # if !defined(NO_DEBUGGING)
828 void GC_print_heap_sects()
830 register unsigned i;
832 GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize);
833 for (i = 0; i < GC_n_heap_sects; i++) {
834 unsigned long start = (unsigned long) GC_heap_sects[i].hs_start;
835 unsigned long len = (unsigned long) GC_heap_sects[i].hs_bytes;
836 struct hblk *h;
837 unsigned nbl = 0;
839 GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i,
840 start, (unsigned long)(start + len));
841 for (h = (struct hblk *)start; h < (struct hblk *)(start + len); h++) {
842 if (GC_is_black_listed(h, HBLKSIZE)) nbl++;
844 GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl,
845 (unsigned long)(len/HBLKSIZE));
848 # endif
850 GC_PTR GC_least_plausible_heap_addr = (GC_PTR)ONES;
851 GC_PTR GC_greatest_plausible_heap_addr = 0;
853 ptr_t GC_max(x,y)
854 ptr_t x, y;
856 return(x > y? x : y);
859 ptr_t GC_min(x,y)
860 ptr_t x, y;
862 return(x < y? x : y);
865 # if defined(__STDC__) || defined(__cplusplus)
866 void GC_set_max_heap_size(GC_word n)
867 # else
868 void GC_set_max_heap_size(n)
869 GC_word n;
870 # endif
872 GC_max_heapsize = n;
875 GC_word GC_max_retries = 0;
878 * this explicitly increases the size of the heap. It is used
879 * internally, but may also be invoked from GC_expand_hp by the user.
880 * The argument is in units of HBLKSIZE.
881 * Tiny values of n are rounded up.
882 * Returns FALSE on failure.
884 GC_bool GC_expand_hp_inner(n)
885 word n;
887 word bytes;
888 struct hblk * space;
889 word expansion_slop; /* Number of bytes by which we expect the */
890 /* heap to expand soon. */
892 if (n < MINHINCR) n = MINHINCR;
893 bytes = n * HBLKSIZE;
894 /* Make sure bytes is a multiple of GC_page_size */
896 word mask = GC_page_size - 1;
897 bytes += mask;
898 bytes &= ~mask;
901 if (GC_max_heapsize != 0 && GC_heapsize + bytes > GC_max_heapsize) {
902 /* Exceeded self-imposed limit */
903 return(FALSE);
905 space = GET_MEM(bytes);
906 if( space == 0 ) {
907 # ifdef CONDPRINT
908 if (GC_print_stats) {
909 GC_printf1("Failed to expand heap by %ld bytes\n",
910 (unsigned long)bytes);
912 # endif
913 return(FALSE);
915 # ifdef CONDPRINT
916 if (GC_print_stats) {
917 GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
918 (unsigned long)bytes,
919 (unsigned long)WORDS_TO_BYTES(GC_words_allocd));
920 # ifdef UNDEFINED
921 GC_printf1("Root size = %lu\n", GC_root_size);
922 GC_print_block_list(); GC_print_hblkfreelist();
923 GC_printf0("\n");
924 # endif
926 # endif
927 expansion_slop = 8 * WORDS_TO_BYTES(min_words_allocd());
928 if (5 * HBLKSIZE * MAXHINCR > expansion_slop) {
929 expansion_slop = 5 * HBLKSIZE * MAXHINCR;
931 if (GC_last_heap_addr == 0 && !((word)space & SIGNB)
932 || GC_last_heap_addr != 0 && GC_last_heap_addr < (ptr_t)space) {
933 /* Assume the heap is growing up */
934 GC_greatest_plausible_heap_addr =
935 GC_max(GC_greatest_plausible_heap_addr,
936 (ptr_t)space + bytes + expansion_slop);
937 } else {
938 /* Heap is growing down */
939 GC_least_plausible_heap_addr =
940 GC_min(GC_least_plausible_heap_addr,
941 (ptr_t)space - expansion_slop);
943 GC_prev_heap_addr = GC_last_heap_addr;
944 GC_last_heap_addr = (ptr_t)space;
945 GC_add_to_heap(space, bytes);
946 return(TRUE);
949 /* Really returns a bool, but it's externally visible, so that's clumsy. */
950 /* Arguments is in bytes. */
951 # if defined(__STDC__) || defined(__cplusplus)
952 int GC_expand_hp(size_t bytes)
953 # else
954 int GC_expand_hp(bytes)
955 size_t bytes;
956 # endif
958 int result;
959 DCL_LOCK_STATE;
961 DISABLE_SIGNALS();
962 LOCK();
963 if (!GC_is_initialized) GC_init_inner();
964 result = (int)GC_expand_hp_inner(divHBLKSZ((word)bytes));
965 if (result) GC_requested_heapsize += bytes;
966 UNLOCK();
967 ENABLE_SIGNALS();
968 return(result);
971 unsigned GC_fail_count = 0;
972 /* How many consecutive GC/expansion failures? */
973 /* Reset by GC_allochblk. */
975 GC_bool GC_collect_or_expand(needed_blocks, ignore_off_page)
976 word needed_blocks;
977 GC_bool ignore_off_page;
979 if (!GC_incremental && !GC_dont_gc &&
980 (GC_dont_expand && GC_words_allocd > 0 || GC_should_collect())) {
981 GC_gcollect_inner();
982 } else {
983 word blocks_to_get = GC_heapsize/(HBLKSIZE*GC_free_space_divisor)
984 + needed_blocks;
986 if (blocks_to_get > MAXHINCR) {
987 word slop;
989 if (ignore_off_page) {
990 slop = 4;
991 } else {
992 slop = 2*divHBLKSZ(BL_LIMIT);
993 if (slop > needed_blocks) slop = needed_blocks;
995 if (needed_blocks + slop > MAXHINCR) {
996 blocks_to_get = needed_blocks + slop;
997 } else {
998 blocks_to_get = MAXHINCR;
1001 if (!GC_expand_hp_inner(blocks_to_get)
1002 && !GC_expand_hp_inner(needed_blocks)) {
1003 if (GC_fail_count++ < GC_max_retries) {
1004 WARN("Out of Memory! Trying to continue ...\n", 0);
1005 GC_gcollect_inner();
1006 } else {
1007 # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
1008 WARN("Out of Memory! Returning NIL!\n", 0);
1009 # endif
1010 return(FALSE);
1012 } else {
1013 # ifdef CONDPRINT
1014 if (GC_fail_count && GC_print_stats) {
1015 GC_printf0("Memory available again ...\n");
1017 # endif
1020 return(TRUE);
1024 * Make sure the object free list for sz is not empty.
1025 * Return a pointer to the first object on the free list.
1026 * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
1027 * Assumes we hold the allocator lock and signals are disabled.
1030 ptr_t GC_allocobj(sz, kind)
1031 word sz;
1032 int kind;
1034 ptr_t * flh = &(GC_obj_kinds[kind].ok_freelist[sz]);
1035 GC_bool tried_minor = FALSE;
1037 if (sz == 0) return(0);
1039 while (*flh == 0) {
1040 ENTER_GC();
1041 /* Do our share of marking work */
1042 if(TRUE_INCREMENTAL) GC_collect_a_little_inner(1);
1043 /* Sweep blocks for objects of this size */
1044 GC_continue_reclaim(sz, kind);
1045 EXIT_GC();
1046 if (*flh == 0) {
1047 GC_new_hblk(sz, kind);
1049 if (*flh == 0) {
1050 ENTER_GC();
1051 if (GC_incremental && GC_time_limit == GC_TIME_UNLIMITED
1052 && ! tried_minor ) {
1053 GC_collect_a_little_inner(1);
1054 tried_minor = TRUE;
1055 } else {
1056 if (!GC_collect_or_expand((word)1,FALSE)) {
1057 EXIT_GC();
1058 return(0);
1061 EXIT_GC();
1064 /* Successful allocation; reset failure count. */
1065 GC_fail_count = 0;
1067 return(*flh);