From 4e6d776289004a3a125481e397d2c925c5f82c28 Mon Sep 17 00:00:00 2001 From: Samir Jindel Date: Thu, 10 Oct 2019 11:21:19 +0200 Subject: [PATCH 1/2] Remove use of deprecated FFI features via package:ffi. --- lib/src/bindings/bindings.dart | 3 +- lib/src/bindings/interpreter_options.dart | 3 +- lib/src/bindings/model.dart | 3 +- lib/src/bindings/tensor.dart | 3 +- lib/src/bindings/utf8.dart | 37 ----------------------- lib/src/model.dart | 4 +-- lib/src/tensor.dart | 13 ++++---- lib/tflite.dart | 3 +- pubspec.yaml | 1 + 9 files changed, 20 insertions(+), 50 deletions(-) delete mode 100644 lib/src/bindings/utf8.dart diff --git a/lib/src/bindings/bindings.dart b/lib/src/bindings/bindings.dart index 2fcbddd..b01ab02 100644 --- a/lib/src/bindings/bindings.dart +++ b/lib/src/bindings/bindings.dart @@ -4,8 +4,9 @@ import 'dart:ffi'; +import 'package:ffi/ffi.dart'; + import 'dlib.dart'; -import 'utf8.dart'; /// Version information for the TensorFlowLite library. Pointer Function() TfLiteVersion = tflitelib diff --git a/lib/src/bindings/interpreter_options.dart b/lib/src/bindings/interpreter_options.dart index b424bb2..dfcf0e4 100644 --- a/lib/src/bindings/interpreter_options.dart +++ b/lib/src/bindings/interpreter_options.dart @@ -4,9 +4,10 @@ import 'dart:ffi'; +import 'package:ffi/ffi.dart'; + import 'dlib.dart'; import 'types.dart'; -import 'utf8.dart'; /// Returns a new interpreter options instances. Pointer Function() TfLiteInterpreterOptionsCreate = diff --git a/lib/src/bindings/model.dart b/lib/src/bindings/model.dart index 2d33c7f..ad74f87 100644 --- a/lib/src/bindings/model.dart +++ b/lib/src/bindings/model.dart @@ -4,9 +4,10 @@ import 'dart:ffi'; +import 'package:ffi/ffi.dart'; + import 'dlib.dart'; import 'types.dart'; -import 'utf8.dart'; /// Returns a model from the provided buffer, or null on failure. Pointer Function(Pointer data, int size) TfLiteNewModel = diff --git a/lib/src/bindings/tensor.dart b/lib/src/bindings/tensor.dart index 79beb65..e465dd6 100644 --- a/lib/src/bindings/tensor.dart +++ b/lib/src/bindings/tensor.dart @@ -4,9 +4,10 @@ import 'dart:ffi'; +import 'package:ffi/ffi.dart'; + import 'dlib.dart'; import 'types.dart'; -import 'utf8.dart'; /// Returns the type of a tensor element. TfLiteType TfLiteTensorType(Pointer t) => diff --git a/lib/src/bindings/utf8.dart b/lib/src/bindings/utf8.dart deleted file mode 100644 index ce04494..0000000 --- a/lib/src/bindings/utf8.dart +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -import 'dart:convert'; -import 'dart:ffi'; - -/// Represents a string (char*) in C memory. -class Utf8 extends Struct { - @Uint8() - int char; - - /// Allocates and stores the given Dart [String] as a [Pointer]. - static Pointer toUtf8(String str) { - final ptr = Pointer.allocate(count: str.length + 1); - final units = const Utf8Encoder().convert(str); - units - .asMap() - .forEach((i, unit) => ptr.elementAt(i).load().char = unit); - ptr.elementAt(units.length).load().char = 0; - return ptr; - } - - /// Gets the Dart [String] representation of a [Pointer]. - static String fromUtf8(Pointer ptr) { - final units = []; - var len = 0; - for (;;) { - final char = ptr.elementAt(len++).load().char; - if (char == 0) { - break; - } - units.add(char); - } - return const Utf8Decoder().convert(units); - } -} diff --git a/lib/src/model.dart b/lib/src/model.dart index 56dab1f..a9175ac 100644 --- a/lib/src/model.dart +++ b/lib/src/model.dart @@ -4,11 +4,11 @@ import 'dart:ffi'; +import 'package:ffi/ffi.dart'; import 'package:quiver/check.dart'; import 'bindings/model.dart'; import 'bindings/types.dart'; -import 'bindings/utf8.dart'; import 'ffi/helper.dart'; /// TensorFlowLite model. @@ -24,7 +24,7 @@ class Model { factory Model.fromFile(String path) { final cpath = Utf8.toUtf8(path); final model = TfLiteModelCreateFromFile(cpath); - cpath.free(); + free(cpath); checkArgument(isNotNull(model), message: 'Unable to create model.'); return Model._(model); } diff --git a/lib/src/tensor.dart b/lib/src/tensor.dart index 3dce4a4..06d1438 100644 --- a/lib/src/tensor.dart +++ b/lib/src/tensor.dart @@ -5,11 +5,11 @@ import 'dart:ffi'; import 'dart:typed_data'; +import 'package:ffi/ffi.dart'; import 'package:quiver/check.dart'; import 'bindings/tensor.dart'; import 'bindings/types.dart'; -import 'bindings/utf8.dart'; import 'ffi/helper.dart'; export 'bindings/types.dart' show TfLiteType; @@ -57,25 +57,26 @@ class Tensor { // TODO(shanehop): Prevent access if unallocated. void copyFrom(Uint8List bytes) { var size = bytes.length; - final ptr = Pointer.allocate(count: size); + final ptr = allocate(count: size); final externalTypedData = ptr.asExternalTypedData(count: size) as Uint8List; externalTypedData.setRange(0, bytes.length, bytes); checkState(TfLiteTensorCopyFromBuffer(_tensor, ptr.cast(), bytes.length) == TfLiteStatus.ok); - ptr.free(); + free(ptr); } /// Returns a copy of the underlying data buffer. // TODO(shanehop): Prevent access if unallocated. Uint8List copyTo() { var size = TfLiteTensorByteSize(_tensor); - final ptr = Pointer.allocate(count: size); + final ptr = allocate(count: size); final externalTypedData = ptr.asExternalTypedData(count: size) as Uint8List; checkState( TfLiteTensorCopyToBuffer(_tensor, ptr.cast(), 4) == TfLiteStatus.ok); - // clone the data, because once `ptr.free()`, `externalTypedData` will be volatile + // Clone the data, because once `free(ptr)`, `externalTypedData` will be + // volatile final bytes = externalTypedData.sublist(0); - ptr.free(); + free(ptr); return bytes; } diff --git a/lib/tflite.dart b/lib/tflite.dart index 7f40b7b..26f600d 100644 --- a/lib/tflite.dart +++ b/lib/tflite.dart @@ -5,8 +5,9 @@ /// TensorFlow Lite for Dart library tflite; +import 'package:ffi/ffi.dart'; + import 'src/bindings/bindings.dart'; -import 'src/bindings/utf8.dart'; export 'src/interpreter.dart'; export 'src/interpreter_options.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index 14c8693..13c77cd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,6 +12,7 @@ environment: dependencies: path: ^1.6.2 quiver: ^2.0.3 + ffi: ^0.1.1+1 dev_dependencies: pedantic: ^1.0.0 From 494d19b5ee8718781c988fecdc3c49e194865e74 Mon Sep 17 00:00:00 2001 From: Samir Jindel Date: Thu, 10 Oct 2019 14:55:08 +0200 Subject: [PATCH 2/2] Update pubspec.yaml --- pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index 13c77cd..05a1a45 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,12 +7,12 @@ description: >- homepage: https://github.com/dart-lang/tflite_native environment: - sdk: '>=2.5.0 <3.0.0' + sdk: '>=2.6.0-dev.6.0 <3.0.0' dependencies: path: ^1.6.2 quiver: ^2.0.3 - ffi: ^0.1.1+1 + ffi: ^0.1.3-dev.1 dev_dependencies: pedantic: ^1.0.0