diff --git a/ffigen/config.yaml b/ffigen/config.yaml index 1149eba838..f53419687d 100644 --- a/ffigen/config.yaml +++ b/ffigen/config.yaml @@ -28,6 +28,7 @@ functions: symbol-address: include: - 'realm_dart_.*' + - 'realm_release' structs: exclude: - '_.*' diff --git a/lib/src/native/realm_bindings.dart b/lib/src/native/realm_bindings.dart index 0a190ad98c..421bf2f80c 100644 --- a/lib/src/native/realm_bindings.dart +++ b/lib/src/native/realm_bindings.dart @@ -2951,27 +2951,6 @@ class RealmLibrary { ffi.Pointer Function( ffi.Pointer)>(); - Dart_FinalizableHandle realm_dart_attach_finalizer( - Object handle, - ffi.Pointer realmPtr, - int size, - ) { - return _realm_dart_attach_finalizer( - handle, - realmPtr, - size, - ); - } - - late final _realm_dart_attach_finalizerPtr = _lookup< - ffi.NativeFunction< - Dart_FinalizableHandle Function(ffi.Handle, ffi.Pointer, - ffi.Int)>>('realm_dart_attach_finalizer'); - late final _realm_dart_attach_finalizer = - _realm_dart_attach_finalizerPtr.asFunction< - Dart_FinalizableHandle Function( - Object, ffi.Pointer, int)>(); - ffi.Pointer realm_dart_create_scheduler( int isolateId, int port, @@ -2989,23 +2968,6 @@ class RealmLibrary { late final _realm_dart_create_scheduler = _realm_dart_create_schedulerPtr .asFunction Function(int, int)>(); - void realm_dart_delete_finalizable( - Dart_FinalizableHandle finalizable_handle, - Object handle, - ) { - return _realm_dart_delete_finalizable( - finalizable_handle, - handle, - ); - } - - late final _realm_dart_delete_finalizablePtr = _lookup< - ffi.NativeFunction< - ffi.Void Function(Dart_FinalizableHandle, - ffi.Handle)>>('realm_dart_delete_finalizable'); - late final _realm_dart_delete_finalizable = _realm_dart_delete_finalizablePtr - .asFunction(); - void realm_dart_delete_persistent_handle( ffi.Pointer handle, ) { @@ -9572,22 +9534,11 @@ class RealmLibrary { class _SymbolAddresses { final RealmLibrary _library; _SymbolAddresses(this._library); - ffi.Pointer< - ffi.NativeFunction< - Dart_FinalizableHandle Function( - ffi.Handle, ffi.Pointer, ffi.Int)>> - get realm_dart_attach_finalizer => - _library._realm_dart_attach_finalizerPtr; ffi.Pointer< ffi.NativeFunction< ffi.Pointer Function(ffi.Uint64, Dart_Port)>> get realm_dart_create_scheduler => _library._realm_dart_create_schedulerPtr; - ffi.Pointer< - ffi.NativeFunction< - ffi.Void Function(Dart_FinalizableHandle, ffi.Handle)>> - get realm_dart_delete_finalizable => - _library._realm_dart_delete_finalizablePtr; ffi.Pointer)>> get realm_dart_delete_persistent_handle => _library._realm_dart_delete_persistent_handlePtr; @@ -9662,10 +9613,10 @@ class _SymbolAddresses { ffi.Pointer)>> get realm_dart_weak_handle_to_object => _library._realm_dart_weak_handle_to_objectPtr; + ffi.Pointer)>> + get realm_release => _library._realm_releasePtr; } -typedef Dart_FinalizableHandle = ffi.Pointer<_Dart_FinalizableHandle>; - /// A port is used to send or receive inter-isolate messages typedef Dart_Port = ffi.Int64; @@ -9708,8 +9659,6 @@ class UnnamedUnion2 extends ffi.Union { external int logic_error_kind; } -class _Dart_FinalizableHandle extends ffi.Opaque {} - class realm_app extends ffi.Opaque {} class realm_app_config extends ffi.Opaque {} diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 1cbf17dbdf..a2b90e2ce8 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -1680,15 +1680,23 @@ class LastError { } } -abstract class HandleBase { +void _traceFinalization(Object o) { + Realm.logger.log(RealmLogLevel.info, 'Finalizing: $o'); +} + +final _debugFinalizer = Finalizer(_traceFinalization); + +final _nativeFinalizer = NativeFinalizer(_realmLib.addresses.realm_release); + +abstract class HandleBase implements Finalizable { final Pointer _pointer; - late final Dart_FinalizableHandle _finalizableHandle; HandleBase(this._pointer, int size) { - _finalizableHandle = _realmLib.realm_dart_attach_finalizer(this, _pointer.cast(), size); - if (_finalizableHandle == nullptr) { - throw Exception("Error creating $runtimeType"); - } + _nativeFinalizer.attach(this, _pointer.cast(), detach: this, externalSize: size); + assert(() { + _debugFinalizer.attach(this, _pointer); + return true; + }()); } HandleBase.unowned(this._pointer); @@ -1746,8 +1754,12 @@ class ReleasableHandle extends HandleBase { if (released) { return; } - - _realmLib.realm_dart_delete_finalizable(_finalizableHandle, this); + _nativeFinalizer.detach(this); + assert(() { + _traceFinalization(_pointer); + _debugFinalizer.detach(this); + return true; + }()); _realmLib.realm_release(_pointer.cast()); released = true; } diff --git a/src/realm_dart.cpp b/src/realm_dart.cpp index 90f69bc4e1..a7292bdb3c 100644 --- a/src/realm_dart.cpp +++ b/src/realm_dart.cpp @@ -28,18 +28,6 @@ RLM_API void realm_dart_initializeDartApiDL(void* data) { Dart_InitializeApiDL(data); } -static void handle_finalizer(void* isolate_callback_data, void* realmPtr) { - realm_release(realmPtr); -} - -RLM_API Dart_FinalizableHandle realm_dart_attach_finalizer(Dart_Handle handle, void* realmPtr, int size) { - return Dart_NewFinalizableHandle_DL(handle, realmPtr, size, handle_finalizer); -} - -RLM_API void realm_dart_delete_finalizable(Dart_FinalizableHandle finalizable_handle, Dart_Handle handle) { - Dart_DeleteFinalizableHandle_DL(finalizable_handle, handle); -} - class WeakHandle { public: WeakHandle(Dart_Handle handle) : m_weakHandle(Dart_NewWeakPersistentHandle_DL(handle, this, 1, finalize_handle)) { diff --git a/src/realm_dart.h b/src/realm_dart.h index d79e898780..3c42ac043a 100644 --- a/src/realm_dart.h +++ b/src/realm_dart.h @@ -24,10 +24,6 @@ RLM_API void realm_dart_initializeDartApiDL(void* data); -RLM_API Dart_FinalizableHandle realm_dart_attach_finalizer(Dart_Handle handle, void* realmPtr, int size); - -RLM_API void realm_dart_delete_finalizable(Dart_FinalizableHandle finalizable_handle, Dart_Handle handle); - RLM_API void* realm_dart_object_to_weak_handle(Dart_Handle handle); RLM_API Dart_Handle realm_dart_weak_handle_to_object(void* handle);