[Cython] a warning on using string/buffer objects for getting tmp memory
Martin C. Martin
martin at martincmartin.com
Sun Apr 13 03:09:12 CEST 2008
Robert Bradshaw wrote:
> On Apr 12, 2008, at 4:09 PM, Lisandro Dalcin wrote:
>
>> I've just realized that using a string or buffer object for automatic
>> management of memory as I proposed has a pitfall: memory alignement is
>> not guaranteed.
>>
>> So perhaps the only way to go is with this trick is to use a custom
>> python object internally calling malloc/free.
>
> It looks like strings are aligned on int boundaries (given their
> struct).
On 64 bit machines, gcc uses 32 bits for ints, so simply aligning on int
boundaries wouldn't get you 64 bit aligned.
> What guarantee does one have about malloc?
It depends on the implementation. For glibc:
http://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html#Aligned-Memory-Blocks
"The address of a block returned by malloc or realloc in the GNU system
is always a multiple of eight (or sixteen on 64-bit systems)."
You could always do what memalign() does: if e.g. you need something
aligned on 8 byte boundaries, but Python's string allocation only uses 4
byte boundaries, then allocate an extra 4 bytes, and if the result isn't
8 byte aligned, return the address + 4.
> I would imagine
> we would have a custom object (which would be very simple) and even
> faster than a stringobject.
Best,
Martin
More information about the Cython-dev
mailing list