Skip to content

Instantly share code, notes, and snippets.

View michael-swan's full-sized avatar

Michael Swan michael-swan

View GitHub Profile
@michael-swan
michael-swan / Alloc.hs
Created April 7, 2018 23:26
allocaBytes in MonadIO
allocaBytes :: MonadIO m => Int -> (Ptr a -> m b) -> m b
allocaBytes (I# size) action = do
barr# <- liftIO $ IO $ \ s0 ->
case newPinnedByteArray# size s0 of { (# s1, mbarr# #) ->
case unsafeFreezeByteArray# mbarr# s1 of { (# s2, barr# #) -> barr# } }
ret <- action (Ptr (byteArrayContents# barr#))
liftIO $ IO $ \ s3 ->
case touch# barr# s3 of { s4 -> (# s4, ret #) }

Keybase proof

I hereby claim:

  • I am michael-swan on github.
  • I am michael_swan (https://keybase.io/michael_swan) on keybase.
  • I have a public key ASA8oHHAcRUSuNZxknsKo4gLLkeGB7fY9-VGFLF26aKu0go

To claim this, I am signing this object: