on master help sign in
Some RTS functions to help with allocation in foreign functions
commit50fd9b5ff22a7c8a5dbf8eddbe5db1685bc359ff authorEdwin Brady <ecb10@st-andrews.ac.uk> dateSat 9 Mar 2013, 12:39 (14 years ago) parent03db88d
2 files changed+46
rts/idris_rts.c+18 rts/idris_rts.h+28
rts/idris_rts.c+18
@@ -77,6 +77,24 @@ void terminate(VM* vm) {
7777 free(vm);
7878}
7979
80+void idris_requireAlloc(VM* vm, size_t size) {
81+ if (!(vm -> heap_next + size < vm -> heap_end)) {
82+ idris_gc(vm);
83+ }
84+
85+ int lock = vm->processes > 0;
86+ if (lock) { // not message passing
87+ pthread_mutex_lock(&vm->alloc_lock);
88+ }
89+}
90+
91+void idris_doneAlloc(VM* vm) {
92+ int lock = vm->processes > 0;
93+ if (lock) { // not message passing
94+ pthread_mutex_unlock(&vm->alloc_lock);
95+ }
96+}
97+
8098void* allocate(VM* vm, size_t size, int outerlock) {
8199// return malloc(size);
82100 int lock = vm->processes > 0 && !outerlock;
rts/idris_rts.h+28
@@ -168,6 +168,15 @@ void SLIDE(VM* vm, int args);
168168void* allocate(VM* vm, size_t size, int outerlock);
169169// void* allocCon(VM* vm, int arity, int outerlock);
170170
171+// When allocating from C, call 'idris_requireAlloc' with a size to
172+// guarantee that no garbage collection will happen (and hence nothing
173+// will move) until at least size bytes have been allocated.
174+// idris_doneAlloc *must* be called when allocation from C is done (as it
175+// may take a lock if other threads are running).
176+
177+void idris_requireAlloc(VM* vm, size_t size);
178+void idris_doneAlloc(VM* vm);
179+
171180#define allocCon(cl, vm, t, a, o) \
172181 cl = allocate(vm, sizeof(Closure) + sizeof(VAL)*a, o); \
173182 SETTY(cl, CON); \
@@ -222,6 +231,25 @@ VAL idris_getArg(VM* vm, int i);
222231
223232void stackOverflow();
224233
234+// Some FFI help (functions and macros below are all which C code should
235+// use)
236+
237+// When allocating from C, call 'idris_requireAlloc' with a size to
238+// guarantee that no garbage collection will happen (and hence nothing
239+// will move) until at least size bytes have been allocated.
240+// idris_doneAlloc *must* be called when allocation from C is done (as it
241+// may take a lock if other threads are running).
242+
243+void idris_requireAlloc(VM* vm, size_t size);
244+void idris_doneAlloc(VM* vm);
245+
246+// I think these names are nicer for an API...
247+
248+#define idris_constructor allocCon
249+#define idris_setConArg SETARG
250+#define idris_getConArg GETARG
251+#define idris_mkInt(x) MKINT((intptr_t)(x))
252+
225253#include "idris_gmp.h"
226254
227255#endif