diff options
author | Olga Telezhnaya <olyatelezhnaya@gmail.com> | 2017-09-30 20:51:01 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-10-01 11:30:26 +0300 |
commit | 8865859dfc346c61f0e75fa429c5d307bd27368c (patch) | |
tree | 81b8f55f9ca4f3e4c523a5f2072feccf792e176e /mru.c | |
parent | 20fed7cad40ed0b96232feb828129e3a2ee9860d (diff) |
mru: use double-linked list from list.h
Simplify mru.[ch] and related code by reusing the double-linked list
implementation from list.h instead of a custom one.
This commit is an intermediate step. Our final goal is to get rid of
mru.[ch] at all and inline all logic.
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored by: Jeff King <peff@peff.net>
Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'mru.c')
-rw-r--r-- | mru.c | 49 |
1 files changed, 13 insertions, 36 deletions
@@ -1,50 +1,27 @@ #include "cache.h" #include "mru.h" -void mru_append(struct mru *mru, void *item) +void mru_append(struct mru *head, void *item) { - struct mru_entry *cur = xmalloc(sizeof(*cur)); + struct mru *cur = xmalloc(sizeof(*cur)); cur->item = item; - cur->prev = mru->tail; - cur->next = NULL; - - if (mru->tail) - mru->tail->next = cur; - else - mru->head = cur; - mru->tail = cur; + list_add_tail(&cur->list, &head->list); } -void mru_mark(struct mru *mru, struct mru_entry *entry) +void mru_mark(struct mru *head, struct mru *entry) { - /* If we're already at the front of the list, nothing to do */ - if (mru->head == entry) - return; - - /* Otherwise, remove us from our current slot... */ - if (entry->prev) - entry->prev->next = entry->next; - if (entry->next) - entry->next->prev = entry->prev; - else - mru->tail = entry->prev; - - /* And insert us at the beginning. */ - entry->prev = NULL; - entry->next = mru->head; - if (mru->head) - mru->head->prev = entry; - mru->head = entry; + /* To mark means to put at the front of the list. */ + list_del(&entry->list); + list_add(&entry->list, &head->list); } -void mru_clear(struct mru *mru) +void mru_clear(struct mru *head) { - struct mru_entry *p = mru->head; + struct list_head *pos; + struct list_head *tmp; - while (p) { - struct mru_entry *to_free = p; - p = p->next; - free(to_free); + list_for_each_safe(pos, tmp, &head->list) { + free(list_entry(pos, struct mru, list)); } - mru->head = mru->tail = NULL; + INIT_LIST_HEAD(&head->list); } |