4#ifndef GREM_CORE_DATA_LINEAR_BUFFER_HPP
5#define GREM_CORE_DATA_LINEAR_BUFFER_HPP
18#include <memory_resource>
25template <
typename... Ts>
30template <
typename T, std::size_t Index,
typename... Ts>
31struct LinearBufferIndexImpl;
33template <
typename T, std::size_t Index,
typename First,
typename... Rest>
34struct LinearBufferIndexImpl<
T, Index, First, Rest...> : LinearBufferIndexImpl<T, Index + 1, Rest...> {};
36template <
typename T, std::size_t Index,
typename... Rest>
37struct LinearBufferIndexImpl<
T, Index,
T, Rest...> : std::integral_constant<std::size_t, Index> {};
40struct LinearBufferMinElementSize : std::integral_constant<std::size_t, sizeof(T)> {};
43struct LinearBufferMinElementSize<
T[]> : std::integral_constant<std::size_t, sizeof(std::size_t)> {};
46struct LinearBufferVisitorParameterType {
51struct LinearBufferVisitorParameterType<
T[]> {
52 using type = Span<const T>;
58template <
typename T,
typename B>
59struct linear_buffer_has_alternative;
61template <
typename T,
typename First,
typename... Rest>
62struct linear_buffer_has_alternative<
T,
LinearBuffer<First, Rest...>> : linear_buffer_has_alternative<T, LinearBuffer<Rest...>> {};
65struct linear_buffer_has_alternative<
T,
LinearBuffer<>> : std::false_type {};
67template <
typename T,
typename... Rest>
68struct linear_buffer_has_alternative<
T,
LinearBuffer<
T, Rest...>> : std::true_type {};
71template <
typename T,
typename B>
75template <
typename T,
typename B>
76struct linear_buffer_index;
78template <
typename T,
typename... Ts>
79struct linear_buffer_index<T,
LinearBuffer<Ts...>> : detail::LinearBufferIndexImpl<T, 0, Ts...> {};
82template <
typename T,
typename B>
86template <std::
size_t Index,
typename B>
87struct linear_buffer_alternative;
89template <std::size_t Index,
typename First,
typename... Rest>
90struct linear_buffer_alternative<Index,
LinearBuffer<First, Rest...>> : linear_buffer_alternative<Index - 1, LinearBuffer<Rest...>> {};
92template <
typename T,
typename... Rest>
93struct linear_buffer_alternative<0, LinearBuffer<T, Rest...>> {
98template <std::
size_t Index,
typename B>
103struct linear_buffer_size;
105template <
typename... Ts>
106struct linear_buffer_size<
LinearBuffer<Ts...>> : std::integral_constant<std::size_t, sizeof...(Ts)> {};
112template <
typename... Ts>
115 static_assert((std::is_trivially_copyable_v<std::remove_extent_t<Ts>> && ...),
"LinearBuffer requires all element types to be trivially copyable.");
119 std::conditional_t<
sizeof...(Ts) < 255ull, std::uint8_t,
120 std::conditional_t<
sizeof...(Ts) < 65535ull, std::uint16_t,
121 std::conditional_t<
sizeof...(Ts) < 4294967295ull, std::uint32_t,
127 explicit LinearBuffer(std::pmr::memory_resource* memoryResource, std::size_t nextChunkSize = 64) noexcept
128 : memoryResource(memoryResource)
129 , nextChunkSize(std::
max(nextChunkSize, MIN_CHUNK_SIZE)) {
140 template <
typename T>
142 constexpr std::size_t REQUIRED_SIZE =
sizeof(TypeHeader) +
sizeof(T) +
sizeof(TypeHeader) +
sizeof(ChunkTrailer);
143 const std::size_t remainingMemorySize =
static_cast<std::size_t
>(remainingMemoryEnd - remainingMemoryBegin);
144 if (remainingMemorySize < REQUIRED_SIZE) {
146 const std::size_t newChunkSize = std::max(REQUIRED_SIZE, nextChunkSize);
147 if (firstChunkBegin) {
148 GREM_ASSERT(remainingMemorySize >=
sizeof(TypeHeader) +
sizeof(ChunkTrailer));
149 std::byte*
const oldChunkTrailer = remainingMemoryBegin;
150 std::byte*
const newChunkBegin = allocateChunk(newChunkSize);
151 std::byte*
const newChunkEnd = newChunkBegin + newChunkSize;
152 const TypeHeader typeHeader{.typeIndex =
npos};
153 std::memcpy(oldChunkTrailer, &typeHeader,
sizeof(TypeHeader));
154 const ChunkTrailer chunkTrailer{.nextChunkBegin = newChunkBegin, .nextChunkEnd = newChunkEnd};
155 std::memcpy(oldChunkTrailer +
sizeof(TypeHeader), &chunkTrailer,
sizeof(ChunkTrailer));
157 firstChunkBegin = allocateChunk(newChunkSize);
158 firstChunkEnd = firstChunkBegin + newChunkSize;
162 std::memcpy(remainingMemoryBegin, &typeHeader,
sizeof(TypeHeader));
163 std::memcpy(remainingMemoryBegin +
sizeof(TypeHeader), &value,
sizeof(T));
164 remainingMemoryBegin +=
sizeof(TypeHeader) +
sizeof(T);
167 template <
typename T,
typename... Args>
172 template <
typename T>
174 constexpr std::size_t MIN_REQUIRED_SIZE =
sizeof(TypeHeader) +
sizeof(ArrayHeader) +
sizeof(TypeHeader) +
sizeof(ChunkTrailer);
175 const std::size_t remainingMemorySize =
static_cast<std::size_t
>(remainingMemoryEnd - remainingMemoryBegin);
176 if (remainingMemorySize < MIN_REQUIRED_SIZE ||
178 !getAligned(
alignof(T), values.size_bytes(), remainingMemoryBegin +
sizeof(TypeHeader) +
sizeof(ArrayHeader), remainingMemorySize - MIN_REQUIRED_SIZE))) {
180 const std::size_t requiredSize =
sizeof(TypeHeader) +
sizeof(ArrayHeader) +
alignof(T) - 1 + values.size_bytes() +
sizeof(TypeHeader) +
sizeof(ChunkTrailer);
181 const std::size_t newChunkSize = std::max(requiredSize, nextChunkSize);
182 if (firstChunkBegin) {
183 GREM_ASSERT(remainingMemorySize >=
sizeof(TypeHeader) +
sizeof(ChunkTrailer));
184 std::byte*
const oldChunkTrailer = remainingMemoryBegin;
185 std::byte*
const newChunkBegin = allocateChunk(newChunkSize);
186 std::byte*
const newChunkEnd = newChunkBegin + newChunkSize;
187 const TypeHeader typeHeader{.typeIndex =
npos};
188 std::memcpy(oldChunkTrailer, &typeHeader,
sizeof(TypeHeader));
189 const ChunkTrailer chunkTrailer{.nextChunkBegin = newChunkBegin, .nextChunkEnd = newChunkEnd};
190 std::memcpy(oldChunkTrailer +
sizeof(TypeHeader), &chunkTrailer,
sizeof(ChunkTrailer));
192 firstChunkBegin = allocateChunk(newChunkSize);
193 firstChunkEnd = firstChunkBegin + newChunkSize;
197 std::memcpy(remainingMemoryBegin, &typeHeader,
sizeof(TypeHeader));
198 const ArrayHeader arrayHeader{.elementCount = values.size()};
199 std::memcpy(remainingMemoryBegin +
sizeof(TypeHeader), &arrayHeader,
sizeof(ArrayHeader));
201 if (values.empty()) {
202 remainingMemoryBegin = remainingMemoryBegin +
sizeof(TypeHeader) +
sizeof(ArrayHeader);
204 void*
const alignedPointer = getAligned(
alignof(T), values.size_bytes(), remainingMemoryBegin +
sizeof(TypeHeader) +
sizeof(ArrayHeader),
205 static_cast<std::size_t
>(remainingMemoryEnd - remainingMemoryBegin) - MIN_REQUIRED_SIZE);
207 std::memcpy(alignedPointer, values.data(), values.size_bytes());
208 result =
Span<const T>{std::launder(
reinterpret_cast<const T*
>(
static_cast<std::byte*
>(alignedPointer))), values.size()};
209 remainingMemoryBegin =
static_cast<std::byte*
>(alignedPointer) + values.
size_bytes();
214 template <
typename T>
219 template <
typename Visitor>
220 auto visit(Visitor&& visitor)
const {
221 using R = std::common_type_t<decltype(std::invoke(std::forward<Visitor>(visitor), std::declval<typename detail::LinearBufferVisitorParameterType<Ts>::type>()))...>;
222 const std::byte*
const end = remainingMemoryBegin;
223 const std::byte* chunkEnd = firstChunkEnd;
224 for (
const std::byte*
pointer = firstChunkBegin;
pointer != end;) {
226 TypeHeader typeHeader{};
227 std::memcpy(&typeHeader,
pointer,
sizeof(TypeHeader));
229 const auto apply = [&]<std::size_t Index>(std::in_place_index_t<Index>) ->
void {
231 if constexpr (std::is_unbounded_array_v<MaybeArrayT>) {
232 using T = std::remove_extent_t<MaybeArrayT>;
234 ArrayHeader arrayHeader{};
235 std::memcpy(&arrayHeader,
pointer,
sizeof(ArrayHeader));
236 pointer +=
sizeof(ArrayHeader);
237 GREM_ASSERT(
static_cast<std::size_t
>(chunkEnd -
pointer) >= arrayHeader.elementCount *
sizeof(T));
239 if (arrayHeader.elementCount > 0) {
240 if constexpr (
alignof(T) > 1) {
241 void*
const aligned = getAligned(
alignof(T), arrayHeader.elementCount *
sizeof(T),
const_cast<void*
>(
static_cast<const void*
>(
pointer)),
242 static_cast<std::size_t
>(chunkEnd -
pointer) - (
sizeof(TypeHeader) +
sizeof(ChunkTrailer)));
244 pointer =
static_cast<const std::byte*
>(aligned);
246 values =
Span{std::launder(
reinterpret_cast<const T*
>(
pointer)), arrayHeader.elementCount};
248 if constexpr (std::is_void_v<R>) {
249 std::invoke(std::forward<Visitor>(visitor), values);
250 pointer += arrayHeader.elementCount *
sizeof(T);
252 if (std::invoke(std::forward<Visitor>(visitor), values)) {
253 pointer += arrayHeader.elementCount *
sizeof(T);
259 using T = MaybeArrayT;
261 alignas(T) std::byte storage[
sizeof(T)];
262 std::memcpy(storage,
pointer,
sizeof(T));
263 const T& value = *std::launder(
reinterpret_cast<const T*
>(storage));
264 if constexpr (std::is_void_v<R>) {
265 std::invoke(std::forward<Visitor>(visitor), value);
268 if (std::invoke(std::forward<Visitor>(visitor), value)) {
276 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) ->
void {
277 if (!(((typeHeader.typeIndex == Indices) ? (
apply(std::in_place_index<Indices>),
true) :
false) || ...)) {
281 ChunkTrailer chunkTrailer{};
282 std::memcpy(&chunkTrailer,
pointer,
sizeof(ChunkTrailer));
285 GREM_ASSERT(chunkTrailer.nextChunkEnd - chunkTrailer.nextChunkBegin >= std::ptrdiff_t{MIN_CHUNK_SIZE});
286 pointer = chunkTrailer.nextChunkBegin;
287 chunkEnd = chunkTrailer.nextChunkEnd;
289 }(std::make_index_sequence<
sizeof...(Ts)>{});
291 if constexpr (!std::is_void_v<R>) {
302 std::size_t elementCount;
305 struct ChunkTrailer {
306 std::byte* nextChunkBegin;
307 std::byte* nextChunkEnd;
310 static constexpr std::size_t MIN_CHUNK_SIZE = std::max({(
sizeof(TypeHeader) + detail::LinearBufferMinElementSize<Ts>::value +
sizeof(TypeHeader) +
sizeof(ChunkTrailer))...});
312 [[nodiscard]]
static void* getAligned(std::size_t alignment, std::size_t size,
void* pointer, std::size_t space) {
313 return std::align(alignment, size, pointer, space);
316 [[nodiscard]] std::byte* allocateChunk(std::size_t newChunkSize) {
318 std::byte*
const newChunk =
static_cast<std::byte*
>(memoryResource->allocate(newChunkSize, 1));
319 remainingMemoryBegin = newChunk;
320 remainingMemoryEnd = newChunk + newChunkSize;
321 nextChunkSize += nextChunkSize / 2;
325 std::pmr::memory_resource* memoryResource;
326 std::byte* firstChunkBegin =
nullptr;
327 std::byte* firstChunkEnd =
nullptr;
328 std::byte* remainingMemoryBegin =
nullptr;
329 std::byte* remainingMemoryEnd =
nullptr;
330 std::size_t nextChunkSize;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition LinearBuffer.hpp:113
LinearBuffer(std::pmr::memory_resource *memoryResource, std::size_t nextChunkSize=64) noexcept
Definition LinearBuffer.hpp:127
void emplace_back(Args &&... args)
Definition LinearBuffer.hpp:168
void push_back(const T &value)
Definition LinearBuffer.hpp:141
LinearBuffer(LinearBuffer &&)=delete
Span< const T > append(Span< const T > values)
Definition LinearBuffer.hpp:173
Span< const T > append(Span< T > values)
Definition LinearBuffer.hpp:215
LinearBuffer & operator=(LinearBuffer &&)=delete
std::conditional_t< sizeof...(Ts)< 255ull, std::uint8_t, std::conditional_t< sizeof...(Ts)< 65535ull, std::uint16_t, std::conditional_t< sizeof...(Ts)< 4294967295ull, std::uint32_t, std::uint64_t > > > index_type
Definition LinearBuffer.hpp:118
LinearBuffer(const LinearBuffer &)=delete
static constexpr index_type npos
Definition LinearBuffer.hpp:125
LinearBuffer & operator=(const LinearBuffer &)=delete
auto visit(Visitor &&visitor) const
Definition LinearBuffer.hpp:220
constexpr size_type size_bytes() const noexcept
Definition Span.hpp:253
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
constexpr bool linear_buffer_has_alternative_v
Definition LinearBuffer.hpp:72
constexpr std::size_t linear_buffer_index_v
Definition LinearBuffer.hpp:83
typename linear_buffer_alternative< Index, B >::type linear_buffer_alternative_t
Definition LinearBuffer.hpp:99
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
constexpr std::size_t linear_buffer_size_v
Definition LinearBuffer.hpp:110
constexpr decltype(auto) apply(Function &&function, Tuple< Ts... > &tuple)
Definition Tuple.hpp:535