[vm/ffi] Migrate to empty Structs to Opaque

This CL migrates empty `Struct`s to `Opaque` native types.
It stops using `.ref` on `Pointer`s to these types.
And stops using `.ref` on `Pointer<Utf8>` which will be migrated in
`package:ffi`.

Issue: https://github.com/dart-lang/sdk/issues/44622
Issue: https://github.com/dart-lang/sdk/issues/43974

Change-Id: I3aa256af7d4ceaa8ee37b1b2ada71f870f43617b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179181
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
This commit is contained in:
Daco Harkes 2021-01-15 10:52:53 +00:00 committed by commit-bot@chromium.org
parent 0fde98290e
commit ac805dff54
14 changed files with 58 additions and 54 deletions

View file

@ -27,7 +27,7 @@ Uint8List inventData(int length) {
return result;
}
Uint8List toUint8List(Bytes bytes, int length) {
Uint8List toUint8List(Pointer<Bytes> bytes, int length) {
final result = Uint8List(length);
final uint8bytes = bytes.asUint8Pointer();
for (int i = 0; i < length; i++) {
@ -36,7 +36,7 @@ Uint8List toUint8List(Bytes bytes, int length) {
return result;
}
void copyFromUint8ListToTarget(Uint8List source, Data target) {
void copyFromUint8ListToTarget(Uint8List source, Pointer<Data> target) {
final int length = source.length;
final uint8target = target.asUint8Pointer();
for (int i = 0; i < length; i++) {
@ -52,7 +52,7 @@ String hash(Pointer<Data> data, int length, Pointer<EVP_MD> hashAlgorithm) {
final Pointer<Bytes> result = calloc<Uint8>(resultSize).cast();
EVP_DigestFinal(context, result, nullptr);
EVP_MD_CTX_free(context);
final String hash = base64Encode(toUint8List(result.ref, resultSize));
final String hash = base64Encode(toUint8List(result, resultSize));
calloc.free(result);
return hash;
}
@ -85,7 +85,7 @@ class DigestCMemory extends BenchmarkBase {
@override
void setup() {
data = calloc<Uint8>(L).cast();
copyFromUint8ListToTarget(inventData(L), data.ref);
copyFromUint8ListToTarget(inventData(L), data);
hash(data, L, hashAlgorithm);
}
@ -115,7 +115,7 @@ class DigestDartMemory extends BenchmarkBase {
void setup() {
data = inventData(L);
final Pointer<Data> dataInC = calloc<Uint8>(L).cast();
copyFromUint8ListToTarget(data, dataInC.ref);
copyFromUint8ListToTarget(data, dataInC);
hash(dataInC, L, hashAlgorithm);
calloc.free(dataInC);
}
@ -126,7 +126,7 @@ class DigestDartMemory extends BenchmarkBase {
@override
void run() {
final Pointer<Data> dataInC = calloc<Uint8>(L).cast();
copyFromUint8ListToTarget(data, dataInC.ref);
copyFromUint8ListToTarget(data, dataInC);
final String result = hash(dataInC, L, hashAlgorithm);
calloc.free(dataInC);
if (result != expectedHash) {

View file

@ -7,21 +7,25 @@
import 'dart:ffi';
/// digest algorithm.
class EVP_MD extends Struct {}
class EVP_MD extends Opaque {}
/// digest context.
class EVP_MD_CTX extends Struct {}
class EVP_MD_CTX extends Opaque {}
/// Type for `void*` used to represent opaque data.
class Data extends Struct {
static Data fromUint8Pointer(Pointer<Uint8> p) => p.cast<Data>().ref;
class Data extends Opaque {
static Pointer<Data> fromUint8Pointer(Pointer<Uint8> p) => p.cast<Data>();
}
Pointer<Uint8> asUint8Pointer() => addressOf.cast();
extension DataPointerAsUint8Pointer on Pointer<Data> {
Pointer<Uint8> asUint8Pointer() => cast();
}
/// Type for `uint8_t*` used to represent byte data.
class Bytes extends Struct {
static Data fromUint8Pointer(Pointer<Uint8> p) => p.cast<Data>().ref;
Pointer<Uint8> asUint8Pointer() => addressOf.cast();
class Bytes extends Opaque {
static Pointer<Data> fromUint8Pointer(Pointer<Uint8> p) => p.cast<Data>();
}
extension BytesPointerAsUint8Pointer on Pointer<Bytes> {
Pointer<Uint8> asUint8Pointer() => cast();
}

View file

@ -29,7 +29,7 @@ Uint8List inventData(int length) {
return result;
}
Uint8List toUint8List(Bytes bytes, int length) {
Uint8List toUint8List(Pointer<Bytes> bytes, int length) {
final result = Uint8List(length);
final uint8bytes = bytes.asUint8Pointer();
for (int i = 0; i < length; i++) {
@ -38,7 +38,7 @@ Uint8List toUint8List(Bytes bytes, int length) {
return result;
}
void copyFromUint8ListToTarget(Uint8List source, Data target) {
void copyFromUint8ListToTarget(Uint8List source, Pointer<Data> target) {
final int length = source.length;
final uint8target = target.asUint8Pointer();
for (int i = 0; i < length; i++) {
@ -54,7 +54,7 @@ String hash(Pointer<Data> data, int length, Pointer<EVP_MD> hashAlgorithm) {
final Pointer<Bytes> result = calloc<Uint8>(resultSize).cast();
EVP_DigestFinal(context, result, nullptr);
EVP_MD_CTX_free(context);
final String hash = base64Encode(toUint8List(result.ref, resultSize));
final String hash = base64Encode(toUint8List(result, resultSize));
calloc.free(result);
return hash;
}
@ -87,7 +87,7 @@ class DigestCMemory extends BenchmarkBase {
@override
void setup() {
data = calloc<Uint8>(L).cast();
copyFromUint8ListToTarget(inventData(L), data.ref);
copyFromUint8ListToTarget(inventData(L), data);
hash(data, L, hashAlgorithm);
}
@ -117,7 +117,7 @@ class DigestDartMemory extends BenchmarkBase {
void setup() {
data = inventData(L);
final Pointer<Data> dataInC = calloc<Uint8>(L).cast();
copyFromUint8ListToTarget(data, dataInC.ref);
copyFromUint8ListToTarget(data, dataInC);
hash(dataInC, L, hashAlgorithm);
calloc.free(dataInC);
}
@ -128,7 +128,7 @@ class DigestDartMemory extends BenchmarkBase {
@override
void run() {
final Pointer<Data> dataInC = calloc<Uint8>(L).cast();
copyFromUint8ListToTarget(data, dataInC.ref);
copyFromUint8ListToTarget(data, dataInC);
final String result = hash(dataInC, L, hashAlgorithm);
calloc.free(dataInC);
if (result != expectedHash) {

View file

@ -9,21 +9,25 @@
import 'dart:ffi';
/// digest algorithm.
class EVP_MD extends Struct {}
class EVP_MD extends Opaque {}
/// digest context.
class EVP_MD_CTX extends Struct {}
class EVP_MD_CTX extends Opaque {}
/// Type for `void*` used to represent opaque data.
class Data extends Struct {
static Data fromUint8Pointer(Pointer<Uint8> p) => p.cast<Data>().ref;
class Data extends Opaque {
static Pointer<Data> fromUint8Pointer(Pointer<Uint8> p) => p.cast<Data>();
}
Pointer<Uint8> asUint8Pointer() => addressOf.cast();
extension DataPointerAsUint8Pointer on Pointer<Data> {
Pointer<Uint8> asUint8Pointer() => cast();
}
/// Type for `uint8_t*` used to represent byte data.
class Bytes extends Struct {
static Data fromUint8Pointer(Pointer<Uint8> p) => p.cast<Data>().ref;
Pointer<Uint8> asUint8Pointer() => addressOf.cast();
class Bytes extends Opaque {
static Pointer<Data> fromUint8Pointer(Pointer<Uint8> p) => p.cast<Data>();
}
extension BytesPointerAsUint8Pointer on Pointer<Bytes> {
Pointer<Uint8> asUint8Pointer() => cast();
}

View file

@ -22,7 +22,7 @@ final bool isolateGropusEnabled =
Platform.executableArguments.contains('--enable-isolate-groups');
final sdkRoot = Platform.script.resolve('../../../../../');
class Isolate extends Struct {}
class Isolate extends Opaque {}
abstract class FfiBindings {
static final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");

View file

@ -16,7 +16,7 @@ import '../../../../../tests/ffi/dylib_utils.dart';
// This should be larger than max-new-space-size/tlab-size.
const int threadCount = 200;
class Isolate extends Struct {}
class Isolate extends Opaque {}
typedef Dart_CurrentIsolateFT = Pointer<Isolate> Function();
typedef Dart_CurrentIsolateNFT = Pointer<Isolate> Function();

View file

@ -22,7 +22,7 @@ final bool isolateGropusEnabled =
Platform.executableArguments.contains('--enable-isolate-groups');
final sdkRoot = Platform.script.resolve('../../../../../');
class Isolate extends Struct {}
class Isolate extends Opaque {}
abstract class FfiBindings {
static final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");

View file

@ -16,7 +16,7 @@ import '../../../../../tests/ffi_2/dylib_utils.dart';
// This should be larger than max-new-space-size/tlab-size.
const int threadCount = 200;
class Isolate extends Struct {}
class Isolate extends Opaque {}
typedef Dart_CurrentIsolateFT = Pointer<Isolate> Function();
typedef Dart_CurrentIsolateNFT = Pointer<Isolate> Function();

View file

@ -101,7 +101,7 @@ class Database {
int columnCount = bindings.sqlite3_column_count(statement);
for (int i = 0; i < columnCount; i++) {
String columnName =
bindings.sqlite3_column_name(statement, i).ref.toString();
Utf8.fromUtf8(bindings.sqlite3_column_name(statement, i));
columnIndices[columnName] = i;
}
@ -109,9 +109,9 @@ class Database {
}
SQLiteException _loadError(int errorCode) {
String errorMessage = bindings.sqlite3_errmsg(_database).ref.toString();
String errorMessage = Utf8.fromUtf8(bindings.sqlite3_errmsg(_database));
String errorCodeExplanation =
bindings.sqlite3_errstr(errorCode).ref.toString();
Utf8.fromUtf8(bindings.sqlite3_errstr(errorCode));
return SQLiteException(
"$errorMessage (Code $errorCode: $errorCodeExplanation)");
}
@ -206,10 +206,8 @@ class Row {
dynamicType =
_typeFromCode(bindings.sqlite3_column_type(_statement, columnIndex));
} else {
dynamicType = _typeFromText(bindings
.sqlite3_column_decltype(_statement, columnIndex)
.ref
.toString());
dynamicType = _typeFromText(Utf8.fromUtf8(
bindings.sqlite3_column_decltype(_statement, columnIndex)));
}
switch (dynamicType) {
@ -244,7 +242,7 @@ class Row {
/// Reads column [columnIndex] and converts to [Type.Text] if not text.
String readColumnByIndexAsText(int columnIndex) {
_checkIsCurrentRow();
return bindings.sqlite3_column_text(_statement, columnIndex).ref.toString();
return Utf8.fromUtf8(bindings.sqlite3_column_text(_statement, columnIndex));
}
void _checkIsCurrentRow() {

View file

@ -167,7 +167,7 @@ void main() {
test("Utf8 unit test", () {
final String test = 'Hasta Mañana';
final medium = Utf8.toUtf8(test);
expect(test, medium.ref.toString());
expect(test, Utf8.fromUtf8(medium));
calloc.free(medium);
});
}

View file

@ -103,7 +103,7 @@ class Database {
int columnCount = bindings.sqlite3_column_count(statement);
for (int i = 0; i < columnCount; i++) {
String columnName =
bindings.sqlite3_column_name(statement, i).ref.toString();
Utf8.fromUtf8(bindings.sqlite3_column_name(statement, i));
columnIndices[columnName] = i;
}
@ -111,12 +111,12 @@ class Database {
}
SQLiteException _loadError([int errorCode]) {
String errorMessage = bindings.sqlite3_errmsg(_database).ref.toString();
String errorMessage = Utf8.fromUtf8(bindings.sqlite3_errmsg(_database));
if (errorCode == null) {
return SQLiteException(errorMessage);
}
String errorCodeExplanation =
bindings.sqlite3_errstr(errorCode).ref.toString();
Utf8.fromUtf8(bindings.sqlite3_errstr(errorCode));
return SQLiteException(
"$errorMessage (Code $errorCode: $errorCodeExplanation)");
}
@ -216,10 +216,8 @@ class Row {
dynamicType =
_typeFromCode(bindings.sqlite3_column_type(_statement, columnIndex));
} else {
dynamicType = _typeFromText(bindings
.sqlite3_column_decltype(_statement, columnIndex)
.ref
.toString());
dynamicType = _typeFromText(Utf8.fromUtf8(
bindings.sqlite3_column_decltype(_statement, columnIndex)));
}
switch (dynamicType) {
@ -255,7 +253,7 @@ class Row {
/// Reads column [columnIndex] and converts to [Type.Text] if not text.
String readColumnByIndexAsText(int columnIndex) {
_checkIsCurrentRow();
return bindings.sqlite3_column_text(_statement, columnIndex).ref.toString();
return Utf8.fromUtf8(bindings.sqlite3_column_text(_statement, columnIndex));
}
void _checkIsCurrentRow() {

View file

@ -171,7 +171,7 @@ void main() {
test("Utf8 unit test", () {
final String test = 'Hasta Mañana';
final medium = Utf8.toUtf8(test);
expect(test, medium.ref.toString());
expect(test, Utf8.fromUtf8(medium));
calloc.free(medium);
});
}

View file

@ -10,7 +10,7 @@ import "package:expect/expect.dart";
import 'dylib_utils.dart';
class EVP_MD extends Struct {}
class EVP_MD extends Opaque {}
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");

View file

@ -10,7 +10,7 @@ import "package:expect/expect.dart";
import 'dylib_utils.dart';
class EVP_MD extends Struct {}
class EVP_MD extends Opaque {}
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");