Subject: [PATCH 15/17] Support unmapping windows on 'temporary' packfiles.

From: "Shawn O. Pearce" <[email-masked]>
Date: Sat, 23 Dec 2006 02:34:44 -0500

If a command opens a packfile for only temporary access and does not
install the struct packed_git* into the global packed_git list then
we are unable to unmap any inactive windows within that packed_git,
causing the overall process to exceed core.packedGitLimit.

We cannot force the callers to install their temporary packfile
into the packed_git chain as doing so would allow that (possibly
corrupt but currently being verified) temporary packfile to become
part of the local ODB, which may allow it to be considered for
object resolution when it may not actually be a valid packfile.

So to support unmapping the windows of these temporary packfiles we
also scan the windows of the struct packed_git which was supplied
to use_pack().  Since commands only work with one temporary packfile
at a time scanning the one supplied to use_pack() and all packs
installed into packed_git should cover everything available in
memory.

We also have to be careful to not close the file descriptor of
the packed_git which was handed to use_pack() when all of that
packfile's windows have been unmapped, as we are already past the
open call that would open the packfile and need the file descriptor
to be ready for mmap() after unuse_one_window returns.

Signed-off-by: Shawn O. Pearce <[email-masked]>
---
 sha1_file.c |   44 ++++++++++++++++++++++++++++----------------
 1 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/sha1_file.c b/sha1_file.c
index 7872c2d..a8a6c10 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -451,23 +451,34 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
 	return 0;
 }
 
-static int unuse_one_window(void)
-{
-	struct packed_git *p, *lru_p = NULL;
-	struct pack_window *w, *w_l, *lru_w = NULL, *lru_l = NULL;
-
-	for (p = packed_git; p; p = p->next) {
-		for (w_l = NULL, w = p->windows; w; w = w->next) {
-			if (!w->inuse_cnt) {
-				if (!lru_w || w->last_used < lru_w->last_used) {
-					lru_p = p;
-					lru_w = w;
-					lru_l = w_l;
-				}
+static void scan_windows(struct packed_git *p,
+	struct packed_git **lru_p,
+	struct pack_window **lru_w,
+	struct pack_window **lru_l)
+{
+	struct pack_window *w, *w_l;
+
+	for (w_l = NULL, w = p->windows; w; w = w->next) {
+		if (!w->inuse_cnt) {
+			if (!*lru_w || w->last_used < (*lru_w)->last_used) {
+				*lru_p = p;
+				*lru_w = w;
+				*lru_l = w_l;
 			}
-			w_l = w;
 		}
+		w_l = w;
 	}
+}
+
+static int unuse_one_window(struct packed_git *current)
+{
+	struct packed_git *p, *lru_p = NULL;
+	struct pack_window *lru_w = NULL, *lru_l = NULL;
+
+	if (current)
+		scan_windows(current, &lru_p, &lru_w, &lru_l);
+	for (p = 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);
 		pack_mapped -= lru_w->len;
@@ -475,7 +486,7 @@ static int unuse_one_window(void)
 			lru_l->next = lru_w->next;
 		else {
 			lru_p->windows = lru_w->next;
-			if (!lru_p->windows) {
+			if (!lru_p->windows && lru_p != current) {
 				close(lru_p->pack_fd);
 				lru_p->pack_fd = -1;
 			}
@@ -584,7 +595,8 @@ unsigned char* use_pack(struct packed_git *p,
 			if (win->len > packed_git_window_size)
 				win->len = packed_git_window_size;
 			pack_mapped += win->len;
-			while (packed_git_limit < pack_mapped && unuse_one_window())
+			while (packed_git_limit < pack_mapped
+				&& unuse_one_window(p))
 				; /* nothing */
 			win->base = mmap(NULL, win->len,
 				PROT_READ, MAP_PRIVATE,
-- 
1.4.4.3.g87d8