forked from idris-lang/Idris-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidris_heap.c
242 lines (204 loc) · 5.8 KB
/
idris_heap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "idris_heap.h"
#include "idris_rts.h"
#include "idris_gc.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
static void c_heap_free_item(CHeap * heap, CHeapItem * item)
{
assert(item->size <= heap->size);
heap->size -= item->size;
// fix links
if (item->next != NULL)
{
item->next->prev_next = item->prev_next;
}
*(item->prev_next) = item->next;
// free payload
item->finalizer(item->data);
// free item struct
free(item);
}
CHeapItem * c_heap_create_item(void * data, size_t size, CDataFinalizer * finalizer)
{
CHeapItem * item = (CHeapItem *) malloc(sizeof(CHeapItem));
item->data = data;
item->size = size;
item->finalizer = finalizer;
item->is_used = false;
item->next = NULL;
item->prev_next = NULL;
return item;
}
void c_heap_insert_if_needed(VM * vm, CHeap * heap, CHeapItem * item)
{
if (item->prev_next != NULL) return; // already inserted
if (heap->first != NULL)
{
heap->first->prev_next = &item->next;
}
item->prev_next = &heap->first;
item->next = heap->first;
heap->first = item;
// at this point, links are done; let's calculate sizes
heap->size += item->size;
if (heap->size >= heap->gc_trigger_size)
{
item->is_used = true; // don't collect what we're inserting
idris_gc(vm);
}
}
void c_heap_mark_item(CHeapItem * item)
{
item->is_used = true;
}
void c_heap_sweep(CHeap * heap)
{
CHeapItem * p = heap->first;
while (p != NULL)
{
if (p->is_used)
{
p->is_used = false;
p = p->next;
}
else
{
CHeapItem * unused_item = p;
p = p->next;
c_heap_free_item(heap, unused_item);
}
}
heap->gc_trigger_size = C_HEAP_GC_TRIGGER_SIZE(heap->size);
}
void c_heap_init(CHeap * heap)
{
heap->first = NULL;
heap->size = 0;
heap->gc_trigger_size = C_HEAP_GC_TRIGGER_SIZE(heap->size);
}
void c_heap_destroy(CHeap * heap)
{
while (heap->first != NULL)
{
c_heap_free_item(heap, heap->first); // will update heap->first via the backward link
}
}
/* Used for initializing the FP heap. */
void alloc_heap(Heap * h, size_t heap_size, size_t growth, char * old)
{
char * mem = malloc(heap_size);
if (mem == NULL) {
fprintf(stderr,
"RTS ERROR: Unable to allocate heap. Requested %zd bytes.\n",
heap_size);
exit(EXIT_FAILURE);
}
memset(mem, 0, heap_size);
h->heap = mem;
h->next = aligned_heap_pointer(h->heap);
h->end = h->heap + heap_size;
h->size = heap_size;
h->growth = growth;
h->old = old;
}
void free_heap(Heap * h) {
free(h->heap);
if (h->old != NULL) {
free(h->old);
}
}
// TODO: more testing
/******************** Heap testing ********************************************/
void heap_check_underflow(Heap * heap) {
if (!(heap->heap <= heap->next)) {
fprintf(stderr, "RTS ERROR: HEAP UNDERFLOW <bot %p> <cur %p>\n",
heap->heap, heap->next);
exit(EXIT_FAILURE);
}
}
void heap_check_overflow(Heap * heap) {
if (!(heap->next <= heap->end)) {
fprintf(stderr, "RTS ERROR: HEAP OVERFLOW <cur %p> <end %p>\n",
heap->next, heap->end);
exit(EXIT_FAILURE);
}
}
int is_valid_ref(VAL v) {
return (v != NULL) && !(ISINT(v));
}
int ref_in_heap(Heap * heap, VAL v) {
return ((VAL)heap->heap <= v) && (v < (VAL)heap->next);
}
char* aligned_heap_pointer(char * heap) {
#ifdef FORCE_ALIGNMENT
if (((i_int)heap&1) == 1) {
return (heap + 1);
} else
#endif
{
return heap;
}
}
// Checks three important properties:
// 1. Closure.
// Check if all pointers in the _heap_ points only to heap.
// 2. Unidirectionality. (if compact gc)
// All references in the heap should be are unidirectional. In other words,
// more recently allocated closure can point only to earlier allocated one.
// 3. After gc there should be no forward references.
//
void heap_check_pointers(Heap * heap) {
char* scan = NULL;
size_t item_size = 0;
for(scan = heap->heap; scan < heap->next; scan += item_size) {
VAL heap_item = (VAL)scan;
item_size = aligned(valSize(heap_item));
switch(GETTY(heap_item)) {
case CT_CON:
{
Con * c = (Con*)heap_item;
size_t ar = CARITY(c);
size_t i;
for(i = 0; i < ar; ++i) {
VAL ptr = c->args[i];
if (is_valid_ref(ptr)) {
// Check for closure.
if (!ref_in_heap(heap, ptr)) {
fprintf(stderr,
"RTS ERROR: heap closure broken. "\
"<HEAP %p %p %p> <REF %p>\n",
heap->heap, heap->next, heap->end, ptr);
exit(EXIT_FAILURE);
}
#if 0 // TODO macro
// Check for unidirectionality.
if (!(ptr < heap_item)) {
fprintf(stderr,
"RTS ERROR: heap unidirectionality broken:" \
"<CT_CON %p> <FIELD %p>\n",
heap_item, ptr);
exit(EXIT_FAILURE);
}
#endif
}
}
break;
}
case CT_FWD:
// Check for artifacts after cheney gc.
fprintf(stderr, "RTS ERROR: CT_FWD in working heap.\n");
exit(EXIT_FAILURE);
break;
default:
break;
}
}
}
void heap_check_all(Heap * heap)
{
heap_check_underflow(heap);
heap_check_overflow(heap);
heap_check_pointers(heap);
}