Skip to content

calloc with generic of native type #334

Answered by timsneath
kirill-21 asked this question in Q&A
Discussion options

You must be logged in to vote

I'm not quite sure what you're trying to do... so forgive me if this isn't helpful.

The challenge is that both of these are consts. You can work around this by implementing a custom allocator, of course:

import 'dart:ffi';
import 'dart:typed_data';

import 'package:win32/win32.dart';

class Allocator2 implements Allocator {
  Allocator2();

  @override
  Pointer<T> allocate<T extends NativeType>(int byteCount, {int? alignment}) {
    Pointer<T> result;
    result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, byteCount)
        .cast();
    if (result.address == 0) {
      throw ArgumentError('Could not allocate $byteCount bytes.');
    }
    return result;
  }

  @override
  void free(P…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kirill-21
Comment options

@timsneath
Comment options

Answer selected by timsneath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants