]> mj.ucw.cz Git - libucw.git/blob - lib/qache.c
9f50b3c01d1575e2b3f46bc14559edede86071ab
[libucw.git] / lib / qache.c
1 /*
2  *      Simple and Quick Shared Memory Cache
3  *
4  *      (c) 2005 Martin Mares <mj@ucw.cz>
5  */
6
7 #include "sherlock/sherlock.h"
8
9 #include <sys/mman.h>
10
11 /* FIXME: do we really need to msync() the regions on Linux? */
12
13 /*
14  *  On-disk format:
15  *      qache_header
16  *      qache_entry[max_entries]        table of entries and their keys
17  *      u32 qache_hash[hash_size]       hash table pointing to keys
18  *      padding                         to a multiple of block size
19  *      blocks[]                        data blocks, each block starts with u32 next_ptr
20  */
21
22 struct qache_header {
23   u32 magic;                            /* QCACHE_MAGIC */
24   u32 block_size;                       /* Parameters as in qache_params */
25   u32 num_blocks;
26   u32 format_id;
27   u32 entry_table_start;                /* Array of qache_entry's */
28   u32 max_entries;
29   u32 hash_table_start;                 /* Hash table containing all keys */
30   u32 hash_size;
31   u32 lru_first;                        /* First entry in the LRU */
32   u32 first_free_entry;                 /* Head of the list of free entries */
33   u32 first_free_block;                 /* Head of the list of free blocks */
34 };
35
36 #define QACHE_MAGIC 0xb79f6d12
37
38 struct qache_entry {
39   u32 lru_prev, lru_next;
40   u32 data_len;                         /* ~0 if a free entry */
41   u32 first_data_block;                 /* next free if a free entry */
42   qache_key_t key;
43   u32 hash_next;
44 };
45
46 struct qache {
47   struct qache_header *hdr;
48   int fd;
49   byte *mmap_data;
50   uns file_size;
51 };