i.e.
unsigned int __attribute__((aligned(16))) myArray[262144]; unsigned int *myDynamicArray = new unsigned int[262144]; // can i align this somehow?
Thanks!
- dega
Posted 26 October 2006 - 05:32 PM
unsigned int __attribute__((aligned(16))) myArray[262144]; unsigned int *myDynamicArray = new unsigned int[262144]; // can i align this somehow?
Posted 26 October 2006 - 06:44 PM
Posted 26 October 2006 - 06:54 PM
typedef int my_aligned_int __attribute__ ((aligned (16)); unsigned int *myDynamicArray = new unsigned my_aligned_int[262144];Or perhaps using aligned_malloc, or alternatively roll your own memory management that is always aligned to particular boundaries.
Posted 26 October 2006 - 08:47 PM
monjardin said:
Posted 27 October 2006 - 02:07 AM
#include <malloc.h>
template<class T, int TALIGN, int TBLOCKSIZE> class AlignedMemoryAllocator: public std::_Allocator_base<T> {
public:
typedef std::_Allocator_base<T> _Mybase;
typedef typename _Mybase::value_type value_type;
typedef value_type _FARQ *pointer;
typedef value_type _FARQ& reference;
typedef const value_type _FARQ *const_pointer;
typedef const value_type _FARQ& const_reference;
typedef _SIZT size_type;
typedef _PDFT difference_type;
template<class _Other> struct rebind { // convert an AlignedMemoryAllocator<T> to an AlignedMemoryAllocator <_Other>
typedef AlignedMemoryAllocator<_Other, TALIGN, TBLOCKSIZE> other;
};
pointer address(reference _Val) const
{ // return address of mutable _Val
return (&_Val);
}
const_pointer address(const_reference _Val) const
{ // return address of nonmutable _Val
return (&_Val);
}
AlignedMemoryAllocator()
{ // construct default AlignedMemoryAllocator (do nothing)
}
AlignedMemoryAllocator(const AlignedMemoryAllocator<T, TALIGN, TBLOCKSIZE>&)
{ // construct by copying (do nothing)
}
template<class _Other>
AlignedMemoryAllocator(const AlignedMemoryAllocator<_Other, TALIGN, TBLOCKSIZE>&)
{ // construct from a related AlignedMemoryAllocator (do nothing)
}
template<class _Other>
AlignedMemoryAllocator<T, TALIGN, TBLOCKSIZE>& operator=(const AlignedMemoryAllocator<_Other, TALIGN, TBLOCKSIZE>&)
{ // assign from a related AlignedMemoryAllocator (do nothing)
return (*this);
}
void deallocate(pointer _Ptr, size_type)
{ // deallocate object at _Ptr, ignore size
if (_Ptr) _aligned_free(_Ptr);
}
pointer allocate(size_type _Count)
{ // allocate array of _Count elements
return allocate(_Count, 0);
}
pointer allocate(size_type _Count, const void _FARQ *hint)
{ // allocate array of _Count elements, ignore hint
size_t byteCount=_Count * sizeof(T);
size_t byteCountLeft = byteCount % TBLOCKSIZE;
if(byteCountLeft) byteCount += TBLOCKSIZE - byteCountLeft;
pointer p = reinterpret_cast<pointer>(_aligned_realloc((void*)hint,byteCount,TALIGN));
//if (!p) throw std::bad_alloc("bad allocation in the custom aligned allocator";
return p;
}
void construct(pointer _Ptr, const T& _Val)
{ // construct object at _Ptr with value _Val
new(_Ptr) T(_Val);
}
void destroy(pointer _Ptr)
{ // destroy object at _Ptr
_Ptr->~T();
}
_SIZT max_size() const
{ // estimate maximum array size
_SIZT _Count = (_SIZT)(-1) / sizeof (T);
return (0 < _Count ? _Count : 1);
}
};
}
Posted 27 October 2006 - 04:49 PM
void *malloc16(int size)
{
char *block = new char[size + 16];
int *aligned = (int*)((int)(block + 20) & 0xFFFFFFF0);
aligned[-1] = (int)block;
return aligned;
}
void free16(void *memory)
{
delete ((int**)memory)[-1];
}
Posted 28 October 2006 - 01:46 PM
Nick said:
Posted 28 October 2006 - 07:36 PM
.oisyn said:
Posted 29 October 2006 - 12:48 AM
Posted 29 October 2006 - 01:35 AM
0 members, 1 guests, 0 anonymous users