-
Notifications
You must be signed in to change notification settings - Fork 2
/
cheney.h
372 lines (345 loc) · 25.9 KB
/
cheney.h
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/******************************************************************************
* FILE: cheney.h *
* AUTHOR: Ryan Rozanski *
* CREATED: 1/14/17 *
* EDITED: 11/11/17 *
* INFO: A library which encapsulates a cheney style garbage collected *
* heap into a well defined structure and exposes its abilities *
* through a simple to use API. The interface allows creation and *
* destruction of an arbitrary number of heaps. Additionally, it *
* exposes the heaps ability to grow dynamically and the capability *
* to switch on/off this attribute at will. Note, that a heap will *
* only grow dynamically when a collection, triggered as a result of *
* a full semi-space, fails to free any memory. The default *
* expansion behavior attempts to double the size of the heap, but *
* the library permits custom implementations to be set, allowing *
* any expanding growth method desired. Furthermore, the heap may be *
* resized manually at any point in time which also permits making *
* it smaller if possible. Although collection happens automatically *
* when needed, it may also be triggered manually whenever wanted. *
* However, to preserve any allocated object(s) they must be *
* reachable from a single root object. When created the heaps root *
* is set to NULL since no objects have been allocated yet. As a *
* result of this, the user is required to explicitly set the root, *
* and getter/setter methods are provided to make managing it easy. *
* The heap only allows allocation of a single object named a cons *
* cell. Cells are capable of holding two references and the library *
* allows querying and updating the fields individually, as well as *
* creating cells initialized with data or not. Cons cells can be *
* used to implement more complex data structures such as stacks, *
* queues, lists, and trees among others. It is also possible to *
* query the heap about its current usage of the semi space and *
* overall size so it can be known how many allocation before a *
* collection will be triggered as well as the resulting size of the *
* heap after a dynamic expansion or manaul resize. Lastly, it is *
* critically important to note that for the collector to properly *
* differentiate between cons cells and all other data types, termed *
* atomic, it imposes a single rule that references to the atomic *
* data must have the lowest bit set. *
* *
******************************************************************************/
#ifndef CHENEY_GC_STRUCT_DEFS
#define CHENEY_GC_STRUCT_DEFS
/******************************************************************************
* *
* T Y P E S *
* *
******************************************************************************/
typedef struct heap heap_t; /* Cheney style garbage collected heap */
typedef struct cell cell_t; /* Cons cells */
/******************************************************************************
* *
* F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
/******************************************************************************
* *
* PURPOSE: Instantiate a new heap with an size in cons cells. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* cells The number of cells to instantiate the heap with. Odd numbers *
* will be reduced by one. *
* *
* RETURNS: A pointer to a newly allocated heap or NULL if allocation fails. *
* *
******************************************************************************/
heap_t *make_heap(unsigned long cells);
/******************************************************************************
* *
* PURPOSE: Free any heap created with the constructor. A pointer to the *
* variable which holds the reference to the heap is taken to help *
* minimize dangling pointers and potential crashes by setting it to *
* NULL when completed. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap Address of the variable holding the heap. *
* *
* RETURNS: void *
* *
******************************************************************************/
void free_heap(heap_t **heap);
/******************************************************************************
* *
* PURPOSE: Update an arbitrary heap to (dis)allow dynamic expansion. If set *
* to allow dynamic expansion, it will only happen if a collection, *
* which is the result of a full semi space, fails to free any *
* allocated memory. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap to update. *
* dynamic 1 if allowed to dynamically resize, 0 otherwise. *
* *
* RETURNS: void *
* *
******************************************************************************/
void set_dynamic(heap_t *heap, int dynamic);
/******************************************************************************
* *
* PURPOSE: Query an arbitrary heap if it can dynamically expand or not. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap to query. *
* *
* RETURNS: 1 if the heap can dynamically expand, 0 otherwise. *
* *
******************************************************************************/
int get_dynamic(heap_t *heap);
/******************************************************************************
* *
* PURPOSE: Dictate how the heap dynamically expands by setting a custom *
* expander. Note that this only affects heaps created with the *
* dynamic option set. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap Which heap to use. *
* expander A function pointer to the custom expander. *
* *
* RETURNS: void *
* *
******************************************************************************/
void set_expander(heap_t *heap, unsigned long (*expander)(unsigned long));
/******************************************************************************
* *
* PURPOSE: Query an arbitrary heap about its dynamic expansion policy. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The given heap to query. *
* *
* RETURNS: The function pointer to the custom set expander or NULL if the *
* default expander is being used. *
* *
******************************************************************************/
unsigned long (*get_expander(heap_t *heap))(unsigned long);
/******************************************************************************
* *
* PURPOSE: Attempt manual resizing of the heap, either bigger or smaller, so *
* that each semi space is half of the number specified. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The given heap to attempt resizing. *
* cells The new size of the heap space in cells. *
* *
* RETURNS: 1 if successful, 0 otherwise. *
* *
******************************************************************************/
int resize(heap_t *heap, unsigned long cells);
/******************************************************************************
* *
* PURPOSE: Set the root of reachable objects. When a collection is triggered *
* all objects which are reachable from this single object will be *
* safe from the garbage collection process. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap to set the root on. *
* root The root to set. *
* *
* RETURNS: void *
* *
******************************************************************************/
void set_root(heap_t *heap, void *root);
/******************************************************************************
* *
* PURPOSE: Query an arbitrary heap for it root. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap to query. *
* *
* RETURNS: The heaps root object or NULL if not set. *
* *
******************************************************************************/
void *get_root(heap_t *heap);
/******************************************************************************
* *
* PURPOSE: Trigger an arbitrary collection at any point in time which will *
* free any unreferenced cons cells not reachable from the root as *
* well as compacting all retained cells into the new semi space. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap to trigger a collection on. *
* *
* RETURNS: void *
* *
******************************************************************************/
void collect(heap_t *heap);
/******************************************************************************
* *
* PURPOSE: Query an arbitrary heap about its size in cells. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap to query. *
* *
* RETURNS: The number of cells the entire heap could theoretically hold. *
* *
******************************************************************************/
unsigned long heap_size(heap_t *heap);
/******************************************************************************
* *
* PURPOSE: Query an arbitrary heap about its semi space size in cells. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap to query. *
* *
* RETURNS: The number of cells the heap can actually hold. *
* *
******************************************************************************/
unsigned long semi_size(heap_t *heap);
/******************************************************************************
* *
* PURPOSE: Query an arbitrary heap about how many cells are allocated. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap to query. *
* *
* RETURNS: The number of currently allocated cells in the heap. *
* *
******************************************************************************/
unsigned long semi_used(heap_t *heap);
/******************************************************************************
* *
* PURPOSE: Query an arbitrary heap about how many cells can be allocated *
* before another garbage collection is triggered. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap to query. *
* *
* RETURNS: The number of cells which can be allocated from the heap. *
* *
******************************************************************************/
unsigned long semi_left(heap_t *heap);
/******************************************************************************
* *
* PURPOSE: Allocate a new cons cell with both fields initialized with the *
* given contents. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap in which to attempt an allocation from. *
* car the contents to store in the first field of the cell. *
* cdr the contents to store in the second field of the cell. *
* *
* RETURNS: Newly allocated cons cell initialized with the given contents or *
* NULL if allocation fails. *
* *
******************************************************************************/
cell_t *cons(heap_t *heap, void *car, void *cdr);
/******************************************************************************
* *
* PURPOSE: Allocate a new cons cell with both fields initialized to NULL. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* heap The heap in which to attempt an allocation from. *
* *
* RETURNS: Newly allocated cons cell or NULL if allocation fails. *
* *
******************************************************************************/
cell_t *cell(heap_t *heap);
/******************************************************************************
* *
* PURPOSE: Query a cons cell about the contents of its first field. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* cell The cons cell in question. *
* *
* RETURNS: The contents of the cells first field. *
* *
******************************************************************************/
void *car(cell_t *cell);
/******************************************************************************
* *
* PURPOSE: Query a cons cell about the contents of its second field. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* cell The cons cell in question. *
* *
* RETURNS: The contents of the cells second field. *
* *
******************************************************************************/
void *cdr(cell_t *cell);
/******************************************************************************
* *
* PURPOSE: Update the contents of the cells first field. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* cell The cons cell to update. *
* value The new value to update the cells field with. *
* *
* RETURNS: void *
* *
******************************************************************************/
void set_car(cell_t *cell, void *value);
/******************************************************************************
* *
* PURPOSE: Update the contents of the cells seoncd field. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* cell The cons cell to update. *
* value The new value to update the cells field with. *
* *
* RETURNS: void *
* *
******************************************************************************/
void set_cdr(cell_t *cell, void *value);
/******************************************************************************
* *
* PURPOSE: Query if a given pointer represents atomic data. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* value The pointer to test. *
* *
* RETURNS: 1 if atomic or NULL, 0 otherwise. *
* *
******************************************************************************/
char is_atom(void **value);
/******************************************************************************
* *
* PURPOSE: Query if a given pointer represents a cons cell. *
* *
* ARGUMENT DESCRIPTION *
* -------- ----------- *
* value The pointer to test. *
* *
* RETURNS: 1 if cons cell, 0 otherwise. *
* *
******************************************************************************/
char is_cell(void **value);
#endif