Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New mallocForeignPtrBytesWithAlignedPtr function #83

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hw-prim.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ library
HaskellWorks.Data.Vector.Storable
HaskellWorks.Data.Vector.StorableVectorLike
HaskellWorks.Data.Word
HaskellWorks.Foreign
other-modules: Paths_hw_prim
autogen-modules: Paths_hw_prim
hs-source-dirs: src
Expand Down
16 changes: 16 additions & 0 deletions src/HaskellWorks/Foreign.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module HaskellWorks.Foreign
( mallocForeignPtrBytesWithAlignedPtr
) where

import Foreign.ForeignPtr (ForeignPtr)
import Foreign.Ptr (Ptr, alignPtr)
import Foreign.Storable (Storable)

import qualified Foreign.ForeignPtr as F
import qualified Foreign.ForeignPtr.Unsafe as F

mallocForeignPtrBytesWithAlignedPtr :: Storable a => Int -> Int -> IO (ForeignPtr a, Ptr a)
mallocForeignPtrBytesWithAlignedPtr alignment n = do
fptr <- F.mallocForeignPtrBytes (n + alignment)
let alignedPtr = alignPtr (F.unsafeForeignPtrToPtr fptr) alignment
return (fptr, alignedPtr)