1#include "cache.h"
2#include "mru.h"
3
4void mru_append(struct mru *mru, void *item)
5{
6 struct mru_entry *cur = xmalloc(sizeof(*cur));
7 cur->item = item;
8 cur->prev = mru->tail;
9 cur->next = NULL;
10
11 if (mru->tail)
12 mru->tail->next = cur;
13 else
14 mru->head = cur;
15 mru->tail = cur;
16}
17
18void mru_mark(struct mru *mru, struct mru_entry *entry)
19{
20 /* If we're already at the front of the list, nothing to do */
21 if (mru->head == entry)
22 return;
23
24 /* Otherwise, remove us from our current slot... */
25 if (entry->prev)
26 entry->prev->next = entry->next;
27 if (entry->next)
28 entry->next->prev = entry->prev;
29 else
30 mru->tail = entry->prev;
31
32 /* And insert us at the beginning. */
33 entry->prev = NULL;
34 entry->next = mru->head;
35 if (mru->head)
36 mru->head->prev = entry;
37 mru->head = entry;
38}
39
40void mru_clear(struct mru *mru)
41{
42 struct mru_entry *p = mru->head;
43
44 while (p) {
45 struct mru_entry *to_free = p;
46 p = p->next;
47 free(to_free);
48 }
49 mru->head = mru->tail = NULL;
50}