1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-17 01:10:10 +00:00
OpenMW/components/resource/multiobjectcache.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

92 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
{
void MultiObjectCache::removeUnreferencedObjectsInCache()
{
std::vector<osg::ref_ptr<osg::Object>> objectsToRemove;
{
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();
while (oitr != _objectCache.end())
{
if (oitr->second->referenceCount() <= 1)
{
objectsToRemove.push_back(oitr->second);
_objectCache.erase(oitr++);
2023-12-21 23:23:49 +00:00
++mExpired;
2016-02-09 17:33:02 +00:00
}
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();
}
void MultiObjectCache::addEntryToObjectCache(VFS::Path::NormalizedView 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);
_objectCache.emplace(filename, object);
2016-02-09 17:33:02 +00:00
}
osg::ref_ptr<osg::Object> MultiObjectCache::takeFromObjectCache(VFS::Path::NormalizedView fileName)
2016-02-09 17:33:02 +00:00
{
2020-06-25 19:46:07 +00:00
std::lock_guard<std::mutex> lock(_objectCacheMutex);
2023-12-21 23:23:49 +00:00
++mGet;
const auto it = _objectCache.find(fileName);
if (it != _objectCache.end())
2016-02-09 17:33:02 +00:00
{
osg::ref_ptr<osg::Object> object = std::move(it->second);
_objectCache.erase(it);
2023-12-21 23:23:49 +00:00
++mHit;
2016-02-09 17:33:02 +00:00
return object;
}
return nullptr;
2016-02-09 17:33:02 +00:00
}
void MultiObjectCache::releaseGLObjects(osg::State* state)
{
2020-06-25 19:46:07 +00:00
std::lock_guard<std::mutex> lock(_objectCacheMutex);
2016-02-09 17:33:02 +00:00
for (ObjectCacheMap::iterator itr = _objectCache.begin(); itr != _objectCache.end(); ++itr)
{
osg::Object* object = itr->second.get();
object->releaseGLObjects(state);
}
}
2023-12-21 23:23:49 +00:00
CacheStats MultiObjectCache::getStats() const
{
2020-06-25 19:46:07 +00:00
std::lock_guard<std::mutex> lock(_objectCacheMutex);
2023-12-21 23:23:49 +00:00
return CacheStats{
.mSize = _objectCache.size(),
.mGet = mGet,
.mHit = mHit,
.mExpired = mExpired,
};
}
2016-02-09 17:33:02 +00:00
}