missing-foreign (empty) → 0.1.0
raw patch · 5 files changed
+168/−0 lines, 5 filesdep +basesetup-changed
Dependencies added: base
Files
- Foreign/Marshal/Alloc/Calloc.hs +60/−0
- Foreign/Marshal/MissingUtils.hs +47/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- missing-foreign.cabal +29/−0
+ Foreign/Marshal/Alloc/Calloc.hs view
@@ -0,0 +1,60 @@+{-# LANGUAGE ForeignFunctionInterface #-}+----------------------------------------------------------------+-- |+-- Module : Foreign.Marshal.Alloc.Calloc+-- Copyright : (c) Jason Dagit 2011+-- License : BSD-style (see the file LICENSE)+-- +-- Maintainer : dagitj@gmail.com+--+-- The module "Foreign.Marshal.Alloc.Calloc" provides access to+-- the 'calloc' (e.g., allocated 0-initialized chunks of memory+-- outside of the Haskell storage manager).+--+-- If any of these allocation functions fails, an exception is+-- raised.+--+-- The storage allocated is alligned to store any basic+-- foreign types.+--+----------------------------------------------------------------+module Foreign.Marshal.Alloc.Calloc (+ -- * Memory allocation+ -- ** Initialized dynamic allocation+ calloc, -- :: Storable a => IO (Ptr a)+ callocBytes -- :: Int -> IO (Ptr a)+) where++import Foreign.C.Types+import Foreign.Storable+import Foreign.Ptr+import Foreign.Marshal.Error++foreign import ccall unsafe "stdlib.h calloc"+ _calloc :: CSize -> CSize -> IO (Ptr a)++-- | Allocate a block of memory that is sufficient to hold values of type+-- @a@. The size of the area allocated is determined by the 'sizeOf'+-- method from the instance of 'Storable' for the appropriate type.+-- The memory is initalized to 0.+--+-- The memory may be deallocated using 'free' or 'finalizerFree' when+-- no longer required.+--+{-# INLINE calloc #-}+calloc :: Storable a => IO (Ptr a)+calloc = doCalloc undefined+ where+ doCalloc :: Storable b => b -> IO (Ptr b)+ doCalloc dummy = callocBytes (sizeOf dummy)++-- | Allocate a block of memory of the given number of bytes.+-- The block of memory is sufficiently aligned for any of the basic+-- foreign tyes that fit into a memory block of the allocated size.+-- The memory is initialized to 0.+--+-- The memory may be deallocated using 'free' or 'finalizerFree' when+-- no longer required.+--+callocBytes :: Int -> IO (Ptr a)+callocBytes size = throwIfNull "calloc" (_calloc (fromIntegral size) 1)
+ Foreign/Marshal/MissingUtils.hs view
@@ -0,0 +1,47 @@+-----------------------------------------------------------------------------+-- |+-- Module : Foreign.Marshal.MissingUtils+-- Copyright : (c) Jason Dagit 2001+-- License : BSD-style (see the file LICENSE)+-- +-- Maintainer : dagitj@gmail.com+-- Stability : provisional+-- Portability : portable+--+-- Utilities for primitive marshaling+--+-----------------------------------------------------------------------------+module Foreign.Marshal.MissingUtils (+ -- * General marshalling utilities+ -- ** Haskellish interface to memcpy and memove+ -- | (argument order: destination, source)+ copy, -- :: Storable a => Ptr a -> Ptr a -> IO ()+ move -- :: Storable a => Ptr a -> Ptr a -> IO ()+) where++import Foreign.Storable+import Foreign.Ptr+import Foreign.Marshal.Utils++-- Haskellish interface to memcpy and memmove+-- ------------------------------------------++-- |Uses 'sizeOf' to copy bytes from the second area (source) into the+-- first (destination); the copied areas may /not/ overlap+--+{-# INLINE copy #-}+copy :: Storable a => Ptr a -> Ptr a -> IO ()+copy dest src = copyBytes dest src (sizeOf (type_ src))+ where+ type_ :: Ptr a -> a+ type_ = undefined++-- |Uses 'sizeOf' to copy bytes from the second area (source) into the+-- first (destination); the copied areas /may/ overlap+--+{-# INLINE move #-}+move :: Storable a => Ptr a -> Ptr a -> IO ()+move dest src = moveBytes dest src (sizeOf (type_ src))+ where+ type_ :: Ptr a -> a+ type_ = undefined
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2011, Jason Dagit++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 Jason Dagit 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
+ missing-foreign.cabal view
@@ -0,0 +1,29 @@+Name: missing-foreign+Version: 0.1.0+Synopsis: Convenience functions for FFI work+Description: Add several functions missing in the standard+ Foreign modules:+ .+ * copy and move based on Storable+ .+ * calloc+License: BSD3+License-file: LICENSE+Author: Jason Dagit+Maintainer: dagitj@gmail.com+Category: FFI+Build-type: Simple+Cabal-version: >=1.6++Library+ -- Modules exported by the library.+ Exposed-modules: Foreign.Marshal.Alloc.Calloc+ Foreign.Marshal.MissingUtils+ + -- Packages needed in order to build this package.+ Build-depends: base >= 3 && < 5+ GHC-options: -Wall++source-repository head+ type: git+ location: git@github.com:dagit/missing-foreign.git