well-typed / hs-bindgen

Automatically generate Haskell bindings from C header files
20 stars 0 forks source link

Avoid leaks due to async exceptions #149

Closed edsko closed 2 months ago

edsko commented 2 months ago

@phadej I'd appreciate your eyes on this.

Closes #136.

edsko commented 2 months ago

Critical changes are:

edsko commented 2 months ago

Perhaps we should instead of masking async exceptions preallocate the buffer Haskell side. Especially during the traversal (maybe only there?) that might be easier. I need to think about this some more.

phadej commented 2 months ago

Perhaps we should instead of masking async exceptions preallocate the buffer Haskell side.

Yes, one could have newtype CXCursor = CXCursor ByteArray. I do something like that e.g. in hashable's xxhash bindings for hash state (https://github.com/haskell-unordered-containers/hashable/blob/57e0d06623d9f2c8a0c5ff13e04b5a4002d699f5/src/Data/Hashable/XXH3.hs#L72), though there state is mutable, the approach is similar.

(EDIT: allocating before hand is closer to actually machine code generated: though the space for result value in C is reserved on stack, and that is not possible in Haskell).