Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NativeFinalizer to go with malloc and calloc #910

Closed
dcharkes opened this issue Sep 23, 2022 · 3 comments
Closed

Add NativeFinalizer to go with malloc and calloc #910

dcharkes opened this issue Sep 23, 2022 · 3 comments

Comments

@dcharkes
Copy link
Collaborator

Our malloc and calloc allocators use a different allocator on Windows.

This means that devs wanting to use a NativeFinalizer with malloc and calloc need to branch on OS.

Also, devs wanting to use NativeFinalizers with malloc and calloc rely on the specific memory allocator that package:ffi uses under the hood. This means we cannot change the underlying allocator without breaking dependencies.

If we would provide a NativeFinalizer that would free the memory backed by calloc and malloc, we could change the implementation without breaking people. (Assuming they do not lookup their own "free" somewhere.)

cc @timsneath heads up for package:win32
cc @mkustermann we're locked in to CoTaskMemAlloc

@mkustermann
Copy link
Member

Would it be sufficient to add a getter to the Allocator interface:

abstract class Allocator {
  Pointer<T> allocate<T extends NativeType>(int byteCount, {int? alignment});
  void free(Pointer<NativeType> pointer);
+  Pointer<NativeFinalizerFunction> get freeFunction;
}

?

@dcharkes
Copy link
Collaborator Author

We would need to make it nullable, because we're not guaranteed that all functions use the same signature.
And Arena should return null, because it is going to free the things itself.

(The freeFunction would then flow into a NativeFinalizer, and would be unpacked again from the NativeFinalizer in the EagerFinalizable. dart-lang/sdk#49906 👌 )

@mkustermann
Copy link
Member

As suggested above, the package has now a Allocator.nativeFree that can be used in finalizers (see dart-archive/ffi#203). I think this should close this issue, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants