2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1994-10-05 10:47:15 adam
8 * Function pr_lru is non-static now. No warning no more.
10 * Revision 1.6 1994/09/06 13:05:14 adam
11 * Further development of insertion. Some special cases are
12 * not properly handled yet! assert(0) are put here. The
13 * binary search in each page definitely reduce usr CPU.
15 * Revision 1.5 1994/09/01 17:49:38 adam
16 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
20 #include <sys/types.h>
30 void dict_pr_lru (Dict_BFile bf)
32 struct Dict_file_block *p;
33 for (p=bf->lru_back; p; p = p->lru_next)
35 printf (" %d", p->no);
41 static struct Dict_file_block *find_block (Dict_BFile bf, int no)
43 struct Dict_file_block *p;
45 for (p=bf->hash_array[no% bf->hash_size]; p; p=p->h_next)
51 static void release_block (Dict_BFile bf, struct Dict_file_block *p)
55 /* remove from lru queue */
57 p->lru_prev->lru_next = p->lru_next;
59 bf->lru_back = p->lru_next;
61 p->lru_next->lru_prev = p->lru_prev;
63 bf->lru_front = p->lru_prev;
65 /* remove from hash chain */
66 *p->h_prev = p->h_next;
68 p->h_next->h_prev = p->h_prev;
70 /* move to list of free blocks */
71 p->h_next = bf->free_list;
75 void dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush)
77 struct Dict_file_block *p;
79 for (i=0; i != no_to_flush && bf->lru_back; i++)
84 bf_write (bf->bf, p->no, 0, 0, p->data);
86 release_block (bf, p);
90 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
92 struct Dict_file_block *p, **pp;
95 dict_bf_flush_blocks (bf, 1);
96 assert (bf->free_list);
98 bf->free_list = p->h_next;
102 /* insert at front in lru chain */
104 p->lru_prev = bf->lru_front;
106 bf->lru_front->lru_next = p;
111 /* insert in hash chain */
112 pp = bf->hash_array + (no % bf->hash_size);
116 (*pp)->h_prev = &p->h_next;
122 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
124 /* Already at front? */
130 p->lru_prev->lru_next = p->lru_next;
132 bf->lru_back = p->lru_next;
133 p->lru_next->lru_prev = p->lru_prev;
135 /* Insert at front */
137 p->lru_prev = bf->lru_front;
139 bf->lru_front->lru_next = p;
145 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
147 struct Dict_file_block *p;
150 if ((p = find_block (bf, no)))
153 move_to_front (bf, p);
158 p = alloc_block (bf, no);
159 i = bf_read (bf->bf, no, 0, 0, p->data);
165 release_block (bf, p);
170 int dict_bf_newp (Dict_BFile dbf, int no, void **bufp)
172 struct Dict_file_block *p;
173 if (!(p = find_block (dbf, no)))
174 p = alloc_block (dbf, no);
176 move_to_front (dbf, p);
178 memset (p->data, 0, dbf->block_size);
181 printf ("bf_newp of %d:", no);
187 int dict_bf_touch (Dict_BFile dbf, int no)
189 struct Dict_file_block *p;
190 if ((p = find_block (dbf, no)))