Merge pull request #1504 from ethomson/git_atomic_ssize git_atomic_ssize for 64-bit atomics only on 64-bit platforms
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6bd25aa..1831c87 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -277,6 +277,15 @@ ELSE()
ENDIF()
FILE(GLOB SRC_GIT2 src/*.c src/transports/*.c src/xdiff/*.c)
+# Determine architecture of the machine
+IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
+ ADD_DEFINITIONS(-DGIT_ARCH_64)
+ELSEIF (CMAKE_SIZEOF_VOID_P EQUAL 4)
+ ADD_DEFINITIONS(-DGIT_ARCH_32)
+ELSE()
+ message(FATAL_ERROR "Unsupported architecture")
+ENDIF()
+
# Compile and link libgit2
ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
diff --git a/src/cache.c b/src/cache.c
index be4b037..1360cc9 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -18,8 +18,8 @@
GIT__USE_OIDMAP
bool git_cache__enabled = true;
-int64_t git_cache__max_storage = (256 * 1024 * 1024);
-git_atomic64 git_cache__current_storage = {0};
+ssize_t git_cache__max_storage = (256 * 1024 * 1024);
+git_atomic_ssize git_cache__current_storage = {0};
static size_t git_cache__max_object_size[8] = {
0, /* GIT_OBJ__EXT1 */
@@ -85,7 +85,7 @@ static void clear_cache(git_cache *cache)
});
kh_clear(oid, cache->map);
- git_atomic64_add(&git_cache__current_storage, -cache->used_memory);
+ git_atomic_ssize_add(&git_cache__current_storage, -cache->used_memory);
cache->used_memory = 0;
}
@@ -111,7 +111,8 @@ void git_cache_free(git_cache *cache)
static void cache_evict_entries(git_cache *cache)
{
uint32_t seed = rand();
- int64_t evicted_memory = 0, evict_count = 8;
+ size_t evict_count = 8;
+ ssize_t evicted_memory = 0;
/* do not infinite loop if there's not enough entries to evict */
if (evict_count > kh_size(cache->map)) {
@@ -134,7 +135,7 @@ static void cache_evict_entries(git_cache *cache)
}
cache->used_memory -= evicted_memory;
- git_atomic64_add(&git_cache__current_storage, -evicted_memory);
+ git_atomic_ssize_add(&git_cache__current_storage, -evicted_memory);
}
static bool cache_should_store(git_otype object_type, size_t object_size)
@@ -195,7 +196,7 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry)
kh_val(cache->map, pos) = entry;
git_cached_obj_incref(entry);
cache->used_memory += entry->size;
- git_atomic64_add(&git_cache__current_storage, (int64_t)entry->size);
+ git_atomic_ssize_add(&git_cache__current_storage, (ssize_t)entry->size);
}
}
/* found */
diff --git a/src/cache.h b/src/cache.h
index 16470e9..53fbcf4 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -31,12 +31,12 @@ typedef struct {
typedef struct {
git_oidmap *map;
git_mutex lock;
- int64_t used_memory;
+ ssize_t used_memory;
} git_cache;
extern bool git_cache__enabled;
-extern int64_t git_cache__max_storage;
-extern git_atomic64 git_cache__current_storage;
+extern ssize_t git_cache__max_storage;
+extern git_atomic_ssize git_cache__current_storage;
int git_cache_set_max_object_size(git_otype type, size_t size);
diff --git a/src/thread-utils.h b/src/thread-utils.h
index 28ecd29..49b5f3b 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -18,6 +18,8 @@ typedef struct {
#endif
} git_atomic;
+#ifdef GIT_ARCH_64
+
typedef struct {
#if defined(GIT_WIN32)
__int64 val;
@@ -26,6 +28,18 @@ typedef struct {
#endif
} git_atomic64;
+typedef git_atomic64 git_atomic_ssize;
+
+#define git_atomic_ssize_add git_atomic64_add
+
+#else
+
+typedef git_atomic git_atomic_ssize;
+
+#define git_atomic_ssize_add git_atomic_add
+
+#endif
+
GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
{
a->val = val;
@@ -68,7 +82,7 @@ GIT_INLINE(int) git_atomic_inc(git_atomic *a)
GIT_INLINE(int) git_atomic_add(git_atomic *a, int32_t addend)
{
#if defined(GIT_WIN32)
- return _InterlockedExchangeAdd(&a->val, addend);
+ return InterlockedExchangeAdd(&a->val, addend);
#elif defined(__GNUC__)
return __sync_add_and_fetch(&a->val, addend);
#else
@@ -101,10 +115,12 @@ GIT_INLINE(void *) git___compare_and_swap(
return (foundval == oldval) ? oldval : newval;
}
-GIT_INLINE(int) git_atomic64_add(git_atomic64 *a, int64_t addend)
+#ifdef GIT_ARCH_64
+
+GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
{
#if defined(GIT_WIN32)
- return _InterlockedExchangeAdd64(&a->val, addend);
+ return InterlockedExchangeAdd64(&a->val, addend);
#elif defined(__GNUC__)
return __sync_add_and_fetch(&a->val, addend);
#else
@@ -112,6 +128,8 @@ GIT_INLINE(int) git_atomic64_add(git_atomic64 *a, int64_t addend)
#endif
}
+#endif
+
#else
#define git_thread unsigned int
@@ -161,7 +179,9 @@ GIT_INLINE(void *) git___compare_and_swap(
return oldval;
}
-GIT_INLINE(int) git_atomic64_add(git_atomic64 *a, int64_t addend)
+#ifdef GIT_ARCH_64
+
+GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
{
a->val += addend;
return a->val;
@@ -169,6 +189,8 @@ GIT_INLINE(int) git_atomic64_add(git_atomic64 *a, int64_t addend)
#endif
+#endif
+
/* Atomically replace oldval with newval
* @return oldval if it was replaced or newval if it was not
*/
diff --git a/src/util.c b/src/util.c
index ce67c7e..8c8bc1a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -103,7 +103,7 @@ int git_libgit2_opts(int key, ...)
}
case GIT_OPT_SET_CACHE_MAX_SIZE:
- git_cache__max_storage = va_arg(ap, int64_t);
+ git_cache__max_storage = va_arg(ap, ssize_t);
break;
case GIT_OPT_ENABLE_CACHING:
@@ -111,8 +111,8 @@ int git_libgit2_opts(int key, ...)
break;
case GIT_OPT_GET_CACHED_MEMORY:
- *(va_arg(ap, int64_t *)) = git_cache__current_storage.val;
- *(va_arg(ap, int64_t *)) = git_cache__max_storage;
+ *(va_arg(ap, ssize_t *)) = git_cache__current_storage.val;
+ *(va_arg(ap, ssize_t *)) = git_cache__max_storage;
break;
}