2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1999-02-02 14:50:21 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.9 1997/09/09 13:38:01 adam
11 * Partial port to WIN95/NT.
13 * Revision 1.8 1995/01/24 11:25:11 adam
14 * Removed stupid assertion.
16 * Revision 1.7 1994/10/05 10:47:15 adam
17 * Function pr_lru is non-static now. No warning no more.
19 * Revision 1.6 1994/09/06 13:05:14 adam
20 * Further development of insertion. Some special cases are
21 * not properly handled yet! assert(0) are put here. The
22 * binary search in each page definitely reduce usr CPU.
24 * Revision 1.5 1994/09/01 17:49:38 adam
25 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
29 #include <sys/types.h>
41 void dict_pr_lru (Dict_BFile bf)
43 struct Dict_file_block *p;
44 for (p=bf->lru_back; p; p = p->lru_next)
46 printf (" %d", p->no);
52 static struct Dict_file_block *find_block (Dict_BFile bf, int no)
54 struct Dict_file_block *p;
56 for (p=bf->hash_array[no% bf->hash_size]; p; p=p->h_next)
62 static void release_block (Dict_BFile bf, struct Dict_file_block *p)
66 /* remove from lru queue */
68 p->lru_prev->lru_next = p->lru_next;
70 bf->lru_back = p->lru_next;
72 p->lru_next->lru_prev = p->lru_prev;
74 bf->lru_front = p->lru_prev;
76 /* remove from hash chain */
77 *p->h_prev = p->h_next;
79 p->h_next->h_prev = p->h_prev;
81 /* move to list of free blocks */
82 p->h_next = bf->free_list;
86 void dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush)
88 struct Dict_file_block *p;
90 for (i=0; i != no_to_flush && bf->lru_back; i++)
95 bf_write (bf->bf, p->no, 0, 0, p->data);
97 release_block (bf, p);
101 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
103 struct Dict_file_block *p, **pp;
106 dict_bf_flush_blocks (bf, 1);
107 assert (bf->free_list);
109 bf->free_list = p->h_next;
113 /* insert at front in lru chain */
115 p->lru_prev = bf->lru_front;
117 bf->lru_front->lru_next = p;
122 /* insert in hash chain */
123 pp = bf->hash_array + (no % bf->hash_size);
127 (*pp)->h_prev = &p->h_next;
133 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
135 /* Already at front? */
141 p->lru_prev->lru_next = p->lru_next;
143 bf->lru_back = p->lru_next;
144 p->lru_next->lru_prev = p->lru_prev;
146 /* Insert at front */
148 p->lru_prev = bf->lru_front;
150 bf->lru_front->lru_next = p;
156 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
158 struct Dict_file_block *p;
160 if ((p = find_block (bf, no)))
163 move_to_front (bf, p);
168 p = alloc_block (bf, no);
169 i = bf_read (bf->bf, no, 0, 0, p->data);
175 release_block (bf, p);
180 int dict_bf_newp (Dict_BFile dbf, int no, void **bufp)
182 struct Dict_file_block *p;
183 if (!(p = find_block (dbf, no)))
184 p = alloc_block (dbf, no);
186 move_to_front (dbf, p);
188 memset (p->data, 0, dbf->block_size);
191 printf ("bf_newp of %d:", no);
197 int dict_bf_touch (Dict_BFile dbf, int no)
199 struct Dict_file_block *p;
200 if ((p = find_block (dbf, no)))