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