Partial port to WIN95/NT.
[idzebra-moved-to-github.git] / dict / dopen.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dopen.c,v $
7  * Revision 1.4  1997-09-09 13:38:01  adam
8  * Partial port to WIN95/NT.
9  *
10  * Revision 1.3  1994/09/01 17:49:37  adam
11  * Removed stupid line. Work on insertion in dictionary. Not finished yet.
12  *
13  */
14
15 #include <sys/types.h>
16 #include <fcntl.h>
17 #ifndef WINDOWS
18 #include <unistd.h>
19 #endif
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include <dict.h>
24
25 static void common_init (Dict_BFile bf, int block_size, int cache)
26 {
27     int i;
28
29     bf->block_size = block_size;
30     bf->cache = cache;
31     bf->hash_size = 31;
32
33     bf->hits = bf->misses = 0;
34
35     /* Allocate all blocks in one chunk. */
36     bf->all_data = xmalloc (block_size * cache);
37
38     /* Allocate and initialize hash array (as empty) */
39     bf->hash_array = xmalloc(sizeof(*bf->hash_array) * bf->hash_size);
40     for (i=bf->hash_size; --i >= 0; )
41         bf->hash_array[i] = NULL;
42
43     /* Allocate all block descriptors in one chunk */
44     bf->all_blocks = xmalloc (sizeof(*bf->all_blocks) * cache);
45
46     /* Initialize the free list */
47     bf->free_list = bf->all_blocks;
48     for (i=0; i<cache-1; i++)
49         bf->all_blocks[i].h_next = bf->all_blocks+(i+1);
50     bf->all_blocks[i].h_next = NULL;
51
52     /* Initialize the data for each block. Will never be moved again */
53     for (i=0; i<cache; i++)
54         bf->all_blocks[i].data = (char*) bf->all_data + i*block_size;
55
56     /* Initialize lru queue */
57     bf->lru_back = NULL;
58     bf->lru_front = NULL;
59 }
60
61
62 Dict_BFile dict_bf_open (const char *name, int block_size, int cache, int rw)
63 {
64     Dict_BFile dbf;
65
66     dbf = xmalloc (sizeof(*dbf));
67     dbf->bf = bf_open (name, block_size, rw);
68     if (!dbf->bf)
69         return NULL;
70     common_init (dbf, block_size, cache);
71     return dbf;
72 }