24#ifndef PXR_USD_SDF_POOL_H
25#define PXR_USD_SDF_POOL_H
28#include "pxr/usd/sdf/api.h"
36#include <tbb/concurrent_queue.h>
41PXR_NAMESPACE_OPEN_SCOPE
47struct Sdf_FastThreadLocalBase
50 static thread_local T *theTPtr =
nullptr;
51 if (ARCH_LIKELY(theTPtr)) {
54 static thread_local T theT;
78 unsigned ElemsPerSpan=16384>
81 static_assert(ElemSize >=
sizeof(uint32_t),
82 "ElemSize must be at least sizeof(uint32_t)");
86 static constexpr uint64_t ElemsPerRegion = 1ull << (32-RegionBits);
89 static constexpr uint32_t MaxIndex = ElemsPerRegion - 1;
92 static constexpr uint32_t RegionMask = ((1 << RegionBits)-1);
98 constexpr Handle() noexcept = default;
99 constexpr Handle(
std::nullptr_t) noexcept : value(0) {}
100 Handle(
unsigned region, uint32_t index)
101 : value((index << RegionBits) | region) {}
102 Handle &operator=(Handle
const &) =
default;
103 Handle &operator=(std::nullptr_t) {
return *
this = Handle(); }
104 inline char *GetPtr() const noexcept {
106 ARCH_PRAGMA_MAYBE_UNINITIALIZED
107 return Sdf_Pool::_GetPtr(value & RegionMask, value >> RegionBits);
110 static inline Handle GetHandle(
char const *ptr)
noexcept {
111 return Sdf_Pool::_GetHandle(ptr);
113 explicit operator bool()
const {
116 inline bool operator ==(Handle
const &r)
const noexcept {
117 return value == r.value;
119 inline bool operator !=(Handle
const &r)
const noexcept {
120 return value != r.value;
122 inline bool operator <(Handle
const &r)
const noexcept {
123 return value < r.value;
125 inline void swap(Handle &r)
noexcept {
126 std::swap(value, r.value);
135 char *p = head.GetPtr();
136 Handle *hp =
reinterpret_cast<Handle *
>(p);
140 inline void Push(Handle h) {
142 char *p = h.GetPtr();
143 Handle *hp =
reinterpret_cast<Handle *
>(p);
156 size_t size()
const {
return endIndex - beginIndex; }
157 inline Handle Alloc() {
158 return Handle(region, beginIndex++);
160 inline bool empty()
const {
161 return beginIndex == endIndex;
168 struct _PerThreadData {
179 struct _RegionState {
180 static constexpr uint32_t LockedState = ~0;
182 _RegionState() =
default;
183 constexpr _RegionState(
unsigned region, uint32_t index)
184 : _state((index << RegionBits) | region) {}
188 inline _RegionState Reserve(
unsigned num)
const;
190 static constexpr _RegionState GetInitState() {
191 return _RegionState(0, 0);
194 static constexpr _RegionState GetLockedState() {
195 return _RegionState(LockedState, LockedState);
198 constexpr bool operator==(_RegionState other)
const {
199 return _state == other._state;
202 uint32_t GetIndex()
const {
203 return _state >> RegionBits;
206 unsigned GetRegion()
const {
207 return _state & RegionMask;
210 bool IsLocked()
const {
211 return _state == LockedState;
219 static inline Handle Allocate();
220 static inline void Free(Handle h);
225 static inline char *_GetPtr(
unsigned region, uint32_t index) {
230 ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE
231 return _regionStarts[region] + (index * ElemSize);
237 static inline Handle _GetHandle(
char const *ptr) {
239 for (
unsigned region = 1; region != NumRegions+1; ++region) {
244 ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE
245 uintptr_t start = (uintptr_t)_regionStarts[region];
250 uintptr_t diff = (uintptr_t)ptr - start;
251 if (diff < (uintptr_t)(ElemsPerRegion*ElemSize)) {
253 region,
static_cast<uint32_t
>(diff / ElemSize));
261 static bool _TakeSharedFreeList(_FreeList &out) {
262 return _sharedFreeLists->try_pop(out);
266 static void _ShareFreeList(_FreeList &in) {
267 _sharedFreeLists->push(in);
272 static inline void _ReserveSpan(_PoolSpan &out);
274 static constexpr int NumRegions = 1 << RegionBits;
276 using _ThreadData = Sdf_FastThreadLocalBase<_PerThreadData>;
277 SDF_API
static _ThreadData _threadData;
278 SDF_API
static char *_regionStarts[NumRegions+1];
279 SDF_API
static std::atomic<_RegionState> _regionState;
283PXR_NAMESPACE_CLOSE_SCOPE
Low-level utilities for informing users of various internal and external diagnostic conditions.
Create or return a previously created object instance of global data.
Demangle C++ typenames generated by the typeid() facility.
Pragmas for controlling compiler-specific behaviors.