@@ -6,32 +6,56 @@
66
77/* *** C heap ***
88 * Objects with finalizers. Mark&sweep-collected.
9+ *
10+ * The C heap is implemented as a doubly linked list
11+ * of pointers coupled with their finalizers.
912 */
1013
11−typedef void CDataFinalizer_t(void *);
14+typedef void CDataFinalizer(void *);
1215
1316typedef struct CHeapItem {
17+ /// Payload.
1418 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;
24+
25+ /// The mark bit set by the FP heap traversal,
26+ /// cleared by C heap sweep.
1627 bool is_used;
28+
29+ /// Next item in the C heap.
1730 struct CHeapItem * next;
31+
32+ /// Pointer to the previous next-pointer.
1833 struct CHeapItem ** prev_next;
1934} CHeapItem;
2035
2136typedef struct CHeap {
37+ /// The first item in the heap. NULL if the heap is empty.
2238 CHeapItem * first;
2339} CHeap;
2440
41+/// Create a C heap.
2542void c_heap_init(CHeap * c_heap);
26−void c_heap_destroy(CHeap * c_heap); // will call finalizers on everything
43+
44+/// Destroy the given C heap. Will not deallocate the given pointer.
45+/// Will call finalizers & deallocate all blocks in the heap.
46+void c_heap_destroy(CHeap * c_heap);
47+
48+/// Insert the given item into the heap if it's not there yet.
2749void c_heap_insert_if_needed(CHeap * c_heap, CHeapItem * item);
50+
51+/// Mark the given item as used.
2852void c_heap_mark_item(CHeapItem * item);
53+
54+/// Sweep the C heap, finalizing and freeing unused blocks.
2955void c_heap_sweep(CHeap * c_heap);
30−CHeapItem * c_heap_create_item(void * data, CDataFinalizer_t * finalizer);
3156
32−// this is the function to use in C binding code
33−CHeapItem * c_heap_allocate(size_t size, CDataFinalizer_t * finalizer);
34−void c_heap_free(CHeapItem * item);
57+/// Create a C heap item from its payload and finalizer.
58+CHeapItem * c_heap_create_item(void * data, CDataFinalizer * finalizer);
3559
3660/* *** Idris heap **
3761 * Objects without finalizers. Cheney-collected.