77#ifndef NANOVDB_HOSTBUFFER_H_HAS_BEEN_INCLUDED
78#define NANOVDB_HOSTBUFFER_H_HAS_BEEN_INCLUDED
86#include <unordered_set>
92#define checkPtr(ptr, msg) \
94 ptrAssert((ptr), (msg), __FILE__, __LINE__); \
99template<
typename BufferT>
110template<
typename BufferT,
typename =
void>
112template<
typename BufferT>
119template<
typename BufferT,
typename =
void>
121template<
typename BufferT>
129template<
typename BufferT>
136template<
typename BufferT,
typename =
void>
138template<
typename BufferT>
140{
static constexpr bool value =
true; };
145template<
typename BufferT,
typename =
void>
147template<
typename BufferT>
149{
static constexpr bool value =
sizeof(
typename BufferT::ElementType) == 1; };
154template<
typename BufferT,
typename =
void>
156template<
typename BufferT>
158{
static constexpr bool value =
true; };
163template<
typename BufferT,
typename =
void>
165template<
typename BufferT>
167{
static constexpr bool value =
true; };
182 std::shared_ptr<Pool> mPool;
186#if defined(DEBUG) || defined(_DEBUG)
187 static inline void ptrAssert(
void* ptr,
const char* msg,
const char* file,
int line,
bool abort =
true)
189 if (ptr ==
nullptr) {
190 fprintf(stderr,
"NULL pointer error: %s %s %d\n", msg, file, line);
195 fprintf(stderr,
"Alignment pointer error: %s %s %d\n", msg, file, line);
201 static inline void ptrAssert(
void*,
const char*,
const char*,
int,
bool =
true)
256 const void*
data()
const {
return mData; }
264 template <
typename T>
265 T*
data(ptrdiff_t count = 0)
const {
return mData ?
reinterpret_cast<T*
>(mData) + count :
nullptr;}
282 bool isEmpty()
const {
return !mPool || mSize == 0 || mData ==
nullptr; }
345 if (
mData ==
nullptr)
throw std::runtime_error(
"Pool::Pool malloc failed");
349 throw std::runtime_error(
"Pool::Pool: external memory buffer is not aligned to " +
351 " bytes.\nHint: use nanovdb::alignPtr or std::aligned_alloc (C++17 only)");
384 std::stringstream ss;
385 ss <<
"HostBuffer::Pool: insufficient memory\n"
387 <<
"-bytes alignment from a pool with "
389 <<
" bytes are used by " <<
mRegister.size() <<
" other buffer(s). "
390 <<
"Pool is " << (
mManaged ?
"internally" :
"externally") <<
" managed.\n";
391 throw std::runtime_error(ss.str());
393 buffer->mSize =
size;
394 const std::lock_guard<std::mutex> lock(
mMutex);
396 buffer->mData = alignedFree;
403 const std::lock_guard<std::mutex> lock(
mMutex);
410 const std::lock_guard<std::mutex> lock(
mMutex);
419 buffer->mPool.reset();
421 buffer->mData =
nullptr;
431 const uint64_t memUsage = this->
usage();
433 const bool managed = (
data ==
nullptr);
436 throw std::runtime_error(
"Pool::resize: external memory buffer is not aligned to " +
440 if (memUsage >
size) {
441 throw std::runtime_error(
"Pool::resize: insufficient memory");
444 uint64_t padding = 0;
453 if (
data ==
nullptr) {
454 throw std::runtime_error(
"Pool::resize: allocation failed");
485 static void* alloc(uint64_t
size)
491 static void* realloc(
void*
const origData,
493 uint64_t desiredSize,
499 if (
data !=
nullptr &&
data != origData) {
502 if (newPadding != padding) {
507 padding = newPadding;
521 mPool = std::make_shared<Pool>(
size);
522 mData = mPool->mFree;
523 mPool->mRegister.insert(
this);
530 if (mPool && mSize != 0) {
531 mPool->replace(&other,
this);
535 other.mData =
nullptr;
541 throw std::runtime_error(
"HostBuffer: invalid buffer size");
560 if (mPool && mSize != 0) {
561 mPool->replace(&other,
this);
565 other.mData =
nullptr;
571 return mPool ? mPool->mSize : 0u;
576 return mPool ? mPool->usage(): 0u;
581 return mPool ? mPool->mManaged :
false;
586 return mPool ? mPool->isFull() :
false;
592 throw std::runtime_error(
"HostBuffer: invalid pool size");
598 buffer.mData =
nullptr;
605 throw std::runtime_error(
"HostBuffer: invalid buffer size");
616 if (pool ==
nullptr || !pool->mPool) {
617 buffer.mPool = std::make_shared<Pool>(
bufferSize);
619 buffer.mPool = pool->mPool;
637 if (this->
size()>0) {
638 throw std::runtime_error(
"HostBuffer: only empty buffers can call reset");
641 throw std::runtime_error(
"HostBuffer: this buffer contains no pool to reset");
649 throw std::runtime_error(
"HostBuffer: this buffer contains no pool to resize");
Implements a light-weight self-contained VDB data-structure in a single file! In other words,...
#define NANOVDB_DATA_ALIGNMENT
Definition NanoVDB.h:133
This is a buffer that contains a shared or private pool to either externally or internally managed ho...
Definition HostBuffer.h:180
void * data()
Definition HostBuffer.h:257
~HostBuffer()
Custom descructor.
Definition HostBuffer.h:214
HostBuffer(const HostBuffer &)=delete
Disallow copy-construction.
const void * data() const
Retuns a pointer to the raw memory buffer managed by this allocator.
Definition HostBuffer.h:256
T * data(ptrdiff_t count=0) const
Returns an offset pointer of a specific type from the allocated host memory.
Definition HostBuffer.h:265
HostBuffer(uint64_t bufferSize=0)
Return a full buffer or an empty buffer.
Definition HostBuffer.h:518
HostBuffer & operator=(HostBuffer &&other)
Move copy assignment operation.
Definition HostBuffer.h:552
uint64_t bufferSize() const
Returns the size in bytes associated with this buffer.
Definition HostBuffer.h:269
bool empty() const
Definition HostBuffer.h:283
static HostBuffer createFull(uint64_t bufferSize, void *data=nullptr)
Return a full buffer which satisfies: buffer.size == bufferSize, buffer.poolSize() == bufferSize,...
Definition HostBuffer.h:602
uint64_t poolSize() const
Returns the size in bytes of the memory pool shared with this instance.
Definition HostBuffer.h:569
uint64_t size() const
Definition HostBuffer.h:270
void init(uint64_t bufferSize, void *data=nullptr)
Initialize as a full buffer with the specified size. If data is NULL the memory is internally allocat...
Definition HostBuffer.h:538
HostBuffer & operator=(const HostBuffer &)=delete
Disallow copy assignment operation.
uint64_t poolUsage() const
Total number of bytes from the pool currently in use by buffers.
Definition HostBuffer.h:574
bool isPool() const
Return true if this is a pool, i.e. an empty buffer with a nonempty internal pool,...
Definition HostBuffer.h:288
bool isManaged() const
Return true if memory is managed (using std::malloc and std:free) by the shared pool in this buffer....
Definition HostBuffer.h:579
static HostBuffer create(uint64_t bufferSize, const HostBuffer *pool=nullptr)
Return a buffer with bufferSize bytes managed by the specified memory pool. If none is provided,...
Definition HostBuffer.h:613
void clear()
Clear this buffer so it is empty.
Definition HostBuffer.h:625
bool isEmpty() const
Returns true if this buffer has no memory associated with it.
Definition HostBuffer.h:282
void reset()
Clears all existing buffers that are registered against the memory pool and resets the pool so it can...
Definition HostBuffer.h:635
bool isFull() const
Return true if the pool exists, is nonempty but has no more available memory.
Definition HostBuffer.h:584
void resizePool(uint64_t poolSize, void *data=nullptr)
resize the pool size. It will attempt to resize the existing memory block, but if that fails a deep c...
Definition HostBuffer.h:646
static HostBuffer createPool(uint64_t poolSize, void *data=nullptr)
Return a pool buffer which satisfies: buffer.size == 0, buffer.poolSize() == poolSize,...
Definition HostBuffer.h:589
__hostdev__ constexpr Type Min(Type a, Type b) noexcept
Same-type minimum (the primary template).
Definition Math.h:161
static DstT * PtrAdd(void *p, int64_t offset)
Adds a byte offset to a non-const pointer to produce another non-const pointer.
Definition Util.h:555
static int64_t PtrDiff(const void *p, const void *q)
Compute the distance, in bytes, between two pointers, dist = p - q.
Definition Util.h:541
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:34
static uint64_t alignmentPadding(const void *p)
return the smallest number of bytes that when added to the specified pointer results in a 32 byte ali...
Definition NanoVDB.h:600
static void ptrAssert(const void *ptr, const char *msg, const char *file, int line, bool abort=true)
Definition Util.h:32
static constexpr bool value
Definition HostBuffer.h:149
Detects whether a buffer's elements are byte-sized. Buffers that expose no ElementType (e....
Definition HostBuffer.h:146
static constexpr bool value
Definition HostBuffer.h:146
static constexpr bool value
Definition HostBuffer.h:167
Detects whether a buffer provides destroy(), the cuda::Buffer spelling for releasing its storage....
Definition HostBuffer.h:164
static constexpr bool value
Definition HostBuffer.h:164
static constexpr bool value
Definition HostBuffer.h:114
Detects whether BufferTraits<BufferT> defines hasDeviceSingle, i.e. whether the buffer manages a sing...
Definition HostBuffer.h:111
static constexpr bool value
Definition HostBuffer.h:111
static constexpr bool value
Definition HostBuffer.h:123
Companion detection for BufferTraits<...>::hasHostSingle: a single-space buffer whose storage is host...
Definition HostBuffer.h:120
static constexpr bool value
Definition HostBuffer.h:120
static constexpr bool value
Definition HostBuffer.h:140
Detects whether a buffer exposes a retained stream (a stream() member), i.e. whether its resource is ...
Definition HostBuffer.h:137
static constexpr bool value
Definition HostBuffer.h:137
static constexpr bool value
Definition HostBuffer.h:158
Detects whether a buffer type is default-constructible, so consumers can name that requirement in a s...
Definition HostBuffer.h:155
static constexpr bool value
Definition HostBuffer.h:155
A single-space buffer whose storage the host cannot read: the device-single family minus its host-acc...
Definition HostBuffer.h:131
static constexpr bool value
Definition HostBuffer.h:131
Definition HostBuffer.h:101
static constexpr bool hasDeviceSingle
Definition HostBuffer.h:102
Definition HostBuffer.h:327
std::unordered_set< HostBuffer * > HashTableT
Definition HostBuffer.h:328
void resize(uint64_t size, void *data=nullptr)
Resize this Pool and update registered buffers as needed. If data is no NULL it is used as externally...
Definition HostBuffer.h:429
uint64_t usage() const
Return the total number of bytes used from this Pool by buffers.
Definition HostBuffer.h:376
void replace(HostBuffer *buffer1, HostBuffer *buffer2)
Replaces buffer1 with buffer2 in the register.
Definition HostBuffer.h:408
Pool(uint64_t size=0, void *data=nullptr)
External memory ctor.
Definition HostBuffer.h:336
uint64_t mSize
Definition HostBuffer.h:332
void * mData
Definition HostBuffer.h:331
Pool(const Pool &)=delete
Disallow copy-construction.
void add(HostBuffer *buffer, uint64_t size)
Allocate a buffer of the specified size and add it to the register.
Definition HostBuffer.h:379
void remove(HostBuffer *buffer)
Remove the specified buffer from the register.
Definition HostBuffer.h:401
void * mFree
Definition HostBuffer.h:331
Pool & operator=(const Pool &&)=delete
Disallow move assignment operation.
std::mutex mMutex
Definition HostBuffer.h:329
Pool(const Pool &&)=delete
Disallow move-construction.
HashTableT mRegister
Definition HostBuffer.h:330
uint64_t mPadding
Definition HostBuffer.h:332
Pool & operator=(const Pool &)=delete
Disallow copy assignment operation.
void reset()
Reset the register and all its buffers.
Definition HostBuffer.h:416
~Pool()
Custom destructor.
Definition HostBuffer.h:357
bool isFull() const
Return true is all the memory in this pool is in use.
Definition HostBuffer.h:477
bool mManaged
Definition HostBuffer.h:333