[Haskell-cafe] advancePtr for ForeignPtr

Felipe Lessa felipe.lessa at gmail.com
Mon Feb 23 08:37:00 EST 2009


On Mon, Feb 23, 2009 at 10:12 AM, Henning Thielemann
<lemming at henning-thielemann.de> wrote:
> Is still someone on haskell.org ?

Sorry, I don't know :).

> I want to have an advancePtr on ForeignPtr in order create a subarray.
> Is this reasonable and possible?

I don't think so. For example,
http://www.haskell.org/ghc/docs/latest/html/libraries/bytestring/Data-ByteString-Internal.html#t%3AByteString
explicitly list the offset.

> Do I have to use 'touchForeignPtr' as finalizer of the subarray's ForeignPtr
> in order to assert that the superarray lives at least as long as the
> subarray?

This may work, but seems like a fragile hack. Why not


  data SubArray a = SA {-# UNPACK #-} !(ForeignPtr a)
                       {-# UNPACK #-} !Int

  withSubArray :: SubArray a -> (Ptr a -> IO b) -> IO b
  withSubArray (SA fptr offset) act =
      withForeignPtr fptr $ \ptr -> act (ptr `plusPtr` offset)

?

HTH,

-- 
Felipe.


More information about the Haskell-Cafe mailing list