1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 09:47:17 +00:00

Add malloc and free to ffi.

Very "unsafe", but a good tool of last resort. In most cases
a buffer is preferable, but the lifetime can be a bit unclear.
This allows very granular control over memory.
This commit is contained in:
Calvin Rose 2022-12-15 21:35:44 -06:00
parent 373cb444fe
commit d5ee6cf521

View File

@ -1467,6 +1467,25 @@ JANET_CORE_FN(janet_core_native_close,
return janet_wrap_nil(); return janet_wrap_nil();
} }
JANET_CORE_FN(cfun_ffi_malloc,
"(ffi/malloc size)",
"Allocates memory directly using the system memory allocator. Memory allocated in this way must be freed manually! Returns a raw pointer, or nil if size = 0.") {
janet_fixarity(argc, 1);
size_t size = janet_getsize(argv, 0);
if (size == 0) return janet_wrap_nil();
return janet_wrap_pointer(malloc(size));
}
JANET_CORE_FN(cfun_ffi_free,
"(ffi/free pointer)",
"Free memory allocated with `ffi/malloc`.") {
janet_fixarity(argc, 1);
if (janet_checktype(argv[0], JANET_NIL)) return janet_wrap_nil();
void *pointer = janet_getpointer(argv, 0);
free(pointer);
return janet_wrap_nil();
}
void janet_lib_ffi(JanetTable *env) { void janet_lib_ffi(JanetTable *env) {
JanetRegExt ffi_cfuns[] = { JanetRegExt ffi_cfuns[] = {
JANET_CORE_REG("ffi/native", janet_core_raw_native), JANET_CORE_REG("ffi/native", janet_core_raw_native),
@ -1481,6 +1500,8 @@ void janet_lib_ffi(JanetTable *env) {
JANET_CORE_REG("ffi/align", cfun_ffi_align), JANET_CORE_REG("ffi/align", cfun_ffi_align),
JANET_CORE_REG("ffi/trampoline", cfun_ffi_get_callback_trampoline), JANET_CORE_REG("ffi/trampoline", cfun_ffi_get_callback_trampoline),
JANET_CORE_REG("ffi/jitfn", cfun_ffi_jitfn), JANET_CORE_REG("ffi/jitfn", cfun_ffi_jitfn),
JANET_CORE_REG("ffi/malloc", cfun_ffi_malloc),
JANET_CORE_REG("ffi/free", cfun_ffi_free),
JANET_REG_END JANET_REG_END
}; };
janet_core_cfuns_ext(env, NULL, ffi_cfuns); janet_core_cfuns_ext(env, NULL, ffi_cfuns);