QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstHwImportCache.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <utility>
7#include <vector>
8
9namespace GstHw {
10
18template <class Key, class Resource, std::size_t Capacity>
20{
21public:
22 using DestroyFn = void (*)(Resource&);
23 using CanDestroyFn = bool (*)(const Resource&);
25 using EntryId = std::uint64_t;
26
27 struct Hit
28 {
29 Resource* resource = nullptr;
30 EntryId id = 0;
31
32 explicit operator bool() const noexcept { return resource != nullptr; }
33 };
34
35 explicit GstHwImportCache(DestroyFn destroy, CanDestroyFn destroyableHere = nullptr)
36 : _destroy(destroy)
37 , _destroyableHere(destroyableHere)
38 {}
39
42 {
43 clear();
45 }
46
49
51 Hit find(const Key& key)
52 {
53 for (Slot& slot : _slots) {
54 if (slot.id != 0 && slot.key == key) {
55 slot.lastUse = ++_clock;
56 return {&slot.resource, slot.id};
57 }
58 }
59 return {};
60 }
61
64 Hit insert(const Key& key, Resource resource)
65 {
66 Slot* target = nullptr;
67 for (Slot& slot : _slots) {
68 if (slot.id != 0 && slot.key == key) {
69 target = &slot;
70 break;
71 }
72 }
73 if (!target) {
74 for (Slot& slot : _slots) {
75 if (slot.id == 0) {
76 target = &slot;
77 break;
78 }
79 if (slot.pins == 0 && (!target || slot.lastUse < target->lastUse)) {
80 target = &slot;
81 }
82 }
83 }
84 if (!target) {
85 retire(std::move(resource), 0, 0);
86 return {};
87 }
88 if (target->id != 0) {
89 vacate(*target);
90 }
91 target->key = key;
92 target->resource = std::move(resource);
93 target->id = ++_lastId;
94 target->lastUse = ++_clock;
95 return {&target->resource, target->id};
96 }
97
99 void pin(EntryId id) noexcept
100 {
101 if (int* pins = pinsOf(id)) {
102 ++*pins;
103 }
104 }
105
107 void unpin(EntryId id) noexcept
108 {
109 if (int* pins = pinsOf(id); pins && *pins > 0) {
110 --*pins;
111 }
112 }
113
115 template <class Pred>
116 void eraseIf(Pred pred)
117 {
118 for (Slot& slot : _slots) {
119 if (slot.id != 0 && pred(std::as_const(slot.key), std::as_const(slot.resource))) {
120 vacate(slot);
121 }
122 }
123 }
124
125 void clear()
126 {
127 eraseIf([](const Key&, const Resource&) { return true; });
128 }
129
132 {
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);
137 } else {
138 ++it;
139 }
140 }
141 }
142
143 std::size_t size() const noexcept
144 {
145 std::size_t live = 0;
146 for (const Slot& slot : _slots) {
147 live += (slot.id != 0) ? 1 : 0;
148 }
149 return live;
150 }
151
153 std::size_t parkedCount() const noexcept { return _parked.size(); }
154
155private:
156 struct Slot
157 {
158 Key key{};
159 Resource resource{};
160 EntryId id = 0;
161 std::uint64_t lastUse = 0;
162 int pins = 0;
163 };
164
165 struct Parked
166 {
167 Resource resource;
168 EntryId id = 0;
169 int pins = 0;
170 };
171
172 bool canDestroyHere(const Resource& resource) const { return !_destroyableHere || _destroyableHere(resource); }
173
174 void retire(Resource&& resource, EntryId id, int pins)
175 {
176 if (pins == 0 && canDestroyHere(resource)) {
177 _destroy(resource);
178 } else {
179 _parked.push_back(Parked{std::move(resource), id, pins});
180 }
181 }
182
183 void vacate(Slot& slot)
184 {
185 retire(std::move(slot.resource), slot.id, slot.pins);
186 slot = Slot{};
187 }
188
189 int* pinsOf(EntryId id) noexcept
190 {
191 if (id == 0) {
192 return nullptr;
193 }
194 for (Slot& slot : _slots) {
195 if (slot.id == id) {
196 return &slot.pins;
197 }
198 }
199 for (Parked& parked : _parked) {
200 if (parked.id == id) {
201 return &parked.pins;
202 }
203 }
204 return nullptr;
205 }
206
207 DestroyFn _destroy;
208 CanDestroyFn _destroyableHere;
209 std::array<Slot, Capacity> _slots{};
210 std::vector<Parked> _parked;
211 std::uint64_t _clock = 0;
212 EntryId _lastId = 0;
213};
214
215} // namespace GstHw
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....