initialize (empty) → 0.1.0.0
raw patch · 5 files changed
+209/−0 lines, 5 filesdep +basesetup-changed
Dependencies added: base
Files
- CHANGELOG.md +5/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- initialize.cabal +39/−0
- src/Initialize.hs +133/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for initialize++## 0.1.0.0 -- 2018-10-25++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, andrewthad++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of andrewthad nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ initialize.cabal view
@@ -0,0 +1,39 @@+cabal-version: 2.2+name:+ initialize+version:+ 0.1.0.0+synopsis:+ Initialization and Deinitialization of 'Storable' values.+description:+ This package provides two typeclasses, 'Initialize' and 'Deinitialize',+ which provide a common interface for initializing and deinitializing data+ backed by a 'Ptr'.+homepage:+ https://github.com/chessai/initialize+license:+ BSD-3-Clause+license-file:+ LICENSE+author:+ andrewthad+maintainer:+ chessai1996@gmail.com+copyright:+ copyright (c) 2018 andrewthad+category:+ Data+build-type:+ Simple+extra-source-files:+ CHANGELOG.md++library+ exposed-modules:+ Initialize+ build-depends:+ base >=4.2 && <4.13+ hs-source-dirs:+ src+ default-language:+ Haskell2010
+ src/Initialize.hs view
@@ -0,0 +1,133 @@+-----------------------------------------------------------------------------------++{-# LANGUAGE BangPatterns #-}+{-# LANGUAGE CPP #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE NoImplicitPrelude #-}+{-# LANGUAGE ScopedTypeVariables #-}++-----------------------------------------------------------------------------------++{-# OPTIONS_GHC -Wall #-}++module Initialize+ ( Initialize(initialize, initializeElemOff, initializeElems)+ , Deinitialize(deinitialize, deinitializeElemOff, deinitializeElems)+ , Uninitialized(..)+ ) where++-----------------------------------------------------------------------------------++import Data.Int (Int, Int8, Int16, Int32, Int64)+import Data.Word (Word, Word8, Word16, Word32, Word64)+import Data.Ord (Ord((<)))+import Data.Eq (Eq)+import Control.Monad (return)+import Data.Char (Char)+import Foreign.Storable (Storable(sizeOf))+import GHC.Ptr (Ptr, plusPtr)+import Foreign.Marshal.Alloc ()+import GHC.IO (IO)+import GHC.Err (undefined)+import GHC.Num (Num((*),(+)))++-----------------------------------------------------------------------------------++-- | The class for initializing memory at a pointer+-- representing 'Storable' values.+class Storable a => Initialize a where+#if __GLASGOW_HASKELL__ >= 708 + {-# MINIMAL initialize #-}+#endif+ initialize :: Ptr a -> IO ()+ -- ^ Initialize the memory at a pointer. An implementation+ -- of this function may do nothing, or if the data contains+ -- more pointers, 'initialize' may allocate additional memory.+ initializeElemOff :: Ptr a -> Int -> IO ()+ -- ^ Initialize the memory at an offset from the pointer.+ -- This has a default implementation but may be overriden for+ -- efficiency.+ initializeElemOff ptr ix = do+ initialize (plusPtr ptr (ix * sizeOf (undefined :: a)) :: Ptr a)+ initializeElems :: Ptr a -> Int -> IO ()+ -- ^ Initialize a pointer representing an array with+ -- a given number of elements.+ -- This has a default implementation but may be overriden for+ -- efficiency.+ initializeElems ptr n = go 0 where+ go !i = if i < n+ then do+ initialize (plusPtr ptr (i * sizeOf (undefined :: a)) :: Ptr a)+ go (i + 1)+ else return ()++-----------------------------------------------------------------------------------++-- | The class for freeing memory at a pointer+-- representing 'Storable' values.+class Storable a => Deinitialize a where+#if __GLASGOW_HASKELL__ >= 708 + {-# MINIMAL deinitialize #-}+#endif+ deinitialize :: Ptr a -> IO ()+ -- ^ Free the memory at a pointer. + deinitializeElemOff :: Ptr a -> Int -> IO ()+ -- ^ Free the memory at an offset from the pointer.+ -- This has a default implementation but may be overriden for+ -- efficiency.+ deinitializeElemOff ptr ix =+ deinitialize (plusPtr ptr (ix * sizeOf (undefined :: a)) :: Ptr a)+ deinitializeElems :: Ptr a -> Int -> IO ()+ -- ^ Free any memory pointed to by elements of the array.+ -- This has a default implementation but may be overriden for+ -- efficiency.+ deinitializeElems ptr n = go 0 where+ go !i = if i < n+ then do+ deinitialize (plusPtr ptr (i * sizeOf (undefined :: a)) :: Ptr a)+ go (i + 1)+ else return ()++-----------------------------------------------------------------------------------++-- | A type which shares a representation with @a@, but for which+-- all underlying memory remains uninitialized - i.e., all typeclass+-- methods of 'Initialize' and 'Deinitialize' do nothing.+newtype Uninitialized a = Uninitialized a+ deriving (Eq, Storable)++instance Storable a => Initialize (Uninitialized a) where+ initialize _ = return ()+ initializeElemOff _ _ = return ()+ initializeElems _ _ = return ()++instance Storable a => Deinitialize (Uninitialized a) where+ deinitialize _ = return ()+ deinitializeElemOff _ _ = return ()+ deinitializeElems _ _ = return ()++-----------------------------------------------------------------------------------++#define deriveInit(ty) \+instance Initialize (ty) where { \+ initialize _ = return () \+; initializeElemOff _ _ = return () \+; initializeElems _ _ = return () \+; {-# INLINE initialize #-} \+; {-# INLINE initializeElemOff #-} \+; {-# INLINE initializeElems #-} \+}++deriveInit(Word)+deriveInit(Word8)+deriveInit(Word16)+deriveInit(Word32)+deriveInit(Word64)+deriveInit(Int)+deriveInit(Int8)+deriveInit(Int16)+deriveInit(Int32)+deriveInit(Int64)+deriveInit(Char)++-----------------------------------------------------------------------------------