1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2006, 2008 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library 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 GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libguile/_scm.h"
27 #include "libguile/pairs.h"
28 #include "libguile/gc.h"
29 #include "libguile/private-gc.h"
35 The table is sorted by the address of the data itself. This makes
36 for easy lookups. This is not portable: according to ANSI C,
37 pointers can only be compared within the same object (i.e. the same
38 block of malloced memory.). For machines with weird architectures,
39 this should be revised.
41 (Apparently, for this reason 1.6 and earlier had macros for pointer
44 perhaps it is worthwhile to remove the 2nd level of indirection in
45 the table, but this certainly makes for cleaner code.
47 scm_t_heap_segment
**scm_i_heap_segment_table
;
48 size_t scm_i_heap_segment_table_size
;
49 static scm_t_cell
*lowest_cell
;
50 static scm_t_cell
*highest_cell
;
54 RETURN: index of inserted segment.
57 scm_i_insert_segment (scm_t_heap_segment
*seg
)
59 size_t size
= (scm_i_heap_segment_table_size
+ 1) * sizeof (scm_t_heap_segment
*);
60 SCM_SYSCALL (scm_i_heap_segment_table
61 = ((scm_t_heap_segment
**)
62 realloc ((char *)scm_i_heap_segment_table
, size
)));
65 We can't alloc 4 more bytes. This is hopeless.
67 if (!scm_i_heap_segment_table
)
69 fprintf (stderr
, "scm_i_get_new_heap_segment: Could not grow heap segment table.\n");
75 lowest_cell
= seg
->bounds
[0];
76 highest_cell
= seg
->bounds
[1];
80 lowest_cell
= SCM_MIN (lowest_cell
, seg
->bounds
[0]);
81 highest_cell
= SCM_MAX (highest_cell
, seg
->bounds
[1]);
89 while (i
< scm_i_heap_segment_table_size
90 && scm_i_heap_segment_table
[i
]->bounds
[0] <= seg
->bounds
[0])
94 We insert a new entry; if that happens to be before the
95 "current" segment of a freelist, we must move the freelist index
98 if (scm_i_master_freelist
.heap_segment_idx
>= i
)
99 scm_i_master_freelist
.heap_segment_idx
++;
100 if (scm_i_master_freelist2
.heap_segment_idx
>= i
)
101 scm_i_master_freelist2
.heap_segment_idx
++;
103 for (j
= scm_i_heap_segment_table_size
; j
> i
; --j
)
104 scm_i_heap_segment_table
[j
] = scm_i_heap_segment_table
[j
- 1];
106 scm_i_heap_segment_table
[i
] = seg
;
107 scm_i_heap_segment_table_size
++;
115 Determine whether the given value does actually represent a cell in
116 some heap segment. If this is the case, the number of the heap
117 segment is returned. Otherwise, -1 is returned. Binary search is
118 used to determine the heap segment that contains the cell.
120 I think this function is too long to be inlined. --hwn
124 scm_i_find_heap_segment_containing_object (SCM obj
)
129 scm_i_find_heap_calls
++;
130 if ((scm_t_cell
*) obj
< lowest_cell
|| (scm_t_cell
*) obj
>= highest_cell
)
134 scm_t_cell
*ptr
= SCM2PTR (obj
);
136 unsigned int j
= scm_i_heap_segment_table_size
- 1;
138 if (ptr
< scm_i_heap_segment_table
[i
]->bounds
[0])
140 else if (scm_i_heap_segment_table
[j
]->bounds
[1] <= ptr
)
146 if (ptr
< scm_i_heap_segment_table
[i
]->bounds
[1])
150 else if (scm_i_heap_segment_table
[j
]->bounds
[0] <= ptr
)
157 unsigned long int k
= (i
+ j
) / 2;
161 else if (ptr
< scm_i_heap_segment_table
[k
]->bounds
[1])
165 if (ptr
< scm_i_heap_segment_table
[i
]->bounds
[0])
168 else if (scm_i_heap_segment_table
[k
]->bounds
[0] <= ptr
)
172 if (scm_i_heap_segment_table
[j
]->bounds
[1] <= ptr
)
178 if (!SCM_DOUBLECELL_ALIGNED_P (obj
) && scm_i_heap_segment_table
[i
]->span
== 2)
180 else if (SCM_GC_IN_CARD_HEADERP (ptr
))
190 scm_i_marked_count (void)
194 for (; i
< scm_i_heap_segment_table_size
; i
++)
196 c
+= scm_i_heap_segment_marked_count (scm_i_heap_segment_table
[i
]);
203 scm_i_sweep_some_segments (scm_t_cell_type_statistics
*freelist
,
204 scm_t_sweep_statistics
*sweep_stats
)
206 int i
= freelist
->heap_segment_idx
;
207 SCM collected
= SCM_EOL
;
209 if (i
== -1) /* huh? --hwn */
213 i
< scm_i_heap_segment_table_size
; i
++)
215 if (scm_i_heap_segment_table
[i
]->freelist
!= freelist
)
218 collected
= scm_i_sweep_some_cards (scm_i_heap_segment_table
[i
],
220 DEFAULT_SWEEP_AMOUNT
);
222 if (collected
!= SCM_EOL
) /* Don't increment i */
226 freelist
->heap_segment_idx
= i
;
232 scm_i_reset_segments (void)
235 for (; i
< scm_i_heap_segment_table_size
; i
++)
237 scm_t_heap_segment
*seg
= scm_i_heap_segment_table
[i
];
238 seg
->next_free_card
= seg
->bounds
[0];
246 Return a hashtab with counts of live objects, with tags as keys.
249 scm_i_all_segments_statistics (SCM tab
)
252 for (; i
< scm_i_heap_segment_table_size
; i
++)
254 scm_t_heap_segment
*seg
= scm_i_heap_segment_table
[i
];
255 scm_i_heap_segment_statistics (seg
, tab
);
263 scm_i_segment_table_info (int* size
)
265 *size
= scm_i_heap_segment_table_size
;
266 unsigned long *bounds
= malloc (sizeof (unsigned long) * *size
* 2);
270 for (i
= *size
; i
-- > 0; )
272 bounds
[2*i
] = (unsigned long)scm_i_heap_segment_table
[i
]->bounds
[0];
273 bounds
[2*i
+1] = (unsigned long)scm_i_heap_segment_table
[i
]->bounds
[1];
280 scm_i_sweep_all_segments (char const *reason
,
281 scm_t_sweep_statistics
*sweep_stats
)
284 for (i
= 0; i
< scm_i_heap_segment_table_size
; i
++)
286 scm_i_sweep_segment (scm_i_heap_segment_table
[i
], sweep_stats
);
292 scm_i_clear_mark_space (void)
295 for (; i
< scm_i_heap_segment_table_size
; i
++)
297 scm_i_clear_segment_mark_space (scm_i_heap_segment_table
[i
]);