Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ByteBuffer.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
10
12using ByteBufferVector = std::vector<uint8, STLAlignedAllocator<uint8, JPH_CACHE_LINE_SIZE>>;
13
16{
17public:
19 size_t Align(size_t inSize)
20 {
21 // Assert power of 2
22 JPH_ASSERT(IsPowerOf2(inSize));
23
24 // Calculate new size and resize buffer
25 size_t s = AlignUp(size(), inSize);
26 resize(s);
27
28 return s;
29 }
30
32 template <class Type>
33 Type * Allocate(size_t inSize = 1)
34 {
35 // Reserve space
36 size_t s = size();
37 resize(s + inSize * sizeof(Type));
38
39 // Get data pointer
40 Type *data = reinterpret_cast<Type *>(&at(s));
41
42 // Construct elements
43 for (Type *d = data, *d_end = data + inSize; d < d_end; ++d)
44 ::new (d) Type;
45
46 // Return pointer
47 return data;
48 }
49
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
60 template <class Type>
61 const Type * Get(size_t inPosition) const
62 {
63 return reinterpret_cast<const Type *>(&at(inPosition));
64 }
65
67 template <class Type>
68 Type * Get(size_t inPosition)
69 {
70 return reinterpret_cast<Type *>(&at(inPosition));
71 }
72};
73
std::vector< uint8, STLAlignedAllocator< uint8, JPH_CACHE_LINE_SIZE > > ByteBufferVector
Underlying data type for ByteBuffer.
Definition: ByteBuffer.h:12
std::uint8_t uint8
Definition: Core.h:440
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
constexpr bool IsPowerOf2(T inV)
Check if inV is a power of 2.
Definition: Math.h:73
T AlignUp(T inV, uint64 inAlignment)
Align inV up to the next inAlignment bytes.
Definition: Math.h:80
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
Simple byte buffer, aligned to a cache line.
Definition: ByteBuffer.h:16
Type * Get(size_t inPosition)
Get object at inPosition (an offset in bytes)
Definition: ByteBuffer.h:68
Type * Allocate(size_t inSize=1)
Allocate block of data of inSize elements and return the pointer.
Definition: ByteBuffer.h:33
void AppendVector(const Array< Type > &inData)
Append inData to the buffer.
Definition: ByteBuffer.h:52
size_t Align(size_t inSize)
Align the size to a multiple of inSize, returns the length after alignment.
Definition: ByteBuffer.h:19
const Type * Get(size_t inPosition) const
Get object at inPosition (an offset in bytes)
Definition: ByteBuffer.h:61