haskell / primitive

This package provides various primitive memory-related operations.
Other
114 stars 58 forks source link

Document why we can't derive Prim instances from Storable ones, and do what we can #207

Open treeowl opened 6 years ago

treeowl commented 6 years ago

We have PrimStorable to derive Storable instances from Prim instances. It would be very nice to be able to offer a StorablePrim to go the other way. Unfortunately, I don't think it's quite possible to do this.

The tricky parts are the ByteArray# and MutableByteArray# operations. We can almost use byteArrayContents# :: ByteArray# -> Addr#, but that only works reliably if the ByteArray# is pinned. The trouble is that the garbage collector could run between the time we get the address of the start of the array and the time the appropriate Storable method uses it. I'm pretty sure the only entirely reliable way to implement these operations would be to copy the entire array, which is just awful.

On the other hand, we can offer adapter functions for all the other Prim methods, so I think we should add them.

treeowl commented 6 years ago
sSizeOf# _
    | I# s <- FS.sizeOf (undefined :: a)
    = s
sAlignment# _
    | I# al <- FS.alignment (undefined :: a)
    = al
sReadOffAddr# addr# i# = internal $ do
    PrimStorable x <- unsafeIOToST $ FS.peekElemOff (Ptr addr#) (I# i#)
    pure x
sWriteOffAddr# addr# i# a = internal_ $ unsafeIOToST $
    FS.pokeElemOff (Ptr addr#) (I# i#) (PrimStorable a)
sIndexOffAddr# addr# i# = unsafeDupablePerformIO $ do
    PrimStorable x <- FS.peekElemOff (Ptr addr#) (I# i#)
    pure x