Browse Source
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>maint


4 changed files with 33 additions and 59 deletions
@ -1,50 +1,27 @@
@@ -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); |
||||
} |
||||
|
Loading…
Reference in new issue