add withByteArray
This commit is contained in:
parent
37557af615
commit
f3bf67df99
@ -28,10 +28,12 @@ import qualified Data.ByteString.Internal as B
|
|||||||
class ByteArray ba where
|
class ByteArray ba where
|
||||||
byteArrayAlloc :: Int -> (Ptr p -> IO ()) -> IO ba
|
byteArrayAlloc :: Int -> (Ptr p -> IO ()) -> IO ba
|
||||||
byteArrayLength :: ba -> Int
|
byteArrayLength :: ba -> Int
|
||||||
|
withByteArray :: ba -> (Ptr p -> IO a) -> IO a
|
||||||
|
|
||||||
instance ByteArray Bytes where
|
instance ByteArray Bytes where
|
||||||
byteArrayAlloc = bytesAlloc
|
byteArrayAlloc = bytesAlloc
|
||||||
byteArrayLength = bytesLength
|
byteArrayLength = bytesLength
|
||||||
|
withByteArray = withBytes
|
||||||
|
|
||||||
instance ByteArray ByteString where
|
instance ByteArray ByteString where
|
||||||
byteArrayAlloc sz f = do
|
byteArrayAlloc sz f = do
|
||||||
@ -39,6 +41,8 @@ instance ByteArray ByteString where
|
|||||||
withForeignPtr fptr (f . castPtr)
|
withForeignPtr fptr (f . castPtr)
|
||||||
return $! B.PS fptr 0 sz
|
return $! B.PS fptr 0 sz
|
||||||
byteArrayLength = B.length
|
byteArrayLength = B.length
|
||||||
|
withByteArray b f = withForeignPtr fptr $ \ptr -> f (ptr `plusPtr` off)
|
||||||
|
where (fptr, off, _) = B.toForeignPtr b
|
||||||
|
|
||||||
instance ByteArray SecureMem where
|
instance ByteArray SecureMem where
|
||||||
byteArrayAlloc sz f = do
|
byteArrayAlloc sz f = do
|
||||||
@ -46,6 +50,7 @@ instance ByteArray SecureMem where
|
|||||||
withSecureMemPtr out (f . castPtr)
|
withSecureMemPtr out (f . castPtr)
|
||||||
return out
|
return out
|
||||||
byteArrayLength = secureMemGetSize
|
byteArrayLength = secureMemGetSize
|
||||||
|
withByteArray b f = withSecureMemPtr b (f . castPtr)
|
||||||
|
|
||||||
byteArrayAllocAndFreeze :: ByteArray a => Int -> (Ptr p -> IO ()) -> a
|
byteArrayAllocAndFreeze :: ByteArray a => Int -> (Ptr p -> IO ()) -> a
|
||||||
byteArrayAllocAndFreeze sz f = unsafeDoIO (byteArrayAlloc sz f)
|
byteArrayAllocAndFreeze sz f = unsafeDoIO (byteArrayAlloc sz f)
|
||||||
|
|||||||
@ -17,6 +17,7 @@ module Crypto.Internal.Memory
|
|||||||
, bytesCopyTemporary
|
, bytesCopyTemporary
|
||||||
, bytesAlloc
|
, bytesAlloc
|
||||||
, bytesLength
|
, bytesLength
|
||||||
|
, withBytes
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Word
|
import Data.Word
|
||||||
@ -77,3 +78,6 @@ bytesAlloc sz f = do
|
|||||||
|
|
||||||
bytesLength :: Bytes -> Int
|
bytesLength :: Bytes -> Int
|
||||||
bytesLength = sizeofBytes
|
bytesLength = sizeofBytes
|
||||||
|
|
||||||
|
withBytes :: Bytes -> (Ptr p -> IO a) -> IO a
|
||||||
|
withBytes = withPtr
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user