Annotation of OpenXM_contrib2/asir2000/gc/alloc.c, Revision 1.10
1.1 noro 1: /*
2: * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
1.2 noro 3: * Copyright (c) 1991-1996 by Xerox Corporation. All rights reserved.
1.1 noro 4: * Copyright (c) 1998 by Silicon Graphics. All rights reserved.
1.2 noro 5: * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
1.1 noro 6: *
7: * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8: * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9: *
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.
15: *
16: */
17:
18:
1.4 noro 19: # include "private/gc_priv.h"
1.1 noro 20:
21: # include <stdio.h>
1.4 noro 22: # if !defined(MACOS) && !defined(MSWINCE)
1.1 noro 23: # include <signal.h>
24: # include <sys/types.h>
25: # endif
26:
27: /*
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:
35: *
36: * opp = &(GC_objfreelist[i]);
37: * if (*opp == 0) GC_allocobj(i, NORMAL);
38: * ptr = *opp;
39: * *opp = obj_link(ptr);
40: *
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.
45: */
46:
47: /*
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.
56: */
57:
58: word GC_non_gc_bytes = 0; /* Number of bytes not intended to be collected */
59:
60: word GC_gc_no = 0;
61:
62: #ifndef SMALL_CONFIG
1.4 noro 63: int GC_incremental = 0; /* By default, stop the world. */
1.1 noro 64: #endif
65:
1.4 noro 66: int GC_parallel = FALSE; /* By default, parallel GC is off. */
67:
1.2 noro 68: int GC_full_freq = 19; /* Every 20th collection is a full */
69: /* collection, whether we need it */
70: /* or not. */
71:
72: GC_bool GC_need_full_gc = FALSE;
73: /* Need full GC do to heap growth. */
74:
1.9 noro 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
81:
1.2 noro 82: word GC_used_heap_size_after_full = 0;
1.1 noro 83:
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. ",
1.6 noro 88: "Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved. ",
1.1 noro 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." };
92:
93: # include "version.h"
94:
95: /* some more variables */
96:
97: extern signed_word GC_mem_found; /* Number of reclaimed longwords */
98: /* after garbage collection */
99:
100: GC_bool GC_dont_expand = 0;
101:
102: word GC_free_space_divisor = 3;
1.7 noro 103: word GC_free_space_numerator = 1;
1.1 noro 104:
105: extern GC_bool GC_collection_in_progress();
106: /* Collection is in progress, or was abandoned. */
107:
1.6 noro 108: extern GC_bool GC_print_back_height;
109:
1.1 noro 110: int GC_never_stop_func GC_PROTO((void)) { return(0); }
111:
1.6 noro 112: unsigned long GC_time_limit = TIME_LIMIT;
113:
1.1 noro 114: CLOCK_TYPE GC_start_time; /* Time at which we stopped world. */
115: /* used only in GC_timeout_stop_func. */
116:
117: int GC_n_attempts = 0; /* Number of attempts at finishing */
1.6 noro 118: /* collection within GC_time_limit. */
1.1 noro 119:
1.6 noro 120: #if defined(SMALL_CONFIG) || defined(NO_CLOCK)
1.1 noro 121: # define GC_timeout_stop_func GC_never_stop_func
122: #else
123: int GC_timeout_stop_func GC_PROTO((void))
124: {
125: CLOCK_TYPE current_time;
126: static unsigned count = 0;
127: unsigned long time_diff;
128:
129: if ((count++ & 3) != 0) return(0);
130: GET_TIME(current_time);
131: time_diff = MS_TIME_DIFF(current_time,GC_start_time);
1.6 noro 132: if (time_diff >= GC_time_limit) {
1.4 noro 133: # ifdef CONDPRINT
134: if (GC_print_stats) {
1.1 noro 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);
1.4 noro 138: }
1.1 noro 139: # endif
140: return(1);
141: }
142: return(0);
143: }
144: #endif /* !SMALL_CONFIG */
145:
146: /* Return the minimum number of words that must be allocated between */
147: /* collections to amortize the collection cost. */
148: static word min_words_allocd()
149: {
150: # ifdef THREADS
151: /* We punt, for now. */
152: register signed_word stack_size = 10000;
153: # else
154: int dummy;
155: register signed_word stack_size = (ptr_t)(&dummy) - GC_stackbottom;
156: # endif
157: word total_root_size; /* includes double stack size, */
158: /* since the stack is expensive */
159: /* to scan. */
160: word scan_size; /* Estimate of memory to be scanned */
161: /* during normal GC. */
162:
163: if (stack_size < 0) stack_size = -stack_size;
164: total_root_size = 2 * stack_size + GC_root_size;
165: scan_size = BYTES_TO_WORDS(GC_heapsize - GC_large_free_bytes
166: + (GC_large_free_bytes >> 2)
167: /* use a bit more of large empty heap */
168: + total_root_size);
1.6 noro 169: if (TRUE_INCREMENTAL) {
1.7 noro 170: return scan_size * GC_free_space_numerator / (2 * GC_free_space_divisor);
1.1 noro 171: } else {
1.7 noro 172: return scan_size * GC_free_space_numerator / GC_free_space_divisor;
1.1 noro 173: }
174: }
175:
176: /* Return the number of words allocated, adjusted for explicit storage */
177: /* management, etc.. This number is used in deciding when to trigger */
178: /* collections. */
179: word GC_adj_words_allocd()
180: {
181: register signed_word result;
182: register signed_word expl_managed =
183: BYTES_TO_WORDS((long)GC_non_gc_bytes
184: - (long)GC_non_gc_bytes_at_gc);
185:
186: /* Don't count what was explicitly freed, or newly allocated for */
187: /* explicit management. Note that deallocating an explicitly */
188: /* managed object should not alter result, assuming the client */
189: /* is playing by the rules. */
190: result = (signed_word)GC_words_allocd
1.6 noro 191: - (signed_word)GC_mem_freed
192: + (signed_word)GC_finalizer_mem_freed - expl_managed;
1.1 noro 193: if (result > (signed_word)GC_words_allocd) {
194: result = GC_words_allocd;
195: /* probably client bug or unfortunate scheduling */
196: }
197: result += GC_words_finalized;
198: /* We count objects enqueued for finalization as though they */
199: /* had been reallocated this round. Finalization is user */
200: /* visible progress. And if we don't count this, we have */
201: /* stability problems for programs that finalize all objects. */
202: result += GC_words_wasted;
203: /* This doesn't reflect useful work. But if there is lots of */
204: /* new fragmentation, the same is probably true of the heap, */
205: /* and the collection will be correspondingly cheaper. */
206: if (result < (signed_word)(GC_words_allocd >> 3)) {
207: /* Always count at least 1/8 of the allocations. We don't want */
208: /* to collect too infrequently, since that would inhibit */
209: /* coalescing of free storage blocks. */
210: /* This also makes us partially robust against client bugs. */
211: return(GC_words_allocd >> 3);
212: } else {
213: return(result);
214: }
215: }
216:
217:
218: /* Clear up a few frames worth of garbage left at the top of the stack. */
219: /* This is used to prevent us from accidentally treating garbade left */
220: /* on the stack by other parts of the collector as roots. This */
221: /* differs from the code in misc.c, which actually tries to keep the */
222: /* stack clear of long-lived, client-generated garbage. */
223: void GC_clear_a_few_frames()
224: {
225: # define NWORDS 64
226: word frames[NWORDS];
227: register int i;
228:
229: for (i = 0; i < NWORDS; i++) frames[i] = 0;
230: }
231:
232: /* Have we allocated enough to amortize a collection? */
233: GC_bool GC_should_collect()
234: {
235: return(GC_adj_words_allocd() >= min_words_allocd());
236: }
237:
1.2 noro 238:
1.1 noro 239: void GC_notify_full_gc()
240: {
1.4 noro 241: if (GC_start_call_back != (void (*) GC_PROTO((void)))0) {
1.1 noro 242: (*GC_start_call_back)();
243: }
244: }
245:
1.2 noro 246: GC_bool GC_is_full_gc = FALSE;
247:
1.1 noro 248: /*
249: * Initiate a garbage collection if appropriate.
250: * Choose judiciously
251: * between partial, full, and stop-world collections.
252: * Assumes lock held, signals disabled.
253: */
254: void GC_maybe_gc()
255: {
256: static int n_partial_gcs = 0;
257:
258: if (GC_should_collect()) {
259: if (!GC_incremental) {
260: GC_gcollect_inner();
261: n_partial_gcs = 0;
262: return;
1.6 noro 263: } else {
264: # ifdef PARALLEL_MARK
265: GC_wait_for_reclaim();
266: # endif
267: if (GC_need_full_gc || n_partial_gcs >= GC_full_freq) {
1.4 noro 268: # ifdef CONDPRINT
269: if (GC_print_stats) {
270: GC_printf2(
271: "***>Full mark for collection %lu after %ld allocd bytes\n",
272: (unsigned long) GC_gc_no+1,
273: (long)WORDS_TO_BYTES(GC_words_allocd));
274: }
1.1 noro 275: # endif
276: GC_promote_black_lists();
277: (void)GC_reclaim_all((GC_stop_func)0, TRUE);
278: GC_clear_marks();
279: n_partial_gcs = 0;
280: GC_notify_full_gc();
1.2 noro 281: GC_is_full_gc = TRUE;
1.6 noro 282: } else {
1.1 noro 283: n_partial_gcs++;
1.6 noro 284: }
285: }
1.1 noro 286: /* We try to mark with the world stopped. */
287: /* If we run out of time, this turns into */
288: /* incremental marking. */
1.6 noro 289: # ifndef NO_CLOCK
290: if (GC_time_limit != GC_TIME_UNLIMITED) { GET_TIME(GC_start_time); }
291: # endif
292: if (GC_stopped_mark(GC_time_limit == GC_TIME_UNLIMITED?
293: GC_never_stop_func : GC_timeout_stop_func)) {
1.1 noro 294: # ifdef SAVE_CALL_CHAIN
295: GC_save_callers(GC_last_stack);
296: # endif
297: GC_finish_collection();
298: } else {
1.2 noro 299: if (!GC_is_full_gc) {
1.1 noro 300: /* Count this as the first attempt */
301: GC_n_attempts++;
302: }
303: }
304: }
305: }
306:
307:
308: /*
309: * Stop the world garbage collection. Assumes lock held, signals disabled.
310: * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
1.9 noro 311: * Return TRUE if we successfully completed the collection.
1.1 noro 312: */
313: GC_bool GC_try_to_collect_inner(stop_func)
314: GC_stop_func stop_func;
315: {
1.6 noro 316: # ifdef CONDPRINT
317: CLOCK_TYPE start_time, current_time;
318: # endif
1.9 noro 319: if (GC_dont_gc) return FALSE;
1.1 noro 320: if (GC_incremental && GC_collection_in_progress()) {
1.4 noro 321: # ifdef CONDPRINT
322: if (GC_print_stats) {
1.1 noro 323: GC_printf0(
324: "GC_try_to_collect_inner: finishing collection in progress\n");
1.4 noro 325: }
326: # endif /* CONDPRINT */
1.1 noro 327: /* Just finish collection already in progress. */
328: while(GC_collection_in_progress()) {
329: if (stop_func()) return(FALSE);
330: GC_collect_a_little_inner(1);
331: }
332: }
1.9 noro 333: if (stop_func == GC_never_stop_func) GC_notify_full_gc();
1.4 noro 334: # ifdef CONDPRINT
335: if (GC_print_stats) {
1.6 noro 336: if (GC_print_stats) GET_TIME(start_time);
1.1 noro 337: GC_printf2(
338: "Initiating full world-stop collection %lu after %ld allocd bytes\n",
339: (unsigned long) GC_gc_no+1,
340: (long)WORDS_TO_BYTES(GC_words_allocd));
1.4 noro 341: }
1.1 noro 342: # endif
343: GC_promote_black_lists();
344: /* Make sure all blocks have been reclaimed, so sweep routines */
345: /* don't see cleared mark bits. */
346: /* If we're guaranteed to finish, then this is unnecessary. */
1.3 noro 347: /* In the find_leak case, we have to finish to guarantee that */
348: /* previously unmarked objects are not reported as leaks. */
1.4 noro 349: # ifdef PARALLEL_MARK
350: GC_wait_for_reclaim();
351: # endif
352: if ((GC_find_leak || stop_func != GC_never_stop_func)
1.1 noro 353: && !GC_reclaim_all(stop_func, FALSE)) {
354: /* Aborted. So far everything is still consistent. */
355: return(FALSE);
356: }
357: GC_invalidate_mark_state(); /* Flush mark stack. */
358: GC_clear_marks();
359: # ifdef SAVE_CALL_CHAIN
360: GC_save_callers(GC_last_stack);
361: # endif
1.2 noro 362: GC_is_full_gc = TRUE;
1.1 noro 363: if (!GC_stopped_mark(stop_func)) {
364: if (!GC_incremental) {
365: /* We're partially done and have no way to complete or use */
366: /* current work. Reestablish invariants as cheaply as */
367: /* possible. */
368: GC_invalidate_mark_state();
369: GC_unpromote_black_lists();
370: } /* else we claim the world is already still consistent. We'll */
371: /* finish incrementally. */
372: return(FALSE);
373: }
374: GC_finish_collection();
1.6 noro 375: # if defined(CONDPRINT)
376: if (GC_print_stats) {
377: GET_TIME(current_time);
378: GC_printf1("Complete collection took %lu msecs\n",
379: MS_TIME_DIFF(current_time,start_time));
380: }
381: # endif
1.1 noro 382: return(TRUE);
383: }
384:
385:
386:
387: /*
388: * Perform n units of garbage collection work. A unit is intended to touch
389: * roughly GC_RATE pages. Every once in a while, we do more than that.
390: * This needa to be a fairly large number with our current incremental
391: * GC strategy, since otherwise we allocate too much during GC, and the
392: * cleanup gets expensive.
393: */
394: # define GC_RATE 10
395: # define MAX_PRIOR_ATTEMPTS 1
396: /* Maximum number of prior attempts at world stop marking */
1.6 noro 397: /* A value of 1 means that we finish the second time, no matter */
1.1 noro 398: /* how long it takes. Doesn't count the initial root scan */
399: /* for a full GC. */
400:
401: int GC_deficit = 0; /* The number of extra calls to GC_mark_some */
402: /* that we have made. */
403:
404: void GC_collect_a_little_inner(n)
405: int n;
406: {
407: register int i;
408:
1.9 noro 409: if (GC_dont_gc) return;
1.1 noro 410: if (GC_incremental && GC_collection_in_progress()) {
411: for (i = GC_deficit; i < GC_RATE*n; i++) {
412: if (GC_mark_some((ptr_t)0)) {
413: /* Need to finish a collection */
414: # ifdef SAVE_CALL_CHAIN
415: GC_save_callers(GC_last_stack);
416: # endif
1.6 noro 417: # ifdef PARALLEL_MARK
418: GC_wait_for_reclaim();
419: # endif
420: if (GC_n_attempts < MAX_PRIOR_ATTEMPTS
421: && GC_time_limit != GC_TIME_UNLIMITED) {
1.1 noro 422: GET_TIME(GC_start_time);
423: if (!GC_stopped_mark(GC_timeout_stop_func)) {
424: GC_n_attempts++;
425: break;
426: }
427: } else {
428: (void)GC_stopped_mark(GC_never_stop_func);
429: }
430: GC_finish_collection();
431: break;
432: }
433: }
434: if (GC_deficit > 0) GC_deficit -= GC_RATE*n;
435: if (GC_deficit < 0) GC_deficit = 0;
436: } else {
437: GC_maybe_gc();
438: }
439: }
440:
441: int GC_collect_a_little GC_PROTO(())
442: {
443: int result;
444: DCL_LOCK_STATE;
445:
446: DISABLE_SIGNALS();
447: LOCK();
448: GC_collect_a_little_inner(1);
449: result = (int)GC_collection_in_progress();
450: UNLOCK();
451: ENABLE_SIGNALS();
1.6 noro 452: if (!result && GC_debugging_started) GC_print_all_smashed();
1.1 noro 453: return(result);
454: }
455:
456: /*
457: * Assumes lock is held, signals are disabled.
458: * We stop the world.
459: * If stop_func() ever returns TRUE, we may fail and return FALSE.
460: * Increment GC_gc_no if we succeed.
461: */
462: GC_bool GC_stopped_mark(stop_func)
463: GC_stop_func stop_func;
464: {
465: register int i;
466: int dummy;
1.6 noro 467: # if defined(PRINTTIMES) || defined(CONDPRINT)
1.1 noro 468: CLOCK_TYPE start_time, current_time;
469: # endif
470:
1.4 noro 471: # ifdef PRINTTIMES
1.1 noro 472: GET_TIME(start_time);
1.4 noro 473: # endif
1.6 noro 474: # if defined(CONDPRINT) && !defined(PRINTTIMES)
475: if (GC_print_stats) GET_TIME(start_time);
476: # endif
1.9 noro 477: # if defined(REGISTER_LIBRARIES_EARLY)
478: GC_cond_register_dynamic_libraries();
479: # endif
1.7 noro 480: GC_timerstart();
1.6 noro 481: STOP_WORLD();
1.9 noro 482: IF_THREADS(GC_world_stopped = TRUE);
1.4 noro 483: # ifdef CONDPRINT
484: if (GC_print_stats) {
1.1 noro 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));
1.4 noro 490: }
1.1 noro 491: # endif
1.6 noro 492: # ifdef MAKE_BACK_GRAPH
493: if (GC_print_back_height) {
494: GC_build_back_graph();
495: }
496: # endif
1.1 noro 497:
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)()) {
1.4 noro 505: # ifdef CONDPRINT
506: if (GC_print_stats) {
1.1 noro 507: GC_printf0("Abandoned stopped marking after ");
508: GC_printf1("%lu iterations\n",
509: (unsigned long)i);
1.4 noro 510: }
1.1 noro 511: # endif
512: GC_deficit = i; /* Give the mutator a chance. */
1.9 noro 513: IF_THREADS(GC_world_stopped = FALSE);
1.1 noro 514: START_WORLD();
1.7 noro 515: GC_timerstop();
1.1 noro 516: return(FALSE);
517: }
518: if (GC_mark_some((ptr_t)(&dummy))) break;
519: }
520:
521: GC_gc_no++;
522: # ifdef PRINTSTATS
523: GC_printf2("Collection %lu reclaimed %ld bytes",
524: (unsigned long) GC_gc_no - 1,
525: (long)WORDS_TO_BYTES(GC_mem_found));
1.4 noro 526: # else
527: # ifdef CONDPRINT
528: if (GC_print_stats) {
529: GC_printf1("Collection %lu finished", (unsigned long) GC_gc_no - 1);
530: }
531: # endif
532: # endif /* !PRINTSTATS */
533: # ifdef CONDPRINT
534: if (GC_print_stats) {
535: GC_printf1(" ---> heapsize = %lu bytes\n",
536: (unsigned long) GC_heapsize);
537: /* Printf arguments may be pushed in funny places. Clear the */
538: /* space. */
539: GC_printf0("");
540: }
541: # endif /* CONDPRINT */
1.1 noro 542:
543: /* Check all debugged objects for consistency */
544: if (GC_debugging_started) {
545: (*GC_check_heap)();
546: }
547:
1.9 noro 548: IF_THREADS(GC_world_stopped = FALSE);
1.6 noro 549: START_WORLD();
1.7 noro 550: GC_timerstop();
1.1 noro 551: # ifdef PRINTTIMES
552: GET_TIME(current_time);
553: GC_printf1("World-stopped marking took %lu msecs\n",
554: MS_TIME_DIFF(current_time,start_time));
1.6 noro 555: # else
556: # ifdef CONDPRINT
557: if (GC_print_stats) {
558: GET_TIME(current_time);
559: GC_printf1("World-stopped marking took %lu msecs\n",
560: MS_TIME_DIFF(current_time,start_time));
561: }
562: # endif
1.1 noro 563: # endif
564: return(TRUE);
565: }
566:
1.6 noro 567: /* Set all mark bits for the free list whose first entry is q */
568: #ifdef __STDC__
569: void GC_set_fl_marks(ptr_t q)
570: #else
571: void GC_set_fl_marks(q)
572: ptr_t q;
573: #endif
574: {
575: ptr_t p;
576: struct hblk * h, * last_h = 0;
577: hdr *hhdr;
578: int word_no;
579:
580: for (p = q; p != 0; p = obj_link(p)){
581: h = HBLKPTR(p);
582: if (h != last_h) {
583: last_h = h;
584: hhdr = HDR(h);
585: }
586: word_no = (((word *)p) - ((word *)h));
587: set_mark_bit_from_hdr(hhdr, word_no);
588: }
589: }
590:
591: /* Clear all mark bits for the free list whose first entry is q */
592: /* Decrement GC_mem_found by number of words on free list. */
593: #ifdef __STDC__
594: void GC_clear_fl_marks(ptr_t q)
595: #else
596: void GC_clear_fl_marks(q)
597: ptr_t q;
598: #endif
599: {
600: ptr_t p;
601: struct hblk * h, * last_h = 0;
602: hdr *hhdr;
603: int word_no;
604:
605: for (p = q; p != 0; p = obj_link(p)){
606: h = HBLKPTR(p);
607: if (h != last_h) {
608: last_h = h;
609: hhdr = HDR(h);
610: }
611: word_no = (((word *)p) - ((word *)h));
612: clear_mark_bit_from_hdr(hhdr, word_no);
613: # ifdef GATHERSTATS
614: GC_mem_found -= hhdr -> hb_sz;
615: # endif
616: }
617: }
1.1 noro 618:
619: /* Finish up a collection. Assumes lock is held, signals are disabled, */
620: /* but the world is otherwise running. */
621: void GC_finish_collection()
622: {
623: # ifdef PRINTTIMES
624: CLOCK_TYPE start_time;
625: CLOCK_TYPE finalize_time;
626: CLOCK_TYPE done_time;
627:
628: GET_TIME(start_time);
629: finalize_time = start_time;
630: # endif
1.7 noro 631: GC_timerstart();
1.1 noro 632:
633: # ifdef GATHERSTATS
634: GC_mem_found = 0;
635: # endif
1.4 noro 636: # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
637: if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
638: GC_print_address_map();
639: }
640: # endif
1.9 noro 641: COND_DUMP;
1.2 noro 642: if (GC_find_leak) {
1.1 noro 643: /* Mark all objects on the free list. All objects should be */
644: /* marked when we're done. */
645: {
646: register word size; /* current object size */
647: int kind;
1.6 noro 648: ptr_t q;
1.1 noro 649:
650: for (kind = 0; kind < GC_n_kinds; kind++) {
651: for (size = 1; size <= MAXOBJSZ; size++) {
1.6 noro 652: q = GC_obj_kinds[kind].ok_freelist[size];
653: if (q != 0) GC_set_fl_marks(q);
1.1 noro 654: }
655: }
656: }
657: GC_start_reclaim(TRUE);
1.2 noro 658: /* The above just checks; it doesn't really reclaim anything. */
659: }
660:
661: GC_finalize();
662: # ifdef STUBBORN_ALLOC
663: GC_clean_changing_list();
664: # endif
665:
666: # ifdef PRINTTIMES
667: GET_TIME(finalize_time);
668: # endif
1.1 noro 669:
1.6 noro 670: if (GC_print_back_height) {
671: # ifdef MAKE_BACK_GRAPH
672: GC_traverse_back_graph();
673: # else
674: # ifndef SMALL_CONFIG
675: GC_err_printf0("Back height not available: "
676: "Rebuild collector with -DMAKE_BACK_GRAPH\n");
677: # endif
678: # endif
679: }
680:
1.2 noro 681: /* Clear free list mark bits, in case they got accidentally marked */
1.6 noro 682: /* (or GC_find_leak is set and they were intentionally marked). */
1.2 noro 683: /* Also subtract memory remaining from GC_mem_found count. */
684: /* Note that composite objects on free list are cleared. */
685: /* Thus accidentally marking a free list is not a problem; only */
686: /* objects on the list itself will be marked, and that's fixed here. */
1.1 noro 687: {
688: register word size; /* current object size */
1.6 noro 689: register ptr_t q; /* pointer to current object */
1.1 noro 690: int kind;
691:
692: for (kind = 0; kind < GC_n_kinds; kind++) {
693: for (size = 1; size <= MAXOBJSZ; size++) {
1.6 noro 694: q = GC_obj_kinds[kind].ok_freelist[size];
695: if (q != 0) GC_clear_fl_marks(q);
1.1 noro 696: }
697: }
698: }
699:
700:
1.2 noro 701: # ifdef PRINTSTATS
1.1 noro 702: GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
703: (long)WORDS_TO_BYTES(GC_mem_found));
1.2 noro 704: # endif
1.1 noro 705: /* Reconstruct free lists to contain everything not marked */
1.2 noro 706: GC_start_reclaim(FALSE);
707: if (GC_is_full_gc) {
708: GC_used_heap_size_after_full = USED_HEAP_SIZE;
709: GC_need_full_gc = FALSE;
710: } else {
711: GC_need_full_gc =
712: BYTES_TO_WORDS(USED_HEAP_SIZE - GC_used_heap_size_after_full)
713: > min_words_allocd();
714: }
1.1 noro 715:
716: # ifdef PRINTSTATS
717: GC_printf2(
718: "Immediately reclaimed %ld bytes in heap of size %lu bytes",
719: (long)WORDS_TO_BYTES(GC_mem_found),
720: (unsigned long)GC_heapsize);
721: # ifdef USE_MUNMAP
722: GC_printf1("(%lu unmapped)", GC_unmapped_bytes);
723: # endif
724: GC_printf2(
725: "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
726: (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use),
727: (unsigned long)WORDS_TO_BYTES(GC_composite_in_use));
728: # endif
729:
730: GC_n_attempts = 0;
1.2 noro 731: GC_is_full_gc = FALSE;
1.1 noro 732: /* Reset or increment counters for next cycle */
733: GC_words_allocd_before_gc += GC_words_allocd;
734: GC_non_gc_bytes_at_gc = GC_non_gc_bytes;
735: GC_words_allocd = 0;
736: GC_words_wasted = 0;
737: GC_mem_freed = 0;
1.6 noro 738: GC_finalizer_mem_freed = 0;
1.1 noro 739:
740: # ifdef USE_MUNMAP
741: GC_unmap_old();
742: # endif
743: # ifdef PRINTTIMES
744: GET_TIME(done_time);
745: GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
746: MS_TIME_DIFF(finalize_time,start_time),
747: MS_TIME_DIFF(done_time,finalize_time));
748: # endif
1.7 noro 749: GC_timerstop();
1.1 noro 750: }
751:
752: /* Externally callable routine to invoke full, stop-world collection */
753: # if defined(__STDC__) || defined(__cplusplus)
754: int GC_try_to_collect(GC_stop_func stop_func)
755: # else
756: int GC_try_to_collect(stop_func)
757: GC_stop_func stop_func;
758: # endif
759: {
760: int result;
761: DCL_LOCK_STATE;
762:
1.6 noro 763: if (GC_debugging_started) GC_print_all_smashed();
1.1 noro 764: GC_INVOKE_FINALIZERS();
765: DISABLE_SIGNALS();
766: LOCK();
767: ENTER_GC();
768: if (!GC_is_initialized) GC_init_inner();
769: /* Minimize junk left in my registers */
770: GC_noop(0,0,0,0,0,0);
771: result = (int)GC_try_to_collect_inner(stop_func);
772: EXIT_GC();
773: UNLOCK();
774: ENABLE_SIGNALS();
1.6 noro 775: if(result) {
776: if (GC_debugging_started) GC_print_all_smashed();
777: GC_INVOKE_FINALIZERS();
778: }
1.1 noro 779: return(result);
780: }
781:
782: void GC_gcollect GC_PROTO(())
783: {
784: (void)GC_try_to_collect(GC_never_stop_func);
1.9 noro 785: if (GC_have_errors) GC_print_all_errors();
1.1 noro 786: }
787:
788: word GC_n_heap_sects = 0; /* Number of sections currently in heap. */
789:
790: /*
791: * Use the chunk of memory starting at p of size bytes as part of the heap.
792: * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
793: */
794: void GC_add_to_heap(p, bytes)
795: struct hblk *p;
796: word bytes;
797: {
798: word words;
799: hdr * phdr;
800:
801: if (GC_n_heap_sects >= MAX_HEAP_SECTS) {
802: ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
803: }
1.3 noro 804: phdr = GC_install_header(p);
805: if (0 == phdr) {
1.1 noro 806: /* This is extremely unlikely. Can't add it. This will */
807: /* almost certainly result in a 0 return from the allocator, */
808: /* which is entirely appropriate. */
809: return;
810: }
811: GC_heap_sects[GC_n_heap_sects].hs_start = (ptr_t)p;
812: GC_heap_sects[GC_n_heap_sects].hs_bytes = bytes;
813: GC_n_heap_sects++;
1.4 noro 814: words = BYTES_TO_WORDS(bytes);
1.1 noro 815: phdr -> hb_sz = words;
1.4 noro 816: phdr -> hb_map = (unsigned char *)1; /* A value != GC_invalid_map */
1.1 noro 817: phdr -> hb_flags = 0;
818: GC_freehblk(p);
819: GC_heapsize += bytes;
1.4 noro 820: if ((ptr_t)p <= (ptr_t)GC_least_plausible_heap_addr
1.1 noro 821: || GC_least_plausible_heap_addr == 0) {
1.4 noro 822: GC_least_plausible_heap_addr = (GC_PTR)((ptr_t)p - sizeof(word));
1.1 noro 823: /* Making it a little smaller than necessary prevents */
824: /* us from getting a false hit from the variable */
825: /* itself. There's some unintentional reflection */
826: /* here. */
827: }
1.4 noro 828: if ((ptr_t)p + bytes >= (ptr_t)GC_greatest_plausible_heap_addr) {
829: GC_greatest_plausible_heap_addr = (GC_PTR)((ptr_t)p + bytes);
1.1 noro 830: }
831: }
832:
833: # if !defined(NO_DEBUGGING)
834: void GC_print_heap_sects()
835: {
836: register unsigned i;
837:
838: GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize);
839: for (i = 0; i < GC_n_heap_sects; i++) {
840: unsigned long start = (unsigned long) GC_heap_sects[i].hs_start;
841: unsigned long len = (unsigned long) GC_heap_sects[i].hs_bytes;
842: struct hblk *h;
843: unsigned nbl = 0;
844:
845: GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i,
846: start, (unsigned long)(start + len));
847: for (h = (struct hblk *)start; h < (struct hblk *)(start + len); h++) {
848: if (GC_is_black_listed(h, HBLKSIZE)) nbl++;
849: }
850: GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl,
851: (unsigned long)(len/HBLKSIZE));
852: }
853: }
854: # endif
855:
1.4 noro 856: GC_PTR GC_least_plausible_heap_addr = (GC_PTR)ONES;
857: GC_PTR GC_greatest_plausible_heap_addr = 0;
1.1 noro 858:
859: ptr_t GC_max(x,y)
860: ptr_t x, y;
861: {
862: return(x > y? x : y);
863: }
864:
865: ptr_t GC_min(x,y)
866: ptr_t x, y;
867: {
868: return(x < y? x : y);
869: }
870:
871: # if defined(__STDC__) || defined(__cplusplus)
872: void GC_set_max_heap_size(GC_word n)
873: # else
874: void GC_set_max_heap_size(n)
875: GC_word n;
876: # endif
877: {
878: GC_max_heapsize = n;
879: }
880:
881: GC_word GC_max_retries = 0;
882:
883: /*
884: * this explicitly increases the size of the heap. It is used
885: * internally, but may also be invoked from GC_expand_hp by the user.
886: * The argument is in units of HBLKSIZE.
887: * Tiny values of n are rounded up.
888: * Returns FALSE on failure.
889: */
890: GC_bool GC_expand_hp_inner(n)
891: word n;
892: {
893: word bytes;
894: struct hblk * space;
895: word expansion_slop; /* Number of bytes by which we expect the */
896: /* heap to expand soon. */
897:
898: if (n < MINHINCR) n = MINHINCR;
899: bytes = n * HBLKSIZE;
900: /* Make sure bytes is a multiple of GC_page_size */
901: {
902: word mask = GC_page_size - 1;
903: bytes += mask;
904: bytes &= ~mask;
905: }
906:
907: if (GC_max_heapsize != 0 && GC_heapsize + bytes > GC_max_heapsize) {
908: /* Exceeded self-imposed limit */
909: return(FALSE);
910: }
911: space = GET_MEM(bytes);
912: if( space == 0 ) {
1.4 noro 913: # ifdef CONDPRINT
914: if (GC_print_stats) {
915: GC_printf1("Failed to expand heap by %ld bytes\n",
916: (unsigned long)bytes);
917: }
918: # endif
1.1 noro 919: return(FALSE);
920: }
1.4 noro 921: # ifdef CONDPRINT
922: if (GC_print_stats) {
1.1 noro 923: GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
924: (unsigned long)bytes,
925: (unsigned long)WORDS_TO_BYTES(GC_words_allocd));
926: # ifdef UNDEFINED
927: GC_printf1("Root size = %lu\n", GC_root_size);
928: GC_print_block_list(); GC_print_hblkfreelist();
929: GC_printf0("\n");
930: # endif
1.4 noro 931: }
1.1 noro 932: # endif
933: expansion_slop = 8 * WORDS_TO_BYTES(min_words_allocd());
934: if (5 * HBLKSIZE * MAXHINCR > expansion_slop) {
935: expansion_slop = 5 * HBLKSIZE * MAXHINCR;
936: }
937: if (GC_last_heap_addr == 0 && !((word)space & SIGNB)
938: || GC_last_heap_addr != 0 && GC_last_heap_addr < (ptr_t)space) {
939: /* Assume the heap is growing up */
940: GC_greatest_plausible_heap_addr =
941: GC_max(GC_greatest_plausible_heap_addr,
942: (ptr_t)space + bytes + expansion_slop);
943: } else {
944: /* Heap is growing down */
945: GC_least_plausible_heap_addr =
946: GC_min(GC_least_plausible_heap_addr,
947: (ptr_t)space - expansion_slop);
948: }
949: GC_prev_heap_addr = GC_last_heap_addr;
950: GC_last_heap_addr = (ptr_t)space;
951: GC_add_to_heap(space, bytes);
1.10 ! ohara 952: #if defined(VISUAL)
1.8 noro 953: SendHeapSize();
954: #endif
1.1 noro 955: return(TRUE);
956: }
957:
958: /* Really returns a bool, but it's externally visible, so that's clumsy. */
959: /* Arguments is in bytes. */
960: # if defined(__STDC__) || defined(__cplusplus)
961: int GC_expand_hp(size_t bytes)
962: # else
963: int GC_expand_hp(bytes)
964: size_t bytes;
965: # endif
966: {
967: int result;
968: DCL_LOCK_STATE;
969:
970: DISABLE_SIGNALS();
971: LOCK();
972: if (!GC_is_initialized) GC_init_inner();
973: result = (int)GC_expand_hp_inner(divHBLKSZ((word)bytes));
1.3 noro 974: if (result) GC_requested_heapsize += bytes;
1.1 noro 975: UNLOCK();
976: ENABLE_SIGNALS();
977: return(result);
978: }
979:
980: unsigned GC_fail_count = 0;
981: /* How many consecutive GC/expansion failures? */
982: /* Reset by GC_allochblk. */
983:
984: GC_bool GC_collect_or_expand(needed_blocks, ignore_off_page)
985: word needed_blocks;
986: GC_bool ignore_off_page;
987: {
1.3 noro 988: if (!GC_incremental && !GC_dont_gc &&
989: (GC_dont_expand && GC_words_allocd > 0 || GC_should_collect())) {
1.1 noro 990: GC_gcollect_inner();
991: } else {
1.6 noro 992: word blocks_to_get = GC_heapsize/(HBLKSIZE*GC_free_space_divisor)
1.1 noro 993: + needed_blocks;
994:
995: if (blocks_to_get > MAXHINCR) {
996: word slop;
997:
998: if (ignore_off_page) {
999: slop = 4;
1000: } else {
1001: slop = 2*divHBLKSZ(BL_LIMIT);
1002: if (slop > needed_blocks) slop = needed_blocks;
1003: }
1004: if (needed_blocks + slop > MAXHINCR) {
1005: blocks_to_get = needed_blocks + slop;
1006: } else {
1007: blocks_to_get = MAXHINCR;
1008: }
1009: }
1010: if (!GC_expand_hp_inner(blocks_to_get)
1011: && !GC_expand_hp_inner(needed_blocks)) {
1012: if (GC_fail_count++ < GC_max_retries) {
1013: WARN("Out of Memory! Trying to continue ...\n", 0);
1014: GC_gcollect_inner();
1015: } else {
1.4 noro 1016: # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
1017: WARN("Out of Memory! Returning NIL!\n", 0);
1018: # endif
1.1 noro 1019: return(FALSE);
1020: }
1021: } else {
1.4 noro 1022: # ifdef CONDPRINT
1023: if (GC_fail_count && GC_print_stats) {
1.1 noro 1024: GC_printf0("Memory available again ...\n");
1025: }
1026: # endif
1027: }
1028: }
1029: return(TRUE);
1030: }
1031:
1032: /*
1033: * Make sure the object free list for sz is not empty.
1034: * Return a pointer to the first object on the free list.
1035: * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
1036: * Assumes we hold the allocator lock and signals are disabled.
1037: *
1038: */
1039: ptr_t GC_allocobj(sz, kind)
1040: word sz;
1041: int kind;
1042: {
1.6 noro 1043: ptr_t * flh = &(GC_obj_kinds[kind].ok_freelist[sz]);
1044: GC_bool tried_minor = FALSE;
1.1 noro 1045:
1046: if (sz == 0) return(0);
1047:
1.8 noro 1048: #if defined(VISUAL)
1049: {
1050: #include <signal.h>
1051: extern int recv_intr;
1052: if ( recv_intr ) {
1053: if ( recv_intr == 1 ) {
1054: recv_intr = 0;
1055: int_handler();
1056: } else {
1057: recv_intr = 0;
1058: ox_usr1_handler(0);
1059: }
1060: }
1061: }
1062: #endif
1.1 noro 1063: while (*flh == 0) {
1064: ENTER_GC();
1065: /* Do our share of marking work */
1.9 noro 1066: if(TRUE_INCREMENTAL) GC_collect_a_little_inner(1);
1.1 noro 1067: /* Sweep blocks for objects of this size */
1.6 noro 1068: GC_continue_reclaim(sz, kind);
1.1 noro 1069: EXIT_GC();
1070: if (*flh == 0) {
1071: GC_new_hblk(sz, kind);
1072: }
1073: if (*flh == 0) {
1074: ENTER_GC();
1.6 noro 1075: if (GC_incremental && GC_time_limit == GC_TIME_UNLIMITED
1076: && ! tried_minor ) {
1077: GC_collect_a_little_inner(1);
1078: tried_minor = TRUE;
1079: } else {
1080: if (!GC_collect_or_expand((word)1,FALSE)) {
1.1 noro 1081: EXIT_GC();
1082: return(0);
1.6 noro 1083: }
1.1 noro 1084: }
1085: EXIT_GC();
1086: }
1087: }
1.9 noro 1088: /* Successful allocation; reset failure count. */
1089: GC_fail_count = 0;
1.1 noro 1090:
1091: return(*flh);
1092: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>