1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 21:59:55 +00:00
openmw/components/resource/multiobjectcache.cpp

89 lines
2.5 KiB
C++
Raw Normal View History

2016-02-09 17:33:02 +00:00
#include "multiobjectcache.hpp"
#include <vector>
#include <osg/Object>
namespace Resource
{
2022-09-22 18:26:05 +00:00
MultiObjectCache::MultiObjectCache() {}
2016-02-09 17:33:02 +00:00
2022-09-22 18:26:05 +00:00
MultiObjectCache::~MultiObjectCache() {}
2016-02-09 17:33:02 +00:00
void MultiObjectCache::removeUnreferencedObjectsInCache()
{
2022-09-22 18:26:05 +00:00
std::vector<osg::ref_ptr<osg::Object>> objectsToRemove;
2016-02-09 17:33:02 +00:00
{
2020-06-25 19:46:07 +00:00
std::lock_guard<std::mutex> lock(_objectCacheMutex);
2016-02-09 17:33:02 +00:00
// Remove unreferenced entries from object cache
ObjectCacheMap::iterator oitr = _objectCache.begin();
2022-09-22 18:26:05 +00:00
while (oitr != _objectCache.end())
2016-02-09 17:33:02 +00:00
{
if (oitr->second->referenceCount() <= 1)
{
objectsToRemove.push_back(oitr->second);
_objectCache.erase(oitr++);
}
else
{
++oitr;
}
}
}
// note, actual unref happens outside of the lock
objectsToRemove.clear();
}
2017-08-21 22:58:38 +00:00
void MultiObjectCache::clear()
{
2020-06-25 19:46:07 +00:00
std::lock_guard<std::mutex> lock(_objectCacheMutex);
2017-08-21 22:58:38 +00:00
_objectCache.clear();
}
2022-09-22 18:26:05 +00:00
void MultiObjectCache::addEntryToObjectCache(const std::string& filename, osg::Object* object)
2016-02-09 17:33:02 +00:00
{
if (!object)
{
OSG_ALWAYS << " trying to add NULL object to cache for " << filename << std::endl;
return;
}
2020-06-25 19:46:07 +00:00
std::lock_guard<std::mutex> lock(_objectCacheMutex);
2016-02-09 17:33:02 +00:00
_objectCache.insert(std::make_pair(filename, object));
}
2022-09-22 18:26:05 +00:00
osg::ref_ptr<osg::Object> MultiObjectCache::takeFromObjectCache(const std::string& fileName)
2016-02-09 17:33:02 +00:00
{
2020-06-25 19:46:07 +00:00
std::lock_guard<std::mutex> lock(_objectCacheMutex);
2016-02-09 17:33:02 +00:00
ObjectCacheMap::iterator found = _objectCache.find(fileName);
if (found == _objectCache.end())
return osg::ref_ptr<osg::Object>();
else
{
osg::ref_ptr<osg::Object> object = found->second;
_objectCache.erase(found);
return object;
}
}
2022-09-22 18:26:05 +00:00
void MultiObjectCache::releaseGLObjects(osg::State* state)
2016-02-09 17:33:02 +00:00
{
2020-06-25 19:46:07 +00:00
std::lock_guard<std::mutex> lock(_objectCacheMutex);
2016-02-09 17:33:02 +00:00
2022-09-22 18:26:05 +00:00
for (ObjectCacheMap::iterator itr = _objectCache.begin(); itr != _objectCache.end(); ++itr)
2016-02-09 17:33:02 +00:00
{
osg::Object* object = itr->second.get();
object->releaseGLObjects(state);
}
}
unsigned int MultiObjectCache::getCacheSize() const
{
2020-06-25 19:46:07 +00:00
std::lock_guard<std::mutex> lock(_objectCacheMutex);
return _objectCache.size();
}
2016-02-09 17:33:02 +00:00
}