Skip to content

Commit 658e837

Browse files
Only use Dart implementation of memcpy if it is not available.
1 parent 84f6aa2 commit 658e837

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

objectbox/lib/src/native/bindings/nativemem.dart

+20-10
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ import 'dart:io';
88
final _dart_memset memset =
99
_stdlib.lookupFunction<_c_memset, _dart_memset>('memset');
1010

11-
/// If the native memcpy function should not be used.
12-
///
13-
/// memcpy is not available to Flutter on iOS 15 simulator,
14-
/// so use Dart API to copy data via asTypedList (which is much slower but works).
15-
///
16-
/// https://github.com/objectbox/objectbox-dart/issues/313
17-
final isMemcpyNotAvailable = Platform.isIOS;
11+
final _dart_memcpy? _memcpyNative = _lookupMemcpyOrNull();
12+
13+
_dart_memcpy? _lookupMemcpyOrNull() {
14+
try {
15+
return _stdlib.lookupFunction<_c_memcpy, _dart_memcpy>('memcpy');
16+
} catch (_) {
17+
return null;
18+
}
19+
}
20+
21+
/// If the native memcpy function is not available
22+
/// and a Dart implementation is used.
23+
final isMemcpyNotAvailable = _memcpyNative == null;
24+
1825
final _dart_memcpy _memcpyDart = (dest, src, length) {
1926
dest
2027
.asTypedList(length)
@@ -23,9 +30,12 @@ final _dart_memcpy _memcpyDart = (dest, src, length) {
2330

2431
/// memcpy (destination, source, num) copies the values of num bytes from the
2532
/// data pointed to by source to the memory block pointed to by destination.
26-
final _dart_memcpy memcpy = isMemcpyNotAvailable
27-
? _memcpyDart
28-
: _stdlib.lookupFunction<_c_memcpy, _dart_memcpy>('memcpy');
33+
///
34+
/// Note: the native memcpy might not be available
35+
/// (e.g. for Flutter on iOS 15 simulator), then a Dart implementation is used
36+
/// to copy data via asTypedList (which is much slower).
37+
/// https://github.com/objectbox/objectbox-dart/issues/313
38+
final _dart_memcpy memcpy = _memcpyNative ?? _memcpyDart;
2939

3040
// FFI signature
3141
typedef _dart_memset = void Function(Pointer<Uint8>, int, int);

0 commit comments

Comments
 (0)