[Haskell-cafe] advancePtr for ForeignPtr

Judah Jacobson judah.jacobson at gmail.com
Mon Feb 23 16:20:49 EST 2009


On Mon, Feb 23, 2009 at 9:02 AM, Judah Jacobson
<judah.jacobson at gmail.com> wrote:
> On Mon, Feb 23, 2009 at 6:48 AM, Henning Thielemann
> <lemming at henning-thielemann.de> wrote:
>>
>> On Mon, 23 Feb 2009, Felipe Lessa wrote:
>>
>>> 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 meant ffi at haskell.org
>>
>>>> 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
>>
>> This would work, but I want to convert from StorableVector to CArray and
>> StorableVector has an offset parameter, which is missing in CArray.
>
> I've used something like the following for that purpose:
>
> advanceForeignPtr :: ForeignPtr a -> Int -> IO (ForeignPtr a)
> advanceForeignPtr fp n = withForeignPtr fp $ \p ->
>        newForeignPtr_ (p `advancePtr` n)

Oops, sorry, that's definitely wrong.  Probably the only fix is to use
touchForeignPtr in the finalizer of the new ForeignPtr, as you said.
Time to fix some of my code... :-)

-Judah


More information about the Haskell-Cafe mailing list