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