Skip to content

Commit

Permalink
[vm/ffi] Migrate runtime/tests/vm to CallocAllocator
Browse files Browse the repository at this point in the history
This CL does not yet roll `package:ffi` to use `Allocator`, because that
breaks the checked in Dart in Flutter in g3. Instead, this uses the copy
of `_CallocAllocator` from `package:ffi` in `calloc.dart` in tests/ffi.

New API landed in: https://dart-review.googlesource.com/c/sdk/+/177705

Issue: #44621
Issue: #38721

Change-Id: Iedfc4a11d4606915a324c824372bca643016f5a3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/178994
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
  • Loading branch information
dcharkes authored and commit-bot@chromium.org committed Jan 14, 2021
1 parent 9922491 commit c7306aa
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'dart:isolate';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';

import '../../../../../tests/ffi/calloc.dart';
import '../../../../../tests/ffi/dylib_utils.dart';

final bool isAOT = Platform.executable.contains('dart_precompiled_runtime');
Expand Down Expand Up @@ -62,7 +63,7 @@ abstract class FfiBindings {
Expect.isTrue(isolate.address != 0);
return isolate;
} finally {
free(cname);
calloc.free(cname);
}
}

Expand All @@ -86,8 +87,8 @@ abstract class FfiBindings {
onError != null ? onError.nativePort : 0,
onExit != null ? onExit.nativePort : 0);

free(libraryUri);
free(functionName);
calloc.free(libraryUri);
calloc.free(functionName);
}
}

Expand Down Expand Up @@ -120,7 +121,7 @@ Future withPeerPointer(fun(Pointer<Void> peer)) async {
// wait a little here to ensure the write of the callback has arrived.
await Future.delayed(const Duration(milliseconds: 100));
Expect.equals('xbz', Utf8.fromUtf8(peer.cast()));
free(peer);
calloc.free(peer);
}
}

Expand Down
6 changes: 4 additions & 2 deletions runtime/tests/vm/dart/regress_41971_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'dart:ffi';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';

import '../../../../tests/ffi/calloc.dart';

class X {
int field;
X(this.field);
Expand All @@ -35,8 +37,8 @@ int loadFrom(Pointer<Int32> p) {
}

void main() {
final p = allocate<Int32>(count: 128);
final p = calloc<Int32>(128);
p[0] = 42;
Expect.equals(42, loadFrom(p));
free(p);
calloc.free(p);
}
10 changes: 6 additions & 4 deletions runtime/tests/vm/dart/thread_priority_macos_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'dart:io';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';

import '../../../../tests/ffi/calloc.dart';

// pthread_t pthread_self()
typedef PthreadSelfFT = int Function();
typedef PthreadSelfNFT = IntPtr Function();
Expand All @@ -34,11 +36,11 @@ class SchedParam extends Struct {

main(args) {
if (Platform.isMacOS) {
final policy = allocate<Int32>(count: 1);
final param = allocate<SchedParam>(count: 1);
final policy = calloc<Int32>(1);
final param = calloc<SchedParam>(1);
Expect.equals(0, pthreadGetSchedParam(pthreadSelf(), policy, param));
Expect.equals(15, param.ref.schedPriority);
free(policy);
free(param);
calloc.free(policy);
calloc.free(param);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'dart:isolate';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';

import '../../../../../tests/ffi/calloc.dart';
import '../../../../../tests/ffi/dylib_utils.dart';

final bool isAOT = Platform.executable.contains('dart_precompiled_runtime');
Expand Down Expand Up @@ -62,7 +63,7 @@ abstract class FfiBindings {
Expect.isTrue(isolate.address != 0);
return isolate;
} finally {
free(cname);
calloc.free(cname);
}
}

Expand All @@ -86,8 +87,8 @@ abstract class FfiBindings {
onError != null ? onError.nativePort : 0,
onExit != null ? onExit.nativePort : 0);

free(libraryUri);
free(functionName);
calloc.free(libraryUri);
calloc.free(functionName);
}
}

Expand Down Expand Up @@ -120,7 +121,7 @@ Future withPeerPointer(fun(Pointer<Void> peer)) async {
// wait a little here to ensure the write of the callback has arrived.
await Future.delayed(const Duration(milliseconds: 100));
Expect.equals('xbz', Utf8.fromUtf8(peer.cast()));
free(peer);
calloc.free(peer);
}
}

Expand Down
6 changes: 4 additions & 2 deletions runtime/tests/vm/dart_2/regress_41971_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'dart:ffi';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';

import '../../../../tests/ffi/calloc.dart';

class X {
int field;
X(this.field);
Expand All @@ -35,8 +37,8 @@ int loadFrom(Pointer<Int32> p) {
}

void main() {
final p = allocate<Int32>(count: 128);
final p = calloc<Int32>(128);
p[0] = 42;
Expect.equals(42, loadFrom(p));
free(p);
calloc.free(p);
}
10 changes: 6 additions & 4 deletions runtime/tests/vm/dart_2/thread_priority_macos_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'dart:io';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';

import '../../../../tests/ffi/calloc.dart';

// pthread_t pthread_self()
typedef PthreadSelfFT = int Function();
typedef PthreadSelfNFT = IntPtr Function();
Expand All @@ -34,11 +36,11 @@ class SchedParam extends Struct {

main(args) {
if (Platform.isMacOS) {
final policy = allocate<Int32>(count: 1);
final param = allocate<SchedParam>(count: 1);
final policy = calloc<Int32>(1);
final param = calloc<SchedParam>(1);
Expect.equals(0, pthreadGetSchedParam(pthreadSelf(), policy, param));
Expect.equals(15, param.ref.schedPriority);
free(policy);
free(param);
calloc.free(policy);
calloc.free(param);
}
}

0 comments on commit c7306aa

Please sign in to comment.