on master help sign in
Improve C heap.
bf37049Matus Tejiscak
rts/idris_heap.h+10−2
@@ -4,11 +4,15 @@
4#include <stdbool.h>
4#include <stdbool.h>
5#include <stddef.h>
5#include <stddef.h>
6
6
7+/* *** C heap ***
8+ * Objects with finalizers. Mark&sweep-collected.
9+ */
10+
7typedef void CDataFinalizer_t(void *);
11typedef void CDataFinalizer_t(void *);
8
12
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 {
19} CHeap;
23} CHeap;
20
24
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);
31
35
36+/* *** Idris heap **
37+ * Objects without finalizers. Cheney-collected.
38+ */
39+
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
rts/idris_rts.c+5
@@ -132,6 +132,11 @@ CData cdata_allocate(size_t size, CDataFinalizer_t finalizer)
132 return c_heap_allocate(size, finalizer);
132 return c_heap_allocate(size, finalizer);
133}
133}
134
134
135+CData cdata_manage(void * data, CDataFinalizer_t finalizer)
136+{
137+ return c_heap_create_item(data, finalizer);
138+}
139+
135void cdata_free(CData cd)
140void cdata_free(CData cd)
136{
141{
137 c_heap_free(cd);
142 c_heap_free(cd);
rts/idris_rts.h+5
@@ -122,11 +122,16 @@ typedef struct VM_t VM;
122 * access it only in the following three ways:
122 * access it only in the following three ways:
123 *
123 *
124 * CData cd = cdata_allocate(...);
124 * CData cd = cdata_allocate(...);
125+ * or
126+ * void * ptr = malloc(...);
127+ * CData cd = cdata_manage(ptr, ...);
128+ *
125 * use(cd->data);
129 * use(cd->data);
126 * cdata_free(cd); // optionally
130 * cdata_free(cd); // optionally
127 *
131 *
128 */
132 */
129typedef CHeapItem * CData;
133typedef CHeapItem * CData;
134+CData cdata_manage(void * data, CDataFinalizer_t * finalizer);
130CData cdata_allocate(size_t size, CDataFinalizer_t * finalizer);
135CData cdata_allocate(size_t size, CDataFinalizer_t * finalizer);
131void cdata_free(CData cd); // explicit freeing not required but available
136void cdata_free(CData cd); // explicit freeing not required but available
132
137