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_front = 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++)
65 bf_write (bf->bf, p->no, 0, 0, p->data);
67 release_block (bf, p);
71 static struct Dict_file_block *alloc_block (Dict_BFile bf, int no)
73 struct Dict_file_block *p, **pp;
76 dict_bf_flush_blocks (bf, 1);
77 assert (bf->free_list);
79 bf->free_list = p->h_next;
83 /* insert at front in lru chain */
85 p->lru_prev = bf->lru_front;
87 bf->lru_front->lru_next = p;
92 /* insert in hash chain */
93 pp = bf->hash_array + (no % bf->hash_size);
97 (*pp)->h_prev = &p->h_next;
103 static void move_to_front (Dict_BFile bf, struct Dict_file_block *p)
105 /* Already at front? */
111 p->lru_prev->lru_next = p->lru_next;
113 bf->lru_back = p->lru_next;
114 p->lru_next->lru_prev = p->lru_prev;
116 /* Insert at front */
118 p->lru_prev = bf->lru_front;
120 bf->lru_front->lru_next = p;
126 int dict_bf_readp (Dict_BFile bf, int no, void **bufp)
128 struct Dict_file_block *p;
130 if ((p = find_block (bf, no)))
133 move_to_front (bf, p);
138 p = alloc_block (bf, no);
139 i = bf_read (bf->bf, no, 0, 0, p->data);
145 release_block (bf, p);
150 int dict_bf_newp (Dict_BFile dbf, int no, void **bufp)
152 struct Dict_file_block *p;
153 if (!(p = find_block (dbf, no)))
154 p = alloc_block (dbf, no);
156 move_to_front (dbf, p);
158 memset (p->data, 0, dbf->block_size);
161 printf ("bf_newp of %d:", no);
167 int dict_bf_touch (Dict_BFile dbf, int no)
169 struct Dict_file_block *p;
170 if ((p = find_block (dbf, no)))