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