Unaligned word-sized reads on ByteArray#
marlowsd at gmail.com
Wed Oct 27 05:40:27 EDT 2010
On 26/10/10 07:10, Antoine Latter wrote:
> On Tue, Oct 26, 2010 at 12:21 AM, Johan Tibell<johan.tibell at gmail.com> wrote:
>> On Sun, Oct 24, 2010 at 2:02 AM, Johan Tibell<johan.tibell at gmail.com> wrote:
>>> I noticed that indexWordArray# only allows for aligned reads (by
>>> forcing the offset to be in words, rather than in bytes.) Is it
>>> possible to perform unaligned reads on a ByteArray# e.g. going via
>>> Addr#? There's the byteArrayContent# primitive but I don't know how to
>>> force the ByteArray# to stay pinned in memory while performing my
>>> reads. The ByteArray# is not guaranteed to be pinned in my case.
>> It seems like I should be able to use (the undocumented) touch#
>> function to achieve this. Is that the case?
> I don't know much about GHC internals, but in the interest of keeping
> the conversation going I'll respond.
> I had thought that touch# prevented it from being collected - that is,
> it forces the GC to believe there is a reference to the region in
> memory so it doesn't disappear out from under the foreign call. So it
> is necessary for what you want, but I do not think sufficient.
> Whenever folks need a region to remain pinned in memory during a call
> use the various 'pined' allocation functions.
> All of the functions in Foreign.Marshal.Alloc use the pinned byte
> array allocation functions (aside from the ones which call malloc),
> which I take as the prototype of creating buffers to pass to foreign
Correct - the right way is to use byteArrayContents# to get the Addr# to
the contents, and use touch# to keep the ByteArray# alive until you're
finished with the Addr#.
More information about the Glasgow-haskell-users