* arm.h (ASM_OUTPUT_REG_PUSH, ASM_OUTPUT_REG_POP): Wrap in
[official-gcc.git] / boehm-gc / alloc.c
blobf53061f872d02096dfb19915e4a6f0ed41fcbd1a
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 #ifndef NO_CLOCK
130 GET_TIME(current_time);
131 time_diff = MS_TIME_DIFF(current_time,GC_start_time);
132 if (time_diff >= GC_time_limit) {
133 # ifdef CONDPRINT
134 if (GC_print_stats) {
135 GC_printf0("Abandoning stopped marking after ");
136 GC_printf1("%lu msecs", (unsigned long)time_diff);
137 GC_printf1("(attempt %d)\n", (unsigned long) GC_n_attempts);
139 # endif
140 return(1);
142 #endif
143 return(0);
145 #endif /* !SMALL_CONFIG */
147 /* Return the minimum number of words that must be allocated between */
148 /* collections to amortize the collection cost. */
149 static word min_words_allocd()
151 # ifdef THREADS
152 /* We punt, for now. */
153 register signed_word stack_size = 10000;
154 # else
155 int dummy;
156 register signed_word stack_size = (ptr_t)(&dummy) - GC_stackbottom;
157 # endif
158 word total_root_size; /* includes double stack size, */
159 /* since the stack is expensive */
160 /* to scan. */
161 word scan_size; /* Estimate of memory to be scanned */
162 /* during normal GC. */
164 if (stack_size < 0) stack_size = -stack_size;
165 total_root_size = 2 * stack_size + GC_root_size;
166 scan_size = BYTES_TO_WORDS(GC_heapsize - GC_large_free_bytes
167 + (GC_large_free_bytes >> 2)
168 /* use a bit more of large empty heap */
169 + total_root_size);
170 if (TRUE_INCREMENTAL) {
171 return scan_size / (2 * GC_free_space_divisor);
172 } else {
173 return scan_size / GC_free_space_divisor;
177 /* Return the number of words allocated, adjusted for explicit storage */
178 /* management, etc.. This number is used in deciding when to trigger */
179 /* collections. */
180 word GC_adj_words_allocd()
182 register signed_word result;
183 register signed_word expl_managed =
184 BYTES_TO_WORDS((long)GC_non_gc_bytes
185 - (long)GC_non_gc_bytes_at_gc);
187 /* Don't count what was explicitly freed, or newly allocated for */
188 /* explicit management. Note that deallocating an explicitly */
189 /* managed object should not alter result, assuming the client */
190 /* is playing by the rules. */
191 result = (signed_word)GC_words_allocd
192 - (signed_word)GC_mem_freed
193 + (signed_word)GC_finalizer_mem_freed - expl_managed;
194 if (result > (signed_word)GC_words_allocd) {
195 result = GC_words_allocd;
196 /* probably client bug or unfortunate scheduling */
198 result += GC_words_finalized;
199 /* We count objects enqueued for finalization as though they */
200 /* had been reallocated this round. Finalization is user */
201 /* visible progress. And if we don't count this, we have */
202 /* stability problems for programs that finalize all objects. */
203 result += GC_words_wasted;
204 /* This doesn't reflect useful work. But if there is lots of */
205 /* new fragmentation, the same is probably true of the heap, */
206 /* and the collection will be correspondingly cheaper. */
207 if (result < (signed_word)(GC_words_allocd >> 3)) {
208 /* Always count at least 1/8 of the allocations. We don't want */
209 /* to collect too infrequently, since that would inhibit */
210 /* coalescing of free storage blocks. */
211 /* This also makes us partially robust against client bugs. */
212 return(GC_words_allocd >> 3);
213 } else {
214 return(result);
219 /* Clear up a few frames worth of garbage left at the top of the stack. */
220 /* This is used to prevent us from accidentally treating garbade left */
221 /* on the stack by other parts of the collector as roots. This */
222 /* differs from the code in misc.c, which actually tries to keep the */
223 /* stack clear of long-lived, client-generated garbage. */
224 void GC_clear_a_few_frames()
226 # define NWORDS 64
227 word frames[NWORDS];
228 register int i;
230 for (i = 0; i < NWORDS; i++) frames[i] = 0;
233 /* Have we allocated enough to amortize a collection? */
234 GC_bool GC_should_collect()
236 return(GC_adj_words_allocd() >= min_words_allocd());
240 void GC_notify_full_gc()
242 if (GC_start_call_back != (void (*) GC_PROTO((void)))0) {
243 (*GC_start_call_back)();
247 GC_bool GC_is_full_gc = FALSE;
250 * Initiate a garbage collection if appropriate.
251 * Choose judiciously
252 * between partial, full, and stop-world collections.
253 * Assumes lock held, signals disabled.
255 void GC_maybe_gc()
257 static int n_partial_gcs = 0;
259 if (GC_should_collect()) {
260 if (!GC_incremental) {
261 GC_gcollect_inner();
262 n_partial_gcs = 0;
263 return;
264 } else {
265 # ifdef PARALLEL_MARK
266 GC_wait_for_reclaim();
267 # endif
268 if (GC_need_full_gc || n_partial_gcs >= GC_full_freq) {
269 # ifdef CONDPRINT
270 if (GC_print_stats) {
271 GC_printf2(
272 "***>Full mark for collection %lu after %ld allocd bytes\n",
273 (unsigned long) GC_gc_no+1,
274 (long)WORDS_TO_BYTES(GC_words_allocd));
276 # endif
277 GC_promote_black_lists();
278 (void)GC_reclaim_all((GC_stop_func)0, TRUE);
279 GC_clear_marks();
280 n_partial_gcs = 0;
281 GC_notify_full_gc();
282 GC_is_full_gc = TRUE;
283 } else {
284 n_partial_gcs++;
287 /* We try to mark with the world stopped. */
288 /* If we run out of time, this turns into */
289 /* incremental marking. */
290 # ifndef NO_CLOCK
291 if (GC_time_limit != GC_TIME_UNLIMITED) { GET_TIME(GC_start_time); }
292 # endif
293 if (GC_stopped_mark(GC_time_limit == GC_TIME_UNLIMITED?
294 GC_never_stop_func : GC_timeout_stop_func)) {
295 # ifdef SAVE_CALL_CHAIN
296 GC_save_callers(GC_last_stack);
297 # endif
298 GC_finish_collection();
299 } else {
300 if (!GC_is_full_gc) {
301 /* Count this as the first attempt */
302 GC_n_attempts++;
310 * Stop the world garbage collection. Assumes lock held, signals disabled.
311 * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
312 * Return TRUE if we successfully completed the collection.
314 GC_bool GC_try_to_collect_inner(stop_func)
315 GC_stop_func stop_func;
317 # ifdef CONDPRINT
318 CLOCK_TYPE start_time, current_time;
319 # endif
320 if (GC_dont_gc) return FALSE;
321 if (GC_incremental && GC_collection_in_progress()) {
322 # ifdef CONDPRINT
323 if (GC_print_stats) {
324 GC_printf0(
325 "GC_try_to_collect_inner: finishing collection in progress\n");
327 # endif /* CONDPRINT */
328 /* Just finish collection already in progress. */
329 while(GC_collection_in_progress()) {
330 if (stop_func()) return(FALSE);
331 GC_collect_a_little_inner(1);
334 if (stop_func == GC_never_stop_func) GC_notify_full_gc();
335 # ifdef CONDPRINT
336 if (GC_print_stats) {
337 if (GC_print_stats) GET_TIME(start_time);
338 GC_printf2(
339 "Initiating full world-stop collection %lu after %ld allocd bytes\n",
340 (unsigned long) GC_gc_no+1,
341 (long)WORDS_TO_BYTES(GC_words_allocd));
343 # endif
344 GC_promote_black_lists();
345 /* Make sure all blocks have been reclaimed, so sweep routines */
346 /* don't see cleared mark bits. */
347 /* If we're guaranteed to finish, then this is unnecessary. */
348 /* In the find_leak case, we have to finish to guarantee that */
349 /* previously unmarked objects are not reported as leaks. */
350 # ifdef PARALLEL_MARK
351 GC_wait_for_reclaim();
352 # endif
353 if ((GC_find_leak || stop_func != GC_never_stop_func)
354 && !GC_reclaim_all(stop_func, FALSE)) {
355 /* Aborted. So far everything is still consistent. */
356 return(FALSE);
358 GC_invalidate_mark_state(); /* Flush mark stack. */
359 GC_clear_marks();
360 # ifdef SAVE_CALL_CHAIN
361 GC_save_callers(GC_last_stack);
362 # endif
363 GC_is_full_gc = TRUE;
364 if (!GC_stopped_mark(stop_func)) {
365 if (!GC_incremental) {
366 /* We're partially done and have no way to complete or use */
367 /* current work. Reestablish invariants as cheaply as */
368 /* possible. */
369 GC_invalidate_mark_state();
370 GC_unpromote_black_lists();
371 } /* else we claim the world is already still consistent. We'll */
372 /* finish incrementally. */
373 return(FALSE);
375 GC_finish_collection();
376 # if defined(CONDPRINT)
377 if (GC_print_stats) {
378 GET_TIME(current_time);
379 GC_printf1("Complete collection took %lu msecs\n",
380 MS_TIME_DIFF(current_time,start_time));
382 # endif
383 return(TRUE);
389 * Perform n units of garbage collection work. A unit is intended to touch
390 * roughly GC_RATE pages. Every once in a while, we do more than that.
391 * This needa to be a fairly large number with our current incremental
392 * GC strategy, since otherwise we allocate too much during GC, and the
393 * cleanup gets expensive.
395 # define GC_RATE 10
396 # define MAX_PRIOR_ATTEMPTS 1
397 /* Maximum number of prior attempts at world stop marking */
398 /* A value of 1 means that we finish the second time, no matter */
399 /* how long it takes. Doesn't count the initial root scan */
400 /* for a full GC. */
402 int GC_deficit = 0; /* The number of extra calls to GC_mark_some */
403 /* that we have made. */
405 void GC_collect_a_little_inner(n)
406 int n;
408 register int i;
410 if (GC_dont_gc) return;
411 if (GC_incremental && GC_collection_in_progress()) {
412 for (i = GC_deficit; i < GC_RATE*n; i++) {
413 if (GC_mark_some((ptr_t)0)) {
414 /* Need to finish a collection */
415 # ifdef SAVE_CALL_CHAIN
416 GC_save_callers(GC_last_stack);
417 # endif
418 # ifdef PARALLEL_MARK
419 GC_wait_for_reclaim();
420 # endif
421 if (GC_n_attempts < MAX_PRIOR_ATTEMPTS
422 && GC_time_limit != GC_TIME_UNLIMITED) {
423 GET_TIME(GC_start_time);
424 if (!GC_stopped_mark(GC_timeout_stop_func)) {
425 GC_n_attempts++;
426 break;
428 } else {
429 (void)GC_stopped_mark(GC_never_stop_func);
431 GC_finish_collection();
432 break;
435 if (GC_deficit > 0) GC_deficit -= GC_RATE*n;
436 if (GC_deficit < 0) GC_deficit = 0;
437 } else {
438 GC_maybe_gc();
442 int GC_collect_a_little GC_PROTO(())
444 int result;
445 DCL_LOCK_STATE;
447 DISABLE_SIGNALS();
448 LOCK();
449 GC_collect_a_little_inner(1);
450 result = (int)GC_collection_in_progress();
451 UNLOCK();
452 ENABLE_SIGNALS();
453 if (!result && GC_debugging_started) GC_print_all_smashed();
454 return(result);
458 * Assumes lock is held, signals are disabled.
459 * We stop the world.
460 * If stop_func() ever returns TRUE, we may fail and return FALSE.
461 * Increment GC_gc_no if we succeed.
463 GC_bool GC_stopped_mark(stop_func)
464 GC_stop_func stop_func;
466 register int i;
467 int dummy;
468 # if defined(PRINTTIMES) || defined(CONDPRINT)
469 CLOCK_TYPE start_time, current_time;
470 # endif
472 # ifdef PRINTTIMES
473 GET_TIME(start_time);
474 # endif
475 # if defined(CONDPRINT) && !defined(PRINTTIMES)
476 if (GC_print_stats) GET_TIME(start_time);
477 # endif
478 # if defined(REGISTER_LIBRARIES_EARLY)
479 GC_cond_register_dynamic_libraries();
480 # endif
481 STOP_WORLD();
482 IF_THREADS(GC_world_stopped = TRUE);
483 # ifdef CONDPRINT
484 if (GC_print_stats) {
485 GC_printf1("--> Marking for collection %lu ",
486 (unsigned long) GC_gc_no + 1);
487 GC_printf2("after %lu allocd bytes + %lu wasted bytes\n",
488 (unsigned long) WORDS_TO_BYTES(GC_words_allocd),
489 (unsigned long) WORDS_TO_BYTES(GC_words_wasted));
491 # endif
492 # ifdef MAKE_BACK_GRAPH
493 if (GC_print_back_height) {
494 GC_build_back_graph();
496 # endif
498 /* Mark from all roots. */
499 /* Minimize junk left in my registers and on the stack */
500 GC_clear_a_few_frames();
501 GC_noop(0,0,0,0,0,0);
502 GC_initiate_gc();
503 for(i = 0;;i++) {
504 if ((*stop_func)()) {
505 # ifdef CONDPRINT
506 if (GC_print_stats) {
507 GC_printf0("Abandoned stopped marking after ");
508 GC_printf1("%lu iterations\n",
509 (unsigned long)i);
511 # endif
512 GC_deficit = i; /* Give the mutator a chance. */
513 IF_THREADS(GC_world_stopped = FALSE);
514 START_WORLD();
515 return(FALSE);
517 if (GC_mark_some((ptr_t)(&dummy))) break;
520 GC_gc_no++;
521 # ifdef PRINTSTATS
522 GC_printf2("Collection %lu reclaimed %ld bytes",
523 (unsigned long) GC_gc_no - 1,
524 (long)WORDS_TO_BYTES(GC_mem_found));
525 # else
526 # ifdef CONDPRINT
527 if (GC_print_stats) {
528 GC_printf1("Collection %lu finished", (unsigned long) GC_gc_no - 1);
530 # endif
531 # endif /* !PRINTSTATS */
532 # ifdef CONDPRINT
533 if (GC_print_stats) {
534 GC_printf1(" ---> heapsize = %lu bytes\n",
535 (unsigned long) GC_heapsize);
536 /* Printf arguments may be pushed in funny places. Clear the */
537 /* space. */
538 GC_printf0("");
540 # endif /* CONDPRINT */
542 /* Check all debugged objects for consistency */
543 if (GC_debugging_started) {
544 (*GC_check_heap)();
547 IF_THREADS(GC_world_stopped = FALSE);
548 START_WORLD();
549 # ifdef PRINTTIMES
550 GET_TIME(current_time);
551 GC_printf1("World-stopped marking took %lu msecs\n",
552 MS_TIME_DIFF(current_time,start_time));
553 # else
554 # ifdef CONDPRINT
555 if (GC_print_stats) {
556 GET_TIME(current_time);
557 GC_printf1("World-stopped marking took %lu msecs\n",
558 MS_TIME_DIFF(current_time,start_time));
560 # endif
561 # endif
562 return(TRUE);
565 /* Set all mark bits for the free list whose first entry is q */
566 #ifdef __STDC__
567 void GC_set_fl_marks(ptr_t q)
568 #else
569 void GC_set_fl_marks(q)
570 ptr_t q;
571 #endif
573 ptr_t p;
574 struct hblk * h, * last_h = 0;
575 hdr *hhdr;
576 int word_no;
578 for (p = q; p != 0; p = obj_link(p)){
579 h = HBLKPTR(p);
580 if (h != last_h) {
581 last_h = h;
582 hhdr = HDR(h);
584 word_no = (((word *)p) - ((word *)h));
585 set_mark_bit_from_hdr(hhdr, word_no);
589 /* Clear all mark bits for the free list whose first entry is q */
590 /* Decrement GC_mem_found by number of words on free list. */
591 #ifdef __STDC__
592 void GC_clear_fl_marks(ptr_t q)
593 #else
594 void GC_clear_fl_marks(q)
595 ptr_t q;
596 #endif
598 ptr_t p;
599 struct hblk * h, * last_h = 0;
600 hdr *hhdr;
601 int word_no;
603 for (p = q; p != 0; p = obj_link(p)){
604 h = HBLKPTR(p);
605 if (h != last_h) {
606 last_h = h;
607 hhdr = HDR(h);
609 word_no = (((word *)p) - ((word *)h));
610 clear_mark_bit_from_hdr(hhdr, word_no);
611 # ifdef GATHERSTATS
612 GC_mem_found -= hhdr -> hb_sz;
613 # endif
617 /* Finish up a collection. Assumes lock is held, signals are disabled, */
618 /* but the world is otherwise running. */
619 void GC_finish_collection()
621 # ifdef PRINTTIMES
622 CLOCK_TYPE start_time;
623 CLOCK_TYPE finalize_time;
624 CLOCK_TYPE done_time;
626 GET_TIME(start_time);
627 finalize_time = start_time;
628 # endif
630 # ifdef GATHERSTATS
631 GC_mem_found = 0;
632 # endif
633 # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
634 if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
635 GC_print_address_map();
637 # endif
638 COND_DUMP;
639 if (GC_find_leak) {
640 /* Mark all objects on the free list. All objects should be */
641 /* marked when we're done. */
643 register word size; /* current object size */
644 int kind;
645 ptr_t q;
647 for (kind = 0; kind < GC_n_kinds; kind++) {
648 for (size = 1; size <= MAXOBJSZ; size++) {
649 q = GC_obj_kinds[kind].ok_freelist[size];
650 if (q != 0) GC_set_fl_marks(q);
654 GC_start_reclaim(TRUE);
655 /* The above just checks; it doesn't really reclaim anything. */
658 GC_finalize();
659 # ifdef STUBBORN_ALLOC
660 GC_clean_changing_list();
661 # endif
663 # ifdef PRINTTIMES
664 GET_TIME(finalize_time);
665 # endif
667 if (GC_print_back_height) {
668 # ifdef MAKE_BACK_GRAPH
669 GC_traverse_back_graph();
670 # else
671 # ifndef SMALL_CONFIG
672 GC_err_printf0("Back height not available: "
673 "Rebuild collector with -DMAKE_BACK_GRAPH\n");
674 # endif
675 # endif
678 /* Clear free list mark bits, in case they got accidentally marked */
679 /* (or GC_find_leak is set and they were intentionally marked). */
680 /* Also subtract memory remaining from GC_mem_found count. */
681 /* Note that composite objects on free list are cleared. */
682 /* Thus accidentally marking a free list is not a problem; only */
683 /* objects on the list itself will be marked, and that's fixed here. */
685 register word size; /* current object size */
686 register ptr_t q; /* pointer to current object */
687 int kind;
689 for (kind = 0; kind < GC_n_kinds; kind++) {
690 for (size = 1; size <= MAXOBJSZ; size++) {
691 q = GC_obj_kinds[kind].ok_freelist[size];
692 if (q != 0) GC_clear_fl_marks(q);
698 # ifdef PRINTSTATS
699 GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
700 (long)WORDS_TO_BYTES(GC_mem_found));
701 # endif
702 /* Reconstruct free lists to contain everything not marked */
703 GC_start_reclaim(FALSE);
704 if (GC_is_full_gc) {
705 GC_used_heap_size_after_full = USED_HEAP_SIZE;
706 GC_need_full_gc = FALSE;
707 } else {
708 GC_need_full_gc =
709 BYTES_TO_WORDS(USED_HEAP_SIZE - GC_used_heap_size_after_full)
710 > min_words_allocd();
713 # ifdef PRINTSTATS
714 GC_printf2(
715 "Immediately reclaimed %ld bytes in heap of size %lu bytes",
716 (long)WORDS_TO_BYTES(GC_mem_found),
717 (unsigned long)GC_heapsize);
718 # ifdef USE_MUNMAP
719 GC_printf1("(%lu unmapped)", GC_unmapped_bytes);
720 # endif
721 GC_printf2(
722 "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
723 (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use),
724 (unsigned long)WORDS_TO_BYTES(GC_composite_in_use));
725 # endif
727 GC_n_attempts = 0;
728 GC_is_full_gc = FALSE;
729 /* Reset or increment counters for next cycle */
730 GC_words_allocd_before_gc += GC_words_allocd;
731 GC_non_gc_bytes_at_gc = GC_non_gc_bytes;
732 GC_words_allocd = 0;
733 GC_words_wasted = 0;
734 GC_mem_freed = 0;
735 GC_finalizer_mem_freed = 0;
737 # ifdef USE_MUNMAP
738 GC_unmap_old();
739 # endif
740 # ifdef PRINTTIMES
741 GET_TIME(done_time);
742 GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
743 MS_TIME_DIFF(finalize_time,start_time),
744 MS_TIME_DIFF(done_time,finalize_time));
745 # endif
748 /* Externally callable routine to invoke full, stop-world collection */
749 # if defined(__STDC__) || defined(__cplusplus)
750 int GC_try_to_collect(GC_stop_func stop_func)
751 # else
752 int GC_try_to_collect(stop_func)
753 GC_stop_func stop_func;
754 # endif
756 int result;
757 DCL_LOCK_STATE;
759 if (GC_debugging_started) GC_print_all_smashed();
760 GC_INVOKE_FINALIZERS();
761 DISABLE_SIGNALS();
762 LOCK();
763 ENTER_GC();
764 if (!GC_is_initialized) GC_init_inner();
765 /* Minimize junk left in my registers */
766 GC_noop(0,0,0,0,0,0);
767 result = (int)GC_try_to_collect_inner(stop_func);
768 EXIT_GC();
769 UNLOCK();
770 ENABLE_SIGNALS();
771 if(result) {
772 if (GC_debugging_started) GC_print_all_smashed();
773 GC_INVOKE_FINALIZERS();
775 return(result);
778 void GC_gcollect GC_PROTO(())
780 (void)GC_try_to_collect(GC_never_stop_func);
781 if (GC_have_errors) GC_print_all_errors();
784 word GC_n_heap_sects = 0; /* Number of sections currently in heap. */
787 * Use the chunk of memory starting at p of size bytes as part of the heap.
788 * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
790 void GC_add_to_heap(p, bytes)
791 struct hblk *p;
792 word bytes;
794 word words;
795 hdr * phdr;
797 if (GC_n_heap_sects >= MAX_HEAP_SECTS) {
798 ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
800 phdr = GC_install_header(p);
801 if (0 == phdr) {
802 /* This is extremely unlikely. Can't add it. This will */
803 /* almost certainly result in a 0 return from the allocator, */
804 /* which is entirely appropriate. */
805 return;
807 GC_heap_sects[GC_n_heap_sects].hs_start = (ptr_t)p;
808 GC_heap_sects[GC_n_heap_sects].hs_bytes = bytes;
809 GC_n_heap_sects++;
810 words = BYTES_TO_WORDS(bytes);
811 phdr -> hb_sz = words;
812 phdr -> hb_map = (unsigned char *)1; /* A value != GC_invalid_map */
813 phdr -> hb_flags = 0;
814 GC_freehblk(p);
815 GC_heapsize += bytes;
816 if ((ptr_t)p <= (ptr_t)GC_least_plausible_heap_addr
817 || GC_least_plausible_heap_addr == 0) {
818 GC_least_plausible_heap_addr = (GC_PTR)((ptr_t)p - sizeof(word));
819 /* Making it a little smaller than necessary prevents */
820 /* us from getting a false hit from the variable */
821 /* itself. There's some unintentional reflection */
822 /* here. */
824 if ((ptr_t)p + bytes >= (ptr_t)GC_greatest_plausible_heap_addr) {
825 GC_greatest_plausible_heap_addr = (GC_PTR)((ptr_t)p + bytes);
829 # if !defined(NO_DEBUGGING)
830 void GC_print_heap_sects()
832 register unsigned i;
834 GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize);
835 for (i = 0; i < GC_n_heap_sects; i++) {
836 unsigned long start = (unsigned long) GC_heap_sects[i].hs_start;
837 unsigned long len = (unsigned long) GC_heap_sects[i].hs_bytes;
838 struct hblk *h;
839 unsigned nbl = 0;
841 GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i,
842 start, (unsigned long)(start + len));
843 for (h = (struct hblk *)start; h < (struct hblk *)(start + len); h++) {
844 if (GC_is_black_listed(h, HBLKSIZE)) nbl++;
846 GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl,
847 (unsigned long)(len/HBLKSIZE));
850 # endif
852 GC_PTR GC_least_plausible_heap_addr = (GC_PTR)ONES;
853 GC_PTR GC_greatest_plausible_heap_addr = 0;
855 ptr_t GC_max(x,y)
856 ptr_t x, y;
858 return(x > y? x : y);
861 ptr_t GC_min(x,y)
862 ptr_t x, y;
864 return(x < y? x : y);
867 # if defined(__STDC__) || defined(__cplusplus)
868 void GC_set_max_heap_size(GC_word n)
869 # else
870 void GC_set_max_heap_size(n)
871 GC_word n;
872 # endif
874 GC_max_heapsize = n;
877 GC_word GC_max_retries = 0;
880 * this explicitly increases the size of the heap. It is used
881 * internally, but may also be invoked from GC_expand_hp by the user.
882 * The argument is in units of HBLKSIZE.
883 * Tiny values of n are rounded up.
884 * Returns FALSE on failure.
886 GC_bool GC_expand_hp_inner(n)
887 word n;
889 word bytes;
890 struct hblk * space;
891 word expansion_slop; /* Number of bytes by which we expect the */
892 /* heap to expand soon. */
894 if (n < MINHINCR) n = MINHINCR;
895 bytes = n * HBLKSIZE;
896 /* Make sure bytes is a multiple of GC_page_size */
898 word mask = GC_page_size - 1;
899 bytes += mask;
900 bytes &= ~mask;
903 if (GC_max_heapsize != 0 && GC_heapsize + bytes > GC_max_heapsize) {
904 /* Exceeded self-imposed limit */
905 return(FALSE);
907 space = GET_MEM(bytes);
908 if( space == 0 ) {
909 # ifdef CONDPRINT
910 if (GC_print_stats) {
911 GC_printf1("Failed to expand heap by %ld bytes\n",
912 (unsigned long)bytes);
914 # endif
915 return(FALSE);
917 # ifdef CONDPRINT
918 if (GC_print_stats) {
919 GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
920 (unsigned long)bytes,
921 (unsigned long)WORDS_TO_BYTES(GC_words_allocd));
922 # ifdef UNDEFINED
923 GC_printf1("Root size = %lu\n", GC_root_size);
924 GC_print_block_list(); GC_print_hblkfreelist();
925 GC_printf0("\n");
926 # endif
928 # endif
929 expansion_slop = 8 * WORDS_TO_BYTES(min_words_allocd());
930 if (5 * HBLKSIZE * MAXHINCR > expansion_slop) {
931 expansion_slop = 5 * HBLKSIZE * MAXHINCR;
933 if (GC_last_heap_addr == 0 && !((word)space & SIGNB)
934 || GC_last_heap_addr != 0 && GC_last_heap_addr < (ptr_t)space) {
935 /* Assume the heap is growing up */
936 GC_greatest_plausible_heap_addr =
937 GC_max(GC_greatest_plausible_heap_addr,
938 (ptr_t)space + bytes + expansion_slop);
939 } else {
940 /* Heap is growing down */
941 GC_least_plausible_heap_addr =
942 GC_min(GC_least_plausible_heap_addr,
943 (ptr_t)space - expansion_slop);
945 GC_prev_heap_addr = GC_last_heap_addr;
946 GC_last_heap_addr = (ptr_t)space;
947 GC_add_to_heap(space, bytes);
948 return(TRUE);
951 /* Really returns a bool, but it's externally visible, so that's clumsy. */
952 /* Arguments is in bytes. */
953 # if defined(__STDC__) || defined(__cplusplus)
954 int GC_expand_hp(size_t bytes)
955 # else
956 int GC_expand_hp(bytes)
957 size_t bytes;
958 # endif
960 int result;
961 DCL_LOCK_STATE;
963 DISABLE_SIGNALS();
964 LOCK();
965 if (!GC_is_initialized) GC_init_inner();
966 result = (int)GC_expand_hp_inner(divHBLKSZ((word)bytes));
967 if (result) GC_requested_heapsize += bytes;
968 UNLOCK();
969 ENABLE_SIGNALS();
970 return(result);
973 unsigned GC_fail_count = 0;
974 /* How many consecutive GC/expansion failures? */
975 /* Reset by GC_allochblk. */
977 GC_bool GC_collect_or_expand(needed_blocks, ignore_off_page)
978 word needed_blocks;
979 GC_bool ignore_off_page;
981 if (!GC_incremental && !GC_dont_gc &&
982 (GC_dont_expand && GC_words_allocd > 0 || GC_should_collect())) {
983 GC_gcollect_inner();
984 } else {
985 word blocks_to_get = GC_heapsize/(HBLKSIZE*GC_free_space_divisor)
986 + needed_blocks;
988 if (blocks_to_get > MAXHINCR) {
989 word slop;
991 if (ignore_off_page) {
992 slop = 4;
993 } else {
994 slop = 2*divHBLKSZ(BL_LIMIT);
995 if (slop > needed_blocks) slop = needed_blocks;
997 if (needed_blocks + slop > MAXHINCR) {
998 blocks_to_get = needed_blocks + slop;
999 } else {
1000 blocks_to_get = MAXHINCR;
1003 if (!GC_expand_hp_inner(blocks_to_get)
1004 && !GC_expand_hp_inner(needed_blocks)) {
1005 if (GC_fail_count++ < GC_max_retries) {
1006 WARN("Out of Memory! Trying to continue ...\n", 0);
1007 GC_gcollect_inner();
1008 } else {
1009 # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
1010 WARN("Out of Memory! Returning NIL!\n", 0);
1011 # endif
1012 return(FALSE);
1014 } else {
1015 # ifdef CONDPRINT
1016 if (GC_fail_count && GC_print_stats) {
1017 GC_printf0("Memory available again ...\n");
1019 # endif
1022 return(TRUE);
1026 * Make sure the object free list for sz is not empty.
1027 * Return a pointer to the first object on the free list.
1028 * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
1029 * Assumes we hold the allocator lock and signals are disabled.
1032 ptr_t GC_allocobj(sz, kind)
1033 word sz;
1034 int kind;
1036 ptr_t * flh = &(GC_obj_kinds[kind].ok_freelist[sz]);
1037 GC_bool tried_minor = FALSE;
1039 if (sz == 0) return(0);
1041 while (*flh == 0) {
1042 ENTER_GC();
1043 /* Do our share of marking work */
1044 if(TRUE_INCREMENTAL) GC_collect_a_little_inner(1);
1045 /* Sweep blocks for objects of this size */
1046 GC_continue_reclaim(sz, kind);
1047 EXIT_GC();
1048 if (*flh == 0) {
1049 GC_new_hblk(sz, kind);
1051 if (*flh == 0) {
1052 ENTER_GC();
1053 if (GC_incremental && GC_time_limit == GC_TIME_UNLIMITED
1054 && ! tried_minor ) {
1055 GC_collect_a_little_inner(1);
1056 tried_minor = TRUE;
1057 } else {
1058 if (!GC_collect_or_expand((word)1,FALSE)) {
1059 EXIT_GC();
1060 return(0);
1063 EXIT_GC();
1066 /* Successful allocation; reset failure count. */
1067 GC_fail_count = 0;
1069 return(*flh);