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