EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_Pool.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
31#ifndef EBGEOMETRY_POOL_HPP
32#define EBGEOMETRY_POOL_HPP
33
34// Std includes
35#include <atomic>
36#include <cstddef>
37#include <cstdint>
38#include <memory>
39#include <type_traits>
40
41// Our includes
42#include "EBGeometry_GPU.hpp"
43#include "EBGeometry_Macros.hpp"
45
46namespace EBGeometry {
47
63{
66 void* m_base = nullptr;
67
69 uint64_t m_id = 0;
70
73 uint64_t m_rootId = 0;
74};
75
76class Pool;
77
99{
110 constexpr PoolLocation() noexcept
111 {}
112
114 const PoolControl* m_control = nullptr;
115
117 void* m_base = nullptr;
118
120 bool m_hostAccessible = false;
121
129 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
130 inline void*
131 base() const noexcept
132 {
133#if defined(EBGEOMETRY_DEVICE_COMPILE)
134 EBGEOMETRY_EXPECT(m_control == nullptr);
135
136 return m_base;
137#else
138 if (m_control != nullptr) {
139 return m_control->m_base;
140 }
141
142 // Unset, or a snapshot of memory the host cannot reach (a device-only mirror).
144
145 return m_base;
146#endif
147 }
148
154 [[nodiscard]] EBGEOMETRY_HOST
155 inline bool
156 isAttachedTo(const Pool& a_pool) const noexcept;
157
171 inline void
172 attach(const Pool& a_pool, const char* a_who, bool a_deviceWritten = false) noexcept;
173
186 [[nodiscard]] EBGEOMETRY_HOST
187 inline PoolLocation
188 rebasedOnto(const Pool& a_pool, uint64_t a_endByte, const char* a_who) const noexcept;
189};
190
191static_assert(std::is_trivially_copyable_v<PoolLocation>,
192 "PoolLocation must be trivially copyable, for a kernel to take a handle");
193
204class Pool
205{
206public:
221 explicit Pool(MemoryResource& a_resource, size_t a_initialBytes = 0);
222
227 ~Pool() noexcept;
228
232 Pool(const Pool&) = delete;
233
237 Pool&
238 operator=(const Pool&) = delete;
239
245 Pool(Pool&& a_other) noexcept;
246
253 Pool&
254 operator=(Pool&& a_other) noexcept;
255
291 [[nodiscard]] EBGEOMETRY_HOST
292 uint64_t
293 reserve(size_t a_count, size_t a_elemSize, size_t a_alignment);
294
302 void
303 freeze() noexcept;
304
310 [[nodiscard]] EBGEOMETRY_HOST
311 void*
312 base() const noexcept
313 {
314 return (m_control != nullptr) ? m_control->m_base : nullptr;
315 }
316
326 [[nodiscard]] EBGEOMETRY_HOST
327 const PoolControl*
328 control() const noexcept
329 {
330 return m_control.get();
331 }
332
339 [[nodiscard]] EBGEOMETRY_HOST
340 uint64_t
341 id() const noexcept
342 {
343 return (m_control != nullptr) ? m_control->m_id : 0;
344 }
345
355 [[nodiscard]] EBGEOMETRY_HOST
356 uint64_t
357 mirrorOf() const noexcept
358 {
359 return m_mirrorOf;
360 }
361
369 [[nodiscard]] EBGEOMETRY_HOST
370 uint64_t
371 rootId() const noexcept
372 {
373 return (m_control != nullptr) ? m_control->m_rootId : 0;
374 }
375
380 [[nodiscard]] EBGEOMETRY_HOST
381 size_t
382 usedBytes() const noexcept
383 {
384 return m_size;
385 }
386
391 [[nodiscard]] EBGEOMETRY_HOST
392 size_t
393 capacityBytes() const noexcept
394 {
395 return m_capacity;
396 }
397
402 [[nodiscard]] EBGEOMETRY_HOST
403 bool
404 isFrozen() const noexcept
405 {
406 return m_frozen;
407 }
408
413 [[nodiscard]] EBGEOMETRY_HOST
415 resource() const noexcept
416 {
417 return *m_resource;
418 }
419
435 [[nodiscard]] EBGEOMETRY_HOST
436 static Pool
437 mirror(const Pool& a_src, MemoryResource& a_dstResource);
438
463 [[nodiscard]] EBGEOMETRY_HOST
464 static Pool
465 forImage(MemoryResource& a_resource, size_t a_bytes);
466
474 [[nodiscard]] EBGEOMETRY_HOST
475 inline PoolLocation
476 location() const noexcept;
477
478private:
481 static constexpr size_t s_maxBytes = SIZE_MAX & ~(PoolBaseAlign - 1);
482
485 struct MirrorTag
486 {
487 };
488
496 Pool(MemoryResource& a_resource, MirrorTag) : m_resource(&a_resource), m_control(makeControl())
497 {}
498
506 [[nodiscard]] EBGEOMETRY_HOST
507 static std::unique_ptr<PoolControl>
508 makeControl()
509 {
510 static std::atomic<uint64_t> s_nextID{1};
511
512 auto control = std::make_unique<PoolControl>();
513
514 control->m_id = s_nextID.fetch_add(1, std::memory_order_relaxed);
515 control->m_rootId = control->m_id;
516
517 return control;
518 }
519
521 MemoryResource* m_resource = nullptr;
522
524 std::unique_ptr<PoolControl> m_control;
525
527 size_t m_size = 0;
528
530 size_t m_capacity = 0;
531
533 uint64_t m_mirrorOf = 0;
534
536 bool m_frozen = false;
537
545 void
546 grow(size_t a_need);
547};
548
563{
564public:
569 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
570 inline PoolLocation
571 location() const noexcept
572 {
573 return m_location;
574 }
575
581 [[nodiscard]] EBGEOMETRY_HOST
582 inline bool
583 isAttachedTo(const Pool& a_pool) const noexcept
584 {
585 return m_location.isAttachedTo(a_pool);
586 }
587
588protected:
592 PoolHandle() noexcept = default;
593
599 explicit PoolHandle(const PoolLocation& a_location) noexcept : m_location(a_location)
600 {}
601
607 PoolHandle(const PoolHandle&) noexcept = default;
608
612 PoolHandle(PoolHandle&&) noexcept = default;
613
619 operator=(const PoolHandle&) noexcept = default;
620
626 operator=(PoolHandle&&) noexcept = default;
627
631 ~PoolHandle() noexcept = default;
632
637 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
638 inline void*
639 base() noexcept
640 {
641 return m_location.base();
642 }
643
650 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
651 inline const void*
652 base() const noexcept
653 {
654 return m_location.base();
655 }
656
664 inline void
665 attach(const Pool& a_pool, const char* a_who, const bool a_deviceWritten = false) noexcept
666 {
667 m_location.attach(a_pool, a_who, a_deviceWritten);
668 }
669
678 [[nodiscard]] EBGEOMETRY_HOST
679 inline PoolLocation
680 rebasedLocation(const Pool& a_pool, uint64_t a_endByte, const char* a_who) const noexcept
681 {
682 return m_location.rebasedOnto(a_pool, a_endByte, a_who);
683 }
684
690 inline void
691 setLocation(const PoolLocation& a_location) noexcept
692 {
693 m_location = a_location;
694 }
695
696private:
700 PoolLocation m_location;
701};
702
707template <class P>
708inline constexpr bool isPoolHandle = std::is_base_of_v<PoolHandle, P>;
709
710} // namespace EBGeometry
711
712#include "EBGeometry_PoolImplem.hpp"
713
714#endif // EBGEOMETRY_POOL_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_EXPECT(cond)
Runtime precondition assertion for EBGeometry.
Definition EBGeometry_Macros.hpp:269
Placement-policy layer for the GPU memory foundation: the abstract MemoryResource and its concrete ho...
Abstract placement policy: allocates and frees raw, correctly-aligned byte blocks.
Definition EBGeometry_MemoryResource.hpp:69
Base of every pool-resident handle: the one PoolLocation its arrays resolve against.
Definition EBGeometry_Pool.hpp:563
PoolHandle(PoolHandle &&) noexcept=default
Move a handle; the same as a copy.
bool isAttachedTo(const Pool &a_pool) const noexcept
Check whether this handle's arrays were reserved from a_pool.
Definition EBGeometry_Pool.hpp:583
PoolLocation location() const noexcept
Where this handle's arrays live.
Definition EBGeometry_Pool.hpp:571
PoolHandle(const PoolHandle &) noexcept=default
Copy a handle, as a derived handle's copy does.
PoolLocation rebasedLocation(const Pool &a_pool, uint64_t a_endByte, const char *a_who) const noexcept
The location of this handle's arrays in another pool of its mirror chain; see PoolLocation::rebasedOn...
Definition EBGeometry_Pool.hpp:680
const void * base() const noexcept
The base address this handle's offsets resolve against, for read-only access.
Definition EBGeometry_Pool.hpp:652
PoolHandle() noexcept=default
An unset location; the first reservation attaches it.
void attach(const Pool &a_pool, const char *a_who, const bool a_deviceWritten=false) noexcept
Follow a_pool, as the first reservation does; see PoolLocation::attach().
Definition EBGeometry_Pool.hpp:665
void * base() noexcept
The base address this handle's offsets resolve against; see PoolLocation::base().
Definition EBGeometry_Pool.hpp:639
void setLocation(const PoolLocation &a_location) noexcept
Replace the location, for a view or a copy made by a derived handle.
Definition EBGeometry_Pool.hpp:691
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:205
size_t capacityBytes() const noexcept
Bytes currently allocated for the block.
Definition EBGeometry_Pool.hpp:393
uint64_t rootId() const noexcept
Identity of the root of this pool's mirror chain.
Definition EBGeometry_Pool.hpp:371
PoolLocation location() const noexcept
A location that follows this pool, for rebuilding a structure from its layout with its (Layout,...
static Pool mirror(const Pool &a_src, MemoryResource &a_dstResource)
Mirror a frozen pool's whole block into another resource.
const PoolControl * control() const noexcept
This pool's stable control block, through which pool-resident objects re-resolve the base on every ac...
Definition EBGeometry_Pool.hpp:328
size_t usedBytes() const noexcept
Bytes currently in use (the bump cursor).
Definition EBGeometry_Pool.hpp:382
bool isFrozen() const noexcept
Whether the pool has been frozen.
Definition EBGeometry_Pool.hpp:404
uint64_t mirrorOf() const noexcept
Identity of the pool this one ultimately mirrors, or zero if it is not a mirror.
Definition EBGeometry_Pool.hpp:357
uint64_t id() const noexcept
Identity of this pool, unique among all pools constructed in this process.
Definition EBGeometry_Pool.hpp:341
~Pool() noexcept
Destructor. Returns the block to the backing resource.
MemoryResource & resource() const noexcept
The backing memory resource.
Definition EBGeometry_Pool.hpp:415
Pool(MemoryResource &a_resource, size_t a_initialBytes=0)
Construct an empty pool over a_resource, optionally pre-reserving a block.
static Pool forImage(MemoryResource &a_resource, size_t a_bytes)
A pool for the image of a frozen pool: a block of exactly a_bytes from a_resource,...
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
constexpr bool isPoolHandle
Whether P is a pool-resident handle (derives from PoolHandle).
Definition EBGeometry_Pool.hpp:708
Stable, heap-resident handle to a Pool's current base address and identity.
Definition EBGeometry_Pool.hpp:63
uint64_t m_id
Identity of the owning pool; see Pool::id. Never zero for a live pool.
Definition EBGeometry_Pool.hpp:69
void * m_base
Current base address of the owning pool's block. Rewritten whenever a Pool::reserve grows the block.
Definition EBGeometry_Pool.hpp:66
uint64_t m_rootId
Identity of the root of the owning pool's mirror chain: its own identity for a pool that is not a mir...
Definition EBGeometry_Pool.hpp:73
Where a pool-resident descriptor resolves its offsets, and the rules for moving it.
Definition EBGeometry_Pool.hpp:99
constexpr PoolLocation() noexcept
An unset location.
Definition EBGeometry_Pool.hpp:110
PoolLocation rebasedOnto(const Pool &a_pool, uint64_t a_endByte, const char *a_who) const noexcept
The location of the same arrays in another pool of the same mirror chain.
const PoolControl * m_control
Control block of the pool the descriptor follows; null for a snapshot or when unset.
Definition EBGeometry_Pool.hpp:114
void attach(const Pool &a_pool, const char *a_who, bool a_deviceWritten=false) noexcept
Start following a pool, as the descriptor's first reservation from it does.
bool m_hostAccessible
Whether host code may dereference a snapshot's base (managed or mapped memory).
Definition EBGeometry_Pool.hpp:120
void * base() const noexcept
The base address the descriptor's offsets resolve against.
Definition EBGeometry_Pool.hpp:131
void * m_base
Base address captured by a snapshot; null otherwise.
Definition EBGeometry_Pool.hpp:117
bool isAttachedTo(const Pool &a_pool) const noexcept
Whether this location follows the given pool.