EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_PODVector.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
27#ifndef EBGEOMETRY_PODVECTOR_HPP
28#define EBGEOMETRY_PODVECTOR_HPP
29
30// Std includes
31#include <cstddef>
32#include <cstdint>
33#include <cstring>
34#include <type_traits>
35
36// Our includes
37#include "EBGeometry_GPU.hpp"
38#include "EBGeometry_Macros.hpp"
39#include "EBGeometry_Pool.hpp"
40
41namespace EBGeometry {
42
50template <class T>
51struct PODSpan
52{
54 T* m_ptr = nullptr;
55
57 uint32_t m_size = 0;
58
64 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
65 T&
66 operator[](uint32_t a_i) const noexcept
67 {
69
70 return m_ptr[a_i];
71 }
72
77 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
78 T*
79 begin() const noexcept
80 {
81 return m_ptr;
82 }
83
88 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
89 T*
90 end() const noexcept
91 {
92 return m_ptr + m_size;
93 }
94
99 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
100 uint32_t
101 size() const noexcept
102 {
103 return m_size;
104 }
105
111 template <class U = T, std::enable_if_t<!std::is_const_v<U>, int> = 0>
113 operator PODSpan<const U>() const noexcept
114 {
116 }
117};
118
119static_assert(std::is_trivially_copyable_v<PODSpan<float>>, "PODSpan<float> must be trivially copyable");
120static_assert(std::is_trivially_copyable_v<PODSpan<double>>, "PODSpan<double> must be trivially copyable");
121
129template <class T>
131{
132 static_assert(std::is_trivially_copyable_v<T>, "PODVector<T>: T must be trivially copyable (device-visible storage)");
133 static_assert(alignof(T) <= PoolBaseAlign,
134 "PODVector<T>: T's alignment exceeds the Pool's base alignment, so no reservation can align it");
135
137 uint64_t m_offset = 0;
138
140 uint32_t m_size = 0;
141
143 uint32_t m_capacity = 0;
144
152 constexpr PODVector() noexcept
153 {}
154
163 void
164 reserveFrom(Pool& a_pool, uint32_t a_capacity)
165 {
166 m_offset = a_pool.reserve(a_capacity, sizeof(T), alignof(T));
167 m_size = 0;
168 m_capacity = a_capacity;
169 }
170
180 void
181 reserveSizedFrom(Pool& a_pool, uint32_t a_count)
182 {
183 this->reserveFrom(a_pool, a_count);
184
185 m_size = a_count;
186 }
187
193 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
194 T*
195 data(void* a_base) const noexcept
196 {
197 return reinterpret_cast<T*>(static_cast<unsigned char*>(a_base) + m_offset);
198 }
199
205 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
206 const T*
207 data(const void* a_base) const noexcept
208 {
209 return reinterpret_cast<const T*>(static_cast<const unsigned char*>(a_base) + m_offset);
210 }
211
218 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
219 T&
220 at(void* a_base, uint32_t a_i) const noexcept
221 {
223
224 return this->data(a_base)[a_i];
225 }
226
233 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
234 const T&
235 at(const void* a_base, uint32_t a_i) const noexcept
236 {
238
239 return this->data(a_base)[a_i];
240 }
241
246 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
247 uint32_t
248 size() const noexcept
249 {
250 return m_size;
251 }
252
262 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
263 uint64_t
264 endByte() const noexcept
265 {
266 return m_offset + static_cast<uint64_t>(m_capacity) * sizeof(T);
267 }
268
273 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
274 bool
275 empty() const noexcept
276 {
277 return m_size == 0;
278 }
279
288 void
289 push_back(void* a_base, const T& a_value)
290 {
291 EBGEOMETRY_EXPECT(m_size < m_capacity); // the no-realloc invariant
292
293 this->data(a_base)[m_size++] = a_value;
294 }
295
306 void
307 assign(void* a_base, const T* a_src, uint32_t a_count)
308 {
310 "PODVector::assign: the element count must not exceed the reserved capacity (%u > %u)",
311 unsigned(a_count),
312 unsigned(m_capacity));
313
314 // Guard the zero-element case: an empty build reserves nothing, so both this->data(a_base) and
315 // a_src are null, and memcpy's parameters are declared nonnull -- UBSan's nonnull check flags
316 // memcpy(nullptr, nullptr, 0) even though it copies nothing.
317 if (a_count > 0) {
318 std::memcpy(this->data(a_base), a_src, static_cast<size_t>(a_count) * sizeof(T));
319 }
320
321 m_size = a_count;
322 }
323
330 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
332 bind(void* a_base) const noexcept
333 {
334 return PODSpan<T>{this->data(a_base), m_size};
335 }
336
343 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
345 bind(const void* a_base) const noexcept
346 {
347 return PODSpan<const T>{this->data(a_base), m_size};
348 }
349};
350
351static_assert(sizeof(PODVector<float>) == 16, "PODVector layout must be 8 (offset) + 4 (size) + 4 (capacity)");
352static_assert(sizeof(PODVector<double>) == 16, "PODVector layout must be 8 (offset) + 4 (size) + 4 (capacity)");
353static_assert(std::is_trivially_copyable_v<PODVector<float>>, "PODVector<float> must be trivially copyable");
354static_assert(std::is_trivially_copyable_v<PODVector<double>>, "PODVector<double> must be trivially copyable");
355
356} // namespace EBGeometry
357
358#endif // EBGEOMETRY_PODVECTOR_HPP
GPU portability layer: host/device decoration macros and backend detection.
#define EBGEOMETRY_HOST_DEVICE
Mark a function as callable from both host and device code (no-op on host compilers).
Definition EBGeometry_GPU.hpp:80
#define EBGEOMETRY_HOST
Mark a function as callable from host code only (no-op on host compilers).
Definition EBGeometry_GPU.hpp:83
Utility macros for EBGeometry.
#define EBGEOMETRY_REQUIRE(cond,...)
Always-on check of user input and one-time host setup.
Definition EBGeometry_Macros.hpp:200
#define EBGEOMETRY_EXPECT(cond)
Runtime precondition assertion for EBGeometry.
Definition EBGeometry_Macros.hpp:269
Growable bump arena for the GPU memory foundation.
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:205
uint64_t reserve(size_t a_count, size_t a_elemSize, size_t a_alignment)
Bump-reserve a_count * a_elemSize bytes aligned to a_alignment.
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
constexpr size_t PoolBaseAlign
Block-base alignment for every Pool, in bytes.
Definition EBGeometry_MemoryResource.hpp:52
Raw pointer + size view into a Pool block, for hot inner loops.
Definition EBGeometry_PODVector.hpp:52
T * begin() const noexcept
Iterator to the first element.
Definition EBGeometry_PODVector.hpp:79
T * m_ptr
Raw pointer to element 0 inside the pool's block, as resolved when the span was bound.
Definition EBGeometry_PODVector.hpp:54
uint32_t size() const noexcept
Number of elements.
Definition EBGeometry_PODVector.hpp:101
uint32_t m_size
Number of elements.
Definition EBGeometry_PODVector.hpp:57
T * end() const noexcept
Iterator past the last element.
Definition EBGeometry_PODVector.hpp:90
T & operator[](uint32_t a_i) const noexcept
Element access.
Definition EBGeometry_PODVector.hpp:66
Placement-independent descriptor of a POD array inside a Pool.
Definition EBGeometry_PODVector.hpp:131
void reserveSizedFrom(Pool &a_pool, uint32_t a_count)
Reserve a_count slots from a_pool and count them all as elements.
Definition EBGeometry_PODVector.hpp:181
void assign(void *a_base, const T *a_src, uint32_t a_count)
Bulk-fill the array from a contiguous host source.
Definition EBGeometry_PODVector.hpp:307
void reserveFrom(Pool &a_pool, uint32_t a_capacity)
Reserve a_capacity slots from a_pool, aligned to alignof(T).
Definition EBGeometry_PODVector.hpp:164
uint64_t endByte() const noexcept
One past the last byte this array occupies, measured from the pool base.
Definition EBGeometry_PODVector.hpp:264
T & at(void *a_base, uint32_t a_i) const noexcept
Mutable element access against a_base.
Definition EBGeometry_PODVector.hpp:220
uint64_t m_offset
Byte offset of element 0 from Pool::base.
Definition EBGeometry_PODVector.hpp:137
PODSpan< const T > bind(const void *a_base) const noexcept
Bind a const raw-pointer view for a hot loop.
Definition EBGeometry_PODVector.hpp:345
PODSpan< T > bind(void *a_base) const noexcept
Bind a mutable raw-pointer view for a hot loop.
Definition EBGeometry_PODVector.hpp:332
uint32_t m_capacity
Reserved element slots (fixed after reserveFrom; never reallocates).
Definition EBGeometry_PODVector.hpp:143
T * data(void *a_base) const noexcept
Mutable pointer to element 0 against a_base.
Definition EBGeometry_PODVector.hpp:195
const T & at(const void *a_base, uint32_t a_i) const noexcept
Const element access against a_base.
Definition EBGeometry_PODVector.hpp:235
bool empty() const noexcept
Whether the array is empty.
Definition EBGeometry_PODVector.hpp:275
uint32_t size() const noexcept
Number of constructed elements.
Definition EBGeometry_PODVector.hpp:248
void push_back(void *a_base, const T &a_value)
Append one element into the pre-reserved capacity.
Definition EBGeometry_PODVector.hpp:289
constexpr PODVector() noexcept
An empty array, reserved from no pool.
Definition EBGeometry_PODVector.hpp:152
uint32_t m_size
Number of constructed elements.
Definition EBGeometry_PODVector.hpp:140
const T * data(const void *a_base) const noexcept
Const pointer to element 0 against a_base.
Definition EBGeometry_PODVector.hpp:207