Skip to content

Commit

Permalink
chore: update free closure
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed May 21, 2024
1 parent bcbe298 commit 4392086
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ const r = load({
retType: DataType.I32, // the return value type
paramsType: [DataType.I32, DataType.I32], // the parameter types
paramsValue: [a, b] // the actual parameter values
// freeResultMemory: true, // whether or not need to free the result of return value memory automatically, default is true
})
equal(r, a + b)
// release library memory when you're not using it.
Expand Down
2 changes: 2 additions & 0 deletions scripts/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export type FuncConstructorOptions = {
paramsType: Array<FieldType>;
retType: FieldType;
needFree?: boolean
// whether or not free function call params memory automatically which are allocated in c side
freeCFuncParamsMemory?: boolean
};

Expand Down Expand Up @@ -160,6 +161,7 @@ export type FFIParams<T extends FieldType, U extends boolean | undefined = undef
// whether need output errno
errno?: U
runInNewThread?: RunInNewThread
// whether or not need to free the result of return value memory automatically, default is true
freeResultMemory?: boolean
}
export function load<T extends FieldType, U extends boolean | undefined = undefined, RunInNewThread extends boolean | undefined = undefined>(
Expand Down
45 changes: 25 additions & 20 deletions src/datatype/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ unsafe fn free_struct_memory(
} else if let FFITag::Function = get_ffi_tag(&obj) {
let func_desc = get_func_desc(&obj);
if func_desc.need_free {
let _ = free(*(ptr as *mut *mut Closure) as *mut c_void);
match ptr_type {
PointerType::CPointer => free(*(ptr as *mut *mut c_void)),
PointerType::RsPointer => free_closure(ptr),
}
}
} else {
// struct
Expand Down Expand Up @@ -311,24 +314,7 @@ pub unsafe fn free_rs_pointer_memory(
} else if let FFITag::Function = ffi_tag {
let func_desc = get_func_desc(&obj);
if func_desc.need_free {
CLOSURE_MAP
.as_ref()
.unwrap()
.get(&ptr)
.unwrap()
.iter()
.enumerate()
.for_each(|(index, p)| {
if index == 0 {
use napi::threadsafe_function::{ErrorStrategy, ThreadsafeFunction};
let _ = Box::from_raw(
(*p) as *mut ThreadsafeFunction<Vec<RsArgsValue>, ErrorStrategy::Fatal>,
)
.abort();
} else {
let _ = Box::from_raw(*p);
}
});
free_closure(ptr)
}
} else {
use super::object_calculate::calculate_struct_size;
Expand All @@ -347,6 +333,25 @@ pub unsafe fn free_rs_pointer_memory(
}
}

unsafe fn free_closure(ptr: *mut c_void) {
CLOSURE_MAP
.as_ref()
.unwrap()
.get(&ptr)
.unwrap()
.iter()
.enumerate()
.for_each(|(index, p)| {
if index == 0 {
use napi::threadsafe_function::{ErrorStrategy, ThreadsafeFunction};
let _ =
Box::from_raw((*p) as *mut ThreadsafeFunction<Vec<RsArgsValue>, ErrorStrategy::Fatal>)
.abort();
} else {
let _ = Box::from_raw(*p);
}
});
}
pub unsafe fn free_c_pointer_memory(
ptr: *mut c_void,
ptr_desc: RsArgsValue,
Expand Down Expand Up @@ -390,7 +395,7 @@ pub unsafe fn free_c_pointer_memory(
} else if let FFITag::Function = ffi_tag {
let func_desc = get_func_desc(&obj);
if func_desc.need_free {
let _ = free(*(ptr as *mut *mut Closure) as *mut c_void);
free(*(ptr as *mut *mut c_void))
}
} else {
// struct
Expand Down
1 change: 0 additions & 1 deletion src/utils/dataprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use napi::{
};
use std::collections::HashMap;
use std::ffi::CStr;
use std::fmt::format;

pub unsafe fn get_js_external_wrap_data(env: &Env, js_external: JsExternal) -> Result<*mut c_void> {
use std::any::TypeId;
Expand Down

0 comments on commit 4392086

Please sign in to comment.