This repository was archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Allocation utilities. #7
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
|
||
export 'src/utf8.dart'; | ||
export 'src/utf16.dart'; | ||
export 'src/allocation.dart' show allocate, free; |
This file contains hidden or 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,78 @@ | ||
// 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. | ||
|
||
import 'dart:ffi'; | ||
import 'dart:io'; | ||
|
||
// Note that kernel32.dll is the correct name in both 32-bit and 64-bit. | ||
final DynamicLibrary stdlib = Platform.isWindows | ||
? DynamicLibrary.open("kernel32.dll") | ||
: DynamicLibrary.process(); | ||
|
||
typedef PosixMallocNative = Pointer Function(IntPtr); | ||
typedef PosixMalloc = Pointer Function(int); | ||
final PosixMalloc posixMalloc = | ||
stdlib.lookupFunction<PosixMallocNative, PosixMalloc>("malloc"); | ||
|
||
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"); | ||
|
||
/// Allocates memory on the native heap. | ||
/// | ||
/// For POSIX-based systems, this uses malloc. On Windows, it uses HeapAlloc | ||
/// against the default public heap. Allocation of either element size or count | ||
/// of 0 is undefined. | ||
/// | ||
/// Throws an ArgumentError on failure to allocate. | ||
Pointer<T> allocate<T extends NativeType>({int count = 1}) { | ||
final int totalSize = count * sizeOf<T>(); | ||
Pointer<T> result; | ||
if (Platform.isWindows) { | ||
result = winHeapAlloc(processHeap, /*flags=*/ 0, totalSize).cast(); | ||
} else { | ||
result = posixMalloc(totalSize).cast(); | ||
} | ||
if (result.address == 0) { | ||
throw ArgumentError("Could not allocate $totalSize bytes."); | ||
} | ||
return result; | ||
} | ||
|
||
/// Releases memory 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 on failure to free. | ||
/// | ||
// 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. | ||
void free(Pointer pointer) { | ||
if (Platform.isWindows) { | ||
if (winHeapFree(processHeap, /*flags=*/ 0, pointer) == 0) { | ||
throw ArgumentError("Could not free $pointer."); | ||
} | ||
} else { | ||
posixFree(pointer); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.