@@ -6,32 +6,56 @@
7/* *** C heap ***
7/* *** C heap ***
8 * Objects with finalizers. Mark&sweep-collected.
8 * Objects with finalizers. Mark&sweep-collected.
10+ * The C heap is implemented as a doubly linked list
11+ * of pointers coupled with their finalizers.
11−typedef void CDataFinalizer_t(void *);
14+typedef void CDataFinalizer(void *);
13typedef struct CHeapItem {
16typedef struct CHeapItem {
14 void * data;
18 void * data;
15− CDataFinalizer_t * finalizer; // should free() the passed data pointer, too
19+
20+ /// Finalizer that will be called on the payload pointer.
21+ /// Its job is to deallocate all associated resources,
22+ /// including the memory pointed to by `data` (if any).
23+ CDataFinalizer * finalizer;
25+ /// The mark bit set by the FP heap traversal,
26+ /// cleared by C heap sweep.
16 bool is_used;
27 bool is_used;
29+ /// Next item in the C heap.
17 struct CHeapItem * next;
30 struct CHeapItem * next;
32+ /// Pointer to the previous next-pointer.
18 struct CHeapItem ** prev_next;
33 struct CHeapItem ** prev_next;
19} CHeapItem;
34} CHeapItem;
21typedef struct CHeap {
36typedef struct CHeap {
37+ /// The first item in the heap. NULL if the heap is empty.
22 CHeapItem * first;
38 CHeapItem * first;
25void c_heap_init(CHeap * c_heap);
42void c_heap_init(CHeap * c_heap);
44+/// Destroy the given C heap. Will not deallocate the given pointer.
45+/// Will call finalizers & deallocate all blocks in the heap.
26−void c_heap_destroy(CHeap * c_heap); // will call finalizers on everything
46+void c_heap_destroy(CHeap * c_heap);
48+/// Insert the given item into the heap if it's not there yet.
27void c_heap_insert_if_needed(CHeap * c_heap, CHeapItem * item);
49void c_heap_insert_if_needed(CHeap * c_heap, CHeapItem * item);
51+/// Mark the given item as used.
28void c_heap_mark_item(CHeapItem * item);
52void c_heap_mark_item(CHeapItem * item);
54+/// Sweep the C heap, finalizing and freeing unused blocks.
29void c_heap_sweep(CHeap * c_heap);
55void c_heap_sweep(CHeap * c_heap);
30−CHeapItem * c_heap_create_item(void * data, CDataFinalizer_t * finalizer);
32−// this is the function to use in C binding code
57+/// Create a C heap item from its payload and finalizer.
33−CHeapItem * c_heap_allocate(size_t size, CDataFinalizer_t * finalizer);
58+CHeapItem * c_heap_create_item(void * data, CDataFinalizer * finalizer);
34−void c_heap_free(CHeapItem * item);
36/* *** Idris heap **
60/* *** Idris heap **
37 * Objects without finalizers. Cheney-collected.
61 * Objects without finalizers. Cheney-collected.