Some small caching primitives. More...
#include <stdlib.h>
#include <unistd.h>
#include "global.h"
#include "cache.h"
Go to the source code of this file.
Functions | |
int | cch__entry_set (struct cache_entry_t **cache, cache_value_t id, const char *data, int len, int copy_old_data, char **copy) |
-. | |
int | cch__find (struct cache_t *cache, cache_value_t id, int *index, char **data, int *len) |
-. | |
int | cch__add (struct cache_t *cache, cache_value_t id, const char *data, int len, char **copy) |
-. | |
int | cch__set_by_id (struct cache_t *cache, cache_value_t id, const char *data, int len, int copy_old_data, char **copy) |
-. | |
void | cch__set_active (struct cache_t *cache, int i) |
-. | |
cache_value_t | cch___string_to_cv (const char *stg) |
A simple hash. | |
int | cch__hash_find (struct cache_t *cache, const char *key, cache_value_t *data) |
-. | |
int | cch__hash_add (struct cache_t *cache, const char *key, cache_value_t value) |
-. |
Some small caching primitives.
We have to do some caching - neither the APR-functions nor glibc caches results of getpwnam()
and similar. On update or commit we call them many, many times ... there it's good to have these values cached.
It's not necessary for performance; but simply getting a char*
back from some function and using it, knowing that it's valid for a few more calls of the same function, eases life tremendously.
apr_uid_get()
calls from update.c
go into that - but they need a hash or something like that. Maybe reverse the test and look whether the number (eg. uid) matches the string (username)? Definition in file cache.c.
cache_value_t cch___string_to_cv | ( | const char * | stg | ) | [inline] |
A simple hash.
Copies the significant bits ' ' .. 'Z' (or, really, \x20 .. \x60) of at most 6 bytes of stg into a packed bitfield, so that 30bits are used.
Definition at line 212 of file cache.c.
References __attribute__().
Referenced by cch__hash_add(), and cch__hash_find().
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.
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.
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.
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().
int cch__hash_find | ( | struct cache_t * | cache, | |
const char * | key, | |||
cache_value_t * | data | |||
) |
-.
Definition at line 248 of file cache.c.
References cch___string_to_cv(), cch__find(), cache_entry_t::data, DEBUGP, cache_t::entries, and cache_entry_t::hash_data.
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.
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.