5

I'm trying to make my code a little more robust when allocating memory for ffi.

I've written the following function:

void withMemory<T extends NativeType>(
    int size, void Function(Pointer<T> memory) action) {
  final memory = calloc<Int8>(size);
  try {
    action(memory.cast());
  } finally {
    calloc.free(memory);
  }
}

To call the above function I use:

withMemory<Uint32>(sizeOf<Uint32>(), (phToken) {
  // use the memory allocated to phToken
});

This works but as you can see I need to pass Uint32 twice as well as the sizeOf.

What I really want to write is:

withMemory<Uint32>((phToken) {
  // use the memory allocated to phToken
});

The problem is that you can't pass a generic type to either calloc or sizeOf without the error:

The type arguments to [...] must be compile time constants but type parameters are not constants. Try changing the type argument to be a constant type.

Is there any way around this problem?

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Brett Sutton
  • 3,900
  • 2
  • 28
  • 53
  • I guess the optimisations the Dart team wanted for FFI that @daco-harkes listed in https://github.com/dart-lang/sdk/issues/44621 meant the need to sacrifice being able to do sizeOf() or memory allocation for generics. – Maks Oct 18 '21 at 04:08

1 Answers1

5

This can now be done with the Arena allocator from package:ffi.

using((Arena arena) {
  final p = arena<Uint32>();
  // Use the memory allocated to `p`.
}
// Memory freed.

Documentation:

Daco Harkes
  • 296
  • 3
  • 13