|
1 | 1 | #include "cache.h" |
2 | 2 | #include "mru.h" |
3 | 3 |
|
4 | | -void mru_append(struct mru *mru, void *item) |
| 4 | +void mru_append(struct mru *head, void *item) |
5 | 5 | { |
6 | | - struct mru_entry *cur = xmalloc(sizeof(*cur)); |
| 6 | + struct mru *cur = xmalloc(sizeof(*cur)); |
7 | 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; |
| 8 | + list_add_tail(&cur->list, &head->list); |
16 | 9 | } |
17 | 10 |
|
18 | | -void mru_mark(struct mru *mru, struct mru_entry *entry) |
| 11 | +void mru_mark(struct mru *head, struct mru *entry) |
19 | 12 | { |
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; |
| 13 | + /* To mark means to put at the front of the list. */ |
| 14 | + list_del(&entry->list); |
| 15 | + list_add(&entry->list, &head->list); |
38 | 16 | } |
39 | 17 |
|
40 | | -void mru_clear(struct mru *mru) |
| 18 | +void mru_clear(struct mru *head) |
41 | 19 | { |
42 | | - struct mru_entry *p = mru->head; |
| 20 | + struct list_head *pos; |
| 21 | + struct list_head *tmp; |
43 | 22 |
|
44 | | - while (p) { |
45 | | - struct mru_entry *to_free = p; |
46 | | - p = p->next; |
47 | | - free(to_free); |
| 23 | + list_for_each_safe(pos, tmp, &head->list) { |
| 24 | + free(list_entry(pos, struct mru, list)); |
48 | 25 | } |
49 | | - mru->head = mru->tail = NULL; |
| 26 | + INIT_LIST_HEAD(&head->list); |
50 | 27 | } |
0 commit comments