<ahref="lru_8c.html">Go to the documentation of this file.</a><divclass="fragment"><divclass="line"><aid="l00001"name="l00001"></a><spanclass="lineno"> 1</span><spanclass="preprocessor">#include <stddef.h></span></div>
<divclass="line"><aid="l00008"name="l00008"></a><spanclass="lineno"> 8</span><spanclass="comment">// LRU cache using uthash with native integer keys.</span></div>
<divclass="line"><aid="l00009"name="l00009"></a><spanclass="lineno"> 9</span><spanclass="comment">// Based on uthash LRU example by Jehiah Czebotar 2011 (public domain).</span></div>
<divclass="line"><aid="l00010"name="l00010"></a><spanclass="lineno"> 10</span><spanclass="comment">// Rewritten to use HASH_FIND/HASH_ADD with uint64_t keys directly,</span></div>
<divclass="line"><aid="l00011"name="l00011"></a><spanclass="lineno"> 11</span><spanclass="comment">// eliminating all string conversion, malloc, and snprintf overhead</span></div>
<divclass="line"><aid="l00012"name="l00012"></a><spanclass="lineno"> 12</span><spanclass="comment">// from the hot path.</span></div>
<divclass="line"><aid="l00047"name="l00047"></a><spanclass="lineno"> 47</span><spanclass="comment">// Remove and re-add to move to front of insertion-order list (LRU refresh).</span></div>
<divclass="line"><aid="l00072"name="l00072"></a><spanclass="lineno"> 72</span><spanclass="comment">// Replace an existing entry for this key. A blind HASH_ADD would leave the old entry in the</span></div>
<divclass="line"><aid="l00073"name="l00073"></a><spanclass="lineno"> 73</span><spanclass="comment">// table unreachable by HASH_FIND, and its value alive until the whole cache is freed.</span></div>
<divclass="line"><aid="l00098"name="l00098"></a><spanclass="lineno"> 98</span><spanclass="comment">// Evict least recently used entries until back under budget. Never evict the entry just</span></div>
<divclass="line"><aid="l00099"name="l00099"></a><spanclass="lineno"> 99</span><spanclass="comment">// inserted: the caller still uses that pointer after this returns.</span></div>
<divclass="ttc"id="alru_8c_html_a112d9186077b4421f4fa0f9c6c76425e"><divclass="ttname"><ahref="lru_8c.html#a112d9186077b4421f4fa0f9c6c76425e">drop_entry</a></div><divclass="ttdeci">static void drop_entry(struct CacheHeader *cache, struct CacheEntry *entry)</div><divclass="ttdoc">Frees a cache entry's value and unlinks it from the cache.</div><divclass="ttdef"><b>Definition</b><ahref="#l00020">lru.c:20</a></div></div>
<divclass="ttc"id="alru_8h_html_a4c0693df23b76b54750218753a748223"><divclass="ttname"><ahref="lru_8h.html#a4c0693df23b76b54750218753a748223">add_to_cache_uint64</a></div><divclass="ttdeci">void add_to_cache_uint64(struct CacheHeader *cache, uint64_t key, void *value, size_t size)</div><divclass="ttdoc">Adds a value to the cache with a uint64_t key, evicting LRU entries if over budget.</div><divclass="ttdef"><b>Definition</b><ahref="#l00068">lru.c:68</a></div></div>
<divclass="ttc"id="alru_8h_html_a8cb7ac8e057627210732ee61d2966e35"><divclass="ttname"><ahref="lru_8h.html#a8cb7ac8e057627210732ee61d2966e35">find_in_cache_uint64</a></div><divclass="ttdeci">void * find_in_cache_uint64(struct CacheHeader *cache, uint64_t key)</div><divclass="ttdoc">Finds a value in the cache by uint64_t key.</div><divclass="ttdef"><b>Definition</b><ahref="#l00041">lru.c:41</a></div></div>
<divclass="ttc"id="alru_8h_html_a92e298695411672c3497ae5c8d9c0427"><divclass="ttname"><ahref="lru_8h.html#a92e298695411672c3497ae5c8d9c0427">free_cache</a></div><divclass="ttdeci">void free_cache(struct CacheHeader *cache)</div><divclass="ttdoc">Frees all entries in the cache and clears it.</div><divclass="ttdef"><b>Definition</b><ahref="#l00118">lru.c:118</a></div></div>
<divclass="ttc"id="astructCacheEntry_html"><divclass="ttname"><ahref="structCacheEntry.html">CacheEntry</a></div><divclass="ttdoc">Single hash entry in the in-memory cache.</div><divclass="ttdef"><b>Definition</b><ahref="lru_8h_source.html#l00024">lru.h:25</a></div></div>
<divclass="ttc"id="astructCacheEntry_html_a7d6d1ed0aab03cffbb34aff16f6f3d56"><divclass="ttname"><ahref="structCacheEntry.html#a7d6d1ed0aab03cffbb34aff16f6f3d56">CacheEntry::value</a></div><divclass="ttdeci">void * value</div><divclass="ttdoc">Opaque value pointer associated with key.</div><divclass="ttdef"><b>Definition</b><ahref="lru_8h_source.html#l00027">lru.h:27</a></div></div>
<divclass="ttc"id="astructCacheEntry_html_ab7bea8f5ce4dc7ace006ccabd45a0d1f"><divclass="ttname"><ahref="structCacheEntry.html#ab7bea8f5ce4dc7ace006ccabd45a0d1f">CacheEntry::size</a></div><divclass="ttdeci">size_t size</div><divclass="ttdoc">Size in bytes of the memory pointed to by value.</div><divclass="ttdef"><b>Definition</b><ahref="lru_8h_source.html#l00028">lru.h:28</a></div></div>
<divclass="ttc"id="astructCacheEntry_html_acee4cc303db10f54cfe2314142f24428"><divclass="ttname"><ahref="structCacheEntry.html#acee4cc303db10f54cfe2314142f24428">CacheEntry::key</a></div><divclass="ttdeci">uint64_t key</div><divclass="ttdoc">64-bit integer key (unique within the cache).</div><divclass="ttdef"><b>Definition</b><ahref="lru_8h_source.html#l00026">lru.h:26</a></div></div>
<divclass="ttc"id="astructCacheEntry_html_ae7e5f03fa52b88d99e27e2433623b4f5"><divclass="ttname"><ahref="structCacheEntry.html#ae7e5f03fa52b88d99e27e2433623b4f5">CacheEntry::hh</a></div><divclass="ttdeci">UT_hash_handle hh</div><divclass="ttdoc">uthash handle (must remain per uthash docs).</div><divclass="ttdef"><b>Definition</b><ahref="lru_8h_source.html#l00029">lru.h:29</a></div></div>
<divclass="ttc"id="astructCacheHeader_html"><divclass="ttname"><ahref="structCacheHeader.html">CacheHeader</a></div><divclass="ttdoc">Cache top-level descriptor encapsulating the hash table root and capacity limit.</div><divclass="ttdef"><b>Definition</b><ahref="lru_8h_source.html#l00050">lru.h:51</a></div></div>
<divclass="ttc"id="astructCacheHeader_html_a44229bb929d8949f3c2700d07123d224"><divclass="ttname"><ahref="structCacheHeader.html#a44229bb929d8949f3c2700d07123d224">CacheHeader::cache</a></div><divclass="ttdeci">struct CacheEntry * cache</div><divclass="ttdoc">Hash root (uthash). NULL when empty.</div><divclass="ttdef"><b>Definition</b><ahref="lru_8h_source.html#l00054">lru.h:54</a></div></div>
<divclass="ttc"id="astructCacheHeader_html_a54d553e78f5d90cce34ee684829a410f"><divclass="ttname"><ahref="structCacheHeader.html#a54d553e78f5d90cce34ee684829a410f">CacheHeader::free_func</a></div><divclass="ttdeci">void(*) free_func(void *)</div><divclass="ttdoc">Optional callback to free cached values. NULL if not needed.</div><divclass="ttdef"><b>Definition</b><ahref="lru_8h_source.html#l00055">lru.h:55</a></div></div>
<divclass="ttc"id="astructCacheHeader_html_ae6521ccf8e75b0c9c70297af637b1eaa"><divclass="ttname"><ahref="structCacheHeader.html#ae6521ccf8e75b0c9c70297af637b1eaa">CacheHeader::cur_bytes</a></div><divclass="ttdeci">uint64_t cur_bytes</div><divclass="ttdoc">Bytes currently held by the cached values.</div><divclass="ttdef"><b>Definition</b><ahref="lru_8h_source.html#l00053">lru.h:53</a></div></div>
<divclass="ttc"id="astructCacheHeader_html_af82718ebf5e59a398bc6e9428a389bb2"><divclass="ttname"><ahref="structCacheHeader.html#af82718ebf5e59a398bc6e9428a389bb2">CacheHeader::max_bytes</a></div><divclass="ttdeci">uint64_t max_bytes</div><divclass="ttdoc">Memory budget in bytes for the cached values. 0 means unlimited.</div><divclass="ttdef"><b>Definition</b><ahref="lru_8h_source.html#l00052">lru.h:52</a></div></div>
<liclass="footer">Generated by <ahref="https://www.doxygen.org/index.html"><imgclass="footer"src="doxygen.svg"width="104"height="31"alt="doxygen"/></a> 1.18.0 </li>