| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/STLAlignedAllocator.h> |
| 8 |
|
| 9 |
JPH_NAMESPACE_BEGIN |
| 10 |
|
| 11 |
|
| 12 |
using ByteBufferVector = Array<uint8, STLAlignedAllocator<uint8, JPH_CACHE_LINE_SIZE>>; |
| 13 |
|
| 14 |
|
| 15 |
class ByteBuffer : public ByteBufferVector |
| 16 |
{ |
| 17 |
public: |
| 18 |
|
| 19 |
size_t Align(size_t inSize) |
| 20 |
{ |
| 21 |
|
| 22 |
JPH_ASSERT(IsPowerOf2(inSize)); |
| 23 |
|
| 24 |
|
| 25 |
size_t s = AlignUp(size(), inSize); |
| 26 |
resize(s, 0); |
| 27 |
|
| 28 |
return s; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
template <class Type> |
| 33 |
Type * Allocate(size_t inSize = 1) |
| 34 |
{ |
| 35 |
|
| 36 |
size_t s = size(); |
| 37 |
resize(s + inSize * sizeof(Type)); |
| 38 |
|
| 39 |
|
| 40 |
Type *data = reinterpret_cast<Type *>(&at(s)); |
| 41 |
|
| 42 |
|
| 43 |
for (Type *d = data, *d_end = data + inSize; d < d_end; ++d) |
| 44 |
new (d) Type; |
| 45 |
|
| 46 |
|
| 47 |
return data; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
template <class Type> |
| 52 |
void AppendVector(const Array<Type> &inData) |
| 53 |
{ |
| 54 |
size_t size = inData.size() * sizeof(Type); |
| 55 |
uint8 *data = Allocate<uint8>(size); |
| 56 |
memcpy(data, &inData[0], size); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
template <class Type> |
| 61 |
const Type * Get(size_t inPosition) const |
| 62 |
{ |
| 63 |
return reinterpret_cast<const Type *>(&at(inPosition)); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
template <class Type> |
| 68 |
Type * Get(size_t inPosition) |
| 69 |
{ |
| 70 |
return reinterpret_cast<Type *>(&at(inPosition)); |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
JPH_NAMESPACE_END |