2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-01-24 11:25:11 adam
8 * Removed stupid assertion.
10 * Revision 1.7 1994/10/05 10:47:15 adam
11 * Function pr_lru is non-static now. No warning no more.
13 * Revision 1.6 1994/09/06 13:05:14 adam
14 * Further development of insertion. Some special cases are
15 * not properly handled yet! assert(0) are put here. The
16 * binary search in each page definitely reduce usr CPU.
18 * Revision 1.5 1994/09/01 17:49:38 adam
19 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
23 #include <sys/types.h>
33 void dict_pr_lru (Dict_BFile bf)
35 struct Dict_file_block *p;
36 for (p=bf->lru_back; p; p = p->lru_next)
38 printf (" %d", p->no);
44 static struct Dict_file_block *find_block (Dict_BFile bf, int no)
46 struct Dict_file_block *p;
48 for (p=bf->hash_array[no% bf->hash_size]; p; p=p->h_next)
54 static void release_block (Dict_BFile bf, struct Dict_file_block *p)
58 /* remove from lru queue */
60 p->lru_prev->lru_next = p->lru_next;
62 bf->lru_back = p->lru_next;
64 p->lru_next->lru_prev = p->lru_prev;
66 bf->lru_front = p->lru_prev;
68 /* remove from hash chain */
69 *p->h_prev = p->h_next;
71 p->h_next->h_prev = p->h_prev;
73 /* move to list of free blocks */
74 p->h_next = bf->free_list;
78 void dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush)
80 struct Dict_file_block *p;
82 for (i=0; i != no_to_flush && bf->lru_back; i++)
87 bf_write (bf->bf, p->no, 0, 0, p->data);
89 release_block (bf, p);
93 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
95 struct Dict_file_block *p, **pp;
98 dict_bf_flush_blocks (bf, 1);
99 assert (bf->free_list);
101 bf->free_list = p->h_next;
105 /* insert at front in lru chain */
107 p->lru_prev = bf->lru_front;
109 bf->lru_front->lru_next = p;
114 /* insert in hash chain */
115 pp = bf->hash_array + (no % bf->hash_size);
119 (*pp)->h_prev = &p->h_next;
125 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
127 /* Already at front? */
133 p->lru_prev->lru_next = p->lru_next;
135 bf->lru_back = p->lru_next;
136 p->lru_next->lru_prev = p->lru_prev;
138 /* Insert at front */
140 p->lru_prev = bf->lru_front;
142 bf->lru_front->lru_next = p;
148 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
150 struct Dict_file_block *p;
152 if ((p = find_block (bf, no)))
155 move_to_front (bf, p);
160 p = alloc_block (bf, no);
161 i = bf_read (bf->bf, no, 0, 0, p->data);
167 release_block (bf, p);
172 int dict_bf_newp (Dict_BFile dbf, int no, void **bufp)
174 struct Dict_file_block *p;
175 if (!(p = find_block (dbf, no)))
176 p = alloc_block (dbf, no);
178 move_to_front (dbf, p);
180 memset (p->data, 0, dbf->block_size);
183 printf ("bf_newp of %d:", no);
189 int dict_bf_touch (Dict_BFile dbf, int no)
191 struct Dict_file_block *p;
192 if ((p = find_block (dbf, no)))