Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Remove use of deprecated FFI features via package:ffi. #29

Merged
merged 2 commits into from
Oct 14, 2019
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
3 changes: 2 additions & 1 deletion lib/src/bindings/bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utf8> Function() TfLiteVersion = tflitelib
Expand Down
3 changes: 2 additions & 1 deletion lib/src/bindings/interpreter_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<TfLiteInterpreterOptions> Function() TfLiteInterpreterOptionsCreate =
Expand Down
3 changes: 2 additions & 1 deletion lib/src/bindings/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<TfLiteModel> Function(Pointer<Void> data, int size) TfLiteNewModel =
Expand Down
3 changes: 2 additions & 1 deletion lib/src/bindings/tensor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<TfLiteTensor> t) =>
Expand Down
37 changes: 0 additions & 37 deletions lib/src/bindings/utf8.dart

This file was deleted.

4 changes: 2 additions & 2 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Expand Down
13 changes: 7 additions & 6 deletions lib/src/tensor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,25 +57,26 @@ class Tensor {
// TODO(shanehop): Prevent access if unallocated.
void copyFrom(Uint8List bytes) {
var size = bytes.length;
final ptr = Pointer<Uint8>.allocate(count: size);
final ptr = allocate<Uint8>(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<Uint8>.allocate(count: size);
final ptr = allocate<Uint8>(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;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/tflite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +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.3-dev.1

dev_dependencies:
pedantic: ^1.0.0
Expand Down