Skip to content

Commit e5a442c

Browse files
On iOS use Dart API instead of memcpy.
1 parent 8481811 commit e5a442c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ class ReaderWithCBuffer {
121121
void clear() => malloc.free(_bufferPtr);
122122

123123
ByteData access(Pointer<Uint8> dataPtr, int size) {
124-
if (size > _maxBuffer) {
124+
// If memcpy is not available, instead of using Dart memcpy implementation,
125+
// directly convert to view which is a little faster.
126+
if (isMemcpyNotAvailable || size > _maxBuffer) {
125127
final uint8List = dataPtr.asTypedList(size);
126128
return ByteData.view(uint8List.buffer, uint8List.offsetInBytes, size);
127129
} else {

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,24 @@ 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;
18+
final _dart_memcpy _memcpyDart = (dest, src, length) {
19+
dest
20+
.asTypedList(length)
21+
.setAll(0, src.asTypedList(length).getRange(0, length));
22+
};
23+
1124
/// memcpy (destination, source, num) copies the values of num bytes from the
1225
/// data pointed to by source to the memory block pointed to by destination.
13-
final _dart_memcpy memcpy =
14-
_stdlib.lookupFunction<_c_memcpy, _dart_memcpy>('memcpy');
26+
final _dart_memcpy memcpy = isMemcpyNotAvailable
27+
? _memcpyDart
28+
: _stdlib.lookupFunction<_c_memcpy, _dart_memcpy>('memcpy');
1529

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

0 commit comments

Comments
 (0)