Global option setters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
diff --git a/include/git2/common.h b/include/git2/common.h
index b8c3e42..80d83d3 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -131,6 +131,8 @@ enum {
GIT_OPT_SET_MWINDOW_MAPPED_LIMIT,
GIT_OPT_GET_SEARCH_PATH,
GIT_OPT_SET_SEARCH_PATH,
+ GIT_OPT_SET_CACHE_LIMIT,
+ GIT_OPT_ENABLE_CACHING
};
/**
diff --git a/src/cache.c b/src/cache.c
index 9f3290f..f8cddea 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -17,6 +17,8 @@
GIT__USE_OIDMAP
+bool git_cache__enabled = true;
+
size_t git_cache__max_object_size[8] = {
0, /* GIT_OBJ__EXT1 */
4096, /* GIT_OBJ_COMMIT */
@@ -109,11 +111,7 @@ static void cache_evict_entries(git_cache *cache, size_t evict_count)
static bool cache_should_store(git_otype object_type, size_t object_size)
{
size_t max_size = git_cache__max_object_size[object_type];
-
- if (max_size == 0 || object_size > max_size)
- return false;
-
- return true;
+ return git_cache__enabled && object_size < max_size;
}
static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags)
@@ -121,7 +119,7 @@ static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags)
khiter_t pos;
git_cached_obj *entry = NULL;
- if (git_mutex_lock(&cache->lock) < 0)
+ if (!git_cache__enabled || git_mutex_lock(&cache->lock) < 0)
return NULL;
pos = kh_get(oid, cache->map, oid);
diff --git a/src/cache.h b/src/cache.h
index 65a6e57..8b2aa1f 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -20,6 +20,9 @@ enum {
GIT_CACHE_STORE_PARSED = 2
};
+extern bool git_cache__enabled;
+extern size_t git_cache__max_object_size[8];
+
typedef struct {
git_oid oid;
int16_t type;
diff --git a/src/util.c b/src/util.c
index c4a8c78..0b5fbdc 100644
--- a/src/util.c
+++ b/src/util.c
@@ -11,6 +11,7 @@
#include <ctype.h>
#include "posix.h"
#include "fileops.h"
+#include "cache.h"
#ifdef _MSC_VER
# include <Shlwapi.h>
@@ -93,6 +94,16 @@ int git_libgit2_opts(int key, ...)
if ((error = config_level_to_futils_dir(va_arg(ap, int))) >= 0)
error = git_futils_dirs_set(error, va_arg(ap, const char *));
break;
+
+ case GIT_OPT_SET_CACHE_LIMIT: {
+ git_otype type = (git_otype)va_arg(ap, int);
+ git_cache__max_object_size[type] = va_arg(ap, size_t);
+ break;
+ }
+
+ case GIT_OPT_ENABLE_CACHING:
+ git_cache__enabled = va_arg(ap, int);
+ break;
}
va_end(ap);