Skip to content

Commit 20450cb

Browse files
authored
Merge pull request libgit2#5546 from libgit2/ethomson/init
Refactor "global" state
2 parents e985864 + 634c285 commit 20450cb

40 files changed

+588
-493
lines changed

src/alloc.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
*/
77

88
#include "alloc.h"
9+
#include "runtime.h"
910

1011
#include "allocators/stdalloc.h"
1112
#include "allocators/win32_crtdbg.h"
1213

14+
#if defined(GIT_MSVC_CRTDBG)
15+
# include "win32/w32_stack.h"
16+
# include "win32/w32_crtdbg_stacktrace.h"
17+
#endif
18+
1319
git_allocator git__allocator;
1420

1521
static int setup_default_allocator(void)
@@ -21,8 +27,24 @@ static int setup_default_allocator(void)
2127
#endif
2228
}
2329

30+
#if defined(GIT_MSVC_CRTDBG)
31+
static void allocator_global_shutdown(void)
32+
{
33+
git_win32__crtdbg_stacktrace_cleanup();
34+
git_win32__stack_cleanup();
35+
}
36+
#endif
37+
2438
int git_allocator_global_init(void)
2539
{
40+
#if defined(GIT_MSVC_CRTDBG)
41+
git_win32__crtdbg_stacktrace_init();
42+
git_win32__stack_init();
43+
44+
if (git_runtime_shutdown_register(allocator_global_shutdown) < 0)
45+
return -1;
46+
#endif
47+
2648
/*
2749
* We don't want to overwrite any allocator which has been set before
2850
* the init function is called.

src/allocators/win32_crtdbg.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#if defined(GIT_MSVC_CRTDBG)
1111

12+
#include "win32/w32_stack.h"
1213
#include "win32/w32_crtdbg_stacktrace.h"
1314

1415
static void *crtdbg__malloc(size_t len, const char *file, int line)

src/errors.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "common.h"
99

10-
#include "global.h"
10+
#include "threadstate.h"
1111
#include "posix.h"
1212
#include "buffer.h"
1313

@@ -22,18 +22,18 @@ static git_error g_git_oom_error = {
2222

2323
static void set_error_from_buffer(int error_class)
2424
{
25-
git_error *error = &GIT_GLOBAL->error_t;
26-
git_buf *buf = &GIT_GLOBAL->error_buf;
25+
git_error *error = &GIT_THREADSTATE->error_t;
26+
git_buf *buf = &GIT_THREADSTATE->error_buf;
2727

2828
error->message = buf->ptr;
2929
error->klass = error_class;
3030

31-
GIT_GLOBAL->last_error = error;
31+
GIT_THREADSTATE->last_error = error;
3232
}
3333

3434
static void set_error(int error_class, char *string)
3535
{
36-
git_buf *buf = &GIT_GLOBAL->error_buf;
36+
git_buf *buf = &GIT_THREADSTATE->error_buf;
3737

3838
git_buf_clear(buf);
3939
if (string) {
@@ -46,7 +46,7 @@ static void set_error(int error_class, char *string)
4646

4747
void git_error_set_oom(void)
4848
{
49-
GIT_GLOBAL->last_error = &g_git_oom_error;
49+
GIT_THREADSTATE->last_error = &g_git_oom_error;
5050
}
5151

5252
void git_error_set(int error_class, const char *fmt, ...)
@@ -64,7 +64,7 @@ void git_error_vset(int error_class, const char *fmt, va_list ap)
6464
DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0;
6565
#endif
6666
int error_code = (error_class == GIT_ERROR_OS) ? errno : 0;
67-
git_buf *buf = &GIT_GLOBAL->error_buf;
67+
git_buf *buf = &GIT_THREADSTATE->error_buf;
6868

6969
git_buf_clear(buf);
7070
if (fmt) {
@@ -97,7 +97,7 @@ void git_error_vset(int error_class, const char *fmt, va_list ap)
9797

9898
int git_error_set_str(int error_class, const char *string)
9999
{
100-
git_buf *buf = &GIT_GLOBAL->error_buf;
100+
git_buf *buf = &GIT_THREADSTATE->error_buf;
101101

102102
assert(string);
103103

@@ -118,9 +118,9 @@ int git_error_set_str(int error_class, const char *string)
118118

119119
void git_error_clear(void)
120120
{
121-
if (GIT_GLOBAL->last_error != NULL) {
121+
if (GIT_THREADSTATE->last_error != NULL) {
122122
set_error(0, NULL);
123-
GIT_GLOBAL->last_error = NULL;
123+
GIT_THREADSTATE->last_error = NULL;
124124
}
125125

126126
errno = 0;
@@ -131,13 +131,13 @@ void git_error_clear(void)
131131

132132
const git_error *git_error_last(void)
133133
{
134-
return GIT_GLOBAL->last_error;
134+
return GIT_THREADSTATE->last_error;
135135
}
136136

137137
int git_error_state_capture(git_error_state *state, int error_code)
138138
{
139-
git_error *error = GIT_GLOBAL->last_error;
140-
git_buf *error_buf = &GIT_GLOBAL->error_buf;
139+
git_error *error = GIT_THREADSTATE->last_error;
140+
git_buf *error_buf = &GIT_THREADSTATE->error_buf;
141141

142142
memset(state, 0, sizeof(git_error_state));
143143

src/filter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "futils.h"
1212
#include "hash.h"
1313
#include "repository.h"
14-
#include "global.h"
14+
#include "runtime.h"
1515
#include "git2/sys/filter.h"
1616
#include "git2/config.h"
1717
#include "blob.h"
@@ -206,7 +206,7 @@ int git_filter_global_init(void)
206206
GIT_FILTER_IDENT, ident, GIT_FILTER_IDENT_PRIORITY) < 0)
207207
error = -1;
208208

209-
git__on_shutdown(git_filter_global_shutdown);
209+
error = git_runtime_shutdown_register(git_filter_global_shutdown);
210210

211211
done:
212212
if (error) {

src/futils.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
#include "futils.h"
99

10-
#include "global.h"
10+
#include "runtime.h"
1111
#include "strmap.h"
12+
#include "hash.h"
1213
#include <ctype.h>
1314
#if GIT_WIN32
1415
#include "win32/findfile.h"

0 commit comments

Comments
 (0)