]> mj.ucw.cz Git - libucw.git/blob - lib/qache.h
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / lib / qache.h
1 /*
2  *      Simple and Quick Shared Memory Cache
3  *
4  *      (c) 2005 Martin Mares <mj@ucw.cz>
5  */
6
7 #ifndef _UCW_QACHE_H
8 #define _UCW_QACHE_H
9
10 struct qache_params {
11   byte *file_name;
12   uns block_size;                       /* Cache block size (a power of two) */
13   uns cache_size;                       /* Size of the whole cache */
14   uns max_entries;                      /* Maximum number of cached entries */
15   int force_reset;                      /* Force creation of a new cache even if the old one seems usable, -1 if reset should never be done */
16   uns format_id;                        /* Data format ID (old cache not used if formats differ) */
17 };
18
19 typedef byte qache_key_t[16];
20
21 struct qache;
22
23 /* Create and destroy a cache */
24 struct qache *qache_open(struct qache_params *p);
25 void qache_close(struct qache *q, uns retain_data);
26
27 /* Insert new item to the cache with a given key and data. If pos_hint is non-zero, it serves
28  * as a hint about the position of the entry (if it's known that an entry with the particular key
29  * was located there a moment ago). Returns position of the new entry.
30  */
31 uns qache_insert(struct qache *q, qache_key_t *key, uns pos_hint, void *data, uns size);
32
33 /* Look up data in the cache, given a key and a position hint (as above). If datap is non-NULL, data
34  * from the cache entry are copied either to *datap (if *datap is NULL, new memory is allocated by
35  * calling xmalloc and *datap is set to point to that memory). The *sizep contains the maximum number
36  * of bytes to be copied (~0U if unlimited) and it is replaced by the number of bytes available (so it
37  * can be greater than the original value requested). The start indicates starting offset inside the
38  * entry's data.
39  */
40 uns qache_lookup(struct qache *q, qache_key_t *key, uns pos_hint, byte **datap, uns *sizep, uns start);
41
42 /* Inspect data in the cache (but don't modify LRU nor anything else), given a position.
43  * If key is non-NULL, it's filled with the cache key. The rest works as in qache_lookup.
44  * Returns 0 if the entry is empty, ~0 for position out of range, entry number otherwise.
45  */
46 uns qache_probe(struct qache *q, qache_key_t *key, uns pos, byte **datap, uns *sizep, uns start);
47
48 /* Delete data from the cache, given a key and a position hint. */
49 uns qache_delete(struct qache *q, qache_key_t *key, uns pos_hint);
50
51 /* Debugging dump (beware, doesn't lock the cache!) */
52 void qache_debug(struct qache *q);
53
54 /* Check consistency of the cache structure */
55 void qache_audit(struct qache *q);
56
57 #endif