diff options
author | Ömer Sinan Ağacan <omeragacan@gmail.com> | 2018-09-08 09:55:45 +0300 |
---|---|---|
committer | Ömer Sinan Ağacan <omeragacan@gmail.com> | 2018-09-08 09:56:09 +0300 |
commit | 62cd44013eaa6f366d4130492a9d6c50b0765d54 (patch) | |
tree | e5ca6fb9f7fd2567a9e0015aa01b97d47468d21f /libraries | |
parent | 2b6694a5368297c1c0c254eee3721f329ec69fc6 (diff) | |
download | haskell-62cd44013eaa6f366d4130492a9d6c50b0765d54.tar.gz |
Refactor Foreign.Marshal modules for more modern style
(use ScopedTypeVariables to remove dummy arguments)
Reviewers: bgamari, RyanGlScott, dfeuer, hvr, monoidal
Reviewed By: monoidal
Subscribers: monoidal, rwbarton, carter
Differential Revision: https://phabricator.haskell.org/D5124
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/base/Foreign/Marshal/Array.hs | 58 | ||||
-rw-r--r-- | libraries/base/Foreign/Marshal/Pool.hs | 33 |
2 files changed, 29 insertions, 62 deletions
diff --git a/libraries/base/Foreign/Marshal/Array.hs b/libraries/base/Foreign/Marshal/Array.hs index 5e103419b6..c0a9164b51 100644 --- a/libraries/base/Foreign/Marshal/Array.hs +++ b/libraries/base/Foreign/Marshal/Array.hs @@ -1,12 +1,12 @@ {-# LANGUAGE Trustworthy #-} -{-# LANGUAGE NoImplicitPrelude, MagicHash #-} +{-# LANGUAGE NoImplicitPrelude, MagicHash, ScopedTypeVariables #-} ----------------------------------------------------------------------------- -- | -- Module : Foreign.Marshal.Array -- Copyright : (c) The FFI task force 2001 -- License : BSD-style (see the file libraries/base/LICENSE) --- +-- -- Maintainer : ffi@haskell.org -- Stability : provisional -- Portability : portable @@ -82,11 +82,8 @@ import GHC.Base -- |Allocate storage for the given number of elements of a storable type -- (like 'Foreign.Marshal.Alloc.malloc', but for multiple elements). -- -mallocArray :: Storable a => Int -> IO (Ptr a) -mallocArray = doMalloc undefined - where - doMalloc :: Storable a' => a' -> Int -> IO (Ptr a') - doMalloc dummy size = mallocBytes (size * sizeOf dummy) +mallocArray :: forall a . Storable a => Int -> IO (Ptr a) +mallocArray size = mallocBytes (size * sizeOf (undefined :: a)) -- |Like 'mallocArray', but add an extra position to hold a special -- termination element. @@ -96,11 +93,8 @@ mallocArray0 size = mallocArray (size + 1) -- |Like 'mallocArray', but allocated memory is filled with bytes of value zero. -- -callocArray :: Storable a => Int -> IO (Ptr a) -callocArray = doCalloc undefined - where - doCalloc :: Storable a' => a' -> Int -> IO (Ptr a') - doCalloc dummy size = callocBytes (size * sizeOf dummy) +callocArray :: forall a . Storable a => Int -> IO (Ptr a) +callocArray size = callocBytes (size * sizeOf (undefined :: a)) -- |Like 'callocArray0', but allocated memory is filled with bytes of value -- zero. @@ -111,12 +105,9 @@ callocArray0 size = callocArray (size + 1) -- |Temporarily allocate space for the given number of elements -- (like 'Foreign.Marshal.Alloc.alloca', but for multiple elements). -- -allocaArray :: Storable a => Int -> (Ptr a -> IO b) -> IO b -allocaArray = doAlloca undefined - where - doAlloca :: Storable a' => a' -> Int -> (Ptr a' -> IO b') -> IO b' - doAlloca dummy size = allocaBytesAligned (size * sizeOf dummy) - (alignment dummy) +allocaArray :: forall a b . Storable a => Int -> (Ptr a -> IO b) -> IO b +allocaArray size = allocaBytesAligned (size * sizeOf (undefined :: a)) + (alignment (undefined :: a)) -- |Like 'allocaArray', but add an extra position to hold a special -- termination element. @@ -129,11 +120,8 @@ allocaArray0 size = allocaArray (size + 1) -- |Adjust the size of an array -- -reallocArray :: Storable a => Ptr a -> Int -> IO (Ptr a) -reallocArray = doRealloc undefined - where - doRealloc :: Storable a' => a' -> Ptr a' -> Int -> IO (Ptr a') - doRealloc dummy ptr size = reallocBytes ptr (size * sizeOf dummy) +reallocArray :: forall a . Storable a => Ptr a -> Int -> IO (Ptr a) +reallocArray ptr size = reallocBytes ptr (size * sizeOf (undefined :: a)) -- |Adjust the size of an array including an extra position for the end marker. -- @@ -153,7 +141,7 @@ peekArray size ptr | size <= 0 = return [] where f 0 acc = do e <- peekElemOff ptr 0; return (e:acc) f n acc = do e <- peekElemOff ptr n; f (n-1) (e:acc) - + -- |Convert an array terminated by the given end marker into a Haskell list -- peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a] @@ -238,20 +226,14 @@ withArrayLen0 marker vals f = -- |Copy the given number of elements from the second array (source) into the -- first array (destination); the copied areas may /not/ overlap -- -copyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO () -copyArray = doCopy undefined - where - doCopy :: Storable a' => a' -> Ptr a' -> Ptr a' -> Int -> IO () - doCopy dummy dest src size = copyBytes dest src (size * sizeOf dummy) +copyArray :: forall a . Storable a => Ptr a -> Ptr a -> Int -> IO () +copyArray dest src size = copyBytes dest src (size * sizeOf (undefined :: a)) -- |Copy the given number of elements from the second array (source) into the -- first array (destination); the copied areas /may/ overlap -- -moveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO () -moveArray = doMove undefined - where - doMove :: Storable a' => a' -> Ptr a' -> Ptr a' -> Int -> IO () - doMove dummy dest src size = moveBytes dest src (size * sizeOf dummy) +moveArray :: forall a . Storable a => Ptr a -> Ptr a -> Int -> IO () +moveArray dest src size = moveBytes dest src (size * sizeOf (undefined :: a)) -- finding the length @@ -272,9 +254,5 @@ lengthArray0 marker ptr = loop 0 -- |Advance a pointer into an array by the given number of elements -- -advancePtr :: Storable a => Ptr a -> Int -> Ptr a -advancePtr = doAdvance undefined - where - doAdvance :: Storable a' => a' -> Ptr a' -> Int -> Ptr a' - doAdvance dummy ptr i = ptr `plusPtr` (i * sizeOf dummy) - +advancePtr :: forall a . Storable a => Ptr a -> Int -> Ptr a +advancePtr ptr i = ptr `plusPtr` (i * sizeOf (undefined :: a)) diff --git a/libraries/base/Foreign/Marshal/Pool.hs b/libraries/base/Foreign/Marshal/Pool.hs index 5d92f6fdd9..8d704c1a2d 100644 --- a/libraries/base/Foreign/Marshal/Pool.hs +++ b/libraries/base/Foreign/Marshal/Pool.hs @@ -1,5 +1,5 @@ {-# LANGUAGE Trustworthy #-} -{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE NoImplicitPrelude, ScopedTypeVariables #-} -------------------------------------------------------------------------------- -- | @@ -102,11 +102,8 @@ withPool act = -- ATTENTION: cut-n-paste from Control.Exception below! -- allocated is determined by the 'sizeOf' method from the instance of -- 'Storable' for the appropriate type. -pooledMalloc :: Storable a => Pool -> IO (Ptr a) -pooledMalloc = pm undefined - where - pm :: Storable a' => a' -> Pool -> IO (Ptr a') - pm dummy pool = pooledMallocBytes pool (sizeOf dummy) +pooledMalloc :: forall a . Storable a => Pool -> IO (Ptr a) +pooledMalloc pool = pooledMallocBytes pool (sizeOf (undefined :: a)) -- | Allocate the given number of bytes of storage in the pool. @@ -120,11 +117,8 @@ pooledMallocBytes (Pool pool) size = do -- | Adjust the storage area for an element in the pool to the given size of -- the required type. -pooledRealloc :: Storable a => Pool -> Ptr a -> IO (Ptr a) -pooledRealloc = pr undefined - where - pr :: Storable a' => a' -> Pool -> Ptr a' -> IO (Ptr a') - pr dummy pool ptr = pooledReallocBytes pool ptr (sizeOf dummy) +pooledRealloc :: forall a . Storable a => Pool -> Ptr a -> IO (Ptr a) +pooledRealloc pool ptr = pooledReallocBytes pool ptr (sizeOf (undefined :: a)) -- | Adjust the storage area for an element in the pool to the given size. @@ -140,11 +134,9 @@ pooledReallocBytes (Pool pool) ptr size = do -- | Allocate storage for the given number of elements of a storable type in the -- pool. -pooledMallocArray :: Storable a => Pool -> Int -> IO (Ptr a) -pooledMallocArray = pma undefined - where - pma :: Storable a' => a' -> Pool -> Int -> IO (Ptr a') - pma dummy pool size = pooledMallocBytes pool (size * sizeOf dummy) +pooledMallocArray :: forall a . Storable a => Pool -> Int -> IO (Ptr a) +pooledMallocArray pool size = + pooledMallocBytes pool (size * sizeOf (undefined :: a)) -- | Allocate storage for the given number of elements of a storable type in the -- pool, but leave room for an extra element to signal the end of the array. @@ -155,11 +147,9 @@ pooledMallocArray0 pool size = -- | Adjust the size of an array in the given pool. -pooledReallocArray :: Storable a => Pool -> Ptr a -> Int -> IO (Ptr a) -pooledReallocArray = pra undefined - where - pra :: Storable a' => a' -> Pool -> Ptr a' -> Int -> IO (Ptr a') - pra dummy pool ptr size = pooledReallocBytes pool ptr (size * sizeOf dummy) +pooledReallocArray :: forall a . Storable a => Pool -> Ptr a -> Int -> IO (Ptr a) +pooledReallocArray pool ptr size = + pooledReallocBytes pool ptr (size * sizeOf (undefined :: a)) -- | Adjust the size of an array with an end marker in the given pool. @@ -195,4 +185,3 @@ pooledNewArray0 pool marker vals = do ptr <- pooledMallocArray0 pool (length vals) pokeArray0 marker ptr vals return ptr - |