on 4e604ef help sign in
49 lines 1.4 kB history raw
1#include "inline.hs"
3-- |
4-- Module : Streamly.Internal.Foreign.Malloc
5-- Copyright : (c) 2019 Composewell Technologies
6--
7-- License : BSD-3-Clause
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
10-- Portability : GHC
11--
12module Streamly.Internal.Foreign.Malloc
13 (
14 mallocForeignPtrAlignedBytes
15 , mallocForeignPtrAlignedUnmanagedBytes
16 )
17where
20-- We use GHC malloc by default
22import Foreign.ForeignPtr (ForeignPtr, newForeignPtr_)
23import Foreign.Marshal.Alloc (mallocBytes)
24#ifdef USE_C_MALLOC
25import Foreign.ForeignPtr (newForeignPtr)
26import Foreign.Marshal.Alloc (finalizerFree)
27#endif
29import qualified GHC.ForeignPtr as GHC
31{-# INLINE mallocForeignPtrAlignedBytes #-}
32mallocForeignPtrAlignedBytes :: Int -> Int -> IO (GHC.ForeignPtr a)
33#ifdef USE_C_MALLOC
34mallocForeignPtrAlignedBytes size _alignment = do
35 p <- mallocBytes size
36 newForeignPtr finalizerFree p
37#else
38mallocForeignPtrAlignedBytes =
39 GHC.mallocPlainForeignPtrAlignedBytes
40#endif
42-- memalign alignment size
43-- foreign import ccall unsafe "stdlib.h posix_memalign" _memalign :: CSize -> CSize -> IO (Ptr a)
45mallocForeignPtrAlignedUnmanagedBytes :: Int -> Int -> IO (ForeignPtr a)
46mallocForeignPtrAlignedUnmanagedBytes size _alignment = do
47 -- XXX use posix_memalign/aligned_alloc for aligned allocation
48 p <- mallocBytes size
49 newForeignPtr_ p