15 #ifndef ALIGNEDALLOCATOR_H 16 #define ALIGNEDALLOCATOR_H 28 template<
typename T,
int alignment = 32>
33 typedef value_type* pointer;
34 typedef const value_type* const_pointer;
35 typedef value_type& reference;
36 typedef const value_type& const_reference;
37 typedef std::size_t size_type;
38 typedef std::ptrdiff_t difference_type;
53 inline pointer address(reference r) {
return &r; }
54 inline const_pointer address(const_reference r) {
return &r; }
57 pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0) {
59 unsigned char* ptr =
new unsigned char[
sizeof(T) * cnt + (alignment-1) + 1];
60 unsigned char* alignedPtr =
reinterpret_cast<unsigned char*
>((size_t(ptr + 1) + alignment-1) & -alignment);
63 alignedPtr[-1] =
static_cast<unsigned char>(alignedPtr - ptr);
65 return reinterpret_cast<pointer
>(alignedPtr);
68 void deallocate(pointer p, size_type) {
70 unsigned char* alignedPtr =
reinterpret_cast<unsigned char*
>(p);
71 unsigned char* unalignedPtr = alignedPtr - alignedPtr[-1];
74 ::operator
delete[](unalignedPtr);
78 size_type max_size()
const {
79 return std::numeric_limits<size_type>::max() /
sizeof(T);
83 void construct(pointer p,
const T& t) {
88 void destroy(pointer p) {
STL-compatible allocator for memory-aligned allocations.