on master help sign in
Provide a flag to use the C malloc instead of the default GHC malloc
9ccf931adithyaov
src/Streamly/Memory/Malloc.hs+7−6
@@ -16,11 +16,12 @@ module Streamly.Memory.Malloc
16 )
16 )
17where
17where
18
18
19−#define USE_GHC_MALLOC
19+
20+-- We use GHC malloc by default
20
21
21import Foreign.ForeignPtr (ForeignPtr, newForeignPtr_)
22import Foreign.ForeignPtr (ForeignPtr, newForeignPtr_)
22import Foreign.Marshal.Alloc (mallocBytes)
23import Foreign.Marshal.Alloc (mallocBytes)
23−#ifndef USE_GHC_MALLOC
24+#ifdef USE_C_MALLOC
24import Foreign.ForeignPtr (newForeignPtr)
25import Foreign.ForeignPtr (newForeignPtr)
25import Foreign.Marshal.Alloc (finalizerFree)
26import Foreign.Marshal.Alloc (finalizerFree)
26#endif
27#endif
@@ -29,13 +30,13 @@ import qualified GHC.ForeignPtr as GHC
29
30
30{-# INLINE mallocForeignPtrAlignedBytes #-}
31{-# INLINE mallocForeignPtrAlignedBytes #-}
31mallocForeignPtrAlignedBytes :: Int -> Int -> IO (GHC.ForeignPtr a)
32mallocForeignPtrAlignedBytes :: Int -> Int -> IO (GHC.ForeignPtr a)
32−#ifdef USE_GHC_MALLOC
33+#ifdef USE_C_MALLOC
33−mallocForeignPtrAlignedBytes =
34− GHC.mallocPlainForeignPtrAlignedBytes
35−#else
36mallocForeignPtrAlignedBytes size _alignment = do
34mallocForeignPtrAlignedBytes size _alignment = do
37 p <- mallocBytes size
35 p <- mallocBytes size
38 newForeignPtr finalizerFree p
36 newForeignPtr finalizerFree p
37+#else
38+mallocForeignPtrAlignedBytes =
39+ GHC.mallocPlainForeignPtrAlignedBytes
39#endif
40#endif
40
41
41-- memalign alignment size
42-- memalign alignment size
streamly.cabal+8
@@ -204,6 +204,11 @@ flag examples-sdl
204 manual: True
204 manual: True
205 default: False
205 default: False
206
206
207+flag use-c-malloc
208+ description: Use C malloc instead of GHC malloc
209+ manual: True
210+ default: False
211+
207-------------------------------------------------------------------------------
212-------------------------------------------------------------------------------
208-- Common stanzas
213-- Common stanzas
209-------------------------------------------------------------------------------
214-------------------------------------------------------------------------------
@@ -222,6 +227,9 @@ common compile-options
222 if flag(inspection)
227 if flag(inspection)
223 cpp-options: -DINSPECTION
228 cpp-options: -DINSPECTION
224
229
230+ if flag(use-c-malloc)
231+ cpp-options: -DUSE_C_MALLOC
232+
225 ghc-options: -Wall
233 ghc-options: -Wall
226 -Wcompat
234 -Wcompat
227 -Wunrecognised-warning-flags
235 -Wunrecognised-warning-flags