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>
111template<
typename BufferT,
typename =
void>
113template<
typename BufferT>
120template<
typename BufferT,
typename =
void>
122template<
typename BufferT>
130template<
typename BufferT>
137template<
typename BufferT,
typename =
void>
139template<
typename BufferT>
141{
static constexpr bool value =
true; };
146template<
typename BufferT,
typename =
void>
148template<
typename BufferT>
150{
static constexpr bool value =
sizeof(
typename BufferT::ElementType) == 1; };
155template<
typename BufferT,
typename =
void>
157template<
typename BufferT>
159{
static constexpr bool value =
true; };
164template<
typename BufferT,
typename =
void>
166template<
typename BufferT>
168{
static constexpr bool value =
true; };
183 std::shared_ptr<Pool> mPool;
187#if defined(DEBUG) || defined(_DEBUG)
188 static inline void ptrAssert(
void* ptr,
const char* msg,
const char* file,
int line,
bool abort =
true)
190 if (ptr ==
nullptr) {
191 fprintf(stderr,
"NULL pointer error: %s %s %d\n", msg, file, line);
196 fprintf(stderr,
"Alignment pointer error: %s %s %d\n", msg, file, line);
202 static inline void ptrAssert(
void*,
const char*,
const char*,
int,
bool =
true)
257 const void*
data()
const {
return mData; }
265 template <
typename T>
266 T*
data(ptrdiff_t count = 0)
const {
return mData ?
reinterpret_cast<T*
>(mData) + count :
nullptr;}
283 bool isEmpty()
const {
return !mPool || mSize == 0 || mData ==
nullptr; }
346 if (
mData ==
nullptr)
throw std::runtime_error(
"Pool::Pool malloc failed");
350 throw std::runtime_error(
"Pool::Pool: external memory buffer is not aligned to " +
352 " bytes.\nHint: use nanovdb::alignPtr or std::aligned_alloc (C++17 only)");
385 std::stringstream ss;
386 ss <<
"HostBuffer::Pool: insufficient memory\n"
388 <<
"-bytes alignment from a pool with "
390 <<
" bytes are used by " <<
mRegister.size() <<
" other buffer(s). "
391 <<
"Pool is " << (
mManaged ?
"internally" :
"externally") <<
" managed.\n";
392 throw std::runtime_error(ss.str());
394 buffer->mSize =
size;
395 const std::lock_guard<std::mutex> lock(
mMutex);
397 buffer->mData = alignedFree;
404 const std::lock_guard<std::mutex> lock(
mMutex);
411 const std::lock_guard<std::mutex> lock(
mMutex);
420 buffer->mPool.reset();
422 buffer->mData =
nullptr;
432 const uint64_t memUsage = this->
usage();
434 const bool managed = (
data ==
nullptr);
437 throw std::runtime_error(
"Pool::resize: external memory buffer is not aligned to " +
441 if (memUsage >
size) {
442 throw std::runtime_error(
"Pool::resize: insufficient memory");
445 uint64_t padding = 0;
454 if (
data ==
nullptr) {
455 throw std::runtime_error(
"Pool::resize: allocation failed");
486 static void* alloc(uint64_t
size)
492 static void* realloc(
void*
const origData,
494 uint64_t desiredSize,
500 if (
data !=
nullptr &&
data != origData) {
503 if (newPadding != padding) {
508 padding = newPadding;
522 mPool = std::make_shared<Pool>(
size);
523 mData = mPool->mFree;
524 mPool->mRegister.insert(
this);
531 if (mPool && mSize != 0) {
532 mPool->replace(&other,
this);
536 other.mData =
nullptr;
542 throw std::runtime_error(
"HostBuffer: invalid buffer size");
561 if (mPool && mSize != 0) {
562 mPool->replace(&other,
this);
566 other.mData =
nullptr;
572 return mPool ? mPool->mSize : 0u;
577 return mPool ? mPool->usage(): 0u;
582 return mPool ? mPool->mManaged :
false;
587 return mPool ? mPool->isFull() :
false;
593 throw std::runtime_error(
"HostBuffer: invalid pool size");
599 buffer.mData =
nullptr;
606 throw std::runtime_error(
"HostBuffer: invalid buffer size");
617 if (pool ==
nullptr || !pool->mPool) {
618 buffer.mPool = std::make_shared<Pool>(
bufferSize);
620 buffer.mPool = pool->mPool;
638 if (this->
size()>0) {
639 throw std::runtime_error(
"HostBuffer: only empty buffers can call reset");
642 throw std::runtime_error(
"HostBuffer: this buffer contains no pool to reset");
650 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:181
void * data()
Definition HostBuffer.h:258
~HostBuffer()
Custom descructor.
Definition HostBuffer.h:215
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:257
T * data(ptrdiff_t count=0) const
Returns an offset pointer of a specific type from the allocated host memory.
Definition HostBuffer.h:266
HostBuffer(uint64_t bufferSize=0)
Return a full buffer or an empty buffer.
Definition HostBuffer.h:519
HostBuffer & operator=(HostBuffer &&other)
Move copy assignment operation.
Definition HostBuffer.h:553
uint64_t bufferSize() const
Returns the size in bytes associated with this buffer.
Definition HostBuffer.h:270
bool empty() const
Definition HostBuffer.h:284
static HostBuffer createFull(uint64_t bufferSize, void *data=nullptr)
Return a full buffer which satisfies: buffer.size == bufferSize, buffer.poolSize() == bufferSize,...
Definition HostBuffer.h:603
uint64_t poolSize() const
Returns the size in bytes of the memory pool shared with this instance.
Definition HostBuffer.h:570
uint64_t size() const
Definition HostBuffer.h:271
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:539
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:575
bool isPool() const
Return true if this is a pool, i.e. an empty buffer with a nonempty internal pool,...
Definition HostBuffer.h:289
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:580
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:614
void clear()
Clear this buffer so it is empty.
Definition HostBuffer.h:626
bool isEmpty() const
Returns true if this buffer has no memory associated with it.
Definition HostBuffer.h:283
void reset()
Clears all existing buffers that are registered against the memory pool and resets the pool so it can...
Definition HostBuffer.h:636
bool isFull() const
Return true if the pool exists, is nonempty but has no more available memory.
Definition HostBuffer.h:585
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:647
static HostBuffer createPool(uint64_t poolSize, void *data=nullptr)
Return a pool buffer which satisfies: buffer.size == 0, buffer.poolSize() == poolSize,...
Definition HostBuffer.h:590
__hostdev__ Type Min(Type a, Type b)
Definition Math.h:133
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:524
static int64_t PtrDiff(const void *p, const void *q)
Compute the distance, in bytes, between two pointers, dist = p - q.
Definition Util.h:510
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:31
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:603
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:150
Detects whether a buffer's elements are byte-sized. Buffers that expose no ElementType (e....
Definition HostBuffer.h:147
static constexpr bool value
Definition HostBuffer.h:147
static constexpr bool value
Definition HostBuffer.h:168
Detects whether a buffer provides destroy(), the cuda::Buffer spelling for releasing its storage....
Definition HostBuffer.h:165
static constexpr bool value
Definition HostBuffer.h:165
static constexpr bool value
Definition HostBuffer.h:115
Detects whether BufferTraits<BufferT> defines hasDeviceSingle, i.e. whether the buffer manages a sing...
Definition HostBuffer.h:112
static constexpr bool value
Definition HostBuffer.h:112
static constexpr bool value
Definition HostBuffer.h:124
Companion detection for BufferTraits<...>::hasHostSingle: a single-space buffer whose storage is host...
Definition HostBuffer.h:121
static constexpr bool value
Definition HostBuffer.h:121
static constexpr bool value
Definition HostBuffer.h:141
Detects whether a buffer exposes a retained stream (a stream() member), i.e. whether its resource is ...
Definition HostBuffer.h:138
static constexpr bool value
Definition HostBuffer.h:138
static constexpr bool value
Definition HostBuffer.h:159
Detects whether a buffer type is default-constructible, so consumers can name that requirement in a s...
Definition HostBuffer.h:156
static constexpr bool value
Definition HostBuffer.h:156
A single-space buffer whose storage the host cannot read: the device-single family minus its host-acc...
Definition HostBuffer.h:132
static constexpr bool value
Definition HostBuffer.h:132
Definition HostBuffer.h:101
static constexpr bool hasDeviceSingle
Definition HostBuffer.h:103
static constexpr bool hasDeviceDual
Definition HostBuffer.h:102
Definition HostBuffer.h:328
std::unordered_set< HostBuffer * > HashTableT
Definition HostBuffer.h:329
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:430
uint64_t usage() const
Return the total number of bytes used from this Pool by buffers.
Definition HostBuffer.h:377
void replace(HostBuffer *buffer1, HostBuffer *buffer2)
Replaces buffer1 with buffer2 in the register.
Definition HostBuffer.h:409
Pool(uint64_t size=0, void *data=nullptr)
External memory ctor.
Definition HostBuffer.h:337
uint64_t mSize
Definition HostBuffer.h:333
void * mData
Definition HostBuffer.h:332
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:380
void remove(HostBuffer *buffer)
Remove the specified buffer from the register.
Definition HostBuffer.h:402
void * mFree
Definition HostBuffer.h:332
Pool & operator=(const Pool &&)=delete
Disallow move assignment operation.
std::mutex mMutex
Definition HostBuffer.h:330
Pool(const Pool &&)=delete
Disallow move-construction.
HashTableT mRegister
Definition HostBuffer.h:331
uint64_t mPadding
Definition HostBuffer.h:333
Pool & operator=(const Pool &)=delete
Disallow copy assignment operation.
void reset()
Reset the register and all its buffers.
Definition HostBuffer.h:417
~Pool()
Custom destructor.
Definition HostBuffer.h:358
bool isFull() const
Return true is all the memory in this pool is in use.
Definition HostBuffer.h:478
bool mManaged
Definition HostBuffer.h:334