Path: blob/master/thirdparty/jolt_physics/Jolt/Core/ByteBuffer.h
9906 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Core/STLAlignedAllocator.h>78JPH_NAMESPACE_BEGIN910/// Underlying data type for ByteBuffer11using ByteBufferVector = Array<uint8, STLAlignedAllocator<uint8, JPH_CACHE_LINE_SIZE>>;1213/// Simple byte buffer, aligned to a cache line14class ByteBuffer : public ByteBufferVector15{16public:17/// Align the size to a multiple of inSize, returns the length after alignment18size_t Align(size_t inSize)19{20// Assert power of 221JPH_ASSERT(IsPowerOf2(inSize));2223// Calculate new size and resize buffer24size_t s = AlignUp(size(), inSize);25resize(s, 0);2627return s;28}2930/// Allocate block of data of inSize elements and return the pointer31template <class Type>32Type * Allocate(size_t inSize = 1)33{34// Reserve space35size_t s = size();36resize(s + inSize * sizeof(Type));3738// Get data pointer39Type *data = reinterpret_cast<Type *>(&at(s));4041// Construct elements42for (Type *d = data, *d_end = data + inSize; d < d_end; ++d)43new (d) Type;4445// Return pointer46return data;47}4849/// Append inData to the buffer50template <class Type>51void AppendVector(const Array<Type> &inData)52{53size_t size = inData.size() * sizeof(Type);54uint8 *data = Allocate<uint8>(size);55memcpy(data, &inData[0], size);56}5758/// Get object at inPosition (an offset in bytes)59template <class Type>60const Type * Get(size_t inPosition) const61{62return reinterpret_cast<const Type *>(&at(inPosition));63}6465/// Get object at inPosition (an offset in bytes)66template <class Type>67Type * Get(size_t inPosition)68{69return reinterpret_cast<Type *>(&at(inPosition));70}71};7273JPH_NAMESPACE_END747576