Someone should double check this patch (and my other email).<div><br></div><div>I tested them by creating this package:</div><div><a href="http://hackage.haskell.org/package/missing-foreign">http://hackage.haskell.org/package/missing-foreign</a></div>
<div><br></div><div>I put that on hackage in case a) these are not accepted to base, or b) someone is using a version of base that doesn't have them yet.</div><div><br></div><div>Thanks,</div><div>Jason<br><br><div class="gmail_quote">
On Sun, Apr 3, 2011 at 1:28 PM, Jason Dagit <span dir="ltr"><<a href="mailto:dagitj@gmail.com">dagitj@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
---<br>
Foreign/Marshal/Alloc.hs | 29 +++++++++++++++++++++++++++++<br>
1 files changed, 29 insertions(+), 0 deletions(-)<br>
<br>
diff --git a/Foreign/Marshal/Alloc.hs b/Foreign/Marshal/Alloc.hs<br>
index 612d2c7..148190b 100644<br>
--- a/Foreign/Marshal/Alloc.hs<br>
+++ b/Foreign/Marshal/Alloc.hs<br>
@@ -53,6 +53,9 @@ module Foreign.Marshal.Alloc (<br>
malloc, -- :: Storable a => IO (Ptr a)<br>
mallocBytes, -- :: Int -> IO (Ptr a)<br>
<br>
+ calloc, -- :: Storable a => IO (Ptr a)<br>
+ callocBytes, -- :: Int -> IO (Ptr a)<br>
+<br>
realloc, -- :: Storable b => Ptr a -> IO (Ptr b)<br>
reallocBytes, -- :: Ptr a -> Int -> IO (Ptr a)<br>
<br>
@@ -168,6 +171,32 @@ allocaBytesAligned :: Int -> Int -> (Ptr a -> IO b) -> IO b<br>
allocaBytesAligned size align = allocaBytes size -- wrong<br>
#endif<br>
<br>
+-- | Allocate a block of memory that is sufficient to hold values of type<br>
+-- @a@. The size of the area allocated is determined by the 'sizeOf'<br>
+-- method from the instance of 'Storable' for the appropriate type.<br>
+-- The memory is initalized to 0.<br>
+--<br>
+-- The memory may be deallocated using 'free' or 'finalizerFree' when<br>
+-- no longer required.<br>
+--<br>
+{-# INLINE calloc #-}<br>
+calloc :: Storable a => IO (Ptr a)<br>
+calloc = doCalloc undefined<br>
+ where<br>
+ doCalloc :: Storable b => b -> IO (Ptr b)<br>
+ doCalloc dummy = callocBytes (sizeOf dummy)<br>
+<br>
+-- | Allocate a block of memory of the given number of bytes.<br>
+-- The block of memory is sufficiently aligned for any of the basic<br>
+-- foreign tyes that fit into a memory block of the allocated size.<br>
+-- The memory is initialized to 0.<br>
+--<br>
+-- The memory may be deallocated using 'free' or 'finalizerFree' when<br>
+-- no longer required.<br>
+--<br>
+callocBytes :: Int -> IO (Ptr a)<br>
+callocBytes size = failWhenNULL "calloc" (_calloc (fromIntegral size) 1)<br>
+<br>
-- |Resize a memory area that was allocated with 'malloc' or 'mallocBytes'<br>
-- to the size needed to store values of type @b@. The returned pointer<br>
-- may refer to an entirely different memory area, but will be suitably<br>
<font color="#888888">--<br>
1.7.4.1<br>
<br>
</font></blockquote></div><br></div>