C Collections

clrucache · Section 3

ccache_construct_scoped

ccache_construct_scoped - declare and initialise an LRU cache that is destroyed at scope exit

C Collections (libccollections, -lccollections)

#include <ccollections/clrucache.h>
 
ccache_construct_scoped(name, KeyT, ValT, capacity, getter, setter,
                        evict_cb);

The same as ccache_construct(), and the cache is also destroyed by
itself when the variable leaves its scope.

name      Variable name for the cache handle
KeyT      Key type
ValT      Value type
capacity  The total number of live entries; see
          clrucache_create_full(3). It must be greater than 0
getter    Remote getter callback, or NULL
setter    Remote setter callback, or NULL
evict_cb  Eviction callback, or NULL

The macro needs GCC or Clang, because it uses __attribute__((cleanup)).
 
An array key type or value type, such as int[2] or char[16], is a
compile error, and the message says to wrap the array in a struct. The
macros copy a key or a value by assignment, C cannot assign an array,
and without the error the cache would store the address of the
caller's array instead of its elements. A struct that holds the
array works.

void process(void) {
    ccache_construct_scoped(cache, int, double, 64, NULL, NULL, NULL);
    /* the cache is destroyed automatically when process() returns */
}

ccache_construct(3), ccache_declare_scoped(3)