summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Packed/Internal/Vector.hs29
1 files changed, 24 insertions, 5 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index 7d6e3d5..8723367 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -1,4 +1,4 @@
1{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} 1{-# LANGUAGE MagicHash, CPP, UnboxedTuples #-}
2----------------------------------------------------------------------------- 2-----------------------------------------------------------------------------
3-- | 3-- |
4-- Module : Data.Packed.Internal.Vector 4-- Module : Data.Packed.Internal.Vector
@@ -21,13 +21,20 @@ import Foreign
21import Complex 21import Complex
22import Control.Monad(when) 22import Control.Monad(when)
23 23
24#if __GLASGOW_HASKELL__ >= 605
25import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
26#else
27import Foreign.ForeignPtr (mallocForeignPtrBytes)
28#endif
29
24import GHC.Base 30import GHC.Base
25import GHC.IOBase 31import GHC.IOBase
26 32
27-- | A one-dimensional array of objects stored in a contiguous memory block. 33-- | A one-dimensional array of objects stored in a contiguous memory block.
28data Vector t = V { dim :: {-# UNPACK #-} !Int -- ^ number of elements 34data Vector t =
29 , fptr :: {-# UNPACK #-}!(ForeignPtr t) -- ^ foreign pointer to the memory block 35 V { dim :: {-# UNPACK #-} !Int -- ^ number of elements
30 } 36 , fptr :: {-# UNPACK #-} !(ForeignPtr t) -- ^ foreign pointer to the memory block
37 }
31 38
32vec = withVector 39vec = withVector
33 40
@@ -40,8 +47,20 @@ withVector (V n fp) f = withForeignPtr fp $ \p -> do
40createVector :: Storable a => Int -> IO (Vector a) 47createVector :: Storable a => Int -> IO (Vector a)
41createVector n = do 48createVector n = do
42 when (n <= 0) $ error ("trying to createVector of dim "++show n) 49 when (n <= 0) $ error ("trying to createVector of dim "++show n)
43 fp <- mallocForeignPtrArray n 50 fp <- doMalloc undefined
44 return $ V n fp 51 return $ V n fp
52 where
53 --
54 -- Use the much cheaper Haskell heap allocated storage
55 -- for foreign pointer space we control
56 --
57 doMalloc :: Storable b => b -> IO (ForeignPtr b)
58 doMalloc dummy = do
59#if __GLASGOW_HASKELL__ >= 605
60 mallocPlainForeignPtrBytes (n * sizeOf dummy)
61#else
62 mallocForeignPtrBytes (n * sizeOf dummy)
63#endif
45 64
46{- | creates a Vector from a list: 65{- | creates a Vector from a list:
47 66