1f1fda3d1a65633a483d74def133507765c518c1
[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.5  1997-09-17 12:19:07  adam
8  * Zebra version corresponds to YAZ version 1.4.
9  * Changed Zebra server so that it doesn't depend on global common_resource.
10  *
11  * Revision 1.4  1997/09/09 13:38:01  adam
12  * Partial port to WIN95/NT.
13  *
14  * Revision 1.3  1994/09/01 17:49:37  adam
15  * Removed stupid line. Work on insertion in dictionary. Not finished yet.
16  *
17  */
18
19 #include <sys/types.h>
20 #include <fcntl.h>
21 #ifndef WINDOWS
22 #include <unistd.h>
23 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include <dict.h>
28
29 static void common_init (Dict_BFile bf, int block_size, int cache)
30 {
31     int i;
32
33     bf->block_size = block_size;
34     bf->cache = cache;
35     bf->hash_size = 31;
36
37     bf->hits = bf->misses = 0;
38
39     /* Allocate all blocks in one chunk. */
40     bf->all_data = xmalloc (block_size * cache);
41
42     /* Allocate and initialize hash array (as empty) */
43     bf->hash_array = xmalloc(sizeof(*bf->hash_array) * bf->hash_size);
44     for (i=bf->hash_size; --i >= 0; )
45         bf->hash_array[i] = NULL;
46
47     /* Allocate all block descriptors in one chunk */
48     bf->all_blocks = xmalloc (sizeof(*bf->all_blocks) * cache);
49
50     /* Initialize the free list */
51     bf->free_list = bf->all_blocks;
52     for (i=0; i<cache-1; i++)
53         bf->all_blocks[i].h_next = bf->all_blocks+(i+1);
54     bf->all_blocks[i].h_next = NULL;
55
56     /* Initialize the data for each block. Will never be moved again */
57     for (i=0; i<cache; i++)
58         bf->all_blocks[i].data = (char*) bf->all_data + i*block_size;
59
60     /* Initialize lru queue */
61     bf->lru_back = NULL;
62     bf->lru_front = NULL;
63 }
64
65
66 Dict_BFile dict_bf_open (BFiles bfs, const char *name, int block_size,
67                          int cache, int rw)
68 {
69     Dict_BFile dbf;
70
71     dbf = xmalloc (sizeof(*dbf));
72     dbf->bf = bf_open (bfs, name, block_size, rw);
73     if (!dbf->bf)
74         return NULL;
75     common_init (dbf, block_size, cache);
76     return dbf;
77 }