[commit: bytestring] ghc-7.6: Add unsafePackMallocCStringLen (c706c00)
Paolo Capriotti
p.capriotti at gmail.com
Thu Sep 6 12:28:39 CEST 2012
Repository : ssh://darcs.haskell.org//srv/darcs/packages/bytestring
On branch : ghc-7.6
http://hackage.haskell.org/trac/ghc/changeset/c706c000f94f044b3042c9a5deaf245372fb0cdd
>---------------------------------------------------------------
commit c706c000f94f044b3042c9a5deaf245372fb0cdd
Author: Lauri Alanko <la at iki.fi>
Date: Wed Jun 20 09:22:32 2012 +0000
Add unsafePackMallocCStringLen
>---------------------------------------------------------------
Data/ByteString/Unsafe.hs | 17 +++++++++++++++++
1 files changed, 17 insertions(+), 0 deletions(-)
diff --git a/Data/ByteString/Unsafe.hs b/Data/ByteString/Unsafe.hs
index cf091a6..2aae1a9 100644
--- a/Data/ByteString/Unsafe.hs
+++ b/Data/ByteString/Unsafe.hs
@@ -36,6 +36,7 @@ module Data.ByteString.Unsafe (
unsafePackCString, -- :: CString -> IO ByteString
unsafePackCStringLen, -- :: CStringLen -> IO ByteString
unsafePackMallocCString,-- :: CString -> IO ByteString
+ unsafePackMallocCStringLen, -- :: CStringLen -> IO ByteString
#if defined(__GLASGOW_HASKELL__)
unsafePackAddress, -- :: Addr# -> IO ByteString
@@ -263,6 +264,22 @@ unsafePackMallocCString cstr = do
len <- c_strlen cstr
return $! PS fp 0 (fromIntegral len)
+-- | /O(n)/ Build a @ByteString@ from a malloced @CStringLen at . This
+-- value will have a @free(3)@ finalizer associated to it.
+--
+-- This funtion is /unsafe/. If the original @CString@ is later
+-- modified, this change will be reflected in the resulting @ByteString@,
+-- breaking referential transparency.
+--
+-- This function is also unsafe if you call its finalizer twice,
+-- which will result in a /double free/ error, or if you pass it
+-- a CString not allocated with 'malloc'.
+--
+unsafePackMallocCStringLen :: CStringLen -> IO ByteString
+unsafePackMallocCStringLen (cstr, len) = do
+ fp <- newForeignPtr c_free_finalizer (castPtr cstr)
+ return $! PS fp 0 len
+
-- ---------------------------------------------------------------------
-- | /O(1) construction/ Use a @ByteString@ with a function requiring a
More information about the Cvs-libraries
mailing list