diff --git a/src/ffi/ffi_handle.rs b/src/ffi/ffi_handle.rs index 82b70c4..d05596a 100644 --- a/src/ffi/ffi_handle.rs +++ b/src/ffi/ffi_handle.rs @@ -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.