Unaligned word-sized reads on ByteArray#

Hi, 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. A somewhat related question: are there any defines (in e.g. MachDeps.h) that I could use to detect whether the machine supports unaligned reads? For example, if I knew the processor architecture, I could figure that out. Johan

On Sun, Oct 24, 2010 at 2:02 AM, Johan Tibell
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? Johan

On Tue, Oct 26, 2010 at 12:21 AM, Johan Tibell
On Sun, Oct 24, 2010 at 2:02 AM, Johan Tibell
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 calls. Antoine

On 26/10/10 07:10, Antoine Latter wrote:
On Tue, Oct 26, 2010 at 12:21 AM, Johan Tibell
wrote: On Sun, Oct 24, 2010 at 2:02 AM, Johan Tibell
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 calls.
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#. Cheers, Simon
participants (3)
-
Antoine Latter
-
Johan Tibell
-
Simon Marlow