-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[vm/ffi] Migrate samples(_2)/ffi to
CallocAllocator
This CL does not yet roll `package:ffi` to use `Allocator`, because that breaks the checked in Dart in Flutter in g3. Instead, this copies `_CallocAllocator` from `package:ffi` into the samples. New API landed in: https://dart-review.googlesource.com/c/sdk/+/177705 Issue: #44621 Issue: #38721 Change-Id: I83da349c2e52d7f079aa1569b4726318fee24c9d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/177706 Commit-Queue: Daco Harkes <dacoharkes@google.com> Reviewed-by: Aske Simon Christensen <askesc@google.com>
- Loading branch information
Showing
36 changed files
with
822 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (c) 2021, 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. | ||
|
||
// TODO(https://dartbug.com/44621): Remove this copy when package:ffi can be | ||
// rolled. We need to wait until the `Allocator` interface has rolled into | ||
// Flutter. | ||
|
||
import 'dart:ffi'; | ||
import 'dart:io'; | ||
|
||
final DynamicLibrary stdlib = Platform.isWindows | ||
? DynamicLibrary.open('kernel32.dll') | ||
: DynamicLibrary.process(); | ||
|
||
typedef PosixCallocNative = Pointer Function(IntPtr num, IntPtr size); | ||
typedef PosixCalloc = Pointer Function(int num, int size); | ||
final PosixCalloc posixCalloc = | ||
stdlib.lookupFunction<PosixCallocNative, PosixCalloc>('calloc'); | ||
|
||
typedef PosixFreeNative = Void Function(Pointer); | ||
typedef PosixFree = void Function(Pointer); | ||
final PosixFree posixFree = | ||
stdlib.lookupFunction<PosixFreeNative, PosixFree>('free'); | ||
|
||
typedef WinGetProcessHeapFn = Pointer Function(); | ||
final WinGetProcessHeapFn winGetProcessHeap = stdlib | ||
.lookupFunction<WinGetProcessHeapFn, WinGetProcessHeapFn>('GetProcessHeap'); | ||
final Pointer processHeap = winGetProcessHeap(); | ||
|
||
typedef WinHeapAllocNative = Pointer Function(Pointer, Uint32, IntPtr); | ||
typedef WinHeapAlloc = Pointer Function(Pointer, int, int); | ||
final WinHeapAlloc winHeapAlloc = | ||
stdlib.lookupFunction<WinHeapAllocNative, WinHeapAlloc>('HeapAlloc'); | ||
|
||
typedef WinHeapFreeNative = Int32 Function( | ||
Pointer heap, Uint32 flags, Pointer memory); | ||
typedef WinHeapFree = int Function(Pointer heap, int flags, Pointer memory); | ||
final WinHeapFree winHeapFree = | ||
stdlib.lookupFunction<WinHeapFreeNative, WinHeapFree>('HeapFree'); | ||
|
||
const int HEAP_ZERO_MEMORY = 8; | ||
|
||
/// Manages memory on the native heap. | ||
/// | ||
/// Initializes newly allocated memory to zero. | ||
/// | ||
/// For POSIX-based systems, this uses `calloc` and `free`. On Windows, it uses | ||
/// `HeapAlloc` with [HEAP_ZERO_MEMORY] and `HeapFree` against the default | ||
/// public heap. | ||
class _CallocAllocator implements Allocator { | ||
const _CallocAllocator(); | ||
|
||
/// Allocates [byteCount] bytes of zero-initialized of memory on the native | ||
/// heap. | ||
/// | ||
/// For POSIX-based systems, this uses `calloc`. On Windows, it uses | ||
/// `HeapAlloc` against the default public heap. | ||
/// | ||
/// Throws an [ArgumentError] if the number of bytes or alignment cannot be | ||
/// satisfied. | ||
// TODO: Stop ignoring alignment if it's large, for example for SSE data. | ||
@override | ||
Pointer<T> allocate<T extends NativeType>(int byteCount, {int? alignment}) { | ||
Pointer<T> result; | ||
if (Platform.isWindows) { | ||
result = winHeapAlloc(processHeap, /*flags=*/ HEAP_ZERO_MEMORY, byteCount) | ||
.cast(); | ||
} else { | ||
result = posixCalloc(byteCount, 1).cast(); | ||
} | ||
if (result.address == 0) { | ||
throw ArgumentError('Could not allocate $byteCount bytes.'); | ||
} | ||
return result; | ||
} | ||
|
||
/// Releases memory allocated on the native heap. | ||
/// | ||
/// For POSIX-based systems, this uses `free`. On Windows, it uses `HeapFree` | ||
/// against the default public heap. It may only be used against pointers | ||
/// allocated in a manner equivalent to [allocate]. | ||
/// | ||
/// Throws an [ArgumentError] if the memory pointed to by [pointer] cannot be | ||
/// freed. | ||
/// | ||
// TODO(dartbug.com/36855): Once we have a ffi.Bool type we can use it instead | ||
// of testing the return integer to be non-zero. | ||
@override | ||
void free(Pointer pointer) { | ||
if (Platform.isWindows) { | ||
if (winHeapFree(processHeap, /*flags=*/ 0, pointer) == 0) { | ||
throw ArgumentError('Could not free $pointer.'); | ||
} | ||
} else { | ||
posixFree(pointer); | ||
} | ||
} | ||
} | ||
|
||
/// Manages memory on the native heap. | ||
/// | ||
/// Initializes newly allocated memory to zero. Use [malloc] for unintialized | ||
/// memory allocation. | ||
/// | ||
/// For POSIX-based systems, this uses `calloc` and `free`. On Windows, it uses | ||
/// `HeapAlloc` with [HEAP_ZERO_MEMORY] and `HeapFree` against the default | ||
/// public heap. | ||
const Allocator calloc = _CallocAllocator(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.