dictionary internal cache modules.
[idzebra-moved-to-github.git] / dict / dopen.c
1 #include <sys/types.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include <dict.h>
8
9 static void common_init (Dict_BFile bf, int block_size, int cache)
10 {
11     int i;
12
13     bf->cache = cache;
14     bf->hash_size = 31;
15
16     bf->hits = bf->misses = 0;
17
18     /* Allocate all blocks in one chunk. */
19     bf->all_data = xmalloc (block_size * cache);
20
21     /* Allocate and initialize hash array (as empty) */
22     bf->hash_array = xmalloc(sizeof(*bf->hash_array) * bf->hash_size);
23     for (i=bf->hash_size; --i >= 0; )
24         bf->hash_array[i] = NULL;
25
26     /* Allocate all block descriptors in one chunk */
27     bf->all_blocks = xmalloc (sizeof(*bf->all_blocks) * cache);
28
29     /* Initialize the free list */
30     bf->free_list = bf->all_blocks;
31     for (i=0; i<cache-1; i++)
32         bf->all_blocks[i].h_next = bf->all_blocks+(i+1);
33     bf->all_blocks[i].h_next = NULL;
34
35     /* Initialize the data for each block. Will never be moved again */
36     for (i=0; i<cache; i++)
37         bf->all_blocks[i].data = (char*) bf->all_data + i*block_size;
38
39     /* Initialize lru queue */
40     bf->lru_back = NULL;
41     bf->lru_front = NULL;
42 }
43
44
45 Dict_BFile dict_bf_open (const char *name, int block_size, int cache, int rw)
46 {
47     Dict_BFile dbf;
48
49     dbf = xmalloc (sizeof(*dbf));
50     dbf->bf = bf_open (name, block_size, rw);
51     common_init (dbf, block_size, cache);
52     return dbf;
53 }