Skip to content

Commit 164778d

Browse files
committed
flatbuffers allocator - backport some changes from main
1 parent ff3c9ac commit 164778d

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

objectbox/lib/src/bindings/flatbuffers.dart

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import '../../flatbuffers/flat_buffers.dart' as fb;
99
// ignore_for_file: public_member_api_docs
1010

1111
class BuilderWithCBuffer {
12-
final _allocator = _Allocator();
12+
final _allocator = Allocator();
1313
final int _initialSize;
1414
final int _resetIfLargerThan;
1515

@@ -34,25 +34,16 @@ class BuilderWithCBuffer {
3434
}
3535
}
3636

37-
void clear() {
38-
if (_allocator._allocs.isEmpty) return;
39-
if (_allocator._allocs.length == 1) {
40-
// This is the most common case so no need to create an intermediary list.
41-
_allocator.deallocate(_allocator._allocs.keys.first);
42-
} else {
43-
_allocator._allocs.keys
44-
.toList(growable: false)
45-
.forEach(_allocator.deallocate);
46-
}
47-
}
37+
void clear() => _allocator.freeAll();
4838
}
4939

5040
// FFI signature
51-
typedef _dart_memset = void Function(Pointer<Void>, int, int);
41+
typedef _dart_memset = void Function(Pointer<Uint8>, int, int);
42+
typedef _c_memset = Void Function(Pointer<Uint8>, Int32, IntPtr);
5243

53-
_dart_memset _memset;
44+
_dart_memset fbMemset;
5445

55-
class _Allocator extends fb.Allocator {
46+
class Allocator extends fb.Allocator {
5647
// we may have multiple allocations at once (e.g. during [reallocate()])
5748
final _allocs = <ByteData, Pointer<Uint8>>{};
5849
int _capacity = 0;
@@ -83,30 +74,38 @@ class _Allocator extends fb.Allocator {
8374

8475
@override
8576
void clear(ByteData data, bool _) {
86-
if (_memset == null) {
87-
try {
88-
DynamicLibrary lib;
89-
if (Platform.isWindows) {
77+
if (fbMemset == null) {
78+
if (Platform.isWindows) {
79+
try {
9080
// DynamicLibrary.process() is not available on Windows, let's load a
9181
// lib that defines 'memset()' it - should be mscvr100 or mscvrt DLL.
9282
// mscvr100.dll is in the frequently installed MSVC Redistributable.
93-
lib = DynamicLibrary.open('msvcr100.dll');
94-
} else {
95-
lib = DynamicLibrary.process();
83+
fbMemset = DynamicLibrary.open('msvcr100.dll')
84+
.lookupFunction<_c_memset, _dart_memset>('memset');
85+
} catch (_) {
86+
// fall back if we can't load a native memset()
87+
fbMemset = (Pointer<Uint8> ptr, int byte, int size) {
88+
final bytes = ptr.cast<Uint8>();
89+
for (var i = 0; i < size; i++) {
90+
bytes[i] = byte;
91+
}
92+
};
9693
}
97-
_memset = lib.lookupFunction<
98-
Void Function(Pointer<Void>, Int32, IntPtr),
99-
_dart_memset>('memset');
100-
} catch (_) {
101-
// fall back if we can't load a native memset()
102-
_memset = (Pointer<Void> ptr, int byte, int size) {
103-
final bytes = ptr.cast<Uint8>();
104-
for (var i = 0; i < size; i++) {
105-
bytes[i] = byte;
106-
}
107-
};
94+
} else {
95+
fbMemset = DynamicLibrary.process()
96+
.lookupFunction<_c_memset, _dart_memset>('memset');
10897
}
10998
}
110-
_memset(_allocs[data].cast<Void>(), 0, data.lengthInBytes);
99+
fbMemset(_allocs[data], 0, data.lengthInBytes);
100+
}
101+
102+
void freeAll() {
103+
if (_allocs.isEmpty) return;
104+
if (_allocs.length == 1) {
105+
// This is the most common case so no need to create an intermediary list.
106+
deallocate(_allocs.keys.first);
107+
} else {
108+
_allocs.keys.toList(growable: false).forEach(deallocate);
109+
}
111110
}
112111
}

0 commit comments

Comments
 (0)