From a0a889ede9c85b8d7b6155a0ce19bcee45c4c654 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 29 Dec 2015 14:46:45 -0800 Subject: [PATCH] Attach FFI closure deallocation to Functor's pool. --- Execute.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Execute.cpp b/Execute.cpp index ced61fc..7875d88 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -901,9 +901,18 @@ static JSValueRef FunctionAdapter_(JSContextRef context, size_t count, JSValueRe return CYCallAsFunction(context, function, NULL, count, values); } +#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) +static void CYFreeFunctor(void *data) { + ffi_closure_free(data); +} +#else +static void CYFreeFunctor(void *data) { + _syscall(munmap(data, sizeof(ffi_closure))); +} +#endif + Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)) { // XXX: in case of exceptions this will leak - // XXX: in point of fact, this may /need/ to leak :( Closure_privateData *internal(new Closure_privateData(context, function, adapter, signature)); #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) @@ -913,6 +922,7 @@ Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, ffi_status status(ffi_prep_closure_loc(writable, &internal->cif_, &CYExecuteClosure, internal, executable)); _assert(status == FFI_OK); + internal->pool_->atexit(&CYFreeFunctor, writable); internal->value_ = executable; #else ffi_closure *closure((ffi_closure *) _syscall(mmap( @@ -926,6 +936,7 @@ Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); + internal->pool_->atexit(&CYFreeFunctor, closure); internal->value_ = closure; #endif -- 2.49.0