fpco / inline-c

284 stars 50 forks source link

Generate FunPtr for ForeignPtr finalisation #63

Closed sumo closed 6 years ago

sumo commented 6 years ago

My primary use case is to get a FunPtr that can be used in newForeignPtr so would like to get inline-c to return a function pointer

For example:

C function:

void pairing_clear(pairing_t pairing);

and corresponding Haskell:

freePairing :: Ptr PBCPairing -> IO () freePairing ptr = [C.exp| void { pairing_clear (struct pairing_s *ptr) } |]

I would like to do:

allocPairing :: IO (ForeignPtr PBCPairing) allocPairing = malloc >>= newForeignPtr [C.??? | ...]

bitonic commented 6 years ago

you can use mkFunPtrFromName, e.g. mkFunPtrFromName 'freePairing.

sumo commented 6 years ago

Unfortunately this does not work for finalizers and results in:

test-pbc: error: a C finalizer called back into Haskell. This was previously allowed, but is disallowed in GHC 6.10.2 and later. To create finalizers that may call back into Haskell, use Foreign.Concurrent.newForeignPtr instead of Foreign.newForeignPtr.

I'm actually not sure if what I am asking for is a good use case for inline-c as the foreign call method is concise and works well.