Allocator class Null safety

Manages memory on the native heap.

When allocating memory, prefer calling this allocator directly as a function (see 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 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;

  Pointer<T> allocate<T extends NativeType>(int byteCount, {int? alignment}) {
    final result =
        _wrappedAllocator.allocate<T>(byteCount, alignment: alignment);
    return result;

  void free(Pointer<NativeType> pointer) {;
Available Extensions
  • @Since('2.12')


hashCode int
The hash code for this object.
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited


allocate<T extends NativeType>(int byteCount, {int? alignment}) Pointer<T>
Allocates byteCount bytes of memory on the native heap.
free(Pointer<NativeType> pointer) → void
Releases memory allocated on the native heap.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
toString() String
A string representation of this object.


operator ==(Object other) bool
The equality operator.