Necesita un asignador personalizado aprobado. Se puede construir una sobre la std::allocator
con bastante facilidad:
template <typename T, size_t TALIGN=16, size_t TBLOCK=8>
class aligned_allocator : public std::allocator<T>
{
public:
aligned_allocator() {}
aligned_allocator& operator=(const aligned_allocator &rhs){
std::allocator<T>::operator=(rhs);
return *this;
}
pointer allocate(size_type n, const void *hint){
pointer p = NULL;
size_t count = sizeof(T) * n;
size_t count_left = count % TBLOCK;
if(count_left != 0)
{
count += TBLOCK - count_left;
}
if (!hint)
{
p = reinterpret_cast<pointer>(aligned_malloc(count,TALIGN));
}else{
p = reinterpret_cast<pointer>(aligned_realloc((void*)hint,count,TALIGN));
}
return p;
}
void deallocate(pointer p, size_type n){
aligned_free(p);
}
void construct(pointer p, const T &val){
new(p) T(val);
}
void destroy(pointer p){
p->~T();
}
};
Lo único que falta aquí es el aligned_malloc
, aligned_realloc
y aligned_free
. O necesita implementarlos usted mismo (no debería ser tan difícil) o buscar versiones de ellos en Internet (he visto al menos uno en el motor OGRE).
@Anycorn, ¿alguna vez implementó esto? Parece que sería genial completar el ejemplo de Kornel Kisielewicz porque realmente no veo un buen ejemplo de esto en ningún otro lado en línea. –