2019-02-13 12:42:47 +00:00
|
|
|
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2019-10-10 13:17:43 +00:00
|
|
|
import 'dart:ffi';
|
|
|
|
import 'package:ffi/ffi.dart';
|
2019-02-13 12:42:47 +00:00
|
|
|
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
main() {
|
2019-02-13 12:42:47 +00:00
|
|
|
print('start main');
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Basic operation: allocate, get, set, and free.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Int64> p = calloc();
|
2019-10-10 13:17:43 +00:00
|
|
|
p.value = 42;
|
|
|
|
int pValue = p.value;
|
2019-02-13 12:42:47 +00:00
|
|
|
print('${p.runtimeType} value: ${pValue}');
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Undefined behavior before set.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Int64> p = calloc();
|
2019-10-10 13:17:43 +00:00
|
|
|
int pValue = p.value;
|
2019-02-13 12:42:47 +00:00
|
|
|
print('If not set, returns garbage: ${pValue}');
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Pointers can be created from an address.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Int64> pHelper = calloc();
|
2019-10-10 13:17:43 +00:00
|
|
|
pHelper.value = 1337;
|
2019-02-13 12:42:47 +00:00
|
|
|
|
|
|
|
int address = pHelper.address;
|
|
|
|
print('Address: ${address}');
|
|
|
|
|
2019-10-10 13:17:43 +00:00
|
|
|
Pointer<Int64> p = Pointer.fromAddress(address);
|
|
|
|
print('${p.runtimeType} value: ${p.value}');
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(pHelper);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Address is zeroed out after free.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Int64> p = calloc();
|
|
|
|
calloc.free(p);
|
2019-02-13 12:42:47 +00:00
|
|
|
print('After free, address is zero: ${p.address}');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Allocating too much throws an exception.
|
2019-02-13 12:42:47 +00:00
|
|
|
try {
|
|
|
|
int maxMint = 9223372036854775807; // 2^63 - 1
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc<Int64>(maxMint);
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
} on Error {
|
2019-02-13 12:42:47 +00:00
|
|
|
print('Expected exception on allocating too much');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
int maxInt1_8 = 1152921504606846975; // 2^60 -1
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc<Int64>(maxInt1_8);
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
} on Error {
|
2019-02-13 12:42:47 +00:00
|
|
|
print('Expected exception on allocating too much');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Pointers can be cast into another type,
|
|
|
|
// resulting in the corresponding bits read.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Int64> p1 = calloc();
|
2019-10-10 13:17:43 +00:00
|
|
|
p1.value = 9223372036854775807; // 2^63 - 1
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2019-10-10 13:17:43 +00:00
|
|
|
Pointer<Int32> p2 = p1.cast();
|
|
|
|
print('${p2.runtimeType} value: ${p2.value}'); // -1
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2019-10-10 13:17:43 +00:00
|
|
|
Pointer<Int32> p3 = p2.elementAt(1);
|
|
|
|
print('${p3.runtimeType} value: ${p3.value}'); // 2^31 - 1
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p1);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Data can be tightly packed in memory.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Int8> p = calloc(8);
|
2019-02-13 12:42:47 +00:00
|
|
|
for (var i in [0, 1, 2, 3, 4, 5, 6, 7]) {
|
2019-10-10 13:17:43 +00:00
|
|
|
p.elementAt(i).value = i * 3;
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
for (var i in [0, 1, 2, 3, 4, 5, 6, 7]) {
|
2019-10-10 13:17:43 +00:00
|
|
|
print('p.elementAt($i) value: ${p.elementAt(i).value}');
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Values that don't fit are truncated.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Int32> p11 = calloc();
|
2019-02-13 12:42:47 +00:00
|
|
|
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
p11.value = 9223372036854775807;
|
|
|
|
|
|
|
|
print(p11);
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p11);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Doubles.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Double> p = calloc();
|
2019-10-10 13:17:43 +00:00
|
|
|
p.value = 3.14159265359;
|
|
|
|
print('${p.runtimeType} value: ${p.value}');
|
|
|
|
p.value = 3.14;
|
|
|
|
print('${p.runtimeType} value: ${p.value}');
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Floats.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Float> p = calloc();
|
2019-10-10 13:17:43 +00:00
|
|
|
p.value = 3.14159265359;
|
|
|
|
print('${p.runtimeType} value: ${p.value}');
|
|
|
|
p.value = 3.14;
|
|
|
|
print('${p.runtimeType} value: ${p.value}');
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// IntPtr varies in size based on whether the platform is 32 or 64 bit.
|
|
|
|
// Addresses of pointers fit in this size.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<IntPtr> p = calloc();
|
2019-02-13 12:42:47 +00:00
|
|
|
int p14addr = p.address;
|
2019-10-10 13:17:43 +00:00
|
|
|
p.value = p14addr;
|
|
|
|
int pValue = p.value;
|
2019-02-13 12:42:47 +00:00
|
|
|
print('${p.runtimeType} value: ${pValue}');
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Void pointers are unsized.
|
|
|
|
// The size of the element it is pointing to is undefined,
|
|
|
|
// they cannot be allocated, read, or written.
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<IntPtr> p1 = calloc();
|
2019-10-10 13:17:43 +00:00
|
|
|
Pointer<Void> p2 = p1.cast();
|
2019-02-13 12:42:47 +00:00
|
|
|
print('${p2.runtimeType} address: ${p2.address}');
|
|
|
|
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p1);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Pointer to a pointer to something.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Int16> pHelper = calloc();
|
2019-10-10 13:17:43 +00:00
|
|
|
pHelper.value = 17;
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Pointer<Int16>> p = calloc();
|
2019-02-13 12:42:47 +00:00
|
|
|
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Storing into a pointer pointer automatically unboxes.
|
2019-10-10 13:17:43 +00:00
|
|
|
p.value = pHelper;
|
2019-02-13 12:42:47 +00:00
|
|
|
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Reading from a pointer pointer automatically boxes.
|
2019-10-10 13:17:43 +00:00
|
|
|
Pointer<Int16> pHelper2 = p.value;
|
|
|
|
print('${pHelper2.runtimeType} value: ${pHelper2.value}');
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2019-10-10 13:17:43 +00:00
|
|
|
int pValue = p.value.value;
|
2019-02-13 12:42:47 +00:00
|
|
|
print('${p.runtimeType} value\'s value: ${pValue}');
|
|
|
|
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(p);
|
|
|
|
calloc.free(pHelper);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// The pointer to pointer types must match up.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Int8> pHelper = calloc();
|
2019-10-10 13:17:43 +00:00
|
|
|
pHelper.value = 123;
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Pointer<Int16>> p = calloc();
|
2019-02-13 12:42:47 +00:00
|
|
|
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// Trying to store `pHelper` into `p.val` would result in a type mismatch.
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(pHelper);
|
|
|
|
calloc.free(p);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// `nullptr` points to address 0 in c++.
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<Pointer<Int8>> pointerToPointer = calloc();
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
Pointer<Int8> value = nullptr;
|
2019-10-10 13:17:43 +00:00
|
|
|
pointerToPointer.value = value;
|
|
|
|
value = pointerToPointer.value;
|
2019-02-13 12:42:47 +00:00
|
|
|
print("Loading a pointer to the 0 address is null: ${value}");
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(pointerToPointer);
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
// The toplevel function sizeOf returns element size in bytes.
|
2019-10-10 13:17:43 +00:00
|
|
|
print('sizeOf<Double>(): ${sizeOf<Double>()}');
|
|
|
|
print('sizeOf<Int16>(): ${sizeOf<Int16>()}');
|
|
|
|
print('sizeOf<IntPtr>(): ${sizeOf<IntPtr>()}');
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-06-15 11:08:28 +00:00
|
|
|
// With IntPtr pointers, one could manually setup arbitrary data
|
|
|
|
// structures in C memory.
|
[samples/ffi] Fix samples and run them as test
Change-Id: I49eeba32d99c9cf916d25150b46fa6d74ece0092
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/122382
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-10-23 22:20:37 +00:00
|
|
|
//
|
|
|
|
// However, it is advised to use Pointer<Pointer<...>> for that.
|
2019-02-13 12:42:47 +00:00
|
|
|
|
2019-10-10 13:17:43 +00:00
|
|
|
void createChain(Pointer<IntPtr> head, int length, int value) {
|
2019-02-13 12:42:47 +00:00
|
|
|
if (length == 0) {
|
2019-10-10 13:17:43 +00:00
|
|
|
head.value = value;
|
2019-02-13 12:42:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<IntPtr> next = calloc<IntPtr>();
|
2019-10-10 13:17:43 +00:00
|
|
|
head.value = next.address;
|
2019-02-13 12:42:47 +00:00
|
|
|
createChain(next, length - 1, value);
|
|
|
|
}
|
|
|
|
|
2019-10-10 13:17:43 +00:00
|
|
|
int getChainValue(Pointer<IntPtr> head, int length) {
|
2019-02-13 12:42:47 +00:00
|
|
|
if (length == 0) {
|
2019-10-10 13:17:43 +00:00
|
|
|
return head.value;
|
2019-02-13 12:42:47 +00:00
|
|
|
}
|
2019-10-10 13:17:43 +00:00
|
|
|
Pointer<IntPtr> next = Pointer.fromAddress(head.value);
|
2019-02-13 12:42:47 +00:00
|
|
|
return getChainValue(next, length - 1);
|
|
|
|
}
|
|
|
|
|
2019-10-10 13:17:43 +00:00
|
|
|
void freeChain(Pointer<IntPtr> head, int length) {
|
|
|
|
Pointer<IntPtr> next = Pointer.fromAddress(head.value);
|
2021-01-14 13:57:03 +00:00
|
|
|
calloc.free(head);
|
2019-02-13 12:42:47 +00:00
|
|
|
if (length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
freeChain(next, length - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int length = 10;
|
2021-01-14 13:57:03 +00:00
|
|
|
Pointer<IntPtr> head = calloc();
|
2019-02-13 12:42:47 +00:00
|
|
|
createChain(head, length, 512);
|
|
|
|
int tailValue = getChainValue(head, length);
|
|
|
|
print('tailValue: ${tailValue}');
|
|
|
|
freeChain(head, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
print("end main");
|
|
|
|
}
|