[Cython] a warning on using string/buffer objects for getting tmp memory
Robert Bradshaw
robertwb at math.washington.edu
Fri Apr 18 08:40:15 CEST 2008
On Apr 12, 2008, at 6:09 PM, Martin C. Martin wrote:
>
>
> 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.
Yes. I was just noting that int was the smallest type in the string
struct before the data.
>
>> 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.
If we're going to provide something like this, I'd make our own
object that's even simpler than a string.
- Robert
More information about the Cython-dev
mailing list