🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

Add ForeignPtr type and builtins for finalizing external resources - #6192

Merged
dolio merged 13 commits into
trunkfrom
topic/foreign-ptr
Mar 27, 2026
Merged

Add ForeignPtr type and builtins for finalizing external resources#6192
dolio merged 13 commits into
trunkfrom
topic/foreign-ptr

Conversation

@dolio

@dolio dolio commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

This PR adds a ForeignPtr type and various related builtins for managing foreign resources in conjunction with garbage collection. This is based on GHC's support for this feature.

ForeignPtrs can be created in two ways

  1. Combining a Ptr a with a finalizer, which is useful for memory allocated and returned from foreign functions
  2. Directly allocating in unison with allocate functions. These are similar to the existing functions for Ptr, except they come equipped with finalizers that automatically release the memory (I think actually the memory is automatically collected because it's pinned memory in the Haskell heap, but that doesn't really matter)

There are also two types of finalizers available

  1. Foreign finalizers with type Func (Ptr a ->{IO} ()). These are foreign functions loaded from a DLL via a new builtin that returns a 'function pointer' rather than a callable unison function.
  2. Unison finalizers of type '{IO} ()

These can't actually be combined, because GHC doesn't allow it. However, anything you could do with the first could be done by the second by calling the foreign function in the thunk.

I didn't add any builtins for dereferencing the function pointers, since the only use we really have for them right now are these finalizers. There is enough information in the representation to get out a callable function if that becomes desirable in the future.

Tests of the new features have been added in transcripts. I didn't exhaustively test all the allocation builtins, but they're implemented uniformly, so testing one (which I did) should be sufficient.

I also had to tweak one of the git hook scripts to actually be able to push this, so that's included.

I have most of the docs written up for inclusion in base if/once this gets merged and released.

Haven't run share tests and benchmarks yet. I'll get on that.

@dolio
dolio requested review from aryairani and pchiusano March 18, 2026 19:15
@dolio
dolio requested a review from a team as a code owner March 18, 2026 19:15
@dolio

dolio commented Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

Share tests pass (aside from a couple expected failures).

Benchmarks look good.

I must not completely understand the new checking framework to get past CI, though.

@aryairani

Copy link
Copy Markdown
Contributor

We're working out some issues with it, sorry. I'll try making it pass.

@pchiusano pchiusano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM assuming conflicts are fixed and CI passes. Can you prep a PR to base after this gets merged, too?

@dolio dolio added the ready-to-merge Apply this to a PR and it will get merged automatically once CI passes and 1 reviewer has approved label Mar 27, 2026
@dolio
dolio added this pull request to the merge queue Mar 27, 2026
Merged via the queue into trunk with commit 8cf7901 Mar 27, 2026
5 checks passed
@dolio
dolio deleted the topic/foreign-ptr branch March 27, 2026 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Apply this to a PR and it will get merged automatically once CI passes and 1 reviewer has approved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants