ObjRefCache.h
Go to the documentation of this file.
1 
22 #pragma once
23 
24 #include "ObjectStore.h"
25 
26 namespace IFS
27 {
28 namespace FWFS
29 {
30 #define FWFS_OBJECT_CACHE
31 
32 // Add a cache entry at regular index positions - pick a number here which is a modulus of 2
33 #define FWFS_CACHE_SPACING 8
34 #define FWFS_CACHE_MASK (FWFS_CACHE_SPACING - 1)
35 
36 #ifdef FWFS_OBJECT_CACHE
37 
41 {
42 public:
43  void initialise(Object::ID objectCount)
44  {
45  clear();
46  if(objectCount < FWFS_CACHE_SPACING) {
47  return;
48  }
49  offsetCount = (objectCount / FWFS_CACHE_SPACING) - 1;
50  offsets.reset(new uint32_t[offsetCount]);
51  if(!offsets) {
52  offsetCount = 0;
53  }
54  }
55 
56  void clear()
57  {
58  offsetCount = 0;
59  offsets.reset();
60  }
61 
62  void add(const ObjRef& ref)
63  {
64  if(ref.id && (ref.id & FWFS_CACHE_MASK) == 0) {
65  auto po = getOffset(ref.id);
66  if(po != 0) {
67  debug_d("Caching #%u @ 0x%08X", ref.id, ref.offset);
68  *po = ref.offset;
69  }
70  }
71  }
72 
73  uint32_t* getOffset(FWFS::Object::ID objIndex)
74  {
75  int pos = (objIndex / FWFS_CACHE_SPACING) - 1;
76  return (pos >= 0 && pos < offsetCount) ? &offsets[pos] : nullptr;
77  }
78 
84  void improve(ObjRef& ref, FWFS::Object::ID objID)
85  {
86  FWFS::Object::ID cachedIndex = objID & ~FWFS_CACHE_MASK;
87  if(cachedIndex <= ref.id) {
88  return;
89  }
90 
91  auto po = getOffset(cachedIndex);
92  if(po != 0) {
93  debug_d("Cache hit #%u @ 0x%08X (for #%u)", cachedIndex, *po, objID);
94  ref.id = cachedIndex;
95  ref.offset = *po;
96  }
97  }
98 
99 private:
100  std::unique_ptr<uint32_t> offsets;
101  FWFS::Object::ID offsetCount{0};
102 };
103 
104 #endif
105 
106 } // namespace FWFS
107 } // namespace IFS
void initialise(Object::ID objectCount)
Definition: ObjRefCache.h:43
#define FWFS_CACHE_MASK
Definition: ObjRefCache.h:34
Definition: DirectoryTemplate.h:36
#define debug_d
Definition: debug_progmem.h:100
void clear()
Definition: ObjRefCache.h:56
uint32_t * getOffset(FWFS::Object::ID objIndex)
Definition: ObjRefCache.h:73
void improve(ObjRef &ref, FWFS::Object::ID objID)
see if the cache can get a better search position to find an object
Definition: ObjRefCache.h:84
#define FWFS_CACHE_SPACING
Definition: ObjRefCache.h:33
gives the identity and location of an FWFS object
Definition: ObjectStore.h:32
Object::ID id
Definition: ObjectStore.h:34
uint32_t offset
Offset from start of image.
Definition: ObjectStore.h:33
void add(const ObjRef &ref)
Definition: ObjRefCache.h:62
Cache the locations of several objects to improve search speed.
Definition: ObjRefCache.h:40