]> mj.ucw.cz Git - libucw.git/blob - lib/qache.h
Wrote the cache module, not debugged yet.
[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   int force_reset;                      /* Force creation of a new cache even if the old one seems usable, -1 if reset should never be done */
15   uns format_id;                        /* Data format ID (old cache not used if formats differ) */
16 };
17
18 typedef byte qache_key_t[16];
19
20 struct qache;
21
22 /* Create and destroy a cache */
23 struct qache *qache_open(struct qache_params *p);
24 void qache_close(struct qache *q, uns retain_data);
25
26 /* Insert new item to the cache with a given key and data. If pos_hint is non-zero, it serves
27  * as a hint about the position of the entry (if it's known that an entry with the particular key
28  * was located there a moment ago. Returns position of the new entry.
29  */
30 uns qache_insert(struct qache *q, qache_key_t *key, uns pos_hint, void *data, uns size);
31
32 /* Look up data in the cache, given a key and a position hint (as above). If datap is non-NULL, data
33  * from the cache entry are copied either to *datap (if *datap is NULL, new memory is allocated by
34  * calling xmalloc and *datap is set to point to that memory). The *sizep contains the maximum number
35  * of bytes to be copied (~0U if unlimited) and it is replaced by the number of bytes available (so it
36  * can be greater than the original value requested). The start indicates starting offset inside the
37  * entry's data.
38  */
39 uns qache_lookup(struct qache *q, qache_key_t *key, uns pos_hint, void **datap, uns *sizep, uns start);
40
41 /* Delete data from the cache, given a key and a position hint. */
42 uns qache_delete(struct qache *q, qache_key_t *key, uns pos_hint);
43
44 /* Debugging dump (beware, doesn't lock the cache!) */
45 void qache_debug(struct qache *q);
46
47 #endif