1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
46 #include "libguile/_scm.h"
47 #include "libguile/pairs.h"
48 #include "libguile/gc.h"
49 #include "libguile/private-gc.h"
53 #define SCM_GC_CARD_BVEC_SIZE_IN_LONGS \
54 ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LONG_BITS - 1) / SCM_C_BVEC_LONG_BITS)
55 #define SCM_GC_IN_CARD_HEADERP(x) \
56 (scm_t_cell *) (x) < SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS
59 size_t scm_max_segment_size
;
62 scm_i_make_empty_heap_segment (scm_t_cell_type_statistics
*fl
)
64 scm_t_heap_segment
* shs
= malloc (sizeof (scm_t_heap_segment
));
68 fprintf (stderr
, "scm_i_get_new_heap_segment: out of memory.\n");
72 shs
->bounds
[0] = NULL
;
73 shs
->bounds
[1] = NULL
;
77 shs
->next_free_card
= NULL
;
84 Fill SEGMENT with memory both for data and mark bits.
86 RETURN: 1 on success, 0 failure
89 scm_i_initialize_heap_segment_data (scm_t_heap_segment
* segment
, size_t requested
)
94 int card_data_cell_count
= (SCM_GC_CARD_N_CELLS
- SCM_GC_CARD_N_HEADER_CELLS
);
95 int card_count
=1 + (requested
/ sizeof (scm_t_cell
)) / card_data_cell_count
;
98 one card extra due to alignment
100 size_t mem_needed
= (1+card_count
) * SCM_GC_SIZEOF_CARD
101 + SCM_GC_CARD_BVEC_SIZE_IN_LONGS
* card_count
* SCM_SIZEOF_LONG
103 scm_t_c_bvec_long
* bvec_ptr
= 0;
104 scm_t_cell
* memory
= 0;
107 We use malloc to alloc the heap. On GNU libc this is
108 equivalent to mmapping /dev/zero
110 SCM_SYSCALL (memory
= (scm_t_cell
* ) calloc (1, mem_needed
));
115 segment
->malloced
= memory
;
116 segment
->bounds
[0] = SCM_GC_CARD_UP (memory
);
117 segment
->bounds
[1] = segment
->bounds
[0] + card_count
* SCM_GC_CARD_N_CELLS
;
119 segment
->freelist
->heap_size
+= scm_i_segment_cell_count (segment
);
121 bvec_ptr
= (scm_t_c_bvec_long
*) segment
->bounds
[1];
125 scm_t_cell
* ptr
= segment
->bounds
[0];
128 ptr
< segment
->bounds
[1]; ptr
+= SCM_GC_CARD_N_CELLS
)
130 SCM_GC_CELL_BVEC (ptr
) = bvec_ptr
;
131 if (segment
->span
== 2)
132 SCM_GC_SET_CARD_DOUBLECELL (ptr
);
134 bvec_ptr
+= SCM_GC_CARD_BVEC_SIZE_IN_LONGS
;
137 Don't init the mem. This is handled by lazy sweeping.
142 segment
->next_free_card
= segment
->bounds
[0];
143 segment
->first_time
= 1;
148 scm_i_segment_card_count (scm_t_heap_segment
* seg
)
150 return (seg
->bounds
[1] - seg
->bounds
[0]) / SCM_GC_CARD_N_CELLS
;
154 Return the number of available single-cell data cells.
157 scm_i_segment_cell_count (scm_t_heap_segment
* seg
)
159 return scm_i_segment_card_count (seg
) * (SCM_GC_CARD_N_CELLS
- SCM_GC_CARD_N_HEADER_CELLS
)
160 + ((seg
->span
== 2) ? -1 : 0);
164 scm_i_clear_segment_mark_space (scm_t_heap_segment
*seg
)
166 scm_t_cell
* markspace
= seg
->bounds
[1];
168 memset (markspace
, 0x00,
169 scm_i_segment_card_count (seg
) * SCM_GC_CARD_BVEC_SIZE_IN_LONGS
* SCM_SIZEOF_LONG
);
178 scm_i_sweep_some_cards (scm_t_heap_segment
*seg
)
183 int (*sweeper
) (scm_t_cell
*, SCM
*, int )
184 = (seg
->first_time
) ? &scm_init_card_freelist
: &scm_i_sweep_card
;
186 scm_t_cell
* next_free
= seg
->next_free_card
;
189 while (collected
< threshold
&& next_free
< seg
->bounds
[1])
191 collected
+= (*sweeper
) (next_free
, &cells
, seg
->span
);
192 next_free
+= SCM_GC_CARD_N_CELLS
;
196 scm_gc_cells_swept
+= cards_swept
* (SCM_GC_CARD_N_CELLS
- SCM_GC_CARD_N_HEADER_CELLS
);
197 scm_gc_cells_collected
+= collected
* seg
->span
;
199 if (!seg
->first_time
)
200 scm_cells_allocated
-= collected
* seg
->span
;
202 seg
->freelist
->collected
+= collected
* seg
->span
;
205 if(next_free
== seg
->bounds
[1])
210 seg
->next_free_card
= next_free
;
216 Force a sweep of this entire segment. This doesn't modify sweep
217 statistics, it just frees the memory pointed to by to-be-swept
220 Implementation is slightly ugh.
222 FIXME: if you do scm_i_sweep_segment(), and then allocate from this
223 segment again, the statistics are off.
226 scm_i_sweep_segment (scm_t_heap_segment
* seg
)
228 scm_t_cell
* p
= seg
->next_free_card
;
229 int yield
= scm_gc_cells_collected
;
230 int coll
= seg
->freelist
->collected
;
231 unsigned long alloc
= scm_cells_allocated
;
233 while (scm_i_sweep_some_cards (seg
) != SCM_EOL
)
236 scm_gc_cells_collected
= yield
;
237 scm_cells_allocated
= alloc
;
238 seg
->freelist
->collected
= coll
;
240 seg
->next_free_card
=p
;
244 scm_i_sweep_all_segments (char const *reason
)
248 for (i
= 0; i
< scm_i_heap_segment_table_size
; i
++)
250 scm_i_sweep_segment (scm_i_heap_segment_table
[i
]);
258 The table is sorted by the address of the data itself. This makes
259 for easy lookups. This is not portable: according to ANSI C,
260 pointers can only be compared within the same object (i.e. the same
261 block of malloced memory.). For machines with weird architectures,
262 this should be revised.
264 (Apparently, for this reason 1.6 and earlier had macros for pointer
267 perhaps it is worthwhile to remove the 2nd level of indirection in
268 the table, but this certainly makes for cleaner code.
270 scm_t_heap_segment
** scm_i_heap_segment_table
;
271 size_t scm_i_heap_segment_table_size
;
272 scm_t_cell
*lowest_cell
;
273 scm_t_cell
*highest_cell
;
277 scm_i_clear_mark_space (void)
280 for (; i
< scm_i_heap_segment_table_size
; i
++)
282 scm_i_clear_segment_mark_space (scm_i_heap_segment_table
[i
]);
288 RETURN: index of inserted segment.
291 scm_i_insert_segment (scm_t_heap_segment
* seg
)
293 size_t size
= (scm_i_heap_segment_table_size
+ 1) * sizeof (scm_t_heap_segment
*);
294 SCM_SYSCALL(scm_i_heap_segment_table
= ((scm_t_heap_segment
**)
295 realloc ((char *)scm_i_heap_segment_table
, size
)));
298 We can't alloc 4 more bytes. This is hopeless.
300 if (!scm_i_heap_segment_table
)
302 fprintf (stderr
, "scm_i_get_new_heap_segment: Could not grow heap segment table.\n");
308 lowest_cell
= seg
->bounds
[0];
309 highest_cell
= seg
->bounds
[1];
313 lowest_cell
= SCM_MIN (lowest_cell
, seg
->bounds
[0]);
314 highest_cell
= SCM_MAX (highest_cell
, seg
->bounds
[1]);
322 while (i
< scm_i_heap_segment_table_size
323 && scm_i_heap_segment_table
[i
]->bounds
[0] <= seg
->bounds
[0])
325 for (j
= scm_i_heap_segment_table_size
; j
> i
; --j
)
326 scm_i_heap_segment_table
[j
] = scm_i_heap_segment_table
[j
- 1];
328 scm_i_heap_segment_table
[i
] = seg
;
329 scm_i_heap_segment_table_size
++;
336 scm_i_sweep_some_segments (scm_t_cell_type_statistics
* fl
)
338 int i
= fl
->heap_segment_idx
;
339 SCM collected
=SCM_EOL
;
345 i
< scm_i_heap_segment_table_size
; i
++)
347 if (scm_i_heap_segment_table
[i
]->freelist
!= fl
)
350 collected
= scm_i_sweep_some_cards (scm_i_heap_segment_table
[i
]);
353 if (collected
!= SCM_EOL
) /* Don't increment i */
357 fl
->heap_segment_idx
= i
;
366 scm_i_reset_segments (void)
369 for (; i
< scm_i_heap_segment_table_size
; i
++)
371 scm_t_heap_segment
* seg
= scm_i_heap_segment_table
[i
];
372 seg
->next_free_card
= seg
->bounds
[0];
378 Determine whether the given value does actually represent a cell in
379 some heap segment. If this is the case, the number of the heap
380 segment is returned. Otherwise, -1 is returned. Binary search is
381 used to determine the heap segment that contains the cell.
384 I think this function is too long to be inlined. --hwn
387 scm_i_find_heap_segment_containing_object (SCM obj
)
392 if ((scm_t_cell
* ) obj
< lowest_cell
|| (scm_t_cell
*) obj
>= highest_cell
)
397 scm_t_cell
* ptr
= SCM2PTR (obj
);
398 unsigned long int i
= 0;
399 unsigned long int j
= scm_i_heap_segment_table_size
- 1;
401 if (ptr
< scm_i_heap_segment_table
[i
]->bounds
[0])
403 else if (scm_i_heap_segment_table
[j
]->bounds
[1] <= ptr
)
409 if (ptr
< scm_i_heap_segment_table
[i
]->bounds
[1])
413 else if (scm_i_heap_segment_table
[j
]->bounds
[0] <= ptr
)
420 unsigned long int k
= (i
+ j
) / 2;
424 else if (ptr
< scm_i_heap_segment_table
[k
]->bounds
[1])
428 if (ptr
< scm_i_heap_segment_table
[i
]->bounds
[0])
431 else if (scm_i_heap_segment_table
[k
]->bounds
[0] <= ptr
)
435 if (scm_i_heap_segment_table
[j
]->bounds
[1] <= ptr
)
441 if (!DOUBLECELL_ALIGNED_P (obj
) && scm_i_heap_segment_table
[i
]->span
== 2)
443 else if (SCM_GC_IN_CARD_HEADERP (ptr
))
453 Important entry point: try to grab some memory, and make it into a
456 RETURN: the index of the segment.
459 scm_i_get_new_heap_segment (scm_t_cell_type_statistics
*freelist
, policy_on_error error_policy
)
463 if (scm_gc_heap_lock
)
465 /* Critical code sections (such as the garbage collector) aren't
466 * supposed to add heap segments.
468 fprintf (stderr
, "scm_i_get_new_heap_segment: Can not extend locked heap.\n");
473 /* Pick a size for the new heap segment.
474 * The rule for picking the size of a segment is explained in
478 /* Assure that the new segment is predicted to be large enough.
480 * New yield should at least equal GC fraction of new heap size, i.e.
482 * y + dh > f * (h + dh)
485 * f : min yield fraction
487 * dh : size of new heap segment
489 * This gives dh > (f * h - y) / (1 - f)
493 where is is this explanation supposed to be? --hwn
495 int f
= freelist
->min_yield_fraction
;
496 unsigned long h
= SCM_HEAP_SIZE
;
497 size_t min_cells
= (f
* h
- 100 * (long) scm_gc_cells_collected
) / (99 - f
);
499 /* Make heap grow with factor 1.5 */
500 len
= freelist
->heap_size
/ 2;
502 fprintf (stderr
, "(%ld < %ld)", (long) len
, (long) min_cells
);
506 Original code adds freelist->cluster_size here.
510 len
*= sizeof (scm_t_cell
);
511 /* force new sampling */
512 freelist
->collected
= LONG_MAX
;
515 if (len
< SCM_MIN_HEAP_SEG_SIZE
)
516 len
= SCM_MIN_HEAP_SEG_SIZE
;
519 scm_t_heap_segment
* seg
= scm_i_make_empty_heap_segment (freelist
);
521 /* Allocate with decaying ambition. */
522 while (len
>= SCM_MIN_HEAP_SEG_SIZE
)
524 if (scm_i_initialize_heap_segment_data (seg
, len
))
526 return scm_i_insert_segment (seg
);
533 if (error_policy
== abort_on_error
)
535 fprintf (stderr
, "scm_i_get_new_heap_segment: Could not grow heap.\n");
544 scm_i_make_initial_segment (size_t init_heap_size
, scm_t_cell_type_statistics
*freelist
)
546 scm_t_heap_segment
* seg
= scm_i_make_empty_heap_segment (freelist
);
548 if (scm_i_initialize_heap_segment_data (seg
, init_heap_size
))
550 freelist
->heap_segment_idx
= scm_i_insert_segment (seg
);
554 Why the fuck try twice? --hwn
558 scm_i_initialize_heap_segment_data (seg
, SCM_HEAP_SEG_SIZE
);
561 if (freelist
->min_yield_fraction
)
562 freelist
->min_yield
= (freelist
->heap_size
* freelist
->min_yield_fraction