7#ifndef PXR_USD_SDF_POOL_H
8#define PXR_USD_SDF_POOL_H
11#include "pxr/usd/sdf/api.h"
19#include <tbb/concurrent_queue.h>
24PXR_NAMESPACE_OPEN_SCOPE
30struct Sdf_FastThreadLocalBase
33 static thread_local T *theTPtr =
nullptr;
34 if (ARCH_LIKELY(theTPtr)) {
37 static thread_local T theT;
61 unsigned ElemsPerSpan=16384>
64 static_assert(ElemSize >=
sizeof(uint32_t),
65 "ElemSize must be at least sizeof(uint32_t)");
69 static constexpr uint64_t ElemsPerRegion = 1ull << (32-RegionBits);
72 static constexpr uint32_t MaxIndex = ElemsPerRegion - 1;
75 static constexpr uint32_t RegionMask = ((1 << RegionBits)-1);
81 constexpr Handle() noexcept = default;
82 constexpr Handle(
std::nullptr_t) noexcept : value(0) {}
83 Handle(
unsigned region, uint32_t index)
84 : value((index << RegionBits) | region) {}
85 Handle &operator=(Handle
const &) =
default;
86 Handle &operator=(std::nullptr_t) {
return *
this = Handle(); }
87 inline char *GetPtr() const noexcept {
89 ARCH_PRAGMA_MAYBE_UNINITIALIZED
90 return Sdf_Pool::_GetPtr(value & RegionMask, value >> RegionBits);
93 static inline Handle GetHandle(
char const *ptr)
noexcept {
94 return Sdf_Pool::_GetHandle(ptr);
96 explicit operator bool()
const {
99 inline bool operator ==(Handle
const &r)
const noexcept {
100 return value == r.value;
102 inline bool operator !=(Handle
const &r)
const noexcept {
103 return value != r.value;
105 inline bool operator <(Handle
const &r)
const noexcept {
106 return value < r.value;
108 inline void swap(Handle &r)
noexcept {
109 std::swap(value, r.value);
118 char *p = head.GetPtr();
119 Handle *hp =
reinterpret_cast<Handle *
>(p);
123 inline void Push(Handle h) {
125 char *p = h.GetPtr();
126 Handle *hp =
reinterpret_cast<Handle *
>(p);
139 size_t size()
const {
return endIndex - beginIndex; }
140 inline Handle Alloc() {
141 return Handle(region, beginIndex++);
143 inline bool empty()
const {
144 return beginIndex == endIndex;
151 struct _PerThreadData {
162 struct _RegionState {
163 static constexpr uint32_t LockedState = ~0;
165 _RegionState() =
default;
166 constexpr _RegionState(
unsigned region, uint32_t index)
167 : _state((index << RegionBits) | region) {}
171 inline _RegionState Reserve(
unsigned num)
const;
173 static constexpr _RegionState GetInitState() {
174 return _RegionState(0, 0);
177 static constexpr _RegionState GetLockedState() {
178 return _RegionState(LockedState, LockedState);
181 constexpr bool operator==(_RegionState other)
const {
182 return _state == other._state;
185 uint32_t GetIndex()
const {
186 return _state >> RegionBits;
189 unsigned GetRegion()
const {
190 return _state & RegionMask;
193 bool IsLocked()
const {
194 return _state == LockedState;
202 static inline Handle Allocate();
203 static inline void Free(Handle h);
208 static inline char *_GetPtr(
unsigned region, uint32_t index) {
213 ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE
214 return _regionStarts[region] + (index * ElemSize);
220 static inline Handle _GetHandle(
char const *ptr) {
222 for (
unsigned region = 1; region != NumRegions+1; ++region) {
227 ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE
228 uintptr_t start = (uintptr_t)_regionStarts[region];
233 uintptr_t diff = (uintptr_t)ptr - start;
234 if (diff < (uintptr_t)(ElemsPerRegion*ElemSize)) {
236 region,
static_cast<uint32_t
>(diff / ElemSize));
244 static bool _TakeSharedFreeList(_FreeList &out) {
245 return _sharedFreeLists->try_pop(out);
249 static void _ShareFreeList(_FreeList &in) {
250 _sharedFreeLists->push(in);
255 static inline void _ReserveSpan(_PoolSpan &out);
257 static constexpr int NumRegions = 1 << RegionBits;
259 using _ThreadData = Sdf_FastThreadLocalBase<_PerThreadData>;
260 SDF_API
static _ThreadData _threadData;
261 SDF_API
static char *_regionStarts[NumRegions+1];
262 SDF_API
static std::atomic<_RegionState> _regionState;
266PXR_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.