34 #ifndef __CU_ALIGNED_H__
35 #define __CU_ALIGNED_H__
42 #pragma mark Aligned Arrays Template
67 #pragma mark Constructors
88 _capacity = size*
sizeof(T)+alignment;
91 _orig = malloc(_capacity);
92 _pntr = (T*)std::align(alignment,_length*
sizeof(T),_orig,_capacity);
101 _capacity = copy._capacity;
102 _length = copy._length;
103 _alignm = copy._alignm;
104 _orig = malloc(_capacity);
105 _pntr = (T*)std::align(_alignm,_length*
sizeof(T),_orig,_capacity);
106 std::memcpy(_pntr,copy._pntr,_length*
sizeof(T));
115 _capacity = other._capacity;
116 _length = other._length;
117 _alignm = other._alignm;
120 other._orig =
nullptr;
121 other._pntr =
nullptr;
133 if (_orig) free(_orig);
148 static std::shared_ptr<Aligned<T>>
alloc(
size_t size,
size_t alignment) {
149 std::shared_ptr<Aligned<T>> result = std::make_shared<Aligned<T>>();
150 return (result->reset(size,alignment) ? result :
nullptr);
154 #pragma mark Array Methods
164 _capacity = size*
sizeof(T)+alignment;
168 _orig = malloc(_capacity);
169 _pntr = (T*)std::align(_alignm,size*
sizeof(T),_orig,_capacity);
177 std::memset(_pntr,0,_length*
sizeof(T));
190 #pragma mark Copy Behavior
199 _capacity = copy._capacity;
200 _length = copy._length;
201 _alignm = copy._alignm;
202 _orig = malloc(_capacity);
203 _pntr = (T*)std::align(_alignm,_length*
sizeof(T),_orig,_capacity);
204 std::memcpy(_pntr,copy._pntr,_length*
sizeof(T));
216 _capacity = other._capacity;
217 _length = other._length;
218 _alignm = other._alignm;
221 other._orig =
nullptr;
222 other._pntr =
nullptr;
227 #pragma mark Pointer Behavior
233 operator T*()
const {
Definition: CUAligned.h:53
size_t size() const
Definition: CUAligned.h:185
T & operator[](size_t idx)
Definition: CUAligned.h:265
Aligned(const Aligned< T > ©)
Definition: CUAligned.h:100
Aligned< T > & operator=(const Aligned< T > ©)
Definition: CUAligned.h:198
void clear()
Definition: CUAligned.h:176
const T & operator[](size_t idx) const
Definition: CUAligned.h:276
bool reset(size_t size, size_t alignment)
Definition: CUAligned.h:161
Aligned(size_t size, size_t alignment)
Definition: CUAligned.h:87
Aligned< T > & operator=(Aligned< T > &&other)
Definition: CUAligned.h:215
T & operator*()
Definition: CUAligned.h:243
Aligned()
Definition: CUAligned.h:72
~Aligned()
Definition: CUAligned.h:127
static std::shared_ptr< Aligned< T > > alloc(size_t size, size_t alignment)
Definition: CUAligned.h:148
T * operator+(size_t offset)
Definition: CUAligned.h:254
Definition: CUAction.h:51
void dispose()
Definition: CUAligned.h:132
Aligned(Aligned< T > &&other)
Definition: CUAligned.h:114