Merge branch 'jk/clone-copy-alternates-fix'
[git/debian.git] / mru.c
blob9dedae0287ed2988437a56b307d7262a4953d9eb
1 #include "cache.h"
2 #include "mru.h"
4 void mru_append(struct mru *mru, void *item)
6 struct mru_entry *cur = xmalloc(sizeof(*cur));
7 cur->item = item;
8 cur->prev = mru->tail;
9 cur->next = NULL;
11 if (mru->tail)
12 mru->tail->next = cur;
13 else
14 mru->head = cur;
15 mru->tail = cur;
18 void mru_mark(struct mru *mru, struct mru_entry *entry)
20 /* If we're already at the front of the list, nothing to do */
21 if (mru->head == entry)
22 return;
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;
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;
40 void mru_clear(struct mru *mru)
42 struct mru_entry *p = mru->head;
44 while (p) {
45 struct mru_entry *to_free = p;
46 p = p->next;
47 free(to_free);
49 mru->head = mru->tail = NULL;