2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1999-05-18 20:00:33 adam
10 * Revision 1.11 1999/05/15 14:36:37 adam
11 * Updated dictionary. Implemented "compression" of dictionary.
13 * Revision 1.10 1999/02/02 14:50:21 adam
14 * Updated WIN32 code specific sections. Changed header.
16 * Revision 1.9 1997/09/09 13:38:01 adam
17 * Partial port to WIN95/NT.
19 * Revision 1.8 1995/01/24 11:25:11 adam
20 * Removed stupid assertion.
22 * Revision 1.7 1994/10/05 10:47:15 adam
23 * Function pr_lru is non-static now. No warning no more.
25 * Revision 1.6 1994/09/06 13:05:14 adam
26 * Further development of insertion. Some special cases are
27 * not properly handled yet! assert(0) are put here. The
28 * binary search in each page definitely reduce usr CPU.
30 * Revision 1.5 1994/09/01 17:49:38 adam
31 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
35 #include <sys/types.h>
47 void dict_pr_lru (Dict_BFile bf)
49 struct Dict_file_block *p;
50 for (p=bf->lru_back; p; p = p->lru_next)
52 printf (" %d", p->no);
58 static struct Dict_file_block *find_block (Dict_BFile bf, int no)
60 struct Dict_file_block *p;
62 for (p=bf->hash_array[no% bf->hash_size]; p; p=p->h_next)
68 static void release_block (Dict_BFile bf, struct Dict_file_block *p)
72 /* remove from lru queue */
74 p->lru_prev->lru_next = p->lru_next;
76 bf->lru_back = p->lru_next;
78 p->lru_next->lru_prev = p->lru_prev;
80 bf->lru_front = p->lru_prev;
82 /* remove from hash chain */
83 *p->h_prev = p->h_next;
85 p->h_next->h_prev = p->h_prev;
87 /* move to list of free blocks */
88 p->h_next = bf->free_list;
92 void dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush)
94 struct Dict_file_block *p;
96 for (i=0; i != no_to_flush && bf->lru_back; i++)
101 if (!bf->compact_flag)
102 bf_write (bf->bf, p->no, 0, 0, p->data);
105 int effective_block = p->no / bf->block_size;
106 int effective_offset = p->no -
107 effective_block * bf->block_size;
108 int remain = bf->block_size - effective_offset;
110 if (remain >= p->nbytes)
112 bf_write (bf->bf, effective_block, effective_offset,
115 logf (LOG_LOG, "bf_write no=%d offset=%d size=%d",
116 effective_block, effective_offset,
124 logf (LOG_LOG, "bf_write1 no=%d offset=%d size=%d",
125 effective_block, effective_offset,
128 bf_write (bf->bf, effective_block, effective_offset,
131 logf (LOG_LOG, "bf_write2 no=%d offset=%d size=%d",
132 effective_block+1, 0, p->nbytes - remain);
134 bf_write (bf->bf, effective_block+1, 0,
135 p->nbytes - remain, (char*)p->data + remain);
139 release_block (bf, p);
143 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
145 struct Dict_file_block *p, **pp;
148 dict_bf_flush_blocks (bf, 1);
149 assert (bf->free_list);
151 bf->free_list = p->h_next;
155 /* insert at front in lru chain */
157 p->lru_prev = bf->lru_front;
159 bf->lru_front->lru_next = p;
164 /* insert in hash chain */
165 pp = bf->hash_array + (no % bf->hash_size);
169 (*pp)->h_prev = &p->h_next;
175 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
177 /* Already at front? */
183 p->lru_prev->lru_next = p->lru_next;
185 bf->lru_back = p->lru_next;
186 p->lru_next->lru_prev = p->lru_prev;
188 /* Insert at front */
190 p->lru_prev = bf->lru_front;
192 bf->lru_front->lru_next = p;
198 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
200 struct Dict_file_block *p;
202 if ((p = find_block (bf, no)))
205 move_to_front (bf, p);
210 p = alloc_block (bf, no);
212 if (!bf->compact_flag)
213 i = bf_read (bf->bf, no, 0, 0, p->data);
216 int effective_block = no / bf->block_size;
217 int effective_offset = no - effective_block * bf->block_size;
219 i = bf_read (bf->bf, effective_block, effective_offset,
220 bf->block_size - effective_offset, p->data);
221 if (i > 0 && effective_offset > 0)
222 i = bf_read (bf->bf, effective_block+1, 0, effective_offset,
223 (char*) p->data + bf->block_size - effective_offset);
231 release_block (bf, p);
236 int dict_bf_newp (Dict_BFile dbf, int no, void **bufp, int nbytes)
238 struct Dict_file_block *p;
239 if (!(p = find_block (dbf, no)))
240 p = alloc_block (dbf, no);
242 move_to_front (dbf, p);
244 memset (p->data, 0, dbf->block_size);
248 printf ("bf_newp of %d:", no);
254 int dict_bf_touch (Dict_BFile dbf, int no)
256 struct Dict_file_block *p;
257 if ((p = find_block (dbf, no)))