on master help sign in
Add support to switch to C malloc vs GHC malloc
commitb38198c3f56cf2d15057d398f3c10daa113213d5 authorHarendra Kumar <harendra.kumar@gmail.com> dateTue 10 Sep 2019, 11:25 (7 years ago) parent7d9c88b
3 files changed+53−5
src/Streamly/Memory/Array/Types.hs+6−5 src/Streamly/Memory/Malloc.hs+46 streamly.cabal+1
src/Streamly/Memory/Array/Types.hs+6−5
@@ -98,8 +98,7 @@ import Text.Read (readPrec, readListPrec, readListPrecDefault)
9898
9999import GHC.Base (Addr#, realWorld#)
100100import GHC.Exts (IsList, IsString(..))
101−import GHC.ForeignPtr
102− (ForeignPtr(..), mallocPlainForeignPtrAlignedBytes, newForeignPtr_)
101+import GHC.ForeignPtr (ForeignPtr(..), newForeignPtr_)
103102import GHC.IO (IO(IO), unsafePerformIO)
104103import GHC.Ptr (Ptr(..))
105104
@@ -112,6 +111,7 @@ import Streamly.SVar (adaptState)
112111import Streamly.FileSystem.FDIO (IOVec(..))
113112#endif
114113
114+import qualified Streamly.Memory.Malloc as Malloc
115115import qualified Streamly.Streams.StreamD.Type as D
116116import qualified Streamly.Streams.StreamK as K
117117import qualified GHC.Exts as Exts
@@ -237,7 +237,7 @@ bytesToElemCount x n =
237237newArray :: forall a. Storable a => Int -> IO (Array a)
238238newArray count = do
239239 let size = count * sizeOf (undefined :: a)
240− fptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: a))
240+ fptr <- Malloc.mallocForeignPtrAlignedBytes size (alignment (undefined :: a))
241241 let p = unsafeForeignPtrToPtr fptr
242242 return $ Array
243243 { aStart = fptr
@@ -275,7 +275,8 @@ snoc arr@Array {..} x = do
275275 let oldStart = unsafeForeignPtrToPtr aStart
276276 size = aEnd `minusPtr` oldStart
277277 newSize = (size + (sizeOf (undefined :: a)))
278− newPtr <- mallocPlainForeignPtrAlignedBytes newSize (alignment (undefined :: a))
278+ newPtr <- Malloc.mallocForeignPtrAlignedBytes
279+ newSize (alignment (undefined :: a))
279280 withForeignPtr newPtr $ \pNew -> do
280281 memcpy (castPtr pNew) (castPtr oldStart) size
281282 poke (pNew `plusPtr` size) x
@@ -298,7 +299,7 @@ realloc newSize Array{..} = do
298299 assert (aEnd <= aBound) (return ())
299300 let oldStart = unsafeForeignPtrToPtr aStart
300301 let size = aEnd `minusPtr` oldStart
301− newPtr <- mallocPlainForeignPtrAlignedBytes
302+ newPtr <- Malloc.mallocForeignPtrAlignedBytes
302303 newSize (alignment (undefined :: a))
303304 withForeignPtr newPtr $ \pNew -> do
304305 memcpy (castPtr pNew) (castPtr oldStart) size
src/Streamly/Memory/Malloc.hsnew file+46
@@ -0,0 +1,46 @@
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 #-}
10+
11+#include "inline.hs"
12+
13+-- |
14+-- Module : Streamly.Memory.Array.Types
15+-- Copyright : (c) 2019 Composewell Technologies
16+--
17+-- License : BSD3
18+-- Maintainer : harendra.kumar@gmail.com
19+-- Stability : experimental
20+-- Portability : GHC
21+--
22+module Streamly.Memory.Malloc
23+ (
24+ mallocForeignPtrAlignedBytes
25+ )
26+where
27+
28+#define USE_GHC_MALLOC
29+
30+#ifndef USE_GHC_MALLOC
31+import Foreign.ForeignPtr (newForeignPtr)
32+import Foreign.Marshal.Alloc (mallocBytes, finalizerFree)
33+#endif
34+
35+import qualified GHC.ForeignPtr as GHC
36+
37+{-# INLINE mallocForeignPtrAlignedBytes #-}
38+mallocForeignPtrAlignedBytes :: Int -> Int -> IO (GHC.ForeignPtr a)
39+#ifdef USE_GHC_MALLOC
40+mallocForeignPtrAlignedBytes size alignment = do
41+ GHC.mallocPlainForeignPtrAlignedBytes size alignment
42+#else
43+mallocForeignPtrAlignedBytes size _alignment = do
44+ p <- mallocBytes size
45+ newForeignPtr finalizerFree p
46+#endif
streamly.cabal+1
@@ -258,6 +258,7 @@ library
258258 , Streamly.Enumeration
259259
260260 -- Memory storage
261+ , Streamly.Memory.Malloc
261262 , Streamly.Memory.Array.Types
262263 , Streamly.Memory.Ring
263264