Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generator/integration-tests/part-partof/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ environment:
dependencies:
objectbox:
json_serializable:
json_annotation: "^4.3.0"
freezed_annotation:

dev_dependencies:
Expand Down
4 changes: 3 additions & 1 deletion objectbox/lib/src/native/bindings/flatbuffers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ class ReaderWithCBuffer {
void clear() => malloc.free(_bufferPtr);

ByteData access(Pointer<Uint8> dataPtr, int size) {
if (size > _maxBuffer) {
// If memcpy is not available, instead of using Dart memcpy implementation,
// directly convert to view which is a little faster.
if (isMemcpyNotAvailable || size > _maxBuffer) {
final uint8List = dataPtr.asTypedList(size);
return ByteData.view(uint8List.buffer, uint8List.offsetInBytes, size);
} else {
Expand Down
28 changes: 26 additions & 2 deletions objectbox/lib/src/native/bindings/nativemem.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,34 @@ import 'dart:io';
final _dart_memset memset =
_stdlib.lookupFunction<_c_memset, _dart_memset>('memset');

final _dart_memcpy? _memcpyNative = _lookupMemcpyOrNull();

_dart_memcpy? _lookupMemcpyOrNull() {
try {
return _stdlib.lookupFunction<_c_memcpy, _dart_memcpy>('memcpy');
} catch (_) {
return null;
}
}

/// If the native memcpy function is not available
/// and a Dart implementation is used.
final isMemcpyNotAvailable = _memcpyNative == null;

final _dart_memcpy _memcpyDart = (dest, src, length) {
dest
.asTypedList(length)
.setAll(0, src.asTypedList(length).getRange(0, length));
};

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

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