[ffi] Remove deprecated Pointer.elementAt uses

Change-Id: I8ab032442db58f41e474ecfdeace4ff65953c8ca
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421186
Reviewed-by: Hossein Yousefi <yousefi@google.com>
This commit is contained in:
Daco Harkes
2025-04-10 11:14:02 -07:00
committed by Commit Queue
parent d9f850f74e
commit 35c0ef8410
18 changed files with 134 additions and 230 deletions
@@ -2830,7 +2830,7 @@ class PointerUint8x02 extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p2 = p1 + 1;
}
@override
@@ -2867,7 +2867,7 @@ class PointerUint8x02Native extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p2 = p1 + 1;
}
@override
@@ -2904,7 +2904,7 @@ class PointerUint8x02NativeLeaf extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p2 = p1 + 1;
}
@override
@@ -2946,9 +2946,9 @@ class PointerUint8x04 extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p3 = p1.elementAt(2);
p4 = p1.elementAt(3);
p2 = p1 + 1;
p3 = p1 + 2;
p4 = p1 + 3;
}
@override
@@ -2989,9 +2989,9 @@ class PointerUint8x04Native extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p3 = p1.elementAt(2);
p4 = p1.elementAt(3);
p2 = p1 + 1;
p3 = p1 + 2;
p4 = p1 + 3;
}
@override
@@ -3032,9 +3032,9 @@ class PointerUint8x04NativeLeaf extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p3 = p1.elementAt(2);
p4 = p1.elementAt(3);
p2 = p1 + 1;
p3 = p1 + 2;
p4 = p1 + 3;
}
@override
@@ -3082,15 +3082,15 @@ class PointerUint8x10 extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p3 = p1.elementAt(2);
p4 = p1.elementAt(3);
p5 = p1.elementAt(4);
p6 = p1.elementAt(5);
p7 = p1.elementAt(6);
p8 = p1.elementAt(7);
p9 = p1.elementAt(8);
p10 = p1.elementAt(9);
p2 = p1 + 1;
p3 = p1 + 2;
p4 = p1 + 3;
p5 = p1 + 4;
p6 = p1 + 5;
p7 = p1 + 6;
p8 = p1 + 7;
p9 = p1 + 8;
p10 = p1 + 9;
}
@override
@@ -3143,15 +3143,15 @@ class PointerUint8x10Native extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p3 = p1.elementAt(2);
p4 = p1.elementAt(3);
p5 = p1.elementAt(4);
p6 = p1.elementAt(5);
p7 = p1.elementAt(6);
p8 = p1.elementAt(7);
p9 = p1.elementAt(8);
p10 = p1.elementAt(9);
p2 = p1 + 1;
p3 = p1 + 2;
p4 = p1 + 3;
p5 = p1 + 4;
p6 = p1 + 5;
p7 = p1 + 6;
p8 = p1 + 7;
p9 = p1 + 8;
p10 = p1 + 9;
}
@override
@@ -3204,15 +3204,15 @@ class PointerUint8x10NativeLeaf extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p3 = p1.elementAt(2);
p4 = p1.elementAt(3);
p5 = p1.elementAt(4);
p6 = p1.elementAt(5);
p7 = p1.elementAt(6);
p8 = p1.elementAt(7);
p9 = p1.elementAt(8);
p10 = p1.elementAt(9);
p2 = p1 + 1;
p3 = p1 + 2;
p4 = p1 + 3;
p5 = p1 + 4;
p6 = p1 + 5;
p7 = p1 + 6;
p8 = p1 + 7;
p9 = p1 + 8;
p10 = p1 + 9;
}
@override
@@ -3270,25 +3270,25 @@ class PointerUint8x20 extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p3 = p1.elementAt(2);
p4 = p1.elementAt(3);
p5 = p1.elementAt(4);
p6 = p1.elementAt(5);
p7 = p1.elementAt(6);
p8 = p1.elementAt(7);
p9 = p1.elementAt(8);
p10 = p1.elementAt(9);
p11 = p1.elementAt(10);
p12 = p1.elementAt(11);
p13 = p1.elementAt(12);
p14 = p1.elementAt(13);
p15 = p1.elementAt(14);
p16 = p1.elementAt(15);
p17 = p1.elementAt(16);
p18 = p1.elementAt(17);
p19 = p1.elementAt(18);
p20 = p1.elementAt(19);
p2 = p1 + 1;
p3 = p1 + 2;
p4 = p1 + 3;
p5 = p1 + 4;
p6 = p1 + 5;
p7 = p1 + 6;
p8 = p1 + 7;
p9 = p1 + 8;
p10 = p1 + 9;
p11 = p1 + 10;
p12 = p1 + 11;
p13 = p1 + 12;
p14 = p1 + 13;
p15 = p1 + 14;
p16 = p1 + 15;
p17 = p1 + 16;
p18 = p1 + 17;
p19 = p1 + 18;
p20 = p1 + 19;
}
@override
@@ -3382,25 +3382,25 @@ class PointerUint8x20Native extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p3 = p1.elementAt(2);
p4 = p1.elementAt(3);
p5 = p1.elementAt(4);
p6 = p1.elementAt(5);
p7 = p1.elementAt(6);
p8 = p1.elementAt(7);
p9 = p1.elementAt(8);
p10 = p1.elementAt(9);
p11 = p1.elementAt(10);
p12 = p1.elementAt(11);
p13 = p1.elementAt(12);
p14 = p1.elementAt(13);
p15 = p1.elementAt(14);
p16 = p1.elementAt(15);
p17 = p1.elementAt(16);
p18 = p1.elementAt(17);
p19 = p1.elementAt(18);
p20 = p1.elementAt(19);
p2 = p1 + 1;
p3 = p1 + 2;
p4 = p1 + 3;
p5 = p1 + 4;
p6 = p1 + 5;
p7 = p1 + 6;
p8 = p1 + 7;
p9 = p1 + 8;
p10 = p1 + 9;
p11 = p1 + 10;
p12 = p1 + 11;
p13 = p1 + 12;
p14 = p1 + 13;
p15 = p1 + 14;
p16 = p1 + 15;
p17 = p1 + 16;
p18 = p1 + 17;
p19 = p1 + 18;
p20 = p1 + 19;
}
@override
@@ -3494,25 +3494,25 @@ class PointerUint8x20NativeLeaf extends FfiBenchmarkBase {
@override
void setup() {
p1 = calloc(N + 1);
p2 = p1.elementAt(1);
p3 = p1.elementAt(2);
p4 = p1.elementAt(3);
p5 = p1.elementAt(4);
p6 = p1.elementAt(5);
p7 = p1.elementAt(6);
p8 = p1.elementAt(7);
p9 = p1.elementAt(8);
p10 = p1.elementAt(9);
p11 = p1.elementAt(10);
p12 = p1.elementAt(11);
p13 = p1.elementAt(12);
p14 = p1.elementAt(13);
p15 = p1.elementAt(14);
p16 = p1.elementAt(15);
p17 = p1.elementAt(16);
p18 = p1.elementAt(17);
p19 = p1.elementAt(18);
p20 = p1.elementAt(19);
p2 = p1 + 1;
p3 = p1 + 2;
p4 = p1 + 3;
p5 = p1 + 4;
p6 = p1 + 5;
p7 = p1 + 6;
p8 = p1 + 7;
p9 = p1 + 8;
p10 = p1 + 9;
p11 = p1 + 10;
p12 = p1 + 11;
p13 = p1 + 12;
p14 = p1 + 13;
p15 = p1 + 14;
p16 = p1 + 15;
p17 = p1 + 16;
p18 = p1 + 17;
p19 = p1 + 18;
p20 = p1 + 19;
}
@override
+1 -4
View File
@@ -243,10 +243,7 @@ void generateBenchmarkPointer(StringBuffer buffer, List<String> types) {
.map((n) => '$type $n = nullptr;')
.join('\n');
final String setup =
List.generate(
number - 1,
(i) => 'p${i + 2} = p1.elementAt(${i + 1});',
).join();
List.generate(number - 1, (i) => 'p${i + 2} = p1 + ${i + 1};').join();
final String functionType = 'Function$number$dartTypeName';
final String functionNativeType = 'NativeFunction$number$typeName';
final String functionNameC = 'Function$number$typeName';
+1 -1
View File
@@ -140,7 +140,7 @@ final finalizerAddress = () {
throw 'Failed to write executable code to the memory.';
}
return finalizerStub.elementAt(offset).cast<Void>();
return (finalizerStub + offset).cast<Void>();
}();
base class PeerData extends Struct {
+1 -1
View File
@@ -84,7 +84,7 @@ main() {
(p + i).value = i * 3;
}
for (var i in [0, 1, 2, 3, 4, 5, 6, 7]) {
print('p.elementAt($i) value: ${(p + i).value}');
print('p + $i value: ${(p + i).value}');
}
calloc.free(p);
}
@@ -104,9 +104,6 @@ final class IncompleteArrayArrayStruct extends Struct {
void testInlineArray2() {
final p = calloc<Int64>(100).cast<IncompleteArrayArrayStruct>();
Expect.throws(() {
p.elementAt(3);
});
Expect.throws(() {
(p + 3);
});
+2 -2
View File
@@ -90,7 +90,7 @@ void testInlineArray2() {
int someValue(int a, int b, int c) => a * 1337 + b * 42 + c;
final p = calloc<WCharArrayArrayStruct>(_dim0);
for (int i0 = 0; i0 < _dim0; i0++) {
final array = p.elementAt(i0).ref.a0;
final array = (p + i0).ref.a0;
for (int i1 = 0; i1 < _dim1; i1++) {
final array2 = array[i1];
for (int i2 = 0; i2 < _dim2; i2++) {
@@ -99,7 +99,7 @@ void testInlineArray2() {
}
}
for (int i0 = 0; i0 < _dim0; i0++) {
final array = p.elementAt(i0).ref.a0;
final array = (p + i0).ref.a0;
for (int i1 = 0; i1 < _dim1; i1++) {
final array2 = array[i1];
for (int i2 = 0; i2 < _dim2; i2++) {
+7 -7
View File
@@ -22,8 +22,8 @@ void main() {
testAliasCast2();
testAliasOffsetBy();
testAliasOffsetBy2();
testAliasElementAt();
testAliasElementAt2();
testAliasOperator();
testAliasOperator2();
testAliasFromAddress();
testAliasFromAddress2();
testAliasFromAddressViaMemory();
@@ -90,9 +90,9 @@ void testAliasOffsetBy2() {
calloc.free(source);
}
void testAliasElementAt() {
void testAliasOperator() {
final source = calloc<Int64>(2);
final alias = source.elementAt(1).elementAt(-1);
final alias = source + 1 - 1;
source.value = 42;
final int a = source.value;
alias.value = 1984;
@@ -101,10 +101,10 @@ void testAliasElementAt() {
calloc.free(source);
}
void testAliasElementAt2() {
void testAliasOperator2() {
final source = calloc<Int64>(3);
final alias = source.elementAt(2).elementAt(-2);
final alias2 = source.elementAt(1).elementAt(-1);
final alias = source + 2 - 2;
final alias2 = source + 1 - 1;
alias.value = 42;
final int a = alias.value;
alias2.value = 1984;
+5 -6
View File
@@ -66,7 +66,7 @@ void testPointerFromPointer() {
void testPointerPointerArithmetic() {
Pointer<Int64> p = calloc(2);
Pointer<Int64> p2 = p.elementAt(1);
Pointer<Int64> p2 = p + 1;
p2.value = 100;
Pointer<Int64> p3 = p.offsetBy(8);
Expect.equals(100, p3.value);
@@ -82,13 +82,13 @@ void testPointerPointerArithmetic() {
void testPointerPointerArithmeticSizes() {
Pointer<Int64> p = calloc(2);
Pointer<Int64> p2 = p.elementAt(1);
Pointer<Int64> p2 = p + 1;
int addr = p.address;
Expect.equals(addr + 8, p2.address);
calloc.free(p);
Pointer<Int32> p3 = calloc(2);
Pointer<Int32> p4 = p3.elementAt(1);
Pointer<Int32> p4 = p3 + 1;
addr = p3.address;
Expect.equals(addr + 4, p4.address);
calloc.free(p3);
@@ -136,8 +136,7 @@ void testCastGeneric() {
}
Pointer<Int16> p = calloc();
// ignore: unused_local_variable
Pointer<Int64> p2 = generic<Int64>(p);
generic(p);
calloc.free(p);
}
@@ -434,7 +433,7 @@ void testDynamicInvocation() {
p.value;
});
Expect.throws(() => p.value = 1);
Expect.throws(() => p.elementAt(5));
Expect.throws(() => p + 5);
Expect.throws(() => p += 5);
p.address;
p.cast<Int16>();
+5 -5
View File
@@ -32,12 +32,12 @@ void testStoreLoad() {
Expect.equals(20, p[1]);
if (sizeOf<IntPtr>() == 4) {
// Test round tripping.
Expect.equals(20, p.elementAt(0x100000001).value);
Expect.equals(20, (p + 0x100000001).value);
Expect.equals(20, p[0x100000001]);
}
// Test negative index.
final pUseNegative = p.elementAt(1);
final pUseNegative = p + 1;
Expect.equals(10, pUseNegative[-1]);
// Test negative index using operators
@@ -89,8 +89,8 @@ void testCompoundLoadAndStore() {
foos[i] = reference;
Expect.isTrue(foos[i].a == 10);
foos.elementAt(i).ref = reference;
Expect.isTrue(foos.elementAt(i).ref.a == 10);
(foos + i).ref = reference;
Expect.isTrue((foos + i).ref.a == 10);
}
for (var i = 1; i < 9; i++) {
@@ -106,7 +106,7 @@ void testCompoundLoadAndStore() {
for (var i = 1; i < 9; i++) {
bars[i] = bars[0];
Expect.isTrue(bars.elementAt(i).ref.foo.a == 10);
Expect.isTrue((bars + i).ref.foo.a == 10);
Expect.isTrue((bars + i).ref.foo.a == 10);
}
+2 -2
View File
@@ -185,10 +185,10 @@ double manyArgs(
}
typedef StoreType = Pointer<Int64> Function(Pointer<Int64>);
Pointer<Int64> store(Pointer<Int64> ptr) => ptr.elementAt(1)..value = 1337;
Pointer<Int64> store(Pointer<Int64> ptr) => (ptr + 1)..value = 1337;
typedef NullPointersType = Pointer<Int64> Function(Pointer<Int64>);
Pointer<Int64> nullPointers(Pointer<Int64> ptr) => ptr.elementAt(1);
Pointer<Int64> nullPointers(Pointer<Int64> ptr) => (ptr + 1);
typedef ReturnVoid = Void Function();
void returnVoid() {}
+9 -9
View File
@@ -75,15 +75,15 @@ void testFunctionWithStructArray({bool isLeaf = false}) {
Coordinate c3 = coordinateArray[2];
c1.x = 10.0;
c1.y = 10.0;
c1.next = coordinateArray.elementAt(2);
c1.next = coordinateArray + 2;
c2.x = 20.0;
c2.y = 20.0;
c2.next = coordinateArray.elementAt(0);
c2.next = coordinateArray + 0;
c3.x = 30.0;
c3.y = 30.0;
c3.next = coordinateArray.elementAt(1);
c3.next = coordinateArray + 1;
Coordinate result = f1(coordinateArray.elementAt(0)).ref;
Coordinate result = f1(coordinateArray).ref;
Expect.approxEquals(20.0, result.x);
Expect.approxEquals(20.0, result.y);
@@ -118,18 +118,18 @@ void testFunctionWithVeryLargeStruct({bool isLeaf = false}) {
struct.k = 1024;
struct.smallLastField = 1;
}
vls1.parent = vlsArray.elementAt(1);
vls1.parent = vlsArray + 1;
vls1.numChildren = 2;
vls1.children = vlsArray.elementAt(0);
vls2.parent = vlsArray.elementAt(1);
vls1.children = vlsArray;
vls2.parent = vlsArray + 1;
vls2.parent = nullptr;
vls2.numChildren = 0;
vls2.children = nullptr;
int result = f(vlsArray.elementAt(0));
int result = f(vlsArray);
Expect.equals(2051, result);
result = f(vlsArray.elementAt(1));
result = f(vlsArray + 1);
Expect.equals(2048, result);
calloc.free(vlsArray);
+1 -1
View File
@@ -536,7 +536,7 @@ void testNativeFunctionPointer() {
Pointer<Int64> result = assign1337Index1(p2);
Expect.equals(1337, result.value);
Expect.equals(1337, p2[1]);
Expect.equals(p2.elementAt(1).address, result.address);
Expect.equals((p2 + 1).address, result.address);
calloc.free(p2);
}
+1 -1
View File
@@ -9,7 +9,7 @@ import "package:ffi/ffi.dart";
main() {
final data = calloc<Uint8>(3);
for (int i = 0; i < 3; ++i) {
data.elementAt(i).value = 1;
(data + i).value = 1;
}
calloc.free(data);
}
@@ -67,8 +67,6 @@ void main() {
testRefStruct();
testSizeOfGeneric();
testSizeOfInvalidType();
testElementAtGeneric();
testElementAtNativeType();
testLookupFunctionIsLeafMustBeConst();
testAsFunctionIsLeafMustBeConst();
testLookupFunctionTakesHandle();
@@ -1175,32 +1173,6 @@ void testSizeOfInvalidType() {
// [analyzer] COMPILE_TIME_ERROR.NON_CONSTANT_TYPE_ARGUMENT
}
void testElementAtGeneric() {
Pointer<T> generic<T extends NativeType>(Pointer<T> pointer) {
Pointer<T> returnValue = pointer;
returnValue = returnValue.elementAt(1);
// ^^^^^^^^^
// [cfe] The method 'elementAt' isn't defined for the class 'Pointer<T>'.
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_METHOD
return returnValue;
}
Pointer<Int8> p = calloc();
p.elementAt(1);
generic(p);
calloc.free(p);
}
void testElementAtNativeType() {
Pointer<Int8> p = calloc();
p.elementAt(1);
Pointer<NativeType> p2 = p;
p2.elementAt(1);
// ^^^^^^^^^
// [cfe] The method 'elementAt' isn't defined for the class 'Pointer<NativeType>'.
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_METHOD
calloc.free(p);
}
final class TestStruct1400 extends Struct {
@Array(8)
+2 -2
View File
@@ -58,8 +58,8 @@ void testStructAllocate() {
/// allocates coordinates consecutively in c memory
void testStructFromAddress() {
Pointer<Coordinate> c1 = calloc(3);
Pointer<Coordinate> c2 = c1.elementAt(1);
Pointer<Coordinate> c3 = c1.elementAt(2);
Pointer<Coordinate> c2 = c1 + 1;
Pointer<Coordinate> c3 = c1 + 2;
c1.ref
..x = 10.0
..y = 10.0
-60
View File
@@ -17,9 +17,7 @@ void main() {
for (int i = 0; i < 100; i++) {
testStructAllocate();
testStructFromAddress();
testStructFromAddressWithOperator();
testStructIndexedAccess();
testStructIndexedAccessWithOperator();
testStructWithNulls();
testUtf8();
testDotDotRef();
@@ -58,37 +56,7 @@ void testStructAllocate() {
calloc.free(c3);
}
/// Allocates coordinates consecutively in c memory.
void testStructFromAddress() {
Pointer<Coordinate> c1 = calloc(3);
Pointer<Coordinate> c2 = c1.elementAt(1);
Pointer<Coordinate> c3 = c1.elementAt(2);
c1.ref
..x = 10.0
..y = 10.0
..next = c3;
c2.ref
..x = 20.0
..y = 20.0
..next = c1;
c3.ref
..x = 30.0
..y = 30.0
..next = c2;
Coordinate currentCoordinate = c1.ref;
Expect.equals(10.0, currentCoordinate.x);
currentCoordinate = currentCoordinate.next.ref;
Expect.equals(30.0, currentCoordinate.x);
currentCoordinate = currentCoordinate.next.ref;
Expect.equals(20.0, currentCoordinate.x);
currentCoordinate = currentCoordinate.next.ref;
Expect.equals(10.0, currentCoordinate.x);
calloc.free(c1);
}
void testStructFromAddressWithOperator() {
Pointer<Coordinate> c1 = calloc(3);
Pointer<Coordinate> c2 = c1 + 1;
Pointer<Coordinate> c3 = c1 + 2;
@@ -117,35 +85,7 @@ void testStructFromAddressWithOperator() {
calloc.free(c1);
}
/// Allocates coordinates consecutively in c memory.
void testStructIndexedAccess() {
Pointer<Coordinate> cs = calloc(3);
cs[0]
..x = 10.0
..y = 10.0
..next = cs.elementAt(2);
cs[1]
..x = 20.0
..y = 20.0
..next = cs;
cs[2]
..x = 30.0
..y = 30.0
..next = cs.elementAt(1);
Coordinate currentCoordinate = cs.ref;
Expect.equals(10.0, currentCoordinate.x);
currentCoordinate = currentCoordinate.next.ref;
Expect.equals(30.0, currentCoordinate.x);
currentCoordinate = currentCoordinate.next.ref;
Expect.equals(20.0, currentCoordinate.x);
currentCoordinate = currentCoordinate.next.ref;
Expect.equals(10.0, currentCoordinate.x);
calloc.free(cs);
}
void testStructIndexedAccessWithOperator() {
Pointer<Coordinate> cs = calloc(3);
cs[0]
..x = 10.0
+1 -1
View File
@@ -92,7 +92,7 @@ void _freeAll() {
Pointer<T> _allocateUnaligned<T extends NativeType>() {
final pointer = calloc<Int8>(16);
_pool.add(pointer);
final misaligned = pointer.elementAt(1).cast<T>();
final misaligned = (pointer + 1).cast<T>();
Expect.equals(1, misaligned.address % 2);
return misaligned;
}
@@ -32,7 +32,6 @@ final List<Function()> functionsToTest = [
() => highAddressPointer.cast<Double>(),
() => Pointer.fromAddress(highAddressPointer.address),
() => highAddressPointer.address,
() => highAddressPointer.elementAt(1),
() => highAddressPointer + 1,
() => highAddressPointer.offsetBy(1),
() => highAddressPointer.asTypedList(1),