2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1999-05-15 14:36:37 adam
8 * Updated dictionary. Implemented "compression" of dictionary.
10 * Revision 1.10 1999/02/02 14:50:21 adam
11 * Updated WIN32 code specific sections. Changed header.
13 * Revision 1.9 1997/09/09 13:38:01 adam
14 * Partial port to WIN95/NT.
16 * Revision 1.8 1995/01/24 11:25:11 adam
17 * Removed stupid assertion.
19 * Revision 1.7 1994/10/05 10:47:15 adam
20 * Function pr_lru is non-static now. No warning no more.
22 * Revision 1.6 1994/09/06 13:05:14 adam
23 * Further development of insertion. Some special cases are
24 * not properly handled yet! assert(0) are put here. The
25 * binary search in each page definitely reduce usr CPU.
27 * Revision 1.5 1994/09/01 17:49:38 adam
28 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
32 #include <sys/types.h>
44 void dict_pr_lru (Dict_BFile bf)
46 struct Dict_file_block *p;
47 for (p=bf->lru_back; p; p = p->lru_next)
49 printf (" %d", p->no);
55 static struct Dict_file_block *find_block (Dict_BFile bf, int no)
57 struct Dict_file_block *p;
59 for (p=bf->hash_array[no% bf->hash_size]; p; p=p->h_next)
65 static void release_block (Dict_BFile bf, struct Dict_file_block *p)
69 /* remove from lru queue */
71 p->lru_prev->lru_next = p->lru_next;
73 bf->lru_back = p->lru_next;
75 p->lru_next->lru_prev = p->lru_prev;
77 bf->lru_front = p->lru_prev;
79 /* remove from hash chain */
80 *p->h_prev = p->h_next;
82 p->h_next->h_prev = p->h_prev;
84 /* move to list of free blocks */
85 p->h_next = bf->free_list;
89 void dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush)
91 struct Dict_file_block *p;
93 for (i=0; i != no_to_flush && bf->lru_back; i++)
98 if (!bf->compact_flag)
99 bf_write (bf->bf, p->no, 0, 0, p->data);
102 int effective_block = p->no / bf->block_size;
103 int effective_offset = p->no -
104 effective_block * bf->block_size;
105 int remain = bf->block_size - effective_offset;
107 if (remain >= p->nbytes)
109 bf_write (bf->bf, effective_block, effective_offset,
112 logf (LOG_LOG, "bf_write no=%d offset=%d size=%d",
113 effective_block, effective_offset,
121 logf (LOG_LOG, "bf_write1 no=%d offset=%d size=%d",
122 effective_block, effective_offset,
125 bf_write (bf->bf, effective_block, effective_offset,
128 logf (LOG_LOG, "bf_write2 no=%d offset=%d size=%d",
129 effective_block+1, 0, p->nbytes - remain);
131 bf_write (bf->bf, effective_block+1, 0,
132 p->nbytes - remain, (char*)p->data + remain);
136 release_block (bf, p);
140 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
142 struct Dict_file_block *p, **pp;
145 dict_bf_flush_blocks (bf, 1);
146 assert (bf->free_list);
148 bf->free_list = p->h_next;
152 /* insert at front in lru chain */
154 p->lru_prev = bf->lru_front;
156 bf->lru_front->lru_next = p;
161 /* insert in hash chain */
162 pp = bf->hash_array + (no % bf->hash_size);
166 (*pp)->h_prev = &p->h_next;
172 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
174 /* Already at front? */
180 p->lru_prev->lru_next = p->lru_next;
182 bf->lru_back = p->lru_next;
183 p->lru_next->lru_prev = p->lru_prev;
185 /* Insert at front */
187 p->lru_prev = bf->lru_front;
189 bf->lru_front->lru_next = p;
195 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
197 struct Dict_file_block *p;
199 if ((p = find_block (bf, no)))
202 move_to_front (bf, p);
207 p = alloc_block (bf, no);
209 //////////////// insert here
211 if (!bf->compact_flag)
212 i = bf_read (bf->bf, no, 0, 0, p->data);
215 int effective_block = no / bf->block_size;
216 int effective_offset = no - effective_block * bf->block_size;
218 i = bf_read (bf->bf, effective_block, effective_offset,
219 bf->block_size - effective_offset, p->data);
220 if (i > 0 && effective_offset > 0)
221 i = bf_read (bf->bf, effective_block+1, 0, effective_offset,
222 p->data + bf->block_size - effective_offset);
230 release_block (bf, p);
235 int dict_bf_newp (Dict_BFile dbf, int no, void **bufp, int nbytes)
237 struct Dict_file_block *p;
238 if (!(p = find_block (dbf, no)))
239 p = alloc_block (dbf, no);
241 move_to_front (dbf, p);
243 memset (p->data, 0, dbf->block_size);
247 printf ("bf_newp of %d:", no);
253 int dict_bf_touch (Dict_BFile dbf, int no)
255 struct Dict_file_block *p;
256 if ((p = find_block (dbf, no)))