@@ -4,11 +4,15 @@
4#include <stdbool.h>
4#include <stdbool.h>
5#include <stddef.h>
5#include <stddef.h>
8+ * Objects with finalizers. Mark&sweep-collected.
7typedef void CDataFinalizer_t(void *);
11typedef void CDataFinalizer_t(void *);
9typedef struct CHeapItem {
13typedef struct CHeapItem {
10 void * data;
14 void * data;
11− CDataFinalizer_t * finalizer; // responsible for freeing the passed pointer, too
15+ CDataFinalizer_t * finalizer; // should free() the passed data pointer, too
12 bool is_used;
16 bool is_used;
13 struct CHeapItem * next;
17 struct CHeapItem * next;
14 struct CHeapItem ** prev_next;
18 struct CHeapItem ** prev_next;
@@ -19,7 +23,7 @@ typedef struct CHeap {
21void c_heap_init(CHeap * c_heap);
25void c_heap_init(CHeap * c_heap);
22−void c_heap_destroy(CHeap * c_heap);
26+void c_heap_destroy(CHeap * c_heap); // will call finalizers on everything
23void c_heap_insert_if_needed(CHeap * c_heap, CHeapItem * item);
27void c_heap_insert_if_needed(CHeap * c_heap, CHeapItem * item);
24void c_heap_mark_item(CHeapItem * item);
28void c_heap_mark_item(CHeapItem * item);
25void c_heap_sweep(CHeap * c_heap);
29void c_heap_sweep(CHeap * c_heap);
@@ -29,6 +33,10 @@ CHeapItem * c_heap_create_item(void * data, CDataFinalizer_t * finalizer);
29CHeapItem * c_heap_allocate(size_t size, CDataFinalizer_t * finalizer);
33CHeapItem * c_heap_allocate(size_t size, CDataFinalizer_t * finalizer);
30void c_heap_free(CHeapItem * item);
34void c_heap_free(CHeapItem * item);
37+ * Objects without finalizers. Cheney-collected.
32typedef struct {
40typedef struct {
33 char* next; // Next allocated chunk. Should always (heap <= next < end).
41 char* next; // Next allocated chunk. Should always (heap <= next < end).
34 char* heap; // Point to bottom of heap
42 char* heap; // Point to bottom of heap