29 #include "DeviceObject.h" 30 #include <unordered_map> 31 #include "STDAllocator.h" 57 template<
typename ResourceDescType>
65 m_RegistryName( RegistryName ),
66 m_DescToObjHashMap(STD_ALLOCATOR_RAW_MEM(HashMapElem, RawAllocator,
"Allocator for unordered_map<ResourceDescType, RefCntWeakPtr<IDeviceObject> >") )
77 VERIFY( m_DescToObjHashMap.empty(),
"DescToObjHashMap is not empty" );
93 ThreadingTools::LockHelper Lock( m_LockFlag );
101 m_NumDeletedObjects = 0;
121 VERIFY( Elems.first->first == ObjectDesc,
"Incorrect object description" );
122 LOG_WARNING_MESSAGE(
"Object named \"", Elems.first->first.Name,
"\" with the same description already exists in the registry." 123 "Replacing with the new object named \"", ObjectDesc.Name ? ObjectDesc.Name :
"",
"\".");
124 Elems.first->second = pObject;
131 VERIFY( *ppObject ==
nullptr,
"Overwriting reference to existing object may cause memory leaks" );
133 ThreadingTools::LockHelper Lock( m_LockFlag );
135 auto It = m_DescToObjHashMap.find( Desc );
136 if( It != m_DescToObjHashMap.end() )
142 auto pObject = It->second.Lock();
145 *ppObject = pObject.Detach();
146 LOG_INFO_MESSAGE(
"Equivalent of the requested state object named \"", Desc.Name ? Desc.Name :
"",
"\" found in the ", m_RegistryName,
" registry. Reusing existing object.");
151 m_DescToObjHashMap.erase(It);
152 Atomics::AtomicDecrement(m_NumDeletedObjects);
160 Uint32 NumPurgedObjects = 0;
161 auto It = m_DescToObjHashMap.begin();
162 while( It != m_DescToObjHashMap.end() )
174 if( !It->second.IsValid() )
176 m_DescToObjHashMap.erase( It );
182 LOG_INFO_MESSAGE(
"Purged ", NumPurgedObjects,
" deleted objects from the ", m_RegistryName,
" registry" );
190 Atomics::AtomicIncrement(m_NumDeletedObjects);
195 ThreadingTools::LockFlag m_LockFlag;
198 Atomics::AtomicLong m_NumDeletedObjects;
201 typedef std::pair< const ResourceDescType, RefCntWeakPtr<IDeviceObject> > HashMapElem;
202 std::unordered_map<ResourceDescType, RefCntWeakPtr<IDeviceObject>, std::hash<ResourceDescType>, std::equal_to<ResourceDescType>, STDAllocatorRawMem<HashMapElem> > m_DescToObjHashMap;
205 const String m_RegistryName;
Base interface for all objects created by the render device Diligent::IRenderDevice.
Definition: DeviceObject.h:40
static constexpr int DeletedObjectsToPurge
Number of outstanding deleted objects to purge the registry.
Definition: StateObjectsRegistry.h:62
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Template class implementing state object registry.
Definition: StateObjectsRegistry.h:58
Implementation of weak pointers.
Definition: RefCntAutoPtr.h:38
void Find(const ResourceDescType &Desc, IDeviceObject **ppObject)
Finds the object in the registry.
Definition: StateObjectsRegistry.h:129
void Add(const ResourceDescType &ObjectDesc, IDeviceObject *pObject)
Adds a new object to the registry.
Definition: StateObjectsRegistry.h:91
void ReportDeletedObject()
Increments the number of outstanding deleted objects. When this number reaches DeletedObjectsToPurge...
Definition: StateObjectsRegistry.h:188
void Purge()
Purges outstanding deleted objects from the registry.
Definition: StateObjectsRegistry.h:158