diff options
Diffstat (limited to 'lib/Data/Packed/Internal/Vector.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 29 |
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 | |||
21 | import Complex | 21 | import Complex |
22 | import Control.Monad(when) | 22 | import Control.Monad(when) |
23 | 23 | ||
24 | #if __GLASGOW_HASKELL__ >= 605 | ||
25 | import GHC.ForeignPtr (mallocPlainForeignPtrBytes) | ||
26 | #else | ||
27 | import Foreign.ForeignPtr (mallocForeignPtrBytes) | ||
28 | #endif | ||
29 | |||
24 | import GHC.Base | 30 | import GHC.Base |
25 | import GHC.IOBase | 31 | import 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. |
28 | data Vector t = V { dim :: {-# UNPACK #-} !Int -- ^ number of elements | 34 | data 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 | ||
32 | vec = withVector | 39 | vec = withVector |
33 | 40 | ||
@@ -40,8 +47,20 @@ withVector (V n fp) f = withForeignPtr fp $ \p -> do | |||
40 | createVector :: Storable a => Int -> IO (Vector a) | 47 | createVector :: Storable a => Int -> IO (Vector a) |
41 | createVector n = do | 48 | createVector 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 | ||