Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
AliasCache.hxx
Go to the documentation of this file.
1
34#ifndef _OPENLCB_ALIASCACHE_HXX_
35#define _OPENLCB_ALIASCACHE_HXX_
36
37#include "openlcb/Defs.hxx"
38#include "utils/Map.hxx"
40#include "utils/macros.h"
41
42namespace openlcb
43{
44
87{
88public:
96 AliasCache(NodeID seed, size_t _entries,
97 void (*remove_callback)(NodeID id, NodeAlias alias, void *) = NULL,
98 void *context = NULL)
99 : pool(new Metadata[_entries])
100 , aliasMap(this)
101 , idMap(this)
102 , seed(seed)
103 , entries(_entries)
104 , removeCallback(remove_callback)
106 {
107 aliasMap.reserve(_entries);
108 idMap.reserve(_entries);
109 clear();
110 }
111
113 static constexpr uint16_t NONE_ENTRY = 0xFFFFu;
114
116 void clear();
117
122 void add(NodeID id, NodeAlias alias);
123
129 void remove(NodeAlias alias);
130
136
141 NodeID lookup(NodeAlias alias);
142
148 void for_each(void (*callback)(void*, NodeID, NodeAlias), void *context);
149
151 size_t size()
152 {
153 return entries;
154 }
155
163 bool retrieve(unsigned entry, NodeID* node, NodeAlias* alias);
164
173 bool next_entry(NodeID bound, NodeID *node, NodeAlias *alias);
174
179
182 {
183 delete [] pool;
184 }
185
188
189private:
190 struct Metadata;
191 class PoolIdx;
192 friend class PoolIdx;
193
196 {
197 public:
201 {
202 }
204 bool empty()
205 {
206 return idx_ == NONE_ENTRY;
207 }
209 uint16_t idx_;
214 {
216 return parent->pool + idx_;
217 }
218 };
219
221 struct Metadata
222 {
226 {
227 nodeIdLow_ = id & 0xFFFFFFFFu;
228 nodeIdHigh_ = (id >> 32) & 0xFFFFu;
229 }
230
233 {
234 uint64_t h = nodeIdHigh_;
235 h <<= 32;
236 h |= nodeIdLow_;
237 return h;
238 }
239
241 uint32_t nodeIdLow_ = 0;
243 uint16_t nodeIdHigh_ = 0;
250 };
251
254
257 {
258 public:
262 : parent_(parent)
263 {
264 }
265
269 bool operator()(PoolIdx e, uint16_t alias) const
270 {
271 return e.deref(parent_)->alias_ < alias;
272 }
273
277 bool operator()(uint16_t alias, PoolIdx e) const
278 {
279 return alias < e.deref(parent_)->alias_;
280 }
281
285 bool operator()(PoolIdx a, PoolIdx b) const
286 {
287 return a.deref(parent_)->alias_ < b.deref(parent_)->alias_;
288 }
289
290 private:
293 };
294
297 {
298 public:
302 : parent_(parent)
303 {
304 }
305
309 bool operator()(PoolIdx e, NodeID id) const
310 {
311 return e.deref(parent_)->get_node_id() < id;
312 }
313
317 bool operator()(NodeID id, PoolIdx e) const
318 {
319 return id < e.deref(parent_)->get_node_id();
320 }
321
325 bool operator()(PoolIdx a, PoolIdx b) const
326 {
327 return a.deref(parent_)->get_node_id() <
329 }
330
331 private:
334 };
335
338
341
344
347
350
353
356
359
361 size_t entries;
362
364 void (*removeCallback)(NodeID id, NodeAlias alias, void *);
365
367 void *context;
368
372 void touch(Metadata* metadata);
373
375};
376
377} /* namespace openlcb */
378
379#endif // _OPENLCB_ALIASCACHE_HXX_
An mostly std::set<> compatible class that stores the internal data in a sorted vector.
void reserve(size_t sz)
Ensures that a given size can be reached without memory allocation.
Comparator object comparing the aliases stored in the pool.
bool operator()(uint16_t alias, PoolIdx e) const
Less-than action.
AliasComparator(AliasCache *parent)
Constructor.
AliasCache * parent_
AliasCache whose pool we are indexing into.
bool operator()(PoolIdx e, uint16_t alias) const
Less-than action.
bool operator()(PoolIdx a, PoolIdx b) const
Less-than action.
Comparator object comparing the aliases stored in the pool.
IdComparator(AliasCache *parent)
Constructor.
bool operator()(PoolIdx a, PoolIdx b) const
Less-than action.
bool operator()(PoolIdx e, NodeID id) const
Less-than action.
AliasCache * parent_
AliasCache whose pool we are indexing into.
bool operator()(NodeID id, PoolIdx e) const
Less-than action.
Encapsulation of a pointer into the pool array.
PoolIdx()
Constructor. Sets the pointer to invalid.
Metadata * deref(AliasCache *parent)
Dereferences a pool index as if it was a pointer.
uint16_t idx_
Indexes the pool of the AliasCache.
Cache of alias to node id mappings.
PoolIdx freeList
list of unused mapping entries (index into pool)
bool next_entry(NodeID bound, NodeID *node, NodeAlias *alias)
Retrieves the next entry by increasing node ID.
AliasCache(NodeID seed, size_t _entries, void(*remove_callback)(NodeID id, NodeAlias alias, void *)=NULL, void *context=NULL)
Constructor.
Metadata * pool
pointer to allocated Metadata pool
NodeAlias generate()
Generate a 12-bit pseudo-random alias for a given alias cache.
void remove(NodeAlias alias)
Remove an alias from an alias cache.
AliasMap aliasMap
Map of alias to corresponding Metadata.
void touch(Metadata *metadata)
Update the time stamp for a given entry.
size_t entries
How many metadata entries have we allocated.
~AliasCache()
Default destructor.
SortedListSet< PoolIdx, AliasComparator > AliasMap
Short hand for the alias Map type.
NodeAlias lookup(NodeID id)
Lookup a node's alias based on its Node ID.
void * context
context pointer to pass in with remove_callback
static constexpr uint16_t NONE_ENTRY
Sentinel entry for empty lists.
void for_each(void(*callback)(void *, NodeID, NodeAlias), void *context)
Call the given callback function once for each alias tracked.
PoolIdx newest
newest, most recently touched entry (index into pool)
IdMap idMap
Map of Node ID to corresponding Metadata.
void clear()
Reinitializes the entire map.
SortedListSet< PoolIdx, IdComparator > IdMap
Short hand for the ID Map type.
void(* removeCallback)(NodeID id, NodeAlias alias, void *)
callback function to be used when we remove an entry from the cache
PoolIdx oldest
oldest untouched entry (index into pool)
bool retrieve(unsigned entry, NodeID *node, NodeAlias *alias)
Retrieves an entry by index.
size_t size()
Returns the total number of aliases that can be cached.
NodeID seed
Seed for the generation of the next alias.
int check_consistency()
Visible for testing.
void add(NodeID id, NodeAlias alias)
Add an alias to an alias cache.
#define DASSERT(x)
Debug assertion facility.
Definition macros.h:159
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
uint64_t NodeID
48-bit NMRAnet Node ID type
uint16_t NodeAlias
Alias to a 48-bit NMRAnet Node ID type.
Interesting information about a given cache entry.
NodeAlias alias_
OpenLCB-CAN alias.
uint16_t nodeIdHigh_
OpenLCB Node ID high 16 bits.
void set_node_id(NodeID id)
Sets the node ID field.
PoolIdx older_
Index of next-older entry according to the LRU linked list.
PoolIdx newer_
Index of next-newer entry according to the LRU linked list.
uint32_t nodeIdLow_
OpenLCB Node ID low 32 bits.