Karya, built on 2023-08-29T07:47:28 (patch 7a412d5d6ba4968ca4155ef276a062ccdeb9109a)
Safe HaskellSafe-Inferred

Util.FFI

Description

Utilities for C wrappers.

  • Functions to convert between haskell and c types.
  • Generic UI debugging functions.
Synopsis

convert

c_rune :: Char -> Word.Word32 Source #

This corresponds to utf8::rune.

c_bool :: Bool -> CChar Source #

bool is C++, not C, so I represent bools as chars.

bytesToCString0 :: ByteString.ByteString -> IO CString Source #

Copy the bytestring to a null-terminated cstring, in malloc'd space. ByteString only has an alloca version of this.

newCString0 :: Text -> IO CString Source #

Allocate a new UTF8-encoded null-terminated CString.

This copies the string twice, but I think I'd need a encodeUtf8 that can write directly to a pointer to solve that.

newCStringNull0 :: Text -> IO CString Source #

Like newCString0, but optimize "" to nullptr. The C++ side has to be prepared for this.

withText :: Text -> (CString -> IO a) -> IO a Source #

ForeignPtr

withForeignPtrs :: [ForeignPtr a] -> ([Ptr a] -> IO b) -> IO b Source #

FunPtr

makeFunPtr :: String -> IO (FunPtr a) -> IO (FunPtr a) Source #

Forgetting to call freeHaskellFunPtr is an easy way to leak memory. So all FunPtrs should be created with this function, and always bee freed with freeFunPtr. That way I can log creates and frees to ensure they are balanced. Use tools/track_funptr.py to automate that.

new :: CStorable a => a -> IO (Ptr a) Source #

This should be in c-storable, but updating via hackage is such a pain I'll inline it for now.