2022-05-19 01:51:26 -07:00
|
|
|
#ifndef CACHE_H
|
|
|
|
#define CACHE_H
|
|
|
|
|
2022-12-08 12:05:50 -08:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2022-05-19 01:51:26 -07:00
|
|
|
#include "heap.h"
|
|
|
|
|
2022-09-23 05:43:44 -07:00
|
|
|
namespace fallout {
|
|
|
|
|
2022-05-19 01:51:26 -07:00
|
|
|
#define INVALID_CACHE_ENTRY ((CacheEntry*)-1)
|
|
|
|
|
|
|
|
typedef enum CacheEntryFlags {
|
|
|
|
// Specifies that cache entry has no references as should be evicted during
|
|
|
|
// the next sweep operation.
|
|
|
|
CACHE_ENTRY_MARKED_FOR_EVICTION = 0x01,
|
|
|
|
} CacheEntryFlags;
|
|
|
|
|
|
|
|
typedef int CacheSizeProc(int key, int* sizePtr);
|
|
|
|
typedef int CacheReadProc(int key, int* sizePtr, unsigned char* buffer);
|
|
|
|
typedef void CacheFreeProc(void* ptr);
|
|
|
|
|
|
|
|
typedef struct CacheEntry {
|
|
|
|
int key;
|
|
|
|
int size;
|
|
|
|
unsigned char* data;
|
|
|
|
unsigned int referenceCount;
|
|
|
|
|
|
|
|
// Total number of hits that this cache entry received during it's
|
|
|
|
// lifetime.
|
|
|
|
unsigned int hits;
|
|
|
|
|
2022-05-21 08:22:03 -07:00
|
|
|
unsigned int flags;
|
2022-05-19 01:51:26 -07:00
|
|
|
|
|
|
|
// The most recent hit in terms of cache hit counter. Used to track most
|
|
|
|
// recently used entries in eviction strategy.
|
|
|
|
unsigned int mru;
|
|
|
|
|
|
|
|
int heapHandleIndex;
|
|
|
|
} CacheEntry;
|
|
|
|
|
|
|
|
typedef struct Cache {
|
|
|
|
// Current size of entries in cache.
|
|
|
|
int size;
|
|
|
|
|
|
|
|
// Maximum size of entries in cache.
|
|
|
|
int maxSize;
|
|
|
|
|
|
|
|
// The length of `entries` array.
|
|
|
|
int entriesLength;
|
|
|
|
|
|
|
|
// The capacity of `entries` array.
|
|
|
|
int entriesCapacity;
|
|
|
|
|
|
|
|
// Total number of hits during cache lifetime.
|
|
|
|
unsigned int hits;
|
|
|
|
|
|
|
|
// List of cache entries.
|
|
|
|
CacheEntry** entries;
|
|
|
|
|
|
|
|
CacheSizeProc* sizeProc;
|
|
|
|
CacheReadProc* readProc;
|
|
|
|
CacheFreeProc* freeProc;
|
|
|
|
Heap heap;
|
|
|
|
} Cache;
|
|
|
|
|
|
|
|
bool cacheInit(Cache* cache, CacheSizeProc* sizeProc, CacheReadProc* readProc, CacheFreeProc* freeProc, int maxSize);
|
|
|
|
bool cacheFree(Cache* cache);
|
|
|
|
bool cacheLock(Cache* cache, int key, void** data, CacheEntry** cacheEntryPtr);
|
|
|
|
bool cacheUnlock(Cache* cache, CacheEntry* cacheEntry);
|
|
|
|
bool cacheFlush(Cache* cache);
|
2022-12-08 12:05:50 -08:00
|
|
|
bool cachePrintStats(Cache* cache, char* dest, size_t size);
|
2022-05-19 01:51:26 -07:00
|
|
|
|
2022-09-23 05:43:44 -07:00
|
|
|
} // namespace fallout
|
|
|
|
|
2022-05-19 01:51:26 -07:00
|
|
|
#endif /* CACHE_H */
|