Skip to content

Commit 24a7e43

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
[vm/ffi] Migrate off .addressOf in samples
Removes invocations of `.addressOf` in samples. Removes the constructor from the Coordinate testing class which has become useless with no access to the underlying pointer. Issue: #40667 Change-Id: I1e55a4b159662654e1d14b2d51b0b43d734d5010 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/181405 Commit-Queue: Daco Harkes <[email protected]> Reviewed-by: Aske Simon Christensen <[email protected]>
1 parent 2525e44 commit 24a7e43

8 files changed

+78
-66
lines changed

samples/ffi/coordinate.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import 'dart:ffi';
66

7-
import "package:ffi/ffi.dart";
8-
97
/// Sample struct for dart:ffi library.
108
class Coordinate extends Struct {
119
@Double()
@@ -15,12 +13,4 @@ class Coordinate extends Struct {
1513
external double y;
1614

1715
external Pointer<Coordinate> next;
18-
19-
factory Coordinate.allocate(
20-
Allocator allocator, double x, double y, Pointer<Coordinate> next) {
21-
return allocator<Coordinate>().ref
22-
..x = x
23-
..y = y
24-
..next = next;
25-
}
2616
}

samples/ffi/sample_ffi_functions_callbacks.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ main() {
4646
Pointer<NativeFunction<CoordinateTrice>> p2 =
4747
ffiTestFunctions.lookup("CoordinateUnOpTrice");
4848
CoordinateTrice coordinateUnOpTrice = p2.asFunction();
49-
Coordinate c1 = Coordinate.allocate(calloc, 10.0, 20.0, nullptr);
50-
c1.next = c1.addressOf;
51-
Coordinate result =
52-
coordinateUnOpTrice(transposeCoordinatePointer, c1.addressOf).ref;
49+
final c1 = calloc<Coordinate>()
50+
..ref.x = 10.0
51+
..ref.y = 20.0;
52+
c1.ref.next = c1;
53+
Coordinate result = coordinateUnOpTrice(transposeCoordinatePointer, c1).ref;
5354
print(result.runtimeType);
5455
print(result.x);
5556
print(result.y);
57+
calloc.free(c1);
5658
}
5759

5860
{

samples/ffi/sample_ffi_functions_structs.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ main() {
2424
ffiTestFunctions.lookup("TransposeCoordinate");
2525
NativeCoordinateOp f1 = p1.asFunction();
2626

27-
Coordinate c1 = Coordinate.allocate(calloc, 10.0, 20.0, nullptr);
28-
Coordinate c2 = Coordinate.allocate(calloc, 42.0, 84.0, c1.addressOf);
29-
c1.next = c2.addressOf;
27+
final c1 = calloc<Coordinate>()
28+
..ref.x = 10.0
29+
..ref.y = 20.0;
30+
final c2 = calloc<Coordinate>()
31+
..ref.x = 42.0
32+
..ref.y = 84.0
33+
..ref.next = c1;
34+
c1.ref.next = c2;
3035

31-
Coordinate result = f1(c1.addressOf).ref;
36+
Coordinate result = f1(c1).ref;
3237

33-
print(c1.x);
34-
print(c1.y);
38+
print(c1.ref.x);
39+
print(c1.ref.y);
3540

3641
print(result.runtimeType);
3742

samples/ffi/sample_ffi_structs.dart

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,28 @@ main() {
1414

1515
{
1616
// Allocates each coordinate separately in c memory.
17-
Coordinate c1 = Coordinate.allocate(calloc, 10.0, 10.0, nullptr);
18-
Coordinate c2 = Coordinate.allocate(calloc, 20.0, 20.0, c1.addressOf);
19-
Coordinate c3 = Coordinate.allocate(calloc, 30.0, 30.0, c2.addressOf);
20-
c1.next = c3.addressOf;
17+
final c1 = calloc<Coordinate>()
18+
..ref.x = 10.0
19+
..ref.y = 10.0;
20+
final c2 = calloc<Coordinate>()
21+
..ref.x = 20.0
22+
..ref.y = 20.0
23+
..ref.next = c1;
24+
final c3 = calloc<Coordinate>()
25+
..ref.x = 30.0
26+
..ref.y = 30.0
27+
..ref.next = c2;
28+
c1.ref.next = c3;
2129

22-
Coordinate currentCoordinate = c1;
30+
Coordinate currentCoordinate = c1.ref;
2331
for (var i in [0, 1, 2, 3, 4]) {
2432
currentCoordinate = currentCoordinate.next.ref;
2533
print("${currentCoordinate.x}; ${currentCoordinate.y}");
2634
}
2735

28-
calloc.free(c1.addressOf);
29-
calloc.free(c2.addressOf);
30-
calloc.free(c3.addressOf);
36+
calloc.free(c1);
37+
calloc.free(c2);
38+
calloc.free(c3);
3139
}
3240

3341
{
@@ -55,11 +63,12 @@ main() {
5563
}
5664

5765
{
58-
Coordinate c = Coordinate.allocate(calloc, 10, 10, nullptr);
59-
print(c is Coordinate);
60-
print(c is Pointer<Void>);
61-
print(c is Pointer);
62-
calloc.free(c.addressOf);
66+
// Allocating in native memory returns a pointer.
67+
final c = calloc<Coordinate>();
68+
print(c is Pointer<Coordinate>);
69+
// `.ref` returns a reference which gives access to the fields.
70+
print(c.ref is Coordinate);
71+
calloc.free(c);
6372
}
6473

6574
print("end main");

samples_2/ffi/coordinate.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import 'dart:ffi';
88

9-
import "package:ffi/ffi.dart";
10-
119
/// Sample struct for dart:ffi library.
1210
class Coordinate extends Struct {
1311
@Double()
@@ -17,12 +15,4 @@ class Coordinate extends Struct {
1715
double y;
1816

1917
Pointer<Coordinate> next;
20-
21-
factory Coordinate.allocate(
22-
Allocator allocator, double x, double y, Pointer<Coordinate> next) {
23-
return allocator<Coordinate>().ref
24-
..x = x
25-
..y = y
26-
..next = next;
27-
}
2818
}

samples_2/ffi/sample_ffi_functions_callbacks.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ main() {
4848
Pointer<NativeFunction<CoordinateTrice>> p2 =
4949
ffiTestFunctions.lookup("CoordinateUnOpTrice");
5050
CoordinateTrice coordinateUnOpTrice = p2.asFunction();
51-
Coordinate c1 = Coordinate.allocate(calloc, 10.0, 20.0, nullptr);
52-
c1.next = c1.addressOf;
53-
Coordinate result =
54-
coordinateUnOpTrice(transposeCoordinatePointer, c1.addressOf).ref;
51+
final c1 = calloc<Coordinate>()
52+
..ref.x = 10.0
53+
..ref.y = 20.0;
54+
c1.ref.next = c1;
55+
Coordinate result = coordinateUnOpTrice(transposeCoordinatePointer, c1).ref;
5556
print(result.runtimeType);
5657
print(result.x);
5758
print(result.y);
59+
calloc.free(c1);
5860
}
5961

6062
{

samples_2/ffi/sample_ffi_functions_structs.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ main() {
2626
ffiTestFunctions.lookup("TransposeCoordinate");
2727
NativeCoordinateOp f1 = p1.asFunction();
2828

29-
Coordinate c1 = Coordinate.allocate(calloc, 10.0, 20.0, nullptr);
30-
Coordinate c2 = Coordinate.allocate(calloc, 42.0, 84.0, c1.addressOf);
31-
c1.next = c2.addressOf;
29+
final c1 = calloc<Coordinate>()
30+
..ref.x = 10.0
31+
..ref.y = 20.0;
32+
final c2 = calloc<Coordinate>()
33+
..ref.x = 42.0
34+
..ref.y = 84.0
35+
..ref.next = c1;
36+
c1.ref.next = c2;
3237

33-
Coordinate result = f1(c1.addressOf).ref;
38+
Coordinate result = f1(c1).ref;
3439

35-
print(c1.x);
36-
print(c1.y);
40+
print(c1.ref.x);
41+
print(c1.ref.y);
3742

3843
print(result.runtimeType);
3944

samples_2/ffi/sample_ffi_structs.dart

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,28 @@ main() {
1616

1717
{
1818
// Allocates each coordinate separately in c memory.
19-
Coordinate c1 = Coordinate.allocate(calloc, 10.0, 10.0, nullptr);
20-
Coordinate c2 = Coordinate.allocate(calloc, 20.0, 20.0, c1.addressOf);
21-
Coordinate c3 = Coordinate.allocate(calloc, 30.0, 30.0, c2.addressOf);
22-
c1.next = c3.addressOf;
19+
final c1 = calloc<Coordinate>()
20+
..ref.x = 10.0
21+
..ref.y = 10.0;
22+
final c2 = calloc<Coordinate>()
23+
..ref.x = 20.0
24+
..ref.y = 20.0
25+
..ref.next = c1;
26+
final c3 = calloc<Coordinate>()
27+
..ref.x = 30.0
28+
..ref.y = 30.0
29+
..ref.next = c2;
30+
c1.ref.next = c3;
2331

24-
Coordinate currentCoordinate = c1;
32+
Coordinate currentCoordinate = c1.ref;
2533
for (var i in [0, 1, 2, 3, 4]) {
2634
currentCoordinate = currentCoordinate.next.ref;
2735
print("${currentCoordinate.x}; ${currentCoordinate.y}");
2836
}
2937

30-
calloc.free(c1.addressOf);
31-
calloc.free(c2.addressOf);
32-
calloc.free(c3.addressOf);
38+
calloc.free(c1);
39+
calloc.free(c2);
40+
calloc.free(c3);
3341
}
3442

3543
{
@@ -57,11 +65,12 @@ main() {
5765
}
5866

5967
{
60-
Coordinate c = Coordinate.allocate(calloc, 10, 10, nullptr);
61-
print(c is Coordinate);
62-
print(c is Pointer<Void>);
63-
print(c is Pointer);
64-
calloc.free(c.addressOf);
68+
// Allocating in native memory returns a pointer.
69+
final c = calloc<Coordinate>();
70+
print(c is Pointer<Coordinate>);
71+
// `.ref` returns a reference which gives access to the fields.
72+
print(c.ref is Coordinate);
73+
calloc.free(c);
6574
}
6675

6776
print("end main");

0 commit comments

Comments
 (0)