11 static void pr_lru (Dict_BFile bf)
13 struct Dict_file_block *p;
14 for (p=bf->lru_back; p; p = p->lru_next)
16 printf (" %d", p->no);
22 static struct Dict_file_block *find_block (Dict_BFile bf, int no)
24 struct Dict_file_block *p;
26 for (p=bf->hash_array[no% bf->hash_size]; p; p=p->h_next)
32 static void release_block (Dict_BFile bf, struct Dict_file_block *p)
36 /* remove from lru queue */
38 p->lru_prev->lru_next = p->lru_next;
40 bf->lru_back = p->lru_next;
42 p->lru_next->lru_prev = p->lru_prev;
44 bf->lru_back = p->lru_prev;
46 /* remove from hash chain */
47 *p->h_prev = p->h_next;
49 p->h_next->h_prev = p->h_prev;
51 /* move to list of free blocks */
52 p->h_next = bf->free_list;
56 void dict_bf_flush_blocks (Dict_BFile bf, int no_to_flush)
58 struct Dict_file_block *p;
60 for (i=0; i != no_to_flush && bf->lru_back; i++)
64 bf_write (bf->bf, p->no, 0, 0, p->data);
65 release_block (bf, p);
69 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
71 struct Dict_file_block *p, **pp;
74 dict_bf_flush_blocks (bf, 1);
75 assert (bf->free_list);
77 bf->free_list = p->h_next;
81 /* insert at front in lru chain */
83 p->lru_prev = bf->lru_front;
85 bf->lru_front->lru_next = p;
90 /* insert in hash chain */
91 pp = bf->hash_array + (no % bf->hash_size);
95 (*pp)->h_prev = &p->h_next;
101 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
103 /* Already at front? */
109 p->lru_prev->lru_next = p->lru_next;
111 bf->lru_back = p->lru_next;
112 p->lru_next->lru_prev = p->lru_prev;
114 /* Insert at front */
116 p->lru_prev = bf->lru_front;
118 bf->lru_front->lru_next = p;
124 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
126 struct Dict_file_block *p;
128 if ((p = find_block (bf, no)))
131 move_to_front (bf, p);
136 p = alloc_block (bf, no);
137 i = bf_read (bf->bf, no, 0, 0, p->data);
143 release_block (bf, p);
148 int dict_bf_newp (Dict_BFile bf, int no, void **bufp)
150 struct Dict_file_block *p;
151 if (!(p = find_block (bf, no)))
152 p = alloc_block (bf, no);
154 move_to_front (bf, p);
156 memset (p->data, 0, bf->bf->block_size);
159 printf ("bf_newp of %d:", no);
165 int dict_bf_touch (Dict_BFile bf, int no)
167 struct Dict_file_block *p;
168 if ((p = find_block (bf, no)))