Manages memory on the native heap.
When allocating memory, prefer calling this allocator directly as a function (see AllocatorAlloc.call for details).
This interface provides only the allocate method to allocate a block of bytes, and the free method to release such a block again. Implementations only need to provide those two methods. The AllocatorAlloc.call extension method is defined in terms of those lower-level operations.
An example of an allocator wrapping another to count the number of allocations:
class CountingAllocator implements Allocator {
final Allocator _wrappedAllocator;
int _totalAllocations = 0;
int _nonFreedAllocations = 0;
CountingAllocator([Allocator? allocator])
: _wrappedAllocator = allocator ?? calloc;
int get totalAllocations => _totalAllocations;
int get nonFreedAllocations => _nonFreedAllocations;
@override
Pointer<T> allocate<T extends NativeType>(int byteCount, {int? alignment}) {
final result =
_wrappedAllocator.allocate<T>(byteCount, alignment: alignment);
_totalAllocations++;
_nonFreedAllocations++;
return result;
}
@override
void free(Pointer<NativeType> pointer) {
_wrappedAllocator.free(pointer);
_nonFreedAllocations--;
}
} byteCount bytes of memory on the native heap.
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-ffi/Allocator-class.html