From: "Оля Тележная" <olyatelezhnaya@gmail.com>
To: git <git@vger.kernel.org>,
Christian Couder <christian.couder@gmail.com>,
Jeff King <peff@peff.net>
Subject: [PATCH] [Outreachy] cleanup: use list.h in mru.h and mru.c
Date: Wed, 27 Sep 2017 13:18:48 +0300 [thread overview]
Message-ID: <CAL21BmnvJSaN+Tnw7Hdc5P5biAnM5dfWR7gX5FrAG1r_D8th=A@mail.gmail.com> (raw)
Remove implementation of double-linked list in mru.c and mru.h and use
implementation from list.h.
Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>, Jeff King
<peff@peff.net>
---
builtin/pack-objects.c | 5 +++--
mru.c | 51 +++++++++++++++-----------------------------------
mru.h | 31 +++++++++++++-----------------
packfile.c | 6 ++++--
4 files changed, 35 insertions(+), 58 deletions(-)
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index f721137ea..fb4c9be89 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -995,8 +995,8 @@ static int want_object_in_pack(const unsigned char *sha1,
struct packed_git **found_pack,
off_t *found_offset)
{
- struct mru_entry *entry;
int want;
+ struct list_head *pos;
if (!exclude && local && has_loose_object_nonlocal(sha1))
return 0;
@@ -1012,7 +1012,8 @@ static int want_object_in_pack(const unsigned char *sha1,
return want;
}
- for (entry = packed_git_mru.head; entry; entry = entry->next) {
+ list_for_each(pos, &packed_git_mru.list) {
+ struct mru *entry = list_entry(pos, struct mru, list);
struct packed_git *p = entry->item;
off_t offset;
diff --git a/mru.c b/mru.c
index 9dedae028..8b6ba3d9b 100644
--- a/mru.c
+++ b/mru.c
@@ -1,50 +1,29 @@
#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;
-
- while (p) {
- struct mru_entry *to_free = p;
- p = p->next;
+ struct list_head *p1;
+ struct list_head *p2;
+ struct mru *to_free;
+
+ list_for_each_safe(p1, p2, &head->list) {
+ to_free = list_entry(p1, struct mru, list);
free(to_free);
}
- mru->head = mru->tail = NULL;
+ INIT_LIST_HEAD(&head->list);
}
diff --git a/mru.h b/mru.h
index 42e4aeaa1..36a332af0 100644
--- a/mru.h
+++ b/mru.h
@@ -1,6 +1,8 @@
#ifndef MRU_H
#define MRU_H
+#include "list.h"
+
/**
* A simple most-recently-used cache, backed by a doubly-linked list.
*
@@ -8,18 +10,15 @@
*
* // Create a list. Zero-initialization is required.
* static struct mru cache;
- * mru_append(&cache, item);
- * ...
+ * INIT_LIST_HEAD(&cache.list);
*
- * // Iterate in MRU order.
- * struct mru_entry *p;
- * for (p = cache.head; p; p = p->next) {
- * if (matches(p->item))
- * break;
- * }
+ * // Add new item to the end of the list.
+ * void *item;
+ * ...
+ * mru_append(&cache, item);
*
* // Mark an item as used, moving it to the front of the list.
- * mru_mark(&cache, p);
+ * mru_mark(&cache, item);
*
* // Reset the list to empty, cleaning up all resources.
* mru_clear(&cache);
@@ -29,17 +28,13 @@
* you will begin traversing the whole list again.
*/
-struct mru_entry {
- void *item;
- struct mru_entry *prev, *next;
-};
-
struct mru {
- struct mru_entry *head, *tail;
+ struct list_head list;
+ void *item;
};
-void mru_append(struct mru *mru, void *item);
-void mru_mark(struct mru *mru, struct mru_entry *entry);
-void mru_clear(struct mru *mru);
+void mru_append(struct mru *head, void *item);
+void mru_mark(struct mru *head, struct mru *entry);
+void mru_clear(struct mru *head);
#endif /* MRU_H */
diff --git a/packfile.c b/packfile.c
index f69a5c8d6..ae3b0b2e9 100644
--- a/packfile.c
+++ b/packfile.c
@@ -876,6 +876,7 @@ void prepare_packed_git(void)
for (alt = alt_odb_list; alt; alt = alt->next)
prepare_packed_git_one(alt->path, 0);
rearrange_packed_git();
+ INIT_LIST_HEAD(&packed_git_mru.list);
prepare_packed_git_mru();
prepare_packed_git_run_once = 1;
}
@@ -1824,13 +1825,14 @@ static int fill_pack_entry(const unsigned char *sha1,
*/
int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
{
- struct mru_entry *p;
+ struct list_head *pos;
prepare_packed_git();
if (!packed_git)
return 0;
- for (p = packed_git_mru.head; p; p = p->next) {
+ list_for_each(pos, &packed_git_mru.list) {
+ struct mru *p = list_entry(pos, struct mru, list);
if (fill_pack_entry(sha1, e, p->item)) {
mru_mark(&packed_git_mru, p);
return 1;
--
2.14.1.727.g9ddaf86b0
next reply other threads:[~2017-09-27 10:18 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-27 10:18 Оля Тележная [this message]
2017-09-27 11:30 ` [PATCH] [Outreachy] cleanup: use list.h in mru.h and mru.c Christian Couder
2017-09-28 8:38 ` [PATCH Outreachy] mru: use double-linked list from list.h Olga Telezhnaya
2017-09-28 11:03 ` Junio C Hamano
2017-09-28 20:47 ` Jeff King
2017-09-28 21:56 ` Junio C Hamano
2017-09-28 22:19 ` Jeff King
2017-09-28 21:04 ` Jeff King
2017-09-28 22:42 ` Jeff King
2017-09-29 7:18 ` Christian Couder
2017-09-29 7:23 ` Jeff King
2017-09-29 11:50 ` Christian Couder
2017-09-29 16:08 ` Оля Тележная
2017-09-29 20:38 ` Оля Тележная
2017-09-29 23:40 ` Jeff King
2017-09-30 18:09 ` Оля Тележная
2017-10-02 8:22 ` Jeff King
2017-09-29 23:37 ` Jeff King
2017-09-30 0:07 ` Junio C Hamano
2017-09-30 17:51 ` [PATCH v2 " Olga Telezhnaya
2017-10-02 8:20 ` Jeff King
2017-10-02 9:37 ` Оля Тележная
2017-10-03 10:10 ` Jeff King
2017-11-08 1:44 ` Junio C Hamano
2017-11-08 4:22 ` Jeff King
2017-11-10 11:51 ` Оля Тележная
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAL21BmnvJSaN+Tnw7Hdc5P5biAnM5dfWR7gX5FrAG1r_D8th=A@mail.gmail.com' \
--to=olyatelezhnaya@gmail.com \
--cc=christian.couder@gmail.com \
--cc=git@vger.kernel.org \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).