Created
April 26, 2020 22:04
-
-
Save jimenezrick/f818e90c82e0f0bbfdf3ba6f9420bccc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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