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"
55 size_t scm_max_segment_size
;
58 scm_i_make_empty_heap_segment (scm_t_cell_type_statistics
*fl
)
60 scm_t_heap_segment
* shs
= malloc (sizeof (scm_t_heap_segment
));
64 fprintf (stderr
, "scm_i_get_new_heap_segment: out of memory.\n");
68 shs
->bounds
[0] = NULL
;
69 shs
->bounds
[1] = NULL
;
73 shs
->next_free_card
= NULL
;
80 Fill SEGMENT with memory both for data and mark bits.
82 RETURN: 1 on success, 0 failure
85 scm_i_initialize_heap_segment_data (scm_t_heap_segment
* segment
, size_t requested
)
90 int card_data_cell_count
= (SCM_GC_CARD_N_CELLS
- SCM_GC_CARD_N_HEADER_CELLS
);
91 int card_count
=1 + (requested
/ sizeof (scm_t_cell
)) / card_data_cell_count
;
94 one card extra due to alignment
96 size_t mem_needed
= (1+card_count
) * SCM_GC_SIZEOF_CARD
97 + SCM_GC_CARD_BVEC_SIZE_IN_LONGS
* card_count
* SCM_SIZEOF_LONG
99 scm_t_c_bvec_long
* bvec_ptr
= 0;
100 scm_t_cell
* memory
= 0;
103 We use malloc to alloc the heap. On GNU libc this is
104 equivalent to mmapping /dev/zero
106 SCM_SYSCALL (memory
= (scm_t_cell
* ) calloc (1, mem_needed
));
111 segment
->malloced
= memory
;
112 segment
->bounds
[0] = SCM_GC_CARD_UP (memory
);
113 segment
->bounds
[1] = segment
->bounds
[0] + card_count
* SCM_GC_CARD_N_CELLS
;
115 segment
->freelist
->heap_size
+= scm_i_segment_cell_count (segment
);
117 bvec_ptr
= (scm_t_c_bvec_long
*) segment
->bounds
[1];
120 Don't init the mem or the bitvector. This is handled by lazy
124 segment
->next_free_card
= segment
->bounds
[0];
125 segment
->first_time
= 1;
130 scm_i_segment_card_count (scm_t_heap_segment
* seg
)
132 return (seg
->bounds
[1] - seg
->bounds
[0]) / SCM_GC_CARD_N_CELLS
;
136 Return the number of available single-cell data cells.
139 scm_i_segment_cell_count (scm_t_heap_segment
* seg
)
141 return scm_i_segment_card_count (seg
) * (SCM_GC_CARD_N_CELLS
- SCM_GC_CARD_N_HEADER_CELLS
)
142 + ((seg
->span
== 2) ? -1 : 0);
146 scm_i_clear_segment_mark_space (scm_t_heap_segment
*seg
)
148 scm_t_cell
* markspace
= seg
->bounds
[1];
150 memset (markspace
, 0x00,
151 scm_i_segment_card_count (seg
) * SCM_GC_CARD_BVEC_SIZE_IN_LONGS
* SCM_SIZEOF_LONG
);
160 scm_i_sweep_some_cards (scm_t_heap_segment
*seg
)
165 int (*sweeper
) (scm_t_cell
*, SCM
*, scm_t_heap_segment
* )
166 = (seg
->first_time
) ? &scm_i_init_card_freelist
: &scm_i_sweep_card
;
168 scm_t_cell
* next_free
= seg
->next_free_card
;
171 while (collected
< threshold
&& next_free
< seg
->bounds
[1])
173 collected
+= (*sweeper
) (next_free
, &cells
, seg
);
174 next_free
+= SCM_GC_CARD_N_CELLS
;
178 scm_gc_cells_swept
+= cards_swept
* (SCM_GC_CARD_N_CELLS
- SCM_GC_CARD_N_HEADER_CELLS
);
179 scm_gc_cells_collected
+= collected
* seg
->span
;
181 if (!seg
->first_time
)
182 scm_cells_allocated
-= collected
* seg
->span
;
184 seg
->freelist
->collected
+= collected
* seg
->span
;
187 if(next_free
== seg
->bounds
[1])
192 seg
->next_free_card
= next_free
;
198 Force a sweep of this entire segment. This doesn't modify sweep
199 statistics, it just frees the memory pointed to by to-be-swept
202 Implementation is slightly ugh.
204 FIXME: if you do scm_i_sweep_segment(), and then allocate from this
205 segment again, the statistics are off.
208 scm_i_sweep_segment (scm_t_heap_segment
* seg
)
210 scm_t_cell
* p
= seg
->next_free_card
;
211 int yield
= scm_gc_cells_collected
;
212 int coll
= seg
->freelist
->collected
;
213 unsigned long alloc
= scm_cells_allocated
;
215 while (scm_i_sweep_some_cards (seg
) != SCM_EOL
)
218 scm_gc_cells_collected
= yield
;
219 scm_cells_allocated
= alloc
;
220 seg
->freelist
->collected
= coll
;
222 seg
->next_free_card
=p
;
226 scm_i_sweep_all_segments (char const *reason
)
230 for (i
= 0; i
< scm_i_heap_segment_table_size
; i
++)
232 scm_i_sweep_segment (scm_i_heap_segment_table
[i
]);
240 The table is sorted by the address of the data itself. This makes
241 for easy lookups. This is not portable: according to ANSI C,
242 pointers can only be compared within the same object (i.e. the same
243 block of malloced memory.). For machines with weird architectures,
244 this should be revised.
246 (Apparently, for this reason 1.6 and earlier had macros for pointer
249 perhaps it is worthwhile to remove the 2nd level of indirection in
250 the table, but this certainly makes for cleaner code.
252 scm_t_heap_segment
** scm_i_heap_segment_table
;
253 size_t scm_i_heap_segment_table_size
;
254 scm_t_cell
*lowest_cell
;
255 scm_t_cell
*highest_cell
;
259 scm_i_clear_mark_space (void)
262 for (; i
< scm_i_heap_segment_table_size
; i
++)
264 scm_i_clear_segment_mark_space (scm_i_heap_segment_table
[i
]);
270 RETURN: index of inserted segment.
273 scm_i_insert_segment (scm_t_heap_segment
* seg
)
275 size_t size
= (scm_i_heap_segment_table_size
+ 1) * sizeof (scm_t_heap_segment
*);
276 SCM_SYSCALL(scm_i_heap_segment_table
= ((scm_t_heap_segment
**)
277 realloc ((char *)scm_i_heap_segment_table
, size
)));
280 We can't alloc 4 more bytes. This is hopeless.
282 if (!scm_i_heap_segment_table
)
284 fprintf (stderr
, "scm_i_get_new_heap_segment: Could not grow heap segment table.\n");
290 lowest_cell
= seg
->bounds
[0];
291 highest_cell
= seg
->bounds
[1];
295 lowest_cell
= SCM_MIN (lowest_cell
, seg
->bounds
[0]);
296 highest_cell
= SCM_MAX (highest_cell
, seg
->bounds
[1]);
304 while (i
< scm_i_heap_segment_table_size
305 && scm_i_heap_segment_table
[i
]->bounds
[0] <= seg
->bounds
[0])
309 We insert a new entry; if that happens to be before the
310 "current" segment of a freelist, we must move the freelist index
313 if (scm_i_master_freelist
.heap_segment_idx
>= i
)
314 scm_i_master_freelist
.heap_segment_idx
++;
315 if (scm_i_master_freelist2
.heap_segment_idx
>= i
)
316 scm_i_master_freelist2
.heap_segment_idx
++;
318 for (j
= scm_i_heap_segment_table_size
; j
> i
; --j
)
319 scm_i_heap_segment_table
[j
] = scm_i_heap_segment_table
[j
- 1];
321 scm_i_heap_segment_table
[i
] = seg
;
322 scm_i_heap_segment_table_size
++;
329 scm_i_sweep_some_segments (scm_t_cell_type_statistics
* fl
)
331 int i
= fl
->heap_segment_idx
;
332 SCM collected
=SCM_EOL
;
338 i
< scm_i_heap_segment_table_size
; i
++)
340 if (scm_i_heap_segment_table
[i
]->freelist
!= fl
)
343 collected
= scm_i_sweep_some_cards (scm_i_heap_segment_table
[i
]);
346 if (collected
!= SCM_EOL
) /* Don't increment i */
350 fl
->heap_segment_idx
= i
;
359 scm_i_reset_segments (void)
362 for (; i
< scm_i_heap_segment_table_size
; i
++)
364 scm_t_heap_segment
* seg
= scm_i_heap_segment_table
[i
];
365 seg
->next_free_card
= seg
->bounds
[0];
371 Determine whether the given value does actually represent a cell in
372 some heap segment. If this is the case, the number of the heap
373 segment is returned. Otherwise, -1 is returned. Binary search is
374 used to determine the heap segment that contains the cell.
377 I think this function is too long to be inlined. --hwn
380 scm_i_find_heap_segment_containing_object (SCM obj
)
385 if ((scm_t_cell
* ) obj
< lowest_cell
|| (scm_t_cell
*) obj
>= highest_cell
)
390 scm_t_cell
* ptr
= SCM2PTR (obj
);
391 unsigned long int i
= 0;
392 unsigned long int j
= scm_i_heap_segment_table_size
- 1;
394 if (ptr
< scm_i_heap_segment_table
[i
]->bounds
[0])
396 else if (scm_i_heap_segment_table
[j
]->bounds
[1] <= ptr
)
402 if (ptr
< scm_i_heap_segment_table
[i
]->bounds
[1])
406 else if (scm_i_heap_segment_table
[j
]->bounds
[0] <= ptr
)
413 unsigned long int k
= (i
+ j
) / 2;
417 else if (ptr
< scm_i_heap_segment_table
[k
]->bounds
[1])
421 if (ptr
< scm_i_heap_segment_table
[i
]->bounds
[0])
424 else if (scm_i_heap_segment_table
[k
]->bounds
[0] <= ptr
)
428 if (scm_i_heap_segment_table
[j
]->bounds
[1] <= ptr
)
434 if (!SCM_DOUBLECELL_ALIGNED_P (obj
) && scm_i_heap_segment_table
[i
]->span
== 2)
436 else if (SCM_GC_IN_CARD_HEADERP (ptr
))
446 Important entry point: try to grab some memory, and make it into a
449 RETURN: the index of the segment.
452 scm_i_get_new_heap_segment (scm_t_cell_type_statistics
*freelist
, policy_on_error error_policy
)
456 if (scm_gc_heap_lock
)
458 /* Critical code sections (such as the garbage collector) aren't
459 * supposed to add heap segments.
461 fprintf (stderr
, "scm_i_get_new_heap_segment: Can not extend locked heap.\n");
466 /* Assure that the new segment is predicted to be large enough.
468 * New yield should at least equal GC fraction of new heap size, i.e.
470 * y + dh > f * (h + dh)
473 * f : min yield fraction
475 * dh : size of new heap segment
477 * This gives dh > (f * h - y) / (1 - f)
479 float f
= freelist
->min_yield_fraction
/ 100.0;
480 float h
= SCM_HEAP_SIZE
;
482 = (f
* h
- scm_gc_cells_collected
) / (1.0 - f
);
484 /* Make heap grow with factor 1.5 */
485 len
= freelist
->heap_size
/ 2;
487 fprintf (stderr
, "(%ld < %ld)", (long) len
, (long) min_cells
);
491 len
= (unsigned long) min_cells
;
492 len
*= sizeof (scm_t_cell
);
493 /* force new sampling */
494 freelist
->collected
= LONG_MAX
;
497 if (len
< SCM_MIN_HEAP_SEG_SIZE
)
498 len
= SCM_MIN_HEAP_SEG_SIZE
;
501 scm_t_heap_segment
* seg
= scm_i_make_empty_heap_segment (freelist
);
503 /* Allocate with decaying ambition. */
504 while (len
>= SCM_MIN_HEAP_SEG_SIZE
)
506 if (scm_i_initialize_heap_segment_data (seg
, len
))
508 return scm_i_insert_segment (seg
);
515 if (error_policy
== abort_on_error
)
517 fprintf (stderr
, "scm_i_get_new_heap_segment: Could not grow heap.\n");
524 scm_i_make_initial_segment (int init_heap_size
, scm_t_cell_type_statistics
*freelist
)
526 scm_t_heap_segment
* seg
= scm_i_make_empty_heap_segment (freelist
);
528 if (init_heap_size
< 1)
530 init_heap_size
= SCM_DEFAULT_INIT_HEAP_SIZE_1
;
533 if (scm_i_initialize_heap_segment_data (seg
, init_heap_size
))
535 freelist
->heap_segment_idx
= scm_i_insert_segment (seg
);
539 Why the fuck try twice? --hwn
543 scm_i_initialize_heap_segment_data (seg
, SCM_HEAP_SEG_SIZE
);
546 if (freelist
->min_yield_fraction
)
547 freelist
->min_yield
= (freelist
->heap_size
* freelist
->min_yield_fraction