allocate<T extends NativeType> method

  1. @override
Pointer<T> allocate<T extends NativeType>(
  1. int byteCount, {
  2. int? alignment,
})
override

Allocates byteCount bytes of zero-initialized of memory on the native heap.

For POSIX-based systems, this uses malloc. On Windows, it uses CoTaskMemAlloc.

Throws an ArgumentError if the number of bytes or alignment cannot be satisfied.

Implementation

// 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 = winCoTaskMemAlloc(byteCount).cast();
  } else {
    result = posixCalloc(byteCount, 1).cast();
  }
  if (result.address == 0) {
    throw ArgumentError('Could not allocate $byteCount bytes.');
  }
  if (Platform.isWindows) {
    _zeroMemory(result, byteCount);
  }
  return result;
}