]> gitweb.factorcode.org Git - factor.git/blob - vm/code_gc.h
Initial import
[factor.git] / vm / code_gc.h
1 typedef enum
2 {
3         B_FREE,
4         B_ALLOCATED,
5         B_MARKED
6 } F_BLOCK_STATUS;
7
8 typedef struct _F_BLOCK
9 {
10         F_BLOCK_STATUS status;
11
12         /* In bytes, includes this header */
13         CELL size;
14
15         /* Filled in on image load */
16         struct _F_BLOCK *next_free;
17
18         /* Used during compaction */
19         struct _F_BLOCK *forwarding;
20
21         /* Alignment padding */
22         CELL padding[4];
23 } F_BLOCK;
24
25 typedef struct {
26         F_SEGMENT *segment;
27         F_BLOCK *free_list;
28 } F_HEAP;
29
30 void new_heap(F_HEAP *heap, CELL size);
31 void build_free_list(F_HEAP *heap, CELL size);
32 CELL heap_allot(F_HEAP *heap, CELL size);
33 void unmark_marked(F_HEAP *heap);
34 void free_unmarked(F_HEAP *heap);
35 CELL heap_usage(F_HEAP *heap, F_BLOCK_STATUS status);
36 CELL heap_size(F_HEAP *heap);
37
38 INLINE F_BLOCK *next_block(F_HEAP *heap, F_BLOCK *block)
39 {
40         CELL next = ((CELL)block + block->size);
41         if(next == heap->segment->end)
42                 return NULL;
43         else
44                 return (F_BLOCK *)next;
45 }
46
47 /* compiled code */
48 F_HEAP code_heap;
49
50 /* The compiled code heap is structured into blocks. */
51 typedef struct
52 {
53         CELL type; /* this is WORD_TYPE or QUOTATION_TYPE */
54         CELL code_length; /* # bytes */
55         CELL reloc_length; /* # bytes */
56         CELL literals_length; /* # bytes */
57         CELL words_length; /* # bytes */
58         CELL finalized; /* has finalize_code_block() been called on this yet? */
59         CELL padding[2];
60 } F_COMPILED;
61
62 typedef void (*CODE_HEAP_ITERATOR)(F_COMPILED *compiled, CELL code_start,
63         CELL reloc_start, CELL literals_start, CELL words_start, CELL words_end);
64
65 INLINE void iterate_code_heap_step(F_COMPILED *compiled, CODE_HEAP_ITERATOR iter)
66 {
67         CELL code_start = (CELL)(compiled + 1);
68         CELL reloc_start = code_start + compiled->code_length;
69         CELL literals_start = reloc_start + compiled->reloc_length;
70         CELL words_start = literals_start + compiled->literals_length;
71         CELL words_end = words_start + compiled->words_length;
72
73         iter(compiled,code_start,reloc_start,literals_start,words_start,words_end);
74 }
75
76 INLINE F_BLOCK *xt_to_block(XT xt)
77 {
78         return (F_BLOCK *)((CELL)xt - sizeof(F_BLOCK) - sizeof(F_COMPILED));
79 }
80
81 INLINE F_COMPILED *xt_to_compiled(XT xt)
82 {
83         return (F_COMPILED *)((CELL)xt - sizeof(F_COMPILED));
84 }
85
86 INLINE F_COMPILED *block_to_compiled(F_BLOCK *block)
87 {
88         return (F_COMPILED *)(block + 1);
89 }
90
91 INLINE XT block_to_xt(F_BLOCK *block)
92 {
93         return (XT)((CELL)block + sizeof(F_BLOCK) + sizeof(F_COMPILED));
94 }
95
96 INLINE F_BLOCK *first_block(F_HEAP *heap)
97 {
98         return (F_BLOCK *)heap->segment->start;
99 }
100
101 INLINE F_BLOCK *last_block(F_HEAP *heap)
102 {
103         return (F_BLOCK *)heap->segment->end;
104 }
105
106 void init_code_heap(CELL size);
107 bool in_code_heap_p(CELL ptr);
108 void iterate_code_heap(CODE_HEAP_ITERATOR iter);
109 void collect_literals(void);
110 void recursive_mark(XT xt);
111 void dump_heap(F_HEAP *heap);
112 void code_gc(void);
113 void compact_code_heap(void);
114
115 DECLARE_PRIMITIVE(code_room);
116 DECLARE_PRIMITIVE(code_gc);