vendor/libgit2/src/mwindow.c in rugged-0.17.0.b7 vs vendor/libgit2/src/mwindow.c in rugged-0.18.0.b1

- old
+ new

@@ -1,7 +1,7 @@ /* - * Copyright (C) 2009-2012 the libgit2 contributors + * Copyright (C) the libgit2 contributors. All rights reserved. * * This file is part of libgit2, distributed under the GNU GPL v2 with * a Linking Exception. For full terms see the included COPYING file. */ @@ -18,21 +18,12 @@ : 32 * 1024 * 1024) #define DEFAULT_MAPPED_LIMIT \ ((1024 * 1024) * (sizeof(void*) >= 8 ? 8192ULL : 256UL)) -/* - * These are the global options for mmmap limits. - * TODO: allow the user to change these - */ -static struct { - size_t window_size; - size_t mapped_limit; -} _mw_options = { - DEFAULT_WINDOW_SIZE, - DEFAULT_MAPPED_LIMIT, -}; +size_t git_mwindow__window_size = DEFAULT_WINDOW_SIZE; +size_t git_mwindow__mapped_limit = DEFAULT_MAPPED_LIMIT; /* Whenever you want to read or modify this, grab git__mwindow_mutex */ static git_mwindow_ctl mem_ctl; /* @@ -40,13 +31,16 @@ * with the file */ void git_mwindow_free_all(git_mwindow_file *mwf) { git_mwindow_ctl *ctl = &mem_ctl; - unsigned int i; + size_t i; - git_mutex_lock(&git__mwindow_mutex); + if (git_mutex_lock(&git__mwindow_mutex)) { + giterr_set(GITERR_THREAD, "unable to lock mwindow mutex"); + return; + } /* * Remove these windows from the global list */ for (i = 0; i < ctl->windowfiles.length; ++i){ @@ -119,11 +113,11 @@ * lock from new_window. */ static int git_mwindow_close_lru(git_mwindow_file *mwf) { git_mwindow_ctl *ctl = &mem_ctl; - unsigned int i; + size_t i; git_mwindow *lru_w = NULL, *lru_l = NULL, **list = &mwf->windows; /* FIXME: Does this give us any advantage? */ if(mwf->windows) git_mwindow_scan_lru(mwf, &lru_w, &lru_l); @@ -161,11 +155,11 @@ git_file fd, git_off_t size, git_off_t offset) { git_mwindow_ctl *ctl = &mem_ctl; - size_t walign = _mw_options.window_size / 2; + size_t walign = git_mwindow__window_size / 2; git_off_t len; git_mwindow *w; w = git__malloc(sizeof(*w)); @@ -174,20 +168,20 @@ memset(w, 0x0, sizeof(*w)); w->offset = (offset / walign) * walign; len = size - w->offset; - if (len > (git_off_t)_mw_options.window_size) - len = (git_off_t)_mw_options.window_size; + if (len > (git_off_t)git_mwindow__window_size) + len = (git_off_t)git_mwindow__window_size; ctl->mapped += (size_t)len; - while (_mw_options.mapped_limit < ctl->mapped && + while (git_mwindow__mapped_limit < ctl->mapped && git_mwindow_close_lru(mwf) == 0) /* nop */; /* - * We treat _mw_options.mapped_limit as a soft limit. If we can't find a + * We treat `mapped_limit` as a soft limit. If we can't find a * window to close and are above the limit, we still mmap the new * window. */ if (git_futils_mmap_ro(&w->window_map, fd, w->offset, (size_t)len) < 0) { @@ -219,11 +213,15 @@ unsigned int *left) { git_mwindow_ctl *ctl = &mem_ctl; git_mwindow *w = *cursor; - git_mutex_lock(&git__mwindow_mutex); + if (git_mutex_lock(&git__mwindow_mutex)) { + giterr_set(GITERR_THREAD, "unable to lock mwindow mutex"); + return NULL; + } + if (!w || !(git_mwindow_contains(w, offset) && git_mwindow_contains(w, offset + extra))) { if (w) { w->inuse_cnt--; } @@ -267,11 +265,15 @@ int git_mwindow_file_register(git_mwindow_file *mwf) { git_mwindow_ctl *ctl = &mem_ctl; int ret; - git_mutex_lock(&git__mwindow_mutex); + if (git_mutex_lock(&git__mwindow_mutex)) { + giterr_set(GITERR_THREAD, "unable to lock mwindow mutex"); + return -1; + } + if (ctl->windowfiles.length == 0 && git_vector_init(&ctl->windowfiles, 8, NULL) < 0) { git_mutex_unlock(&git__mwindow_mutex); return -1; } @@ -280,34 +282,37 @@ git_mutex_unlock(&git__mwindow_mutex); return ret; } -int git_mwindow_file_deregister(git_mwindow_file *mwf) +void git_mwindow_file_deregister(git_mwindow_file *mwf) { git_mwindow_ctl *ctl = &mem_ctl; git_mwindow_file *cur; - unsigned int i; + size_t i; - git_mutex_lock(&git__mwindow_mutex); + if (git_mutex_lock(&git__mwindow_mutex)) + return; + git_vector_foreach(&ctl->windowfiles, i, cur) { if (cur == mwf) { git_vector_remove(&ctl->windowfiles, i); git_mutex_unlock(&git__mwindow_mutex); - return 0; + return; } } git_mutex_unlock(&git__mwindow_mutex); - - 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; if (w) { - git_mutex_lock(&git__mwindow_mutex); + if (git_mutex_lock(&git__mwindow_mutex)) { + giterr_set(GITERR_THREAD, "unable to lock mwindow mutex"); + return; + } + w->inuse_cnt--; git_mutex_unlock(&git__mwindow_mutex); *window = NULL; } }