Subject: [PATCH 1/5] Remove global pointer "packed_git" in favor or set/get function pair

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Tue, 10 Apr 2012 21:39:27 +0700

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 builtin/count-objects.c  |    4 ++--
 builtin/fsck.c           |    4 ++--
 builtin/gc.c             |    2 +-
 builtin/pack-objects.c   |   10 +++++-----
 builtin/pack-redundant.c |    4 ++--
 cache.h                  |    7 +++++--
 pack-revindex.c          |    4 ++--
 server-info.c            |    4 ++--
 sha1_file.c              |   33 ++++++++++++++++++++++-----------
 sha1_name.c              |    2 +-
 10 files changed, 44 insertions(+), 30 deletions(-)

diff --git a/builtin/count-objects.c b/builtin/count-objects.c
index c37cb98..dc217db 100644
--- a/builtin/count-objects.c
+++ b/builtin/count-objects.c
@@ -104,9 +104,9 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
 		struct packed_git *p;
 		unsigned long num_pack = 0;
 		off_t size_pack = 0;
-		if (!packed_git)
+		if (!get_packed_git())
 			prepare_packed_git();
-		for (p = packed_git; p; p = p->next) {
+		for (p = get_packed_git(); p; p = p->next) {
 			if (!p->pack_local)
 				continue;
 			if (open_pack_index(p))
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 67eb553..9dbe6d8 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -669,7 +669,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 		prepare_packed_git();
 
 		if (show_progress) {
-			for (p = packed_git; p; p = p->next) {
+			for (p = get_packed_git(); p; p = p->next) {
 				if (open_pack_index(p))
 					continue;
 				total += p->num_objects;
@@ -677,7 +677,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 
 			progress = start_progress("Checking objects", total);
 		}
-		for (p = packed_git; p; p = p->next) {
+		for (p = get_packed_git(); p; p = p->next) {
 			/* verify gives error messages itself */
 			if (verify_pack(p, fsck_obj_buffer,
 					progress, count))
diff --git a/builtin/gc.c b/builtin/gc.c
index 271376d..3007650 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -130,7 +130,7 @@ static int too_many_packs(void)
 		return 0;
 
 	prepare_packed_git();
-	for (cnt = 0, p = packed_git; p; p = p->next) {
+	for (cnt = 0, p = get_packed_git(); p; p = p->next) {
 		if (!p->pack_local)
 			continue;
 		if (p->pack_keep)
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 7b07c09..2f242c4 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -840,7 +840,7 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,
 	if (!exclude && local && has_loose_object_nonlocal(sha1))
 		return 0;
 
-	for (p = packed_git; p; p = p->next) {
+	for (p = get_packed_git(); p; p = p->next) {
 		off_t offset = find_pack_entry_one(sha1, p);
 		if (offset) {
 			if (!found_pack) {
@@ -2183,7 +2183,7 @@ static void add_objects_in_unpacked_packs(struct rev_info *revs)
 
 	memset(&in_pack, 0, sizeof(in_pack));
 
-	for (p = packed_git; p; p = p->next) {
+	for (p = get_packed_git(); p; p = p->next) {
 		const unsigned char *sha1;
 		struct object *o;
 
@@ -2221,7 +2221,7 @@ static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1)
 	static struct packed_git *last_found = (void *)1;
 	struct packed_git *p;
 
-	p = (last_found != (void *)1) ? last_found : packed_git;
+	p = (last_found != (void *)1) ? last_found : get_packed_git();
 
 	while (p) {
 		if ((!p->pack_local || p->pack_keep) &&
@@ -2230,7 +2230,7 @@ static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1)
 			return 1;
 		}
 		if (p == last_found)
-			p = packed_git;
+			p = get_packed_git();
 		else
 			p = p->next;
 		if (p == last_found)
@@ -2245,7 +2245,7 @@ static void loosen_unused_packed_objects(struct rev_info *revs)
 	uint32_t i;
 	const unsigned char *sha1;
 
-	for (p = packed_git; p; p = p->next) {
+	for (p = get_packed_git(); p; p = p->next) {
 		if (!p->pack_local || p->pack_keep)
 			continue;
 
diff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c
index f5c6afc..8fece6a 100644
--- a/builtin/pack-redundant.c
+++ b/builtin/pack-redundant.c
@@ -569,7 +569,7 @@ static struct pack_list * add_pack(struct packed_git *p)
 
 static struct pack_list * add_pack_file(const char *filename)
 {
-	struct packed_git *p = packed_git;
+	struct packed_git *p = get_packed_git();
 
 	if (strlen(filename) < 40)
 		die("Bad pack filename: %s", filename);
@@ -584,7 +584,7 @@ static struct pack_list * add_pack_file(const char *filename)
 
 static void load_all(void)
 {
-	struct packed_git *p = packed_git;
+	struct packed_git *p = get_packed_git();
 
 	while (p) {
 		add_pack(p);
diff --git a/cache.h b/cache.h
index e5e1aa4..26d14b4 100644
--- a/cache.h
+++ b/cache.h
@@ -975,7 +975,7 @@ struct pack_window {
 	unsigned int inuse_cnt;
 };
 
-extern struct packed_git {
+struct packed_git {
 	struct packed_git *next;
 	struct pack_window *windows;
 	off_t pack_size;
@@ -993,7 +993,10 @@ extern struct packed_git {
 	unsigned char sha1[20];
 	/* something like ".git/objects/pack/xxxxx.pack" */
 	char pack_name[FLEX_ARRAY]; /* more */
-} *packed_git;
+};
+
+extern struct packed_git *get_packed_git(void);
+extern void set_packed_git(struct packed_git *);
 
 struct pack_entry {
 	off_t offset;
diff --git a/pack-revindex.c b/pack-revindex.c
index 77a0465..636d35d 100644
--- a/pack-revindex.c
+++ b/pack-revindex.c
@@ -45,13 +45,13 @@ static void init_pack_revindex(void)
 	int num;
 	struct packed_git *p;
 
-	for (num = 0, p = packed_git; p; p = p->next)
+	for (num = 0, p = get_packed_git(); p; p = p->next)
 		num++;
 	if (!num)
 		return;
 	pack_revindex_hashsz = num * 11;
 	pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
-	for (p = packed_git; p; p = p->next) {
+	for (p = get_packed_git(); p; p = p->next) {
 		num = pack_revindex_ix(p);
 		num = - 1 - num;
 		pack_revindex[num].p = p;
diff --git a/server-info.c b/server-info.c
index 9ec744e..b7951ca 100644
--- a/server-info.c
+++ b/server-info.c
@@ -161,7 +161,7 @@ static void init_pack_info(const char *infofile, int force)
 	objdirlen = strlen(objdir);
 
 	prepare_packed_git();
-	for (p = packed_git; p; p = p->next) {
+	for (p = get_packed_git(); p; p = p->next) {
 		/* we ignore things on alternate path since they are
 		 * not available to the pullers in general.
 		 */
@@ -171,7 +171,7 @@ static void init_pack_info(const char *infofile, int force)
 	}
 	num_pack = i;
 	info = xcalloc(num_pack, sizeof(struct pack_info *));
-	for (i = 0, p = packed_git; p; p = p->next) {
+	for (i = 0, p = get_packed_git(); p; p = p->next) {
 		if (!p->pack_local)
 			continue;
 		info[i] = xcalloc(1, sizeof(struct pack_info));
diff --git a/sha1_file.c b/sha1_file.c
index 4f06a0e..4fd4e2c 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -33,6 +33,17 @@ static inline uintmax_t sz_fmt(size_t s) { return s; }
 
 const unsigned char null_sha1[20];
 
+static struct packed_git *packed_git_;
+struct packed_git *get_packed_git(void)
+{
+	return packed_git_;
+}
+void set_packed_git(struct packed_git *p)
+{
+	packed_git_ = p;
+}
+
+
 /*
  * This is meant to hold a *small* number of objects that you would
  * want read_sha1_file() to be able to return, but yet you do not want
@@ -618,7 +629,7 @@ static int unuse_one_window(struct packed_git *current, int keep_fd)
 
 	if (current)
 		scan_windows(current, &lru_p, &lru_w, &lru_l);
-	for (p = packed_git; p; p = p->next)
+	for (p = get_packed_git(); p; p = p->next)
 		scan_windows(p, &lru_p, &lru_w, &lru_l);
 	if (lru_p) {
 		munmap(lru_w->base, lru_w->len);
@@ -708,7 +719,7 @@ void close_pack_index(struct packed_git *p)
  */
 void free_pack_by_name(const char *pack_name)
 {
-	struct packed_git *p, **pp = &packed_git;
+	struct packed_git *p, **pp = &packed_git_;
 
 	while (*pp) {
 		p = *pp;
@@ -990,8 +1001,8 @@ void install_packed_git(struct packed_git *pack)
 	if (pack->pack_fd != -1)
 		pack_open_fds++;
 
-	pack->next = packed_git;
-	packed_git = pack;
+	pack->next = get_packed_git();
+	set_packed_git(pack);
 }
 
 static void prepare_packed_git_one(char *objdir, int local)
@@ -1026,7 +1037,7 @@ static void prepare_packed_git_one(char *objdir, int local)
 
 		/* Don't reopen a pack we already have. */
 		strcpy(path + len, de->d_name);
-		for (p = packed_git; p; p = p->next) {
+		for (p = get_packed_git(); p; p = p->next) {
 			if (!memcmp(path, p->pack_name, len + namelen - 4))
 				break;
 		}
@@ -1076,14 +1087,14 @@ static void rearrange_packed_git(void)
 	struct packed_git **ary, *p;
 	int i, n;
 
-	for (n = 0, p = packed_git; p; p = p->next)
+	for (n = 0, p = get_packed_git(); p; p = p->next)
 		n++;
 	if (n < 2)
 		return;
 
 	/* prepare an array of packed_git for easier sorting */
 	ary = xcalloc(n, sizeof(struct packed_git *));
-	for (n = 0, p = packed_git; p; p = p->next)
+	for (n = 0, p = get_packed_git(); p; p = p->next)
 		ary[n++] = p;
 
 	qsort(ary, n, sizeof(struct packed_git *), sort_pack);
@@ -1092,7 +1103,7 @@ static void rearrange_packed_git(void)
 	for (i = 0; i < n - 1; i++)
 		ary[i]->next = ary[i + 1];
 	ary[n - 1]->next = NULL;
-	packed_git = ary[0];
+	set_packed_git(ary[0]);
 
 	free(ary);
 }
@@ -1139,7 +1150,7 @@ static const struct packed_git *has_packed_and_bad(const unsigned char *sha1)
 	struct packed_git *p;
 	unsigned i;
 
-	for (p = packed_git; p; p = p->next)
+	for (p = get_packed_git(); p; p = p->next)
 		for (i = 0; i < p->num_bad_objects; i++)
 			if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
 				return p;
@@ -2058,13 +2069,13 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
 	struct packed_git *p;
 
 	prepare_packed_git();
-	if (!packed_git)
+	if (!get_packed_git())
 		return 0;
 
 	if (last_found_pack && fill_pack_entry(sha1, e, last_found_pack))
 		return 1;
 
-	for (p = packed_git; p; p = p->next) {
+	for (p = get_packed_git(); p; p = p->next) {
 		if (p == last_found_pack || !fill_pack_entry(sha1, e, p))
 			continue;
 
diff --git a/sha1_name.c b/sha1_name.c
index 03ffc2c..756b226 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -78,7 +78,7 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
 	int found = 0;
 
 	prepare_packed_git();
-	for (p = packed_git; p && found < 2; p = p->next) {
+	for (p = get_packed_git(); p && found < 2; p = p->next) {
 		uint32_t num, last;
 		uint32_t first = 0;
 		open_pack_index(p);
-- 
1.7.8.36.g69ee2