Commit 6554b40e42df831d7fc9c623d34b2738227dd8a2

Edward Thomson 2020-05-13T10:39:33

settings: localize global data Move the settings global data teardown into its own separate function, instead of intermingled with the global state.

diff --git a/src/global.c b/src/global.c
index d2a25a5..0642977 100644
--- a/src/global.c
+++ b/src/global.c
@@ -11,6 +11,7 @@
 #include "hash.h"
 #include "sysdir.h"
 #include "filter.h"
+#include "settings.h"
 #include "merge_driver.h"
 #include "pool.h"
 #include "streams/registry.h"
@@ -37,15 +38,14 @@ static git_global_init_fn git__init_callbacks[] = {
 	git_openssl_stream_global_init,
 	git_mbedtls_stream_global_init,
 	git_mwindow_global_init,
-	git_pool_global_init
+	git_pool_global_init,
+	git_settings_global_init
 };
 
 static git_global_shutdown_fn git__shutdown_callbacks[ARRAY_SIZE(git__init_callbacks)];
 
 static git_atomic git__n_shutdown_callbacks;
 static git_atomic git__n_inits;
-char *git__user_agent;
-char *git__ssl_ciphers;
 
 void git__on_shutdown(git_global_shutdown_fn callback)
 {
@@ -93,9 +93,6 @@ static void shutdown_common(void)
 		if (cb != NULL)
 			cb();
 	}
-
-	git__free(git__user_agent);
-	git__free(git__ssl_ciphers);
 }
 
 /**
diff --git a/src/global.h b/src/global.h
index db41dad..2a7c727 100644
--- a/src/global.h
+++ b/src/global.h
@@ -35,7 +35,4 @@ typedef void (*git_global_shutdown_fn)(void);
 
 extern void git__on_shutdown(git_global_shutdown_fn callback);
 
-extern const char *git_libgit2__user_agent(void);
-extern const char *git_libgit2__ssl_ciphers(void);
-
 #endif
diff --git a/src/settings.c b/src/settings.c
index 69ebcb7..0426093 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -29,6 +29,28 @@
 #include "streams/openssl.h"
 #include "streams/mbedtls.h"
 
+/* Declarations for tuneable settings */
+extern size_t git_mwindow__window_size;
+extern size_t git_mwindow__mapped_limit;
+extern size_t git_mwindow__file_limit;
+extern size_t git_indexer__max_objects;
+extern bool git_disable_pack_keep_file_checks;
+
+char *git__user_agent;
+char *git__ssl_ciphers;
+
+static void git_settings_global_shutdown(void)
+{
+	git__free(git__user_agent);
+	git__free(git__ssl_ciphers);
+}
+
+int git_settings_global_init(void)
+{
+	git__on_shutdown(git_settings_global_shutdown);
+	return 0;
+}
+
 int git_libgit2_version(int *major, int *minor, int *rev)
 {
 	*major = LIBGIT2_VER_MAJOR;
@@ -56,13 +78,6 @@ int git_libgit2_features(void)
 	;
 }
 
-/* Declarations for tuneable settings */
-extern size_t git_mwindow__window_size;
-extern size_t git_mwindow__mapped_limit;
-extern size_t git_mwindow__file_limit;
-extern size_t git_indexer__max_objects;
-extern bool git_disable_pack_keep_file_checks;
-
 static int config_level_to_sysdir(int config_level)
 {
 	int val = -1;
@@ -88,9 +103,6 @@ static int config_level_to_sysdir(int config_level)
 	return val;
 }
 
-extern char *git__user_agent;
-extern char *git__ssl_ciphers;
-
 const char *git_libgit2__user_agent(void)
 {
 	return git__user_agent;
diff --git a/src/settings.h b/src/settings.h
new file mode 100644
index 0000000..dc42ce9
--- /dev/null
+++ b/src/settings.h
@@ -0,0 +1,11 @@
+/*
+ * 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.
+ */
+
+extern int git_settings_global_init(void);
+
+extern const char *git_libgit2__user_agent(void);
+extern const char *git_libgit2__ssl_ciphers(void);
diff --git a/src/streams/openssl.c b/src/streams/openssl.c
index 6a490d1..58265c1 100644
--- a/src/streams/openssl.c
+++ b/src/streams/openssl.c
@@ -12,6 +12,7 @@
 #include <ctype.h>
 
 #include "global.h"
+#include "settings.h"
 #include "posix.h"
 #include "stream.h"
 #include "streams/socket.h"
diff --git a/src/transports/http.h b/src/transports/http.h
index c02109c..5c360b8 100644
--- a/src/transports/http.h
+++ b/src/transports/http.h
@@ -9,6 +9,7 @@
 #define INCLUDE_transports_http_h__
 
 #include "buffer.h"
+#include "settings.h"
 #include "httpclient.h"
 
 #define GIT_HTTP_REPLAY_MAX 15
diff --git a/tests/core/useragent.c b/tests/core/useragent.c
index c6c5220..2ce935b 100644
--- a/tests/core/useragent.c
+++ b/tests/core/useragent.c
@@ -1,5 +1,5 @@
 #include "clar_libgit2.h"
-#include "global.h"
+#include "settings.h"
 
 void test_core_useragent__get(void)
 {