allocate<T extends NativeType> method

Pointer<T> allocate<T extends NativeType>(
  1. int sizeInBytes
)

Allocate memory using calloc and track it for automatic cleanup.

Dart 3 Compatibility

Due to Dart 3 FFI changes, the caller must provide the exact byte size using sizeOf<T>(). The generic type T is used for pointer casting only.

Parameters

  • sizeInBytes: The size in bytes to allocate. Use sizeOf<T>() to get the correct size for your type. Defaults to 8 bytes (size of a pointer).

Example

// Allocate a dpiData structure
final data = manager.allocate<dpiData>(sizeOf<dpiData>());

// Allocate a pointer to pointer
final ptrPtr = manager.allocate<Pointer<dpiConn>>(sizeOf<Pointer<dpiConn>>());

// Allocate a simple integer
final intPtr = manager.allocate<Uint32>(sizeOf<Uint32>());

Throws

Implementation

Pointer<T> allocate<T extends NativeType>(int sizeInBytes) {
  if (_disposed) {
    throw OracleMemoryException('MemoryManager has been disposed');
  }

  try {
    final pointer = calloc.allocate<T>(sizeInBytes);
    _allocatedPointers.add(pointer);
    return pointer;
  } catch (e) {
    throw OracleMemoryException(
      'Failed to allocate memory for type $T: $e',
    );
  }
}