From 1d8943c640bad4425b8578aae6f680fa8e513bc7 Mon Sep 17 00:00:00 2001 From: Carlos Martin Nieto Date: Thu, 28 Jun 2012 12:05:49 +0200 Subject: [PATCH] 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. --- src/indexer.c | 2 ++ src/mwindow.c | 17 +++++++++++++++++ src/mwindow.h | 1 + src/pack.c | 1 + 4 files changed, 21 insertions(+) diff --git a/src/indexer.c b/src/indexer.c index b4312e15a..1f0ca82a2 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 74fbf7834..1a5446b9c 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 058027251..d4fd19569 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 9b5e0e18f..808ceb70c 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);