depend include change.
[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->block_size = block_size;
14     bf->cache = cache;
15     bf->hash_size = 31;
16
17     bf->hits = bf->misses = 0;
18
19     /* Allocate all blocks in one chunk. */
20     bf->all_data = xmalloc (block_size * cache);
21
22     /* Allocate and initialize hash array (as empty) */
23     bf->hash_array = xmalloc(sizeof(*bf->hash_array) * bf->hash_size);
24     for (i=bf->hash_size; --i >= 0; )
25         bf->hash_array[i] = NULL;
26
27     /* Allocate all block descriptors in one chunk */
28     bf->all_blocks = xmalloc (sizeof(*bf->all_blocks) * cache);
29
30     /* Initialize the free list */
31     bf->free_list = bf->all_blocks;
32     for (i=0; i<cache-1; i++)
33         bf->all_blocks[i].h_next = bf->all_blocks+(i+1);
34     bf->all_blocks[i].h_next = NULL;
35
36     /* Initialize the data for each block. Will never be moved again */
37     for (i=0; i<cache; i++)
38         bf->all_blocks[i].data = (char*) bf->all_data + i*block_size;
39
40     /* Initialize lru queue */
41     bf->lru_back = NULL;
42     bf->lru_front = NULL;
43 }
44
45
46 Dict_BFile dict_bf_open (const char *name, int block_size, int cache, int rw)
47 {
48     Dict_BFile dbf;
49
50     dbf = xmalloc (sizeof(*dbf));
51     dbf->bf = bf_open (name, block_size, rw);
52     if (!dbf->bf)
53         return NULL;
54     common_init (dbf, block_size, cache);
55     return dbf;
56 }