Skip to content

Commit

Permalink
HandleBase implements Finalizable, and uses a common NativeFinalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Aug 15, 2022
1 parent 4af02d4 commit 5ec672f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 77 deletions.
1 change: 1 addition & 0 deletions ffigen/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ functions:
symbol-address:
include:
- 'realm_dart_.*'
- 'realm_release'
structs:
exclude:
- '_.*'
Expand Down
55 changes: 2 additions & 53 deletions lib/src/native/realm_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2951,27 +2951,6 @@ class RealmLibrary {
ffi.Pointer<realm_thread_safe_reference_t> Function(
ffi.Pointer<ffi.Void>)>();

Dart_FinalizableHandle realm_dart_attach_finalizer(
Object handle,
ffi.Pointer<ffi.Void> 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.Void>,
ffi.Int)>>('realm_dart_attach_finalizer');
late final _realm_dart_attach_finalizer =
_realm_dart_attach_finalizerPtr.asFunction<
Dart_FinalizableHandle Function(
Object, ffi.Pointer<ffi.Void>, int)>();

ffi.Pointer<realm_scheduler_t> realm_dart_create_scheduler(
int isolateId,
int port,
Expand All @@ -2989,23 +2968,6 @@ class RealmLibrary {
late final _realm_dart_create_scheduler = _realm_dart_create_schedulerPtr
.asFunction<ffi.Pointer<realm_scheduler_t> 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 Function(Dart_FinalizableHandle, Object)>();

void realm_dart_delete_persistent_handle(
ffi.Pointer<ffi.Void> handle,
) {
Expand Down Expand Up @@ -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.Void>, ffi.Int)>>
get realm_dart_attach_finalizer =>
_library._realm_dart_attach_finalizerPtr;
ffi.Pointer<
ffi.NativeFunction<
ffi.Pointer<realm_scheduler_t> 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<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void>)>>
get realm_dart_delete_persistent_handle =>
_library._realm_dart_delete_persistent_handlePtr;
Expand Down Expand Up @@ -9662,10 +9613,10 @@ class _SymbolAddresses {
ffi.Pointer<ffi.NativeFunction<ffi.Handle Function(ffi.Pointer<ffi.Void>)>>
get realm_dart_weak_handle_to_object =>
_library._realm_dart_weak_handle_to_objectPtr;
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void>)>>
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;

Expand Down Expand Up @@ -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 {}
Expand Down
28 changes: 20 additions & 8 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1680,15 +1680,23 @@ class LastError {
}
}

abstract class HandleBase<T extends NativeType> {
void _traceFinalization(Object o) {
Realm.logger.log(RealmLogLevel.info, 'Finalizing: $o');
}

final _debugFinalizer = Finalizer<Object>(_traceFinalization);

final _nativeFinalizer = NativeFinalizer(_realmLib.addresses.realm_release);

abstract class HandleBase<T extends NativeType> implements Finalizable {
final Pointer<T> _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);
Expand Down Expand Up @@ -1746,8 +1754,12 @@ class ReleasableHandle<T extends NativeType> extends HandleBase<T> {
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;
}
Expand Down
12 changes: 0 additions & 12 deletions src/realm_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 0 additions & 4 deletions src/realm_dart.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 5ec672f

Please sign in to comment.