Skip to content

Instantly share code, notes, and snippets.

@psibi
Last active November 8, 2016 16:16
Show Gist options
  • Save psibi/e01ae8e9fe760488685253916b27efbb to your computer and use it in GitHub Desktop.
Save psibi/e01ae8e9fe760488685253916b27efbb to your computer and use it in GitHub Desktop.
Sample Haskell FFI read
foreign import ccall unsafe "read" c_read :: CInt -> Ptr () -> CSize -> IO CSsize
read :: Int -- ^ file descriptor
-> Int64 -- ^ Bytes to allocate
-> Word64 -- ^ Read upto this many bytes
-> IO (ByteString, Int64)
read fd bytes len = do
(ptr :: Ptr ()) <- mallocBytes (fromIntegral bytes)
size <- c_read (fromIntegral fd) ptr (fromIntegral len)
bstring <- packCString (castPtr ptr)
free ptr
return (bstring, (fromIntegral size))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment