// gadget cache copy (both cache must have the same size) struct cache_t *cache_copy (struct cache_t *dest, struct cache_t *src) { int idx_cache; int dest_capacity, src_capacity; void *copied, *cached; // check parameters if ((!dest || !src) || (src == dest)) { fprintf(stderr, "error: cache_copy(): Bad parameters\n"); return NULL; } // cache must be of same sizes dest_capacity = cache_get_capacity(dest); src_capacity = cache_get_capacity(src); if (dest_capacity != src_capacity) { fprintf(stderr, "error: cache_copy(): dest and src are not of the same size\n"); return NULL; } // copy cache for (idx_cache = 0; idx_cache < cache_get_size(src); idx_cache++) { // copying cached = cache_get (src, idx_cache); cache_set (dest, idx_cache, copied); } dest->used = src->used; return dest; }
ham_bool_t cache_too_big(ham_cache_t *cache) { if (cache_get_cur_elements(cache)*env_get_pagesize(cache_get_env(cache)) >cache_get_capacity(cache)) return (HAM_TRUE); return (HAM_FALSE); }
// allocate gadget cache by copy struct cache_t *cache_new_copy (struct cache_t *cache) { struct cache_t *copy, *res; // allocate copy copy = cache_new(cache_get_capacity(cache)); // make copy res = cache_copy(copy, cache); if (res == NULL) cache_destroy(©, NULL); return copy; }
// zero entirely the cache int cache_zero (struct cache_t *cache) { int idx_object; // check parameters if (!cache) return -ERR_CACHE_UNDEFINED; // reset cache (heavy one) for (idx_object = 0; idx_object < cache_get_capacity(cache); idx_object++) cache_set(cache, idx_object, NULL); // reset used counter cache->used = 0; return CACHE_OK; }
// purge cache: "free" and re-init the cache int cache_purge (struct cache_t *cache, void (*destroy)(void **data)) { int idx_object; // check parameters if (!cache) return -ERR_CACHE_UNDEFINED; // reinit whole cache for (idx_object = 0; idx_object < cache_get_capacity(cache); idx_object++) { if (destroy) destroy(&(cache->objects[idx_object])); cache_set(cache, idx_object, NULL); } // reset the used counter cache->used = 0; return CACHE_OK; }