18template <
class Key,
class Resource, std::
size_t Capacity>
32 explicit operator bool() const noexcept {
return resource !=
nullptr; }
37 , _destroyableHere(destroyableHere)
53 for (Slot& slot : _slots) {
54 if (slot.id != 0 && slot.key == key) {
55 slot.lastUse = ++_clock;
56 return {&slot.resource, slot.id};
66 Slot* target =
nullptr;
67 for (Slot& slot : _slots) {
68 if (slot.id != 0 && slot.key == key) {
74 for (Slot& slot : _slots) {
79 if (slot.pins == 0 && (!target || slot.lastUse < target->lastUse)) {
85 retire(std::move(resource), 0, 0);
88 if (target->id != 0) {
92 target->resource = std::move(resource);
93 target->id = ++_lastId;
94 target->lastUse = ++_clock;
95 return {&target->resource, target->id};
101 if (
int* pins = pinsOf(
id)) {
109 if (
int* pins = pinsOf(
id); pins && *pins > 0) {
115 template <
class Pred>
118 for (Slot& slot : _slots) {
119 if (slot.id != 0 && pred(std::as_const(slot.key), std::as_const(slot.resource))) {
127 eraseIf([](
const Key&,
const Resource&) {
return true; });
133 for (
auto it = _parked.begin(); it != _parked.end();) {
134 if (it->pins == 0 && canDestroyHere(it->resource)) {
135 _destroy(it->resource);
136 it = _parked.erase(it);
143 std::size_t
size() const noexcept
145 std::size_t live = 0;
146 for (
const Slot& slot : _slots) {
147 live += (slot.id != 0) ? 1 : 0;
153 std::size_t
parkedCount() const noexcept {
return _parked.size(); }
161 std::uint64_t lastUse = 0;
172 bool canDestroyHere(
const Resource& resource)
const {
return !_destroyableHere || _destroyableHere(resource); }
174 void retire(Resource&& resource,
EntryId id,
int pins)
176 if (pins == 0 && canDestroyHere(resource)) {
179 _parked.push_back(Parked{std::move(resource), id, pins});
183 void vacate(Slot& slot)
185 retire(std::move(slot.resource), slot.id, slot.pins);
189 int* pinsOf(
EntryId id)
noexcept
194 for (Slot& slot : _slots) {
199 for (Parked& parked : _parked) {
200 if (parked.id ==
id) {
209 std::array<Slot, Capacity> _slots{};
210 std::vector<Parked> _parked;
211 std::uint64_t _clock = 0;
GstHwImportCache(const GstHwImportCache &)=delete
bool(*)(const Resource &) CanDestroyFn
void(*)(Resource &) DestroyFn
void unpin(EntryId id) noexcept
Releases one pin(). A parked entry is destroyed by the next drainDeferred() once its last pin is gone...
~GstHwImportCache()
Static teardown: entries still pinned or not destroyable on this thread are leaked,...
Hit insert(const Key &key, Resource resource)
std::size_t size() const noexcept
void drainDeferred()
Destroys parked entries that are unpinned and destroyable on the calling thread.
void pin(EntryId id) noexcept
Keeps entry id from being destroyed until a matching unpin(); no-op for an unknown id.
Hit find(const Key &key)
MRU-promoting lookup. The resource pointer is invalidated by the next insert/eraseIf/clear on this ca...
GstHwImportCache(DestroyFn destroy, CanDestroyFn destroyableHere=nullptr)
std::size_t parkedCount() const noexcept
Entries removed from the cache but not yet destroyed (pinned, or awaiting their owner thread).
std::uint64_t EntryId
Identifies one inserted entry for pin()/unpin(), also after it is parked; never 0.
void eraseIf(Pred pred)
Removes every live entry for which pred(key, resource) holds.
GstHwImportCache & operator=(const GstHwImportCache &)=delete
Defined only in builds with the DMABuf path (QGC_HAS_GST_DMABUF_GPU_PATH) on GStreamer 1....