Commit | Line | Data |
---|---|---|
c7743d02 HWN |
1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002 Free Software Foundation, Inc. |
2 | * | |
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) | |
6 | * any later version. | |
7 | * | |
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. | |
12 | * | |
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 | |
17 | * | |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
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. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
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. | |
37 | * | |
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. */ | |
41 | ||
42 | #include <assert.h> | |
43 | #include <stdio.h> | |
44 | #include <string.h> | |
45 | ||
46 | #include "libguile/_scm.h" | |
47 | #include "libguile/pairs.h" | |
48 | #include "libguile/gc.h" | |
49 | #include "libguile/private-gc.h" | |
50 | ||
51 | ||
52 | ||
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 | |
57 | ||
58 | ||
59 | size_t scm_max_segment_size; | |
60 | ||
61 | scm_t_heap_segment * | |
62 | scm_i_make_empty_heap_segment (scm_t_cell_type_statistics *fl) | |
63 | { | |
64 | scm_t_heap_segment * shs = malloc (sizeof (scm_t_heap_segment)); | |
65 | ||
66 | if (!shs) | |
67 | { | |
68 | fprintf (stderr, "scm_i_get_new_heap_segment: out of memory.\n"); | |
69 | abort (); | |
70 | } | |
71 | ||
72 | shs->bounds[0] = NULL; | |
73 | shs->bounds[1] = NULL; | |
74 | shs->malloced = NULL; | |
75 | shs->span = fl->span; | |
76 | shs->freelist = fl; | |
77 | shs->next_free_card = NULL; | |
78 | ||
79 | return shs; | |
80 | } | |
81 | ||
82 | ||
83 | /* | |
84 | Fill SEGMENT with memory both for data and mark bits. | |
85 | ||
86 | RETURN: 1 on success, 0 failure | |
87 | */ | |
88 | int | |
89 | scm_i_initialize_heap_segment_data (scm_t_heap_segment * segment, size_t requested) | |
90 | { | |
91 | /* | |
92 | round upwards | |
93 | */ | |
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; | |
96 | ||
97 | /* | |
98 | one card extra due to alignment | |
99 | */ | |
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 | |
102 | ; | |
103 | scm_t_c_bvec_long * bvec_ptr = 0; | |
104 | scm_t_cell * memory = 0; | |
105 | ||
106 | /* | |
107 | We use malloc to alloc the heap. On GNU libc this is | |
108 | equivalent to mmapping /dev/zero | |
109 | */ | |
110 | SCM_SYSCALL (memory = (scm_t_cell * ) calloc (1, mem_needed)); | |
111 | ||
112 | if (memory == NULL) | |
113 | return 0; | |
114 | ||
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; | |
118 | ||
119 | segment->freelist->heap_size += scm_i_segment_cell_count (segment); | |
120 | ||
121 | bvec_ptr = (scm_t_c_bvec_long*) segment->bounds[1]; | |
122 | ||
123 | ||
124 | { | |
125 | scm_t_cell * ptr = segment->bounds [0]; | |
126 | ||
127 | for (; | |
128 | ptr < segment->bounds[1]; ptr += SCM_GC_CARD_N_CELLS) | |
129 | { | |
130 | SCM_GC_CELL_BVEC (ptr) = bvec_ptr; | |
131 | if (segment->span == 2) | |
132 | SCM_GC_SET_CARD_DOUBLECELL (ptr); | |
133 | ||
134 | bvec_ptr += SCM_GC_CARD_BVEC_SIZE_IN_LONGS; | |
135 | ||
136 | /* | |
137 | Don't init the mem. This is handled by lazy sweeping. | |
138 | */ | |
139 | } | |
140 | } | |
141 | ||
142 | segment->next_free_card = segment->bounds[0]; | |
143 | segment->first_time = 1; | |
144 | return 1; | |
145 | } | |
146 | ||
147 | int | |
148 | scm_i_segment_card_count (scm_t_heap_segment * seg) | |
149 | { | |
150 | return (seg->bounds[1] - seg->bounds[0]) / SCM_GC_CARD_N_CELLS; | |
151 | } | |
152 | ||
153 | /* | |
154 | Return the number of available single-cell data cells. | |
155 | */ | |
156 | int | |
157 | scm_i_segment_cell_count (scm_t_heap_segment * seg) | |
158 | { | |
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); | |
161 | } | |
162 | ||
163 | void | |
164 | scm_i_clear_segment_mark_space (scm_t_heap_segment *seg) | |
165 | { | |
166 | scm_t_cell * markspace = seg->bounds[1]; | |
167 | ||
168 | memset (markspace, 0x00, | |
169 | scm_i_segment_card_count (seg) * SCM_GC_CARD_BVEC_SIZE_IN_LONGS * SCM_SIZEOF_LONG); | |
170 | } | |
171 | ||
172 | /* | |
173 | RETURN: | |
174 | ||
175 | Freelist. | |
176 | */ | |
177 | SCM | |
178 | scm_i_sweep_some_cards (scm_t_heap_segment *seg) | |
179 | { | |
180 | SCM cells = SCM_EOL; | |
181 | int threshold = 512; | |
182 | int collected = 0; | |
183 | int (*sweeper) (scm_t_cell *, SCM *, int ) | |
184 | = (seg->first_time) ? &scm_init_card_freelist : &scm_i_sweep_card; | |
185 | ||
186 | scm_t_cell * next_free = seg->next_free_card; | |
187 | int cards_swept = 0; | |
188 | ||
189 | while (collected < threshold && next_free < seg->bounds[1]) | |
190 | { | |
191 | collected += (*sweeper) (next_free, &cells, seg->span); | |
192 | next_free += SCM_GC_CARD_N_CELLS; | |
193 | cards_swept ++; | |
194 | } | |
195 | ||
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; | |
c2cbcc57 HWN |
198 | |
199 | if (!seg->first_time) | |
200 | scm_cells_allocated -= collected * seg->span; | |
201 | ||
c7743d02 | 202 | seg->freelist->collected += collected * seg->span; |
c2cbcc57 | 203 | |
c7743d02 HWN |
204 | |
205 | if(next_free == seg->bounds[1]) | |
206 | { | |
207 | seg->first_time = 0; | |
208 | } | |
209 | ||
210 | seg->next_free_card = next_free; | |
211 | return cells; | |
212 | } | |
213 | ||
214 | ||
215 | /* | |
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 | |
218 | cells. | |
219 | ||
c2cbcc57 HWN |
220 | Implementation is slightly ugh. |
221 | ||
222 | FIXME: if you do scm_i_sweep_segment(), and then allocate from this | |
223 | segment again, the statistics are off. | |
c7743d02 HWN |
224 | */ |
225 | void | |
226 | scm_i_sweep_segment (scm_t_heap_segment * seg) | |
227 | { | |
228 | scm_t_cell * p = seg->next_free_card; | |
229 | int yield = scm_gc_cells_collected; | |
230 | int coll = seg->freelist->collected; | |
f2893a25 | 231 | unsigned long alloc = scm_cells_allocated ; |
c2cbcc57 | 232 | |
c7743d02 HWN |
233 | while (scm_i_sweep_some_cards (seg) != SCM_EOL) |
234 | ; | |
235 | ||
236 | scm_gc_cells_collected = yield; | |
c2cbcc57 | 237 | scm_cells_allocated = alloc; |
c7743d02 HWN |
238 | seg->freelist->collected = coll; |
239 | ||
240 | seg->next_free_card =p; | |
241 | } | |
242 | ||
243 | void | |
244 | scm_i_sweep_all_segments (char const *reason) | |
245 | { | |
246 | int i= 0; | |
247 | ||
248 | for (i = 0; i < scm_i_heap_segment_table_size; i++) | |
249 | { | |
250 | scm_i_sweep_segment (scm_i_heap_segment_table[i]); | |
251 | } | |
252 | } | |
253 | ||
254 | ||
255 | /* | |
256 | Heap segment table. | |
257 | ||
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. | |
263 | ||
264 | (Apparently, for this reason 1.6 and earlier had macros for pointer | |
265 | comparison. ) | |
266 | ||
267 | perhaps it is worthwhile to remove the 2nd level of indirection in | |
268 | the table, but this certainly makes for cleaner code. | |
269 | */ | |
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; | |
274 | ||
275 | ||
276 | void | |
277 | scm_i_clear_mark_space (void) | |
278 | { | |
279 | int i = 0; | |
280 | for (; i < scm_i_heap_segment_table_size; i++) | |
281 | { | |
282 | scm_i_clear_segment_mark_space (scm_i_heap_segment_table[i]); | |
283 | } | |
284 | } | |
285 | ||
286 | ||
287 | /* | |
288 | RETURN: index of inserted segment. | |
289 | */ | |
290 | int | |
291 | scm_i_insert_segment (scm_t_heap_segment * seg) | |
292 | { | |
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))); | |
296 | ||
297 | /* | |
298 | We can't alloc 4 more bytes. This is hopeless. | |
299 | */ | |
300 | if (!scm_i_heap_segment_table) | |
301 | { | |
302 | fprintf (stderr, "scm_i_get_new_heap_segment: Could not grow heap segment table.\n"); | |
303 | abort (); | |
304 | } | |
305 | ||
306 | if (!lowest_cell) | |
307 | { | |
308 | lowest_cell = seg->bounds[0]; | |
309 | highest_cell = seg->bounds[1]; | |
310 | } | |
311 | else | |
312 | { | |
313 | lowest_cell = SCM_MIN (lowest_cell, seg->bounds[0]); | |
314 | highest_cell = SCM_MAX (highest_cell, seg->bounds[1]); | |
315 | } | |
316 | ||
317 | ||
318 | { | |
319 | int i = 0; | |
320 | int j = 0; | |
321 | ||
322 | while (i < scm_i_heap_segment_table_size | |
323 | && scm_i_heap_segment_table[i]->bounds[0] <= seg->bounds[0]) | |
324 | i++; | |
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]; | |
327 | ||
328 | scm_i_heap_segment_table [i] = seg; | |
329 | scm_i_heap_segment_table_size ++; | |
330 | ||
331 | return i; | |
332 | } | |
333 | } | |
334 | ||
335 | SCM | |
336 | scm_i_sweep_some_segments (scm_t_cell_type_statistics * fl) | |
337 | { | |
338 | int i = fl->heap_segment_idx; | |
339 | SCM collected =SCM_EOL; | |
340 | ||
341 | if (i == -1) | |
342 | i++; | |
343 | ||
344 | for (; | |
345 | i < scm_i_heap_segment_table_size; i++) | |
346 | { | |
347 | if (scm_i_heap_segment_table[i]->freelist != fl) | |
348 | continue; | |
349 | ||
350 | collected = scm_i_sweep_some_cards (scm_i_heap_segment_table[i]); | |
351 | ||
352 | ||
353 | if (collected != SCM_EOL) /* Don't increment i */ | |
354 | break; | |
355 | } | |
356 | ||
357 | fl->heap_segment_idx = i; | |
358 | ||
359 | return collected; | |
360 | } | |
361 | ||
362 | ||
363 | ||
364 | ||
365 | void | |
366 | scm_i_reset_segments (void) | |
367 | { | |
368 | int i = 0; | |
369 | for (; i < scm_i_heap_segment_table_size; i++) | |
370 | { | |
371 | scm_t_heap_segment * seg = scm_i_heap_segment_table[i]; | |
372 | seg->next_free_card = seg->bounds[0]; | |
373 | } | |
374 | } | |
375 | ||
376 | ||
377 | /* | |
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. | |
382 | ||
383 | ||
384 | I think this function is too long to be inlined. --hwn | |
385 | */ | |
386 | long int | |
387 | scm_i_find_heap_segment_containing_object (SCM obj) | |
388 | { | |
389 | if (!CELL_P (obj)) | |
390 | return -1; | |
391 | ||
392 | if ((scm_t_cell* ) obj < lowest_cell || (scm_t_cell*) obj >= highest_cell) | |
393 | return -1; | |
394 | ||
395 | ||
396 | { | |
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; | |
400 | ||
401 | if (ptr < scm_i_heap_segment_table[i]->bounds[0]) | |
402 | return -1; | |
403 | else if (scm_i_heap_segment_table[j]->bounds[1] <= ptr) | |
404 | return -1; | |
405 | else | |
406 | { | |
407 | while (i < j) | |
408 | { | |
409 | if (ptr < scm_i_heap_segment_table[i]->bounds[1]) | |
410 | { | |
411 | break; | |
412 | } | |
413 | else if (scm_i_heap_segment_table[j]->bounds[0] <= ptr) | |
414 | { | |
415 | i = j; | |
416 | break; | |
417 | } | |
418 | else | |
419 | { | |
420 | unsigned long int k = (i + j) / 2; | |
421 | ||
422 | if (k == i) | |
423 | return -1; | |
424 | else if (ptr < scm_i_heap_segment_table[k]->bounds[1]) | |
425 | { | |
426 | j = k; | |
427 | ++i; | |
428 | if (ptr < scm_i_heap_segment_table[i]->bounds[0]) | |
429 | return -1; | |
430 | } | |
431 | else if (scm_i_heap_segment_table[k]->bounds[0] <= ptr) | |
432 | { | |
433 | i = k; | |
434 | --j; | |
435 | if (scm_i_heap_segment_table[j]->bounds[1] <= ptr) | |
436 | return -1; | |
437 | } | |
438 | } | |
439 | } | |
440 | ||
441 | if (!DOUBLECELL_ALIGNED_P (obj) && scm_i_heap_segment_table[i]->span == 2) | |
442 | return -1; | |
443 | else if (SCM_GC_IN_CARD_HEADERP (ptr)) | |
444 | return -1; | |
445 | else | |
446 | return i; | |
447 | } | |
448 | } | |
449 | } | |
450 | ||
451 | ||
452 | /* | |
453 | Important entry point: try to grab some memory, and make it into a | |
454 | segment. | |
455 | ||
456 | RETURN: the index of the segment. | |
457 | */ | |
458 | int | |
459 | scm_i_get_new_heap_segment (scm_t_cell_type_statistics *freelist, policy_on_error error_policy) | |
460 | { | |
461 | size_t len; | |
462 | ||
463 | if (scm_gc_heap_lock) | |
464 | { | |
465 | /* Critical code sections (such as the garbage collector) aren't | |
466 | * supposed to add heap segments. | |
467 | */ | |
468 | fprintf (stderr, "scm_i_get_new_heap_segment: Can not extend locked heap.\n"); | |
469 | abort (); | |
470 | } | |
471 | ||
c7743d02 HWN |
472 | { |
473 | /* Assure that the new segment is predicted to be large enough. | |
474 | * | |
475 | * New yield should at least equal GC fraction of new heap size, i.e. | |
476 | * | |
477 | * y + dh > f * (h + dh) | |
478 | * | |
479 | * y : yield | |
480 | * f : min yield fraction | |
481 | * h : heap size | |
482 | * dh : size of new heap segment | |
483 | * | |
484 | * This gives dh > (f * h - y) / (1 - f) | |
485 | */ | |
38d1262a HWN |
486 | float f = freelist->min_yield_fraction / 100.0; |
487 | float h = SCM_HEAP_SIZE; | |
488 | float min_cells | |
489 | = (f * h - scm_gc_cells_collected) / (1.0 - f); | |
c7743d02 HWN |
490 | |
491 | /* Make heap grow with factor 1.5 */ | |
492 | len = freelist->heap_size / 2; | |
493 | #ifdef DEBUGINFO | |
494 | fprintf (stderr, "(%ld < %ld)", (long) len, (long) min_cells); | |
495 | #endif | |
496 | ||
c7743d02 | 497 | if (len < min_cells) |
38d1262a | 498 | len = (unsigned long) min_cells; |
c7743d02 HWN |
499 | len *= sizeof (scm_t_cell); |
500 | /* force new sampling */ | |
501 | freelist->collected = LONG_MAX; | |
502 | } | |
503 | ||
4a5309c9 | 504 | if (len < SCM_MIN_HEAP_SEG_SIZE) |
67329a9e | 505 | len = SCM_MIN_HEAP_SEG_SIZE; |
c7743d02 HWN |
506 | |
507 | { | |
c7743d02 | 508 | scm_t_heap_segment * seg = scm_i_make_empty_heap_segment (freelist); |
c7743d02 HWN |
509 | |
510 | /* Allocate with decaying ambition. */ | |
67329a9e | 511 | while (len >= SCM_MIN_HEAP_SEG_SIZE) |
c7743d02 HWN |
512 | { |
513 | if (scm_i_initialize_heap_segment_data (seg, len)) | |
514 | { | |
515 | return scm_i_insert_segment (seg); | |
516 | } | |
517 | ||
518 | len /= 2; | |
519 | } | |
520 | } | |
521 | ||
522 | if (error_policy == abort_on_error) | |
523 | { | |
524 | fprintf (stderr, "scm_i_get_new_heap_segment: Could not grow heap.\n"); | |
525 | abort (); | |
526 | } | |
527 | return -1; | |
528 | } | |
529 | ||
530 | ||
eab1b259 | 531 | |
c7743d02 HWN |
532 | void |
533 | scm_i_make_initial_segment (size_t init_heap_size, scm_t_cell_type_statistics *freelist) | |
534 | { | |
535 | scm_t_heap_segment * seg = scm_i_make_empty_heap_segment (freelist); | |
536 | ||
537 | if (scm_i_initialize_heap_segment_data (seg, init_heap_size)) | |
538 | { | |
539 | freelist->heap_segment_idx = scm_i_insert_segment (seg); | |
540 | } | |
541 | ||
542 | /* | |
543 | Why the fuck try twice? --hwn | |
544 | */ | |
545 | if (!seg->malloced) | |
546 | { | |
547 | scm_i_initialize_heap_segment_data (seg, SCM_HEAP_SEG_SIZE); | |
548 | } | |
549 | ||
550 | if (freelist->min_yield_fraction) | |
551 | freelist->min_yield = (freelist->heap_size * freelist->min_yield_fraction | |
552 | / 100); | |
553 | } |