Files
Bret Curtis 4addabaf95 Let the resource cache be told what it stores, and free its values from OSG
GenericObjectCache stored osg::ref_ptr<osg::Object>, so everything cached had to
derive from osg::Object whether that meant anything or not: NifFileHolder and
BgsmFileHolder existed for no other reason, Resource::BulletShape and
ESMTerrain::LandObject carried META_Object and a CopyOp constructor nothing ever
called, and every getter paid a static_cast on the way out. The cache and
MultiObjectCache now take their value type as a template parameter, ask "is
anything else holding this?" through a useCount overload rather than a base class
method, and constrain accept and releaseGLObjects so a value type with neither is
not an error. Both holders are deleted; BulletShape, BulletShapeInstance and
LandObject become plain classes held by std::shared_ptr across physics,
pathfinding, navmeshtool and terrain storage; MWPhysics::HeightField's
type-erased osg::Object keep-alive becomes the LandObject it was always holding;
every remaining manager names the type it actually caches through the
NodeResourceManager alias, which removes eight static_casts and stops
clang-format wrapping the declarations. The preloader needs two keep-alive sets
for now, because the render-owned entries are still osg::Object; they merge once
those move behind the render backend.
2026-09-18 16:10:18 +02:00

121 lines
3.5 KiB
C++

#ifndef OPENMW_COMPONENTS_MULTIOBJECTCACHE_H
#define OPENMW_COMPONENTS_MULTIOBJECTCACHE_H
#include <map>
#include <mutex>
#include <utility>
#include <vector>
#include <components/debug/debuglog.hpp>
#include <components/vfs/pathutil.hpp>
#include "cachestats.hpp"
#include "objectcache.hpp"
namespace osg
{
class State;
}
namespace Resource
{
/// @brief Cache for "non reusable" objects. Templated on its value type for the
/// same reason GenericObjectCache is; see objectcache.hpp.
template <class ValueType>
class MultiObjectCache
{
public:
void removeUnreferencedObjectsInCache()
{
std::vector<ValueType> objectsToRemove;
{
std::lock_guard<std::mutex> lock(mObjectCacheMutex);
auto oitr = mObjectCache.begin();
while (oitr != mObjectCache.end())
{
if (useCount(oitr->second) <= 1)
{
objectsToRemove.push_back(oitr->second);
mObjectCache.erase(oitr++);
++mExpired;
}
else
{
++oitr;
}
}
}
// note, actual release happens outside of the lock
objectsToRemove.clear();
}
/** Remove all objects from the cache. */
void clear()
{
std::lock_guard<std::mutex> lock(mObjectCacheMutex);
mObjectCache.clear();
}
void addEntryToObjectCache(VFS::Path::NormalizedView filename, ValueType object)
{
if (object == nullptr)
{
Log(Debug::Warning) << "Trying to add NULL object to cache for " << filename;
return;
}
std::lock_guard<std::mutex> lock(mObjectCacheMutex);
mObjectCache.emplace(filename, std::move(object));
}
/** Take an object from cache. Return nullptr if no object found. */
ValueType takeFromObjectCache(VFS::Path::NormalizedView fileName)
{
std::lock_guard<std::mutex> lock(mObjectCacheMutex);
++mGet;
const auto it = mObjectCache.find(fileName);
if (it != mObjectCache.end())
{
ValueType object = std::move(it->second);
mObjectCache.erase(it);
++mHit;
return object;
}
return ValueType();
}
/** call releaseGLObjects on all objects attached to the object cache. */
void releaseGLObjects(osg::State* state) requires ReleasesGLObjects<ValueType>
{
std::lock_guard<std::mutex> lock(mObjectCacheMutex);
for (const auto& [key, value] : mObjectCache)
value->releaseGLObjects(state);
}
CacheStats getStats() const
{
std::lock_guard<std::mutex> lock(mObjectCacheMutex);
return CacheStats{
.mSize = mObjectCache.size(),
.mGet = mGet,
.mHit = mHit,
.mExpired = mExpired,
};
}
protected:
using ObjectCacheMap = std::multimap<VFS::Path::Normalized, ValueType, std::less<>>;
ObjectCacheMap mObjectCache;
mutable std::mutex mObjectCacheMutex;
std::size_t mGet = 0;
std::size_t mHit = 0;
std::size_t mExpired = 0;
};
}
#endif