6 void factorvm::clear_free_list(heap *heap)
8 memset(&heap->free,0,sizeof(heap_free_list));
11 /* This malloc-style heap code is reasonably generic. Maybe in the future, it
12 will be used for the data heap too, if we ever get incremental
13 mark/sweep/compact GC. */
14 void factorvm::new_heap(heap *heap, cell size)
16 heap->seg = alloc_segment(align_page(size));
18 fatal_error("Out of memory in new_heap",size);
20 clear_free_list(heap);
23 void factorvm::add_to_free_list(heap *heap, free_heap_block *block)
25 if(block->size < free_list_count * block_size_increment)
27 int index = block->size / block_size_increment;
28 block->next_free = heap->free.small_blocks[index];
29 heap->free.small_blocks[index] = block;
33 block->next_free = heap->free.large_blocks;
34 heap->free.large_blocks = block;
38 /* Called after reading the code heap from the image file, and after code GC.
40 In the former case, we must add a large free block from compiling.base + size to
42 void factorvm::build_free_list(heap *heap, cell size)
44 heap_block *prev = NULL;
46 clear_free_list(heap);
48 size = (size + block_size_increment - 1) & ~(block_size_increment - 1);
50 heap_block *scan = first_block(heap);
51 free_heap_block *end = (free_heap_block *)(heap->seg->start + size);
53 /* Add all free blocks to the free list */
54 while(scan && scan < (heap_block *)end)
59 add_to_free_list(heap,(free_heap_block *)scan);
64 critical_error("Invalid scan->status",(cell)scan);
69 scan = next_block(heap,scan);
72 /* If there is room at the end of the heap, add a free block. This
73 branch is only taken after loading a new image, not after code GC */
74 if((cell)(end + 1) <= heap->seg->end)
77 end->size = heap->seg->end - (cell)end;
79 /* add final free block */
80 add_to_free_list(heap,end);
82 /* This branch is taken if the newly loaded image fits exactly, or
86 /* even if there's no room at the end of the heap for a new
87 free block, we might have to jigger it up by a few bytes in
88 case prev + prev->size */
89 if(prev) prev->size = heap->seg->end - (cell)prev;
94 void factorvm::assert_free_block(free_heap_block *block)
96 if(block->status != B_FREE)
97 critical_error("Invalid block in free list",(cell)block);
101 free_heap_block *factorvm::find_free_block(heap *heap, cell size)
105 while(attempt < free_list_count * block_size_increment)
107 int index = attempt / block_size_increment;
108 free_heap_block *block = heap->free.small_blocks[index];
111 assert_free_block(block);
112 heap->free.small_blocks[index] = block->next_free;
119 free_heap_block *prev = NULL;
120 free_heap_block *block = heap->free.large_blocks;
124 assert_free_block(block);
125 if(block->size >= size)
128 prev->next_free = block->next_free;
130 heap->free.large_blocks = block->next_free;
135 block = block->next_free;
141 free_heap_block *factorvm::split_free_block(heap *heap, free_heap_block *block, cell size)
143 if(block->size != size )
145 /* split the block in two */
146 free_heap_block *split = (free_heap_block *)((cell)block + size);
147 split->status = B_FREE;
148 split->size = block->size - size;
149 split->next_free = block->next_free;
151 add_to_free_list(heap,split);
157 /* Allocate a block of memory from the mark and sweep GC heap */
158 heap_block *factorvm::heap_allot(heap *heap, cell size)
160 size = (size + block_size_increment - 1) & ~(block_size_increment - 1);
162 free_heap_block *block = find_free_block(heap,size);
165 block = split_free_block(heap,block,size);
167 block->status = B_ALLOCATED;
174 /* Deallocates a block manually */
175 void factorvm::heap_free(heap *heap, heap_block *block)
177 block->status = B_FREE;
178 add_to_free_list(heap,(free_heap_block *)block);
181 void factorvm::mark_block(heap_block *block)
183 /* If already marked, do nothing */
184 switch(block->status)
189 block->status = B_MARKED;
192 critical_error("Marking the wrong block",(cell)block);
197 /* If in the middle of code GC, we have to grow the heap, data GC restarts from
198 scratch, so we have to unmark any marked blocks. */
199 void factorvm::unmark_marked(heap *heap)
201 heap_block *scan = first_block(heap);
205 if(scan->status == B_MARKED)
206 scan->status = B_ALLOCATED;
208 scan = next_block(heap,scan);
212 /* After code GC, all referenced code blocks have status set to B_MARKED, so any
213 which are allocated and not marked can be reclaimed. */
214 void factorvm::free_unmarked(heap *heap, heap_iterator iter)
216 clear_free_list(heap);
218 heap_block *prev = NULL;
219 heap_block *scan = first_block(heap);
227 memset(scan + 1,0,scan->size - sizeof(heap_block));
229 if(prev && prev->status == B_FREE)
230 prev->size += scan->size;
233 scan->status = B_FREE;
238 if(prev && prev->status == B_FREE)
239 prev->size += scan->size;
244 if(prev && prev->status == B_FREE)
245 add_to_free_list(heap,(free_heap_block *)prev);
246 scan->status = B_ALLOCATED;
251 critical_error("Invalid scan->status",(cell)scan);
254 scan = next_block(heap,scan);
257 if(prev && prev->status == B_FREE)
258 add_to_free_list(heap,(free_heap_block *)prev);
261 /* Compute total sum of sizes of free blocks, and size of largest free block */
262 void factorvm::heap_usage(heap *heap, cell *used, cell *total_free, cell *max_free)
268 heap_block *scan = first_block(heap);
278 *total_free += scan->size;
279 if(scan->size > *max_free)
280 *max_free = scan->size;
283 critical_error("Invalid scan->status",(cell)scan);
286 scan = next_block(heap,scan);
290 /* The size of the heap, not including the last block if it's free */
291 cell factorvm::heap_size(heap *heap)
293 heap_block *scan = first_block(heap);
295 while(next_block(heap,scan) != NULL)
296 scan = next_block(heap,scan);
298 /* this is the last block in the heap, and it is free */
299 if(scan->status == B_FREE)
300 return (cell)scan - heap->seg->start;
301 /* otherwise the last block is allocated */
303 return heap->seg->size;
306 /* Compute where each block is going to go, after compaction */
307 cell factorvm::compute_heap_forwarding(heap *heap, unordered_map<heap_block *,char *> &forwarding)
309 heap_block *scan = first_block(heap);
310 char *address = (char *)first_block(heap);
314 if(scan->status == B_ALLOCATED)
316 forwarding[scan] = address;
317 address += scan->size;
319 else if(scan->status == B_MARKED)
320 critical_error("Why is the block marked?",0);
322 scan = next_block(heap,scan);
325 return (cell)address - heap->seg->start;
328 void factorvm::compact_heap(heap *heap, unordered_map<heap_block *,char *> &forwarding)
330 heap_block *scan = first_block(heap);
334 heap_block *next = next_block(heap,scan);
336 if(scan->status == B_ALLOCATED)
337 memmove(forwarding[scan],scan,scan->size);