Cache header file. More...
#include "helper.h"
Go to the source code of this file.
Data Structures | |
struct | cache_entry_t |
What an internal cache entry looks like. More... | |
struct | cache_t |
Cache structure. More... | |
Defines | |
#define | CACHE_DEFAULT (4) |
Typedefs | |
typedef long | cache_value_t |
Type of data we're caching; must be size-compatible with a pointer, as such is stored in some cases (eg ops__build_path()). | |
Functions | |
int | cch__add (struct cache_t *cache, cache_value_t id, const char *data, int len, char **copy) |
Adds a copy of the given data (id, data with len) to the cache; return the new allocated data pointer in copy. | |
int | cch__find (struct cache_t *cache, cache_value_t id, int *index, char **data, int *len) |
Find an entry, return index and/or others. | |
int | cch__entry_set (struct cache_entry_t **cache, cache_value_t id, const char *data, int len, int copy_old_data, char **copy) |
Copy the given data into the given cache entry. | |
int | cch__set_by_id (struct cache_t *cache, cache_value_t id, const char *data, int len, int copy_old_data, char **copy) |
Look for the same id in the cache, and overwrite or append the given data. | |
void | cch__set_active (struct cache_t *cache, int index) |
Makes the given index the head of the LRU list. | |
__attribute__ ((gnu_inline, always_inline)) static inline int cch__new_cache(struct cache_t **cache | |
Create a new cache, with a user-defined size. | |
int | cch__hash_add (struct cache_t *cache, const char *key, cache_value_t value) |
Interpret the cache as a hash and store the given value to the key. |
Cache header file.
Definition in file cache.h.
#define CACHE_DEFAULT (4) |
Definition at line 42 of file cache.h.
Referenced by hlp__get_grname(), and hlp__get_uname().
typedef long cache_value_t |
Type of data we're caching; must be size-compatible with a pointer, as such is stored in some cases (eg ops__build_path()).
__attribute__ | ( | (gnu_inline, always_inline) | ) |
Create a new cache, with a user-defined size.
I'd liked to do something like
static struct cache_t *cache=cch__new_cache(32);
but that couldn't return error codes (eg. ENOMEM
). We'd need something like exceptions ....
So I take the easy route with an inline function. Additional cost: a single "test if zero".
static struct cache_t *cache; static int status2=cch__new_cache(&cache, 32); if (status2) { status=status2; goto ex; } ex: return status;
Referenced by cch___string_to_cv().
int cch__add | ( | struct cache_t * | cache, | |
cache_value_t | id, | |||
const char * | data, | |||
int | len, | |||
char ** | copy | |||
) |
Adds a copy of the given data (id, data with len) to the cache; return the new allocated data pointer in copy.
Adds a copy of the given data (id, data with len) to the cache; return the new allocated data pointer in copy.
The given data is just inserted into the cache and marked as LRU. An old entry is removed if necessary.
Definition at line 123 of file cache.c.
References cch__entry_set(), cache_t::entries, cache_t::lru, cache_t::max, and cache_t::used.
Referenced by _st___string_from_bits(), cch__hash_add(), cch__set_by_id(), cm___absolute_path(), hlp___do_convert(), hlp___dummy_convert(), hlp__get_grname(), hlp__get_uname(), ops__build_path(), ops__link_to_string(), url__other_full_url(), and waa__get_tmp_name().
int cch__entry_set | ( | struct cache_entry_t ** | cache, | |
cache_value_t | id, | |||
const char * | data, | |||
int | len, | |||
int | copy_old_data, | |||
char ** | copy | |||
) | [inline] |
Copy the given data into the given cache entry.
Copy the given data into the given cache entry.
If a struct cache_entry_t is used as a string, this might be useful.
If memory should be allocated, but not copied, specify data as NULL
. For len ==-1
calls strlen()
.
If copy_old_data is set, old value in this cache entry is kept.
Please note that memory may have to be reallocated, causing *cache
to change!
Definition at line 49 of file cache.c.
References cache_entry_t::data, hlp__alloc(), hlp__realloc(), cache_entry_t::id, cache_entry_t::len, and STOPIF.
Referenced by _st___string_from_bits(), cch__add(), cch__set_by_id(), cm___rev_path_to_string(), hlp___do_convert(), hlp__format_path(), and waa__get_tmp_name().
int cch__find | ( | struct cache_t * | cache, | |
cache_value_t | id, | |||
int * | index, | |||
char ** | data, | |||
int * | len | |||
) | [inline] |
Find an entry, return index and/or others.
Find an entry, return index and/or others.
Can return ENOENT
if not found.
Definition at line 99 of file cache.c.
References cache_entry_t::data, cache_t::entries, cache_entry_t::id, cache_entry_t::len, and cache_t::used.
Referenced by cch__hash_find(), cch__set_by_id(), hlp__get_grname(), and hlp__get_uname().
int cch__hash_add | ( | struct cache_t * | cache, | |
const char * | key, | |||
cache_value_t | value | |||
) |
Interpret the cache as a hash and store the given value to the key.
Definition at line 273 of file cache.c.
References cch___string_to_cv(), cch__add(), cache_t::entries, cache_entry_t::hash_data, cache_t::lru, and STOPIF.
Referenced by hlp__get_gid(), and hlp__get_uid().
void cch__set_active | ( | struct cache_t * | cache, | |
int | i | |||
) |
Makes the given index the head of the LRU list.
Definition at line 173 of file cache.c.
References BUG_ON, cache_t::entries, cache_t::lru, and cache_t::max.
Referenced by cch__set_by_id(), and ops__build_path().
int cch__set_by_id | ( | struct cache_t * | cache, | |
cache_value_t | id, | |||
const char * | data, | |||
int | len, | |||
int | copy_old_data, | |||
char ** | copy | |||
) |
Look for the same id in the cache, and overwrite or append the given data.
Look for the same id in the cache, and overwrite or append the given data.
id is a distinct numeric value for addressing this item. The entry is set as LRU, eventually discarding older entries.
Definition at line 148 of file cache.c.
References cch__add(), cch__entry_set(), cch__find(), cch__set_active(), and cache_t::entries.