]> mj.ucw.cz Git - libucw.git/blob - lib/qache.h
b5d65f192763d97eb11364694a975ec85d925f0e
[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 /* Create and destroy a cache */
21 struct qache *qache_init(struct qache_params *p);
22 void qache_cleanup(struct qache *q, uns retain_data);
23
24 /* Insert new item to the cache with a given key and data. If pos_hint is non-zero, it serves
25  * as a hint about the position of the entry (if it's known that an entry with the particular key
26  * was located there a moment ago. Returns position of the new entry.
27  */
28 uns qache_insert(struct qache *q, qache_key_t *key, uns pos_hint, void *data, uns size);
29
30 /* Look up data in the cache, given a key and a position hint (as above). If datap is non-NULL, data
31  * from the cache entry are copied either to *datap (if *datap is NULL, new memory is allocated by
32  * calling xmalloc and *datap is set to point to that memory). The *sizep contains the maximum number
33  * of bytes to be copied (~0U if unlimited) and it is replaced by the number of bytes available (so it
34  * can be greater than the original value requested). The start indicates starting offset inside the
35  * entry's data.
36  */
37 uns qache_lookup(struct qache *q, qache_key_t *key, uns pos_hint, void **datap, uns **sizep, uns start);
38
39 /* Delete data from the cache, given a key and a position hint. */
40 uns qache_delete(struct qache *q, qache_key_t *key, uns pos_hint);
41
42 #endif