Commit 1d8943c640bad4425b8578aae6f680fa8e513bc7

Carlos Martin Nieto 2012-06-28T12:05:49

mwindow: allow memory-window files to deregister Once a file is registered, there is no way to deregister it, even after the structure that contains it is no longer needed and has been freed. This may be the source of #624. Allow and use the deregister function to remove our file from the global list.

diff --git a/src/indexer.c b/src/indexer.c
index b4312e1..1f0ca82 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -773,6 +773,7 @@ int git_indexer_write(git_indexer *idx)
 
 cleanup:
 	git_mwindow_free_all(&idx->pack->mwf);
+	git_mwindow_file_deregister(&idx->pack->mwf);
 	if (error < 0)
 		git_filebuf_cleanup(&idx->file);
 	git_buf_free(&filename);
@@ -886,6 +887,7 @@ void git_indexer_free(git_indexer *idx)
 		return;
 
 	p_close(idx->pack->mwf.fd);
+	git_mwindow_file_deregister(&idx->pack->mwf);
 	git_vector_foreach(&idx->objects, i, e)
 		git__free(e);
 	git_vector_free(&idx->objects);
diff --git a/src/mwindow.c b/src/mwindow.c
index 74fbf78..1a5446b 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -261,6 +261,23 @@ int git_mwindow_file_register(git_mwindow_file *mwf)
 	return git_vector_insert(&ctl->windowfiles, mwf);
 }
 
+int git_mwindow_file_deregister(git_mwindow_file *mwf)
+{
+	git_mwindow_ctl *ctl = &GIT_GLOBAL->mem_ctl;
+	git_mwindow_file *cur;
+	unsigned int i;
+
+	git_vector_foreach(&ctl->windowfiles, i, cur) {
+		if (cur == mwf) {
+			git_vector_remove(&ctl->windowfiles, i);
+			return 0;
+		}
+	}
+
+	giterr_set(GITERR_ODB, "Failed to find the memory window file to deregister");
+	return -1;
+}
+
 void git_mwindow_close(git_mwindow **window)
 {
 	git_mwindow *w = *window;
diff --git a/src/mwindow.h b/src/mwindow.h
index 0580272..d4fd195 100644
--- a/src/mwindow.h
+++ b/src/mwindow.h
@@ -40,6 +40,7 @@ void git_mwindow_free_all(git_mwindow_file *mwf);
 unsigned char *git_mwindow_open(git_mwindow_file *mwf, git_mwindow **cursor, git_off_t offset, size_t extra, unsigned int *left);
 void git_mwindow_scan_lru(git_mwindow_file *mwf, git_mwindow **lru_w, git_mwindow **lru_l);
 int git_mwindow_file_register(git_mwindow_file *mwf);
+int git_mwindow_file_deregister(git_mwindow_file *mwf);
 void git_mwindow_close(git_mwindow **w_cursor);
 
 #endif
diff --git a/src/pack.c b/src/pack.c
index 9b5e0e1..808ceb7 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -535,6 +535,7 @@ void packfile_free(struct git_pack_file *p)
 
 	/* clear_delta_base_cache(); */
 	git_mwindow_free_all(&p->mwf);
+	git_mwindow_file_deregister(&p->mwf);
 
 	if (p->mwf.fd != -1)
 		p_close(p->mwf.fd);