Skip to content

Commit 521aa8c

Browse files
committed
win32: teach the allocator to deal with crtdbg
Move the MSVC C runtime debugging bits into the allocator's global init function.
1 parent fe12423 commit 521aa8c

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/alloc.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#include "allocators/stdalloc.h"
1111
#include "allocators/win32_crtdbg.h"
1212

13+
#if defined(GIT_MSVC_CRTDBG)
14+
# include "win32/w32_stack.h"
15+
# include "win32/w32_crtdbg_stacktrace.h"
16+
#endif
17+
1318
git_allocator git__allocator;
1419

1520
static int setup_default_allocator(void)
@@ -21,8 +26,23 @@ static int setup_default_allocator(void)
2126
#endif
2227
}
2328

29+
#if defined(GIT_MSVC_CRTDBG)
30+
static void allocator_global_shutdown(void)
31+
{
32+
git_win32__crtdbg_stacktrace_cleanup();
33+
git_win32__stack_cleanup();
34+
}
35+
#endif
36+
2437
int git_allocator_global_init(void)
2538
{
39+
#if defined(GIT_MSVC_CRTDBG)
40+
git_win32__crtdbg_stacktrace_init();
41+
git_win32__stack_init();
42+
43+
git__on_shutdown(allocator_global_shutdown);
44+
#endif
45+
2646
/*
2747
* We don't want to overwrite any allocator which has been set before
2848
* 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/global.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
#include "thread-utils.h"
2020
#include "git2/global.h"
2121
#include "transports/ssh.h"
22-
23-
#if defined(GIT_MSVC_CRTDBG)
2422
#include "win32/w32_stack.h"
25-
#include "win32/w32_crtdbg_stacktrace.h"
26-
#endif
2723

2824
git_mutex git__mwindow_mutex;
2925

@@ -72,12 +68,6 @@ static int init_common(void)
7268
size_t i;
7369
int ret;
7470

75-
/* Initialize the CRT debug allocator first, before our first malloc */
76-
#if defined(GIT_MSVC_CRTDBG)
77-
git_win32__crtdbg_stacktrace_init();
78-
git_win32__stack_init();
79-
#endif
80-
8171
/* Initialize subsystems that have global state */
8272
for (i = 0; i < ARRAY_SIZE(git__init_callbacks); i++)
8373
if ((ret = git__init_callbacks[i]()) != 0)
@@ -200,11 +190,6 @@ int git_libgit2_shutdown(void)
200190

201191
FlsFree(_fls_index);
202192
git_mutex_free(&git__mwindow_mutex);
203-
204-
#if defined(GIT_MSVC_CRTDBG)
205-
git_win32__crtdbg_stacktrace_cleanup();
206-
git_win32__stack_cleanup();
207-
#endif
208193
}
209194

210195
/* Exit the lock */

0 commit comments

Comments
 (0)