Skip to content

Instantly share code, notes, and snippets.

@jimenezrick
Created April 26, 2020 22:04
Show Gist options
  • Save jimenezrick/f818e90c82e0f0bbfdf3ba6f9420bccc to your computer and use it in GitHub Desktop.
Save jimenezrick/f818e90c82e0f0bbfdf3ba6f9420bccc to your computer and use it in GitHub Desktop.
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
import Data.ByteString
import Data.ByteString.Unsafe
import Data.Time.Clock
import qualified Language.C.Inline as C
import qualified Language.C.Inline.Unsafe as CU
C.context (C.baseCtx <> C.bsCtx)
C.include "<stdlib.h>"
C.include "<string.h>"
measureTime :: IO a -> IO a
measureTime f = do
t0 <- getCurrentTime
a <- f
t1 <- getCurrentTime
print $ diffUTCTime t1 t0
return a
alloc3BS :: IO ByteString
alloc3BS = do
buf <-
[C.block| char * {
char *buf = malloc(3);
return strncpy(buf, "ok!", 3);
} |]
-- buf :: _
unsafePackMallocCStringLen (buf, 3)
main :: IO ()
main = do
bs <- alloc3BS
print bs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment