Skip to content

Commit

Permalink
ffi: don't unnecessarily create SharedArrayBuffer instances
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
saghul committed Sep 9, 2024
1 parent 5ecea22 commit 7e961ef
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/mod_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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++) {
Expand Down
7 changes: 0 additions & 7 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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);
}

Expand Down

0 comments on commit 7e961ef

Please sign in to comment.