From 7e961ef3911e0a98f8e87c3aaa115a596dfcf9a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Mon, 9 Sep 2024 21:12:13 +0200 Subject: [PATCH] ffi: don't unnecessarily create SharedArrayBuffer instances What we really want is an ArrayBuffer where memory is not freed when the JS object is collected, since the memory is allocated and freed by libffi. In addition, we can remove that hack with the magic number in the SAB allocator. --- src/mod_ffi.c | 8 ++++---- src/vm.c | 7 ------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/mod_ffi.c b/src/mod_ffi.c index 401f9995..f9fbcff3 100644 --- a/src/mod_ffi.c +++ b/src/mod_ffi.c @@ -844,8 +844,8 @@ static JSValue js_get_cstring(JSContext *ctx, JSValue this_val, int argc, JSValu } -static JSValue TJS_NewUint8ArrayShared(JSContext *ctx, uint8_t *data, size_t size) { - return JS_NewUint8Array(ctx, data, size, NULL, NULL, true); +static JSValue TJS_NewUint8ArrayExternal(JSContext *ctx, uint8_t *data, size_t size) { + return JS_NewUint8Array(ctx, data, size, NULL, NULL, false); } static JSValue js_ptr_to_buffer(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { @@ -855,7 +855,7 @@ static JSValue js_ptr_to_buffer(JSContext *ctx, JSValue this_val, int argc, JSVa JS_TO_UINTPTR_T(ctx, &ptr, argv[0]); size_t sz; JS_TO_SIZE_T(ctx, &sz, argv[1]); - return TJS_NewUint8ArrayShared(ctx, ptr, sz); + return TJS_NewUint8ArrayExternal(ctx, ptr, sz); } static JSValue js_deref_ptr(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { @@ -916,7 +916,7 @@ void js_ffi_closure_invoke(ffi_cif *cif, void *ret, void **args, void *userptr) JSValue *jsargs = js_malloc(ctx, sizeof(JSValue) * cif->nargs); for (unsigned i = 0; i < cif->nargs; i++) { - jsargs[i] = TJS_NewUint8ArrayShared(ctx, args[i], ffi_type_get_sz(cif->arg_types[i])); + jsargs[i] = TJS_NewUint8ArrayExternal(ctx, args[i], ffi_type_get_sz(cif->arg_types[i])); } JSValue jsret = JS_Call(ctx, jscl->func, JS_UNDEFINED, cif->nargs, jsargs); for (unsigned i = 0; i < cif->nargs; i++) { diff --git a/src/vm.c b/src/vm.c index 277a108a..b47278e8 100644 --- a/src/vm.c +++ b/src/vm.c @@ -97,9 +97,7 @@ static const JSMallocFunctions tjs_mf = { /* SharedArrayBuffer functions */ -#define TJS__SAB_MAGIC 0xCAFECAFECAFECAFEULL typedef struct { - uint64_t magic; int ref_count; uint8_t buf[0]; } TJSSABHeader; @@ -112,15 +110,12 @@ static void *tjs__sab_alloc(void *opaque, size_t size) { TJSSABHeader *sab = tjs__malloc(sizeof(*sab) + size); if (!sab) return NULL; - sab->magic = TJS__SAB_MAGIC; sab->ref_count = 1; return sab->buf; } void tjs__sab_free(void *opaque, void *ptr) { TJSSABHeader *sab = (TJSSABHeader *) ((uint8_t *) ptr - sizeof(TJSSABHeader)); - if (sab->magic != TJS__SAB_MAGIC) - return; int ref_count = atomic_add_int(&sab->ref_count, -1); assert(ref_count >= 0); if (ref_count == 0) @@ -129,8 +124,6 @@ void tjs__sab_free(void *opaque, void *ptr) { void tjs__sab_dup(void *opaque, void *ptr) { TJSSABHeader *sab = (TJSSABHeader *) ((uint8_t *) ptr - sizeof(TJSSABHeader)); - if (sab->magic != TJS__SAB_MAGIC) - return; atomic_add_int(&sab->ref_count, 1); }