cache_callbacks(3) [osx man page]
cache_callbacks(3) BSD Library Functions Manual cache_callbacks(3) NAME
cache_callbacks -- Pre-defined cache callbacks used to configure a cache SYNOPSIS
#include <cache.h> #include <cache_callbacks.h> uintptr_t cache_key_hash_cb_cstring(void *key, void *unused); uintptr_t cache_key_hash_cb_integer(void *key, void *unused); bool cache_key_is_equal_cb_cstring(void *key1, void *key2, void *unused); bool cache_key_is_equal_cb_integer(void *key1, void *key2, void *unused); void cache_release_cb_free(void *key_or_value, void *unused); void cache_value_make_purgeable_cb(void *value, void *unused); bool cache_value_make_nonpurgeable_cb(void *value, void *unused); uintptr_t cache_hash_byte_string(const char *data, size_t bytes); DESCRIPTION
These functions are intended to be used as callbacks to configure how a cache functions. They should be set in the cache_attributes_t passed into cache_create. They support common key types and offer support for using purgeable memory to allocate cache values. cache_key_hash_cb_cstring() A key_hash_cb() function for NULL-terminated cstring keys. cache_key_hash_cb_integer() A key_hash_cb() function for integer keys. cache_key_is_equal_cb_cstring() A key_is_equal_cb() function for cstring keys. cache_key_is_equal_cb_integer() A key_is_equal_cb() function for integer keys. cache_release_cb_free() Can be used for key_release_cb() or value_release_cb() for keys/values allocated from malloc and family. cache_value_make_purgeable_cb() Can be used for value_make_purgeable() with values allocated from the purgeable malloc zone (see malloc/mal- loc.h). Calls malloc_make_purgeable() on value when it is unreferenced in order to reduce paging under memory pressure. value_make_purgeable() with values allocated from the purgeable malloc zone (see malloc/malloc.h). Calls malloc_make_purgeable() on value when it is unreferenced in order to reduce paging under memory pressure. cache_hash_byte_string() Calculates a hash from a bytes string data of length bytes. SEE ALSO
libcache(3) cache_create(3) Darwin May 7, 2009 Darwin
Check Out this Related Man Page
cache_set_and_retain(3) BSD Library Functions Manual cache_set_and_retain(3) NAME
cache_set_and_retain, cache_get_and_retain, cache_release_value, cache_remove -- Routines used to manage cached values SYNOPSIS
#include <cache.h> int cache_set_and_retain(cache_t *cache, void *key, void *value, size_t cost); int cache_get_and_retain(cache_t *cache, void *key, void **value_out); int cache_release_value(cache_t *cache, void *value); int cache_remove(cache_t *cache, void *key); DESCRIPTION
These routines are used to manipulate values added to an in memory cache created by cache_create(3). cache_set_and_retain() Adds value with cost to cache and associates it with key. The caller retains a reference to value that will prevent value from being evicted from the cache until value is released in cache_release_value(). cache_get_and_retain() Fetches value for key from cache and places value in value_out. The caller retains a reference to value that will prevent value from being evicted from the cache until value is release in cache_release_value(). cache_release_value() Releases a reference on value back to cache so that value may be evicted. Signals that the client is not actively using value and will use cache_get_and_retain() before using again. cache_remove() Removes the value associated with key from cache. Note that if the value is referenced by a client, the value will not be finalized until the reference is released using cache_release_value(). RETURN VALUES
All functions return 0 for success and non-zero for failure. The value ENOENT (see errno.h) indicates that a key or value passed as an argu- ment does not exist in the cache. EINVAL is used for invalid arguments. EXAMPLE
The following example attempts to fetch a value from a cache using a key. If the value is not present in the cache then it is created and added to the cache. The value is then used and released back to the cache to allow the cache to evict it when needed. cache_t *mycache; cache_create("com.mycompany.mycache", &cache_attributes, &mycache); void *mykey = my_create_key(); void *myvalue = NULL; if (cache_get_and_retain(mycache, mykey, &myvalue) != 0) { myvalue = my_create_value_from_key(mykey); cache_set_and_retain(mycache, mykey, myvalue, 0); } my_use_value(value); cache_release_value(mycache, myvalue); SEE ALSO
cache(3) Darwin May 7, 2009 Darwin