2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1997-09-09 13:38:01 adam
8 * Partial port to WIN95/NT.
10 * Revision 1.8 1995/01/24 11:25:11 adam
11 * Removed stupid assertion.
13 * Revision 1.7 1994/10/05 10:47:15 adam
14 * Function pr_lru is non-static now. No warning no more.
16 * Revision 1.6 1994/09/06 13:05:14 adam
17 * Further development of insertion. Some special cases are
18 * not properly handled yet! assert(0) are put here. The
19 * binary search in each page definitely reduce usr CPU.
21 * Revision 1.5 1994/09/01 17:49:38 adam
22 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
26 #include <sys/types.h>
38 void dict_pr_lru (Dict_BFile bf)
40 struct Dict_file_block *p;
41 for (p=bf->lru_back; p; p = p->lru_next)
43 printf (" %d", p->no);
49 static struct Dict_file_block *find_block (Dict_BFile bf, int no)
51 struct Dict_file_block *p;
53 for (p=bf->hash_array[no% bf->hash_size]; p; p=p->h_next)
59 static void release_block (Dict_BFile bf, struct Dict_file_block *p)
63 /* remove from lru queue */
65 p->lru_prev->lru_next = p->lru_next;
67 bf->lru_back = p->lru_next;
69 p->lru_next->lru_prev = p->lru_prev;
71 bf->lru_front = p->lru_prev;
73 /* remove from hash chain */
74 *p->h_prev = p->h_next;
76 p->h_next->h_prev = p->h_prev;
78 /* move to list of free blocks */
79 p->h_next = bf->free_list;
83 void dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush)
85 struct Dict_file_block *p;
87 for (i=0; i != no_to_flush && bf->lru_back; i++)
92 bf_write (bf->bf, p->no, 0, 0, p->data);
94 release_block (bf, p);
98 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
100 struct Dict_file_block *p, **pp;
103 dict_bf_flush_blocks (bf, 1);
104 assert (bf->free_list);
106 bf->free_list = p->h_next;
110 /* insert at front in lru chain */
112 p->lru_prev = bf->lru_front;
114 bf->lru_front->lru_next = p;
119 /* insert in hash chain */
120 pp = bf->hash_array + (no % bf->hash_size);
124 (*pp)->h_prev = &p->h_next;
130 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
132 /* Already at front? */
138 p->lru_prev->lru_next = p->lru_next;
140 bf->lru_back = p->lru_next;
141 p->lru_next->lru_prev = p->lru_prev;
143 /* Insert at front */
145 p->lru_prev = bf->lru_front;
147 bf->lru_front->lru_next = p;
153 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
155 struct Dict_file_block *p;
157 if ((p = find_block (bf, no)))
160 move_to_front (bf, p);
165 p = alloc_block (bf, no);
166 i = bf_read (bf->bf, no, 0, 0, p->data);
172 release_block (bf, p);
177 int dict_bf_newp (Dict_BFile dbf, int no, void **bufp)
179 struct Dict_file_block *p;
180 if (!(p = find_block (dbf, no)))
181 p = alloc_block (dbf, no);
183 move_to_front (dbf, p);
185 memset (p->data, 0, dbf->block_size);
188 printf ("bf_newp of %d:", no);
194 int dict_bf_touch (Dict_BFile dbf, int no)
196 struct Dict_file_block *p;
197 if ((p = find_block (dbf, no)))