Dear LazyWeb...
I know i can use the gcc aligned attribute to align a variable, but is it possible to do that with memory i get doing a new or a malloc?
That is, i have a class that needs to have one of it's members aligned, so i need to declare the member to be aligned but that is not enough, i need to align the object itself, how?
5 comments:
Either use a placement new with a allocator that can do aligned allocating, or just allocate a little extra memory if needed, and offset the pointer a few bytes.
We had the same issue with Eigen, and we currently use posix_memalign for the aligned allocation. However, we also designed some helper classes to deal with that problem:
http://eigen.tuxfamily.org/api/structEigen_1_1WithAlignedOperatorNew.html
and the respective piece of code:
http://websvn.kde.org/trunk/kdesupport/eigen2/Eigen/src/Core/util/Memory.h?revision=858770&view=markup
also don't miss the class ei_new_allocator which allows you to use your class as the value type of any STL container...
But but but... malloc() allready returns memory aligned on a word boundary (I know that, having written a malloc for a platform where "unaligned read" meant "segfault").
I'm curious : when do you need more alignment than what malloc ang gcc attributes give you ?
@moltonel: ffmpeg wants the output buffers to be aligned to 16bits boundary so he can do nice SSSE3 operations
From "man posix_memalign":
The function posix_memalign() allocates size bytes and places the address of the allocated memory in *memptr. The address of the allocated memory will be a multiple of alignment, which must be a power of two and a multiple of sizeof(void *).
Post a Comment