Adds FFI function to release a handle
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2023-06-24 15:22:49 +02:00
parent 3400d9ec1e
commit 8d49e44e94
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
1 changed files with 14 additions and 0 deletions

View File

@ -9,6 +9,20 @@ use std::hash::Hash;
use std::sync::atomic::AtomicUsize;
use std::sync::{Arc, LazyLock};
/// This function can be called from the FFI to release a handle when it is no longer needed. This
/// does not drop the object per se, but will reduce the reference count of the object. If the object
/// is then no longer referenced, it will be dropped.
#[no_mangle]
extern "C" fn ffi_release_handle(handle: usize) {
let mut write_lock = FFI_OBJECTS.write();
let mut write_lock_inverse = FFI_OBJECTS_INVERSE.write();
let obj = write_lock.remove(&handle);
if let Some(obj) = obj {
write_lock_inverse.remove(&obj);
}
}
/// A handle of an object that can be passed over FFI. We use this to avoid passing pointers over the
/// FFI boundary. This allows us to be able to move the data around in memory without having to worry
/// about the pointers being invalidated. It also allows us to have a type-safe interface.