Provide a flag to use the C malloc instead of the default GHC malloc

This commit is contained in:
adithyaov 2020-08-28 00:41:15 +05:30 committed by Harendra Kumar
parent 02f7ce0714
commit 9ccf93168b
2 changed files with 15 additions and 6 deletions

View File

@ -16,11 +16,12 @@ module Streamly.Memory.Malloc
)
where
#define USE_GHC_MALLOC
-- We use GHC malloc by default
import Foreign.ForeignPtr (ForeignPtr, newForeignPtr_)
import Foreign.Marshal.Alloc (mallocBytes)
#ifndef USE_GHC_MALLOC
#ifdef USE_C_MALLOC
import Foreign.ForeignPtr (newForeignPtr)
import Foreign.Marshal.Alloc (finalizerFree)
#endif
@ -29,13 +30,13 @@ import qualified GHC.ForeignPtr as GHC
{-# INLINE mallocForeignPtrAlignedBytes #-}
mallocForeignPtrAlignedBytes :: Int -> Int -> IO (GHC.ForeignPtr a)
#ifdef USE_GHC_MALLOC
mallocForeignPtrAlignedBytes =
GHC.mallocPlainForeignPtrAlignedBytes
#else
#ifdef USE_C_MALLOC
mallocForeignPtrAlignedBytes size _alignment = do
p <- mallocBytes size
newForeignPtr finalizerFree p
#else
mallocForeignPtrAlignedBytes =
GHC.mallocPlainForeignPtrAlignedBytes
#endif
-- memalign alignment size

View File

@ -204,6 +204,11 @@ flag examples-sdl
manual: True
default: False
flag use-c-malloc
description: Use C malloc instead of GHC malloc
manual: True
default: False
-------------------------------------------------------------------------------
-- Common stanzas
-------------------------------------------------------------------------------
@ -222,6 +227,9 @@ common compile-options
if flag(inspection)
cpp-options: -DINSPECTION
if flag(use-c-malloc)
cpp-options: -DUSE_C_MALLOC
ghc-options: -Wall
-Wcompat
-Wunrecognised-warning-flags