27#include <unordered_map>
102 entries_.push_back({
nullptr, 0,
"", 0});
122 if (!freeList_.empty()) {
123 index = freeList_.back();
124 freeList_.pop_back();
125 gen = entries_[index].generation;
126 entries_[index] = {std::move(
resource), 1, path, gen, bytes,
false, 0, 0};
128 index =
static_cast<u32>(entries_.size());
130 entries_.push_back({std::move(
resource), 1, path, 0, bytes,
false, 0, 0});
132 residentBytes_ += bytes;
135 pathToId_[path] = handle.id();
146 template<
typename Fn>
148 for (
usize i = 1; i < entries_.size(); ++i) {
149 Entry& entry = entries_[i];
155 template<
typename Fn>
157 for (
usize i = 1; i < entries_.size(); ++i) {
158 const Entry& entry = entries_[i];
161 static_cast<const T&
>(*entry.
resource));
171 if (!handle.
isValid())
return nullptr;
173 if (idx >= entries_.size())
return nullptr;
174 auto& entry = entries_[idx];
175 if (entry.refCount == 0 || entry.generation != handle.
generation()) {
178 return entry.resource.get();
187 if (!handle.
isValid())
return nullptr;
189 if (idx >= entries_.size())
return nullptr;
190 const auto& entry = entries_[idx];
191 if (entry.refCount == 0 || entry.generation != handle.
generation()) {
194 return entry.resource.get();
203 auto it = pathToId_.find(path);
204 if (it == pathToId_.end())
return Handle<T>();
206 u32 idx = handle.index();
207 if (idx < entries_.size() && entries_[idx].generation == handle.generation()) {
221 if (idx >= entries_.size())
return;
222 auto& entry = entries_[idx];
223 if (entry.refCount > 0 && entry.generation == handle.
generation() && !path.empty()) {
224 if (!entry.path.empty()) {
225 pathToId_.erase(entry.path);
228 pathToId_[path] = handle.
id();
239 if (idx >= entries_.size())
return;
240 auto& entry = entries_[idx];
241 if (entry.generation != handle.
generation())
return;
242 if (entry.evictable) {
246 entry.evictable =
false;
248 }
else if (entry.refCount > 0) {
260 if (index >= entries_.size())
return;
261 auto& entry = entries_[index];
262 if (entry.refCount == 0 || entry.generation != gen)
return;
263 if (--entry.refCount == 0) {
264 if (budget_ == 0 || entry.path.empty()) {
273 entry.evictable =
true;
285 return entries_.size() - freeList_.size() - 1;
298 entries_.push_back({
nullptr, 0,
"", 0, 0,
false, 0, 0});
329 while (lruHead_ != 0) {
330 freeEntry_(lruHead_);
347 auto it = pathToId_.find(path);
348 if (it == pathToId_.end())
return false;
352 if (index >= entries_.size())
return true;
353 auto& entry = entries_[index];
354 if (entry.generation != gen)
return true;
356 if (entry.evictable) {
368 for (
u32 i = lruHead_; i != 0; i = entries_[i].lruNext) ++n;
374 if (!handle.
isValid())
return false;
376 if (idx >= entries_.size())
return false;
377 const auto& entry = entries_[idx];
378 return entry.generation == handle.
generation() && entry.evictable;
387 if (!handle.
isValid())
return 0;
389 if (idx >= entries_.size())
return 0;
390 const auto& entry = entries_[idx];
391 if (entry.generation != handle.
generation())
return 0;
392 return entry.refCount;
401 static const std::string empty;
402 if (!handle.
isValid())
return empty;
404 if (idx >= entries_.size())
return empty;
405 const auto& entry = entries_[idx];
406 if (entry.generation != handle.
generation())
return empty;
412 void freeEntry_(
u32 index) {
413 auto& entry = entries_[index];
414 if (entry.evictable) lruRemove_(index);
417 if (!entry.path.empty()) {
418 auto it = pathToId_.find(entry.path);
423 residentBytes_ -= entry.bytes;
424 entry.resource.reset();
427 entry.evictable =
false;
429 freeList_.push_back(index);
435 void enforceBudget_() {
436 if (budget_ == 0)
return;
437 while (residentBytes_ > budget_ && lruHead_ != 0) {
438 freeEntry_(lruHead_);
443 void lruPushBack_(
u32 index) {
444 auto& e = entries_[index];
446 e.lruPrev = lruTail_;
447 if (lruTail_ != 0) entries_[lruTail_].lruNext = index;
448 else lruHead_ = index;
453 void lruRemove_(
u32 index) {
454 auto& e = entries_[index];
455 if (e.lruPrev != 0) entries_[e.lruPrev].lruNext = e.lruNext;
456 else lruHead_ = e.lruNext;
457 if (e.lruNext != 0) entries_[e.lruNext].lruPrev = e.lruPrev;
458 else lruTail_ = e.lruPrev;
463 std::vector<Entry> entries_;
464 std::vector<u32> freeList_;
465 std::unordered_map<std::string, u32> pathToId_;
467 usize residentBytes_ = 0;
Type-safe resource handle system.
Core type definitions and utilities for ESEngine.
Type-safe resource handle.
static u32 extractIndex(IdType packed)
Extracts index from a raw packed ID.
u32 index() const
Extracts the slot index from the packed handle.
static Handle fromParts(u32 index, u32 generation)
Creates a handle from separate index and generation.
bool isValid() const
Checks if the handle references a valid resource.
IdType id() const
Gets the packed identifier (index + generation).
u32 generation() const
Extracts the generation from the packed handle.
static constexpr u32 GEN_MASK
static u32 extractGeneration(IdType packed)
Extracts generation from a raw packed ID.
Abstract base class for resource pools.
virtual ~ResourcePoolBase()=default
virtual void clear()=0
Releases all resources.
virtual usize size() const =0
Gets the number of active resources.
virtual void release(u32 id)=0
Releases a resource by ID.
Handle< T > add(Unique< T > resource, const std::string &path="", usize bytes=0)
Adds a resource to the pool.
void forEachAlive(Fn &&fn) const
usize budget() const
The current resident-byte budget (0 = caching disabled).
u32 getRefCount(Handle< T > handle) const
Gets the current reference count for a resource.
bool invalidatePath(const std::string &path)
Severs a path's cache identity so the entry can't be revived.
void clear() override
Releases all resources.
usize evictableCount() const
Number of evictable (refCount==0, cached) entries.
const T * get(Handle< T > handle) const
Gets a resource by handle (const).
bool isEvictable(Handle< T > handle) const
True if the handle's entry is alive but evictable (cached).
void setPath(Handle< T > handle, const std::string &path)
Associates a path with an existing resource.
void addRef(Handle< T > handle)
Increments the reference count for a resource.
Handle< T > findByPath(const std::string &path) const
Finds a resource by its cached path.
usize trimEvictables()
Frees every evictable (refCount==0) cache entry immediately.
ResourcePool(const ResourcePool &)=delete
const std::string & getPath(Handle< T > handle) const
Gets the cached path for a resource.
ResourcePool(ResourcePool &&)=default
void setBudget(usize bytes)
Sets the resident-byte budget. 0 (default) disables caching — a resource is freed the instant its ref...
usize size() const override
Gets the number of active resources.
ResourcePool & operator=(const ResourcePool &)=delete
T * get(Handle< T > handle)
Gets a resource by handle.
usize residentBytes() const
Bytes currently resident (held + evictable entries).
ResourcePool & operator=(ResourcePool &&)=default
void release(u32 id) override
Decrements the reference count and frees if zero.
void forEachAlive(Fn &&fn)
Visits every live resource, with the handle naming it.
~ResourcePool() override=default
std::size_t usize
Size type for indexing and sizes.
std::unique_ptr< T > Unique
Unique ownership smart pointer.
std::uint32_t u32
Unsigned 32-bit integer.
Entry storing a resource with metadata.
std::string path
Optional path for caching.
usize bytes
Resource size, for the eviction budget (0 = untracked).
bool evictable
refCount==0 but retained as a cache entry (in the LRU)
u32 generation
Reuse counter for stale handle detection.
u32 lruNext
Intrusive LRU links (entry index; 0 = sentinel/none).
u32 lruPrev
Intrusive LRU links (entry index; 0 = sentinel/none).
Unique< T > resource
The owned resource.
u32 refCount
Reference count (0 = freed or evictable).