@@ -9,7 +9,7 @@ import '../../flatbuffers/flat_buffers.dart' as fb;
9
9
// ignore_for_file: public_member_api_docs
10
10
11
11
class BuilderWithCBuffer {
12
- final _allocator = _Allocator ();
12
+ final _allocator = Allocator ();
13
13
final int _initialSize;
14
14
final int _resetIfLargerThan;
15
15
@@ -34,25 +34,16 @@ class BuilderWithCBuffer {
34
34
}
35
35
}
36
36
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 ();
48
38
}
49
39
50
40
// 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 );
52
43
53
- _dart_memset _memset ;
44
+ _dart_memset fbMemset ;
54
45
55
- class _Allocator extends fb.Allocator {
46
+ class Allocator extends fb.Allocator {
56
47
// we may have multiple allocations at once (e.g. during [reallocate()])
57
48
final _allocs = < ByteData , Pointer <Uint8 >> {};
58
49
int _capacity = 0 ;
@@ -83,30 +74,38 @@ class _Allocator extends fb.Allocator {
83
74
84
75
@override
85
76
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 {
90
80
// DynamicLibrary.process() is not available on Windows, let's load a
91
81
// lib that defines 'memset()' it - should be mscvr100 or mscvrt DLL.
92
82
// 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
+ };
96
93
}
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' );
108
97
}
109
98
}
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
+ }
111
110
}
112
111
}
0 commit comments