15 #ifndef ALIGNEDALLOCATOR_H
16 #define ALIGNEDALLOCATOR_H
22 namespace visiontransfer {
31 template<
typename T,
int alignment = 32>
32 class AlignedAllocator {
36 typedef value_type* pointer;
37 typedef const value_type* const_pointer;
38 typedef value_type& reference;
39 typedef const value_type& const_reference;
40 typedef std::size_t size_type;
41 typedef std::ptrdiff_t difference_type;
46 typedef AlignedAllocator<U> other;
49 explicit AlignedAllocator() {}
50 ~AlignedAllocator() {}
51 explicit AlignedAllocator(AlignedAllocator
const&) {}
53 explicit AlignedAllocator(AlignedAllocator<U>
const&) {}
56 inline pointer address(reference r) {
return &r; }
57 inline const_pointer address(const_reference r) {
return &r; }
60 pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0) {
62 unsigned char* ptr =
new unsigned char[
sizeof(T) * cnt + (alignment-1) + 1];
63 unsigned char* alignedPtr =
reinterpret_cast<unsigned char*
>((size_t(ptr + 1) + alignment-1) & -alignment);
66 alignedPtr[-1] =
static_cast<unsigned char>(alignedPtr - ptr);
68 return reinterpret_cast<pointer
>(alignedPtr);
71 void deallocate(pointer p, size_type) {
74 unsigned char* alignedPtr =
reinterpret_cast<unsigned char*
>(p);
75 unsigned char* unalignedPtr = alignedPtr - alignedPtr[-1];
78 ::operator
delete[](unalignedPtr);
83 size_type max_size()
const {
84 return (std::numeric_limits<size_type>::max)() /
sizeof(T);
88 void construct(pointer p,
const T& t) {
93 void destroy(pointer p) {