2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1994-09-01 17:49:38 adam
8 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
12 #include <sys/types.h>
22 static void pr_lru (Dict_BFile bf)
24 struct Dict_file_block *p;
25 for (p=bf->lru_back; p; p = p->lru_next)
27 printf (" %d", p->no);
33 static struct Dict_file_block *find_block (Dict_BFile bf, int no)
35 struct Dict_file_block *p;
37 for (p=bf->hash_array[no% bf->hash_size]; p; p=p->h_next)
43 static void release_block (Dict_BFile bf, struct Dict_file_block *p)
47 /* remove from lru queue */
49 p->lru_prev->lru_next = p->lru_next;
51 bf->lru_back = p->lru_next;
53 p->lru_next->lru_prev = p->lru_prev;
55 bf->lru_front = p->lru_prev;
57 /* remove from hash chain */
58 *p->h_prev = p->h_next;
60 p->h_next->h_prev = p->h_prev;
62 /* move to list of free blocks */
63 p->h_next = bf->free_list;
67 void dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush)
69 struct Dict_file_block *p;
71 for (i=0; i != no_to_flush && bf->lru_back; i++)
76 bf_write (bf->bf, p->no, 0, 0, p->data);
78 release_block (bf, p);
82 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
84 struct Dict_file_block *p, **pp;
87 dict_bf_flush_blocks (bf, 1);
88 assert (bf->free_list);
90 bf->free_list = p->h_next;
94 /* insert at front in lru chain */
96 p->lru_prev = bf->lru_front;
98 bf->lru_front->lru_next = p;
103 /* insert in hash chain */
104 pp = bf->hash_array + (no % bf->hash_size);
108 (*pp)->h_prev = &p->h_next;
114 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
116 /* Already at front? */
122 p->lru_prev->lru_next = p->lru_next;
124 bf->lru_back = p->lru_next;
125 p->lru_next->lru_prev = p->lru_prev;
127 /* Insert at front */
129 p->lru_prev = bf->lru_front;
131 bf->lru_front->lru_next = p;
137 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
139 struct Dict_file_block *p;
141 if ((p = find_block (bf, no)))
144 move_to_front (bf, p);
149 p = alloc_block (bf, no);
150 i = bf_read (bf->bf, no, 0, 0, p->data);
156 release_block (bf, p);
161 int dict_bf_newp (Dict_BFile dbf, int no, void **bufp)
163 struct Dict_file_block *p;
164 if (!(p = find_block (dbf, no)))
165 p = alloc_block (dbf, no);
167 move_to_front (dbf, p);
169 memset (p->data, 0, dbf->block_size);
172 printf ("bf_newp of %d:", no);
178 int dict_bf_touch (Dict_BFile dbf, int no)
180 struct Dict_file_block *p;
181 if ((p = find_block (dbf, no)))