| 55 | } |
| 56 | |
| 57 | Cache *Cache_New(uint cap, CacheEntryFreeFunc freeFunc, CacheEntryCopyFunc copyFunc) { |
| 58 | ASSERT(cap > 0); |
| 59 | ASSERT(copyFunc != NULL); |
| 60 | |
| 61 | Cache *cache = rm_malloc(sizeof(Cache)); |
| 62 | cache->cap = cap; |
| 63 | cache->size = 0; |
| 64 | cache->lookup = raxNew(); // Instantiate key entry mapping. |
| 65 | cache->counter = 0; // Initialize counter to zero. |
| 66 | cache->copy_item = copyFunc; |
| 67 | cache->free_item = freeFunc; |
| 68 | cache->arr = rm_calloc(cap, sizeof(CacheEntry)); // Array of cached values. |
| 69 | |
| 70 | // Initialize the read-write lock to protect access to the cache. |
| 71 | int res = pthread_rwlock_init(&cache->_cache_rwlock, NULL); |
| 72 | UNUSED(res); |
| 73 | ASSERT(res == 0); |
| 74 | |
| 75 | return cache; |
| 76 | } |
| 77 | |
| 78 | void *Cache_GetValue(Cache *cache, const char *key) { |
| 79 | void *item = NULL; |