Commit 03c0938f5b389ef33946849636510fbc0c4388f4

lhchavez 2020-08-25T06:13:38

Avoid using atomics in pool.c Instead, globally initialize the system page size.

diff --git a/src/global.c b/src/global.c
index 5af35aa..9fe8cd5 100644
--- a/src/global.c
+++ b/src/global.c
@@ -12,6 +12,7 @@
 #include "sysdir.h"
 #include "filter.h"
 #include "merge_driver.h"
+#include "pool.h"
 #include "streams/registry.h"
 #include "streams/mbedtls.h"
 #include "streams/openssl.h"
@@ -38,7 +39,8 @@ static git_global_init_fn git__init_callbacks[] = {
 	git_stream_registry_global_init,
 	git_openssl_stream_global_init,
 	git_mbedtls_stream_global_init,
-	git_mwindow_global_init
+	git_mwindow_global_init,
+	git_pool_global_init
 };
 
 static git_global_shutdown_fn git__shutdown_callbacks[ARRAY_SIZE(git__init_callbacks)];
diff --git a/src/pool.c b/src/pool.c
index 34d0adc..0c423dd 100644
--- a/src/pool.c
+++ b/src/pool.c
@@ -21,23 +21,19 @@ struct git_pool_page {
 
 static void *pool_alloc_page(git_pool *pool, size_t size);
 
-static size_t pool_system_page_size(void)
-{
-	static git_atomic_ssize cached_size = {0};
-	size_t page_size = 0;
+#ifndef GIT_DEBUG_POOL
 
-	if ((page_size = (size_t)git_atomic_ssize_get(&cached_size)) == 0) {
-		if (git__page_size(&page_size) < 0)
-			page_size = 4096;
-		/* allow space for malloc overhead */
-		page_size -= (2 * sizeof(void *)) + sizeof(git_pool_page);
-		git_atomic_ssize_set(&cached_size, (int64_t)page_size);
-	}
+static size_t system_page_size = 0;
 
-	return page_size;
+int git_pool_global_init(void)
+{
+	if (git__page_size(&system_page_size) < 0)
+		system_page_size = 4096;
+	/* allow space for malloc overhead */
+	system_page_size -= (2 * sizeof(void *)) + sizeof(git_pool_page);
+	return 0;
 }
 
-#ifndef GIT_DEBUG_POOL
 int git_pool_init(git_pool *pool, size_t item_size)
 {
 	assert(pool);
@@ -45,7 +41,7 @@ int git_pool_init(git_pool *pool, size_t item_size)
 
 	memset(pool, 0, sizeof(git_pool));
 	pool->item_size = item_size;
-	pool->page_size = pool_system_page_size();
+	pool->page_size = system_page_size;
 
 	return 0;
 }
@@ -115,6 +111,11 @@ bool git_pool__ptr_in_pool(git_pool *pool, void *ptr)
 
 #else
 
+int git_pool_global_init(void)
+{
+	return 0;
+}
+
 static int git_pool__ptr_cmp(const void * a, const void * b)
 {
 	if(a > b) {
diff --git a/src/pool.h b/src/pool.h
index 969d0e7..cecb846 100644
--- a/src/pool.h
+++ b/src/pool.h
@@ -135,4 +135,12 @@ extern uint32_t git_pool__open_pages(git_pool *pool);
 #endif
 extern bool git_pool__ptr_in_pool(git_pool *pool, void *ptr);
 
+/**
+ * This function is being called by our global setup routines to
+ * initialize the system pool size.
+ *
+ * @return 0 on success, <0 on failure
+ */
+extern int git_pool_global_init(void);
+
 #endif
diff --git a/src/thread-utils.h b/src/thread-utils.h
index e0bb381..ecb4909 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -21,8 +21,7 @@
 
 # if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1))
 #  error Atomic primitives do not exist on this version of gcc; configure libgit2 with -DTHREADSAFE=OFF
-# endif
-# if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
+# elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
 #  define GIT_BUILTIN_ATOMIC
 # else
 #  define GIT_BUILTIN_SYNC