on 9851424 help sign in
50 lines 1.4 kB history raw
1{-# LANGUAGE TypeFamilies #-}
3#include "inline.hs"
5-- |
6-- Module : Streamly.Memory.Malloc
7-- Copyright : (c) 2019 Composewell Technologies
8--
9-- License : BSD3
10-- Maintainer : streamly@composewell.com
11-- Stability : experimental
12-- Portability : GHC
13--
14module Streamly.Memory.Malloc
15 (
16 mallocForeignPtrAlignedBytes
17 , mallocForeignPtrAlignedUnmanagedBytes
18 )
19where
21#define USE_GHC_MALLOC
23import Foreign.ForeignPtr (ForeignPtr, newForeignPtr_)
24import Foreign.Marshal.Alloc (mallocBytes)
25#ifndef USE_GHC_MALLOC
26import Foreign.ForeignPtr (newForeignPtr)
27import Foreign.Marshal.Alloc (finalizerFree)
28#endif
30import qualified GHC.ForeignPtr as GHC
32{-# INLINE mallocForeignPtrAlignedBytes #-}
33mallocForeignPtrAlignedBytes :: Int -> Int -> IO (GHC.ForeignPtr a)
34#ifdef USE_GHC_MALLOC
35mallocForeignPtrAlignedBytes =
36 GHC.mallocPlainForeignPtrAlignedBytes
37#else
38mallocForeignPtrAlignedBytes size _alignment = do
39 p <- mallocBytes size
40 newForeignPtr finalizerFree p
41#endif
43-- memalign alignment size
44-- foreign import ccall unsafe "stdlib.h posix_memalign" _memalign :: CSize -> CSize -> IO (Ptr a)
46mallocForeignPtrAlignedUnmanagedBytes :: Int -> Int -> IO (ForeignPtr a)
47mallocForeignPtrAlignedUnmanagedBytes size _alignment = do
48 -- XXX use posix_memalign/aligned_alloc for aligned allocation
49 p <- mallocBytes size
50 newForeignPtr_ p