Skip to content

Commit d9d86e4

Browse files
authored
Merge pull request libgit2#5580 from libgit2/ethomson/win32_leakcheck
msvc crtdbg -> win32 leakcheck
2 parents 045748b + 6d44c87 commit d9d86e4

File tree

18 files changed

+672
-689
lines changed

18 files changed

+672
-689
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,15 @@ jobs:
124124
env:
125125
ARCH: amd64
126126
CMAKE_GENERATOR: Visual Studio 16 2019
127-
CMAKE_OPTIONS: -A x64 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON
127+
CMAKE_OPTIONS: -A x64 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON
128128
SKIP_SSH_TESTS: true
129129
SKIP_NEGOTIATE_TESTS: true
130130
- # Windows x86 Visual Studio
131131
os: windows-2019
132132
env:
133133
ARCH: x86
134134
CMAKE_GENERATOR: Visual Studio 16 2019
135-
CMAKE_OPTIONS: -A Win32 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS -DUSE_BUNDLED_ZLIB=ON
135+
CMAKE_OPTIONS: -A Win32 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS -DUSE_BUNDLED_ZLIB=ON
136136
SKIP_SSH_TESTS: true
137137
SKIP_NEGOTIATE_TESTS: true
138138
- # Windows amd64 mingw

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ IF(WIN32)
8989
ENDIF()
9090

9191
IF(MSVC)
92-
# Enable MSVC CRTDBG memory leak reporting when in debug mode.
93-
OPTION(MSVC_CRTDBG "Enable CRTDBG memory leak reporting" OFF)
92+
# Enable leak checking using the debugging C runtime.
93+
OPTION(WIN32_LEAKCHECK "Enable leak reporting via crtdbg" OFF)
9494
ENDIF()
9595

9696
IF (DEPRECATE_HARD)
@@ -129,8 +129,8 @@ IF (MSVC)
129129
SET(CRT_FLAG_RELEASE "/MD")
130130
ENDIF()
131131

132-
IF (MSVC_CRTDBG)
133-
SET(GIT_MSVC_CRTDBG 1)
132+
IF (WIN32_LEAKCHECK)
133+
SET(GIT_WIN32_LEAKCHECK 1)
134134
SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
135135
SET(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
136136
ENDIF()

azure-pipelines.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ jobs:
9696
parameters:
9797
environmentVariables:
9898
CMAKE_GENERATOR: Visual Studio 15 2017
99-
CMAKE_OPTIONS: -A x64 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON
99+
CMAKE_OPTIONS: -A x64 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON
100100
SKIP_SSH_TESTS: true
101101
SKIP_NEGOTIATE_TESTS: true
102102

@@ -109,7 +109,7 @@ jobs:
109109
parameters:
110110
environmentVariables:
111111
CMAKE_GENERATOR: Visual Studio 15 2017
112-
CMAKE_OPTIONS: -A Win32 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS -DUSE_BUNDLED_ZLIB=ON
112+
CMAKE_OPTIONS: -A Win32 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS -DUSE_BUNDLED_ZLIB=ON
113113
SKIP_SSH_TESTS: true
114114
SKIP_NEGOTIATE_TESTS: true
115115

azure-pipelines/nightly.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jobs:
9292
parameters:
9393
environmentVariables:
9494
CMAKE_GENERATOR: Visual Studio 15 2017
95-
CMAKE_OPTIONS: -A x64 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON
95+
CMAKE_OPTIONS: -A x64 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON
9696
RUN_INVASIVE_TESTS: true
9797
SKIP_SSH_TESTS: true
9898

@@ -105,7 +105,7 @@ jobs:
105105
parameters:
106106
environmentVariables:
107107
CMAKE_GENERATOR: Visual Studio 15 2017
108-
CMAKE_OPTIONS: -A Win32 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS
108+
CMAKE_OPTIONS: -A Win32 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS
109109
RUN_INVASIVE_TESTS: true
110110
SKIP_SSH_TESTS: true
111111

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ vNext
1313
replaces `git_reference_is_valid_name`. Tthe former functions are
1414
deprecated.
1515

16+
### Breaking CMake configuration changes
17+
18+
The `MVSC_CRTDBG` configuration option is now `WIN32_LEAKCHECK`.
19+
1620
v1.1
1721
----
1822

src/alloc.c

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,21 @@
99
#include "runtime.h"
1010

1111
#include "allocators/stdalloc.h"
12-
#include "allocators/win32_crtdbg.h"
13-
14-
#if defined(GIT_MSVC_CRTDBG)
15-
# include "win32/w32_stack.h"
16-
# include "win32/w32_crtdbg_stacktrace.h"
17-
#endif
12+
#include "allocators/win32_leakcheck.h"
1813

1914
git_allocator git__allocator;
2015

2116
static int setup_default_allocator(void)
2217
{
23-
#if defined(GIT_MSVC_CRTDBG)
24-
return git_win32_crtdbg_init_allocator(&git__allocator);
18+
#if defined(GIT_WIN32_LEAKCHECK)
19+
return git_win32_leakcheck_init_allocator(&git__allocator);
2520
#else
2621
return git_stdalloc_init_allocator(&git__allocator);
2722
#endif
2823
}
2924

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-
3825
int git_allocator_global_init(void)
3926
{
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-
4827
/*
4928
* We don't want to overwrite any allocator which has been set before
5029
* the init function is called.

src/allocators/win32_crtdbg.c

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/allocators/win32_leakcheck.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "win32_leakcheck.h"
9+
10+
#if defined(GIT_WIN32_LEAKCHECK)
11+
12+
#include "win32/w32_leakcheck.h"
13+
14+
static void *leakcheck_malloc(size_t len, const char *file, int line)
15+
{
16+
void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line);
17+
if (!ptr) git_error_set_oom();
18+
return ptr;
19+
}
20+
21+
static void *leakcheck_calloc(size_t nelem, size_t elsize, const char *file, int line)
22+
{
23+
void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line);
24+
if (!ptr) git_error_set_oom();
25+
return ptr;
26+
}
27+
28+
static char *leakcheck_strdup(const char *str, const char *file, int line)
29+
{
30+
char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line);
31+
if (!ptr) git_error_set_oom();
32+
return ptr;
33+
}
34+
35+
static char *leakcheck_strndup(const char *str, size_t n, const char *file, int line)
36+
{
37+
size_t length = 0, alloclength;
38+
char *ptr;
39+
40+
length = p_strnlen(str, n);
41+
42+
if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
43+
!(ptr = leakcheck_malloc(alloclength, file, line)))
44+
return NULL;
45+
46+
if (length)
47+
memcpy(ptr, str, length);
48+
49+
ptr[length] = '\0';
50+
51+
return ptr;
52+
}
53+
54+
static char *leakcheck_substrdup(const char *start, size_t n, const char *file, int line)
55+
{
56+
char *ptr;
57+
size_t alloclen;
58+
59+
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
60+
!(ptr = leakcheck_malloc(alloclen, file, line)))
61+
return NULL;
62+
63+
memcpy(ptr, start, n);
64+
ptr[n] = '\0';
65+
return ptr;
66+
}
67+
68+
static void *leakcheck_realloc(void *ptr, size_t size, const char *file, int line)
69+
{
70+
void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line);
71+
if (!new_ptr) git_error_set_oom();
72+
return new_ptr;
73+
}
74+
75+
static void *leakcheck_reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
76+
{
77+
size_t newsize;
78+
79+
if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize))
80+
return NULL;
81+
82+
return leakcheck_realloc(ptr, newsize, file, line);
83+
}
84+
85+
static void *leakcheck_mallocarray(size_t nelem, size_t elsize, const char *file, int line)
86+
{
87+
return leakcheck_reallocarray(NULL, nelem, elsize, file, line);
88+
}
89+
90+
static void leakcheck_free(void *ptr)
91+
{
92+
free(ptr);
93+
}
94+
95+
int git_win32_leakcheck_init_allocator(git_allocator *allocator)
96+
{
97+
allocator->gmalloc = leakcheck_malloc;
98+
allocator->gcalloc = leakcheck_calloc;
99+
allocator->gstrdup = leakcheck_strdup;
100+
allocator->gstrndup = leakcheck_strndup;
101+
allocator->gsubstrdup = leakcheck_substrdup;
102+
allocator->grealloc = leakcheck_realloc;
103+
allocator->greallocarray = leakcheck_reallocarray;
104+
allocator->gmallocarray = leakcheck_mallocarray;
105+
allocator->gfree = leakcheck_free;
106+
return 0;
107+
}
108+
109+
#else
110+
111+
int git_win32_leakcheck_init_allocator(git_allocator *allocator)
112+
{
113+
GIT_UNUSED(allocator);
114+
git_error_set(GIT_EINVALID, "leakcheck memory allocator not available");
115+
return -1;
116+
}
117+
118+
#endif
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* a Linking Exception. For full terms see the included COPYING file.
66
*/
77

8-
#ifndef INCLUDE_allocators_crtdbg_h
9-
#define INCLUDE_allocators_crtdbg_h
8+
#ifndef INCLUDE_allocators_win32_leakcheck_h
9+
#define INCLUDE_allocators_win32_leakcheck_h
1010

1111
#include "common.h"
1212

1313
#include "alloc.h"
1414

15-
int git_win32_crtdbg_init_allocator(git_allocator *allocator);
15+
int git_win32_leakcheck_init_allocator(git_allocator *allocator);
1616

1717
#endif

src/features.h.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#cmakedefine GIT_DEBUG_POOL 1
55
#cmakedefine GIT_TRACE 1
66
#cmakedefine GIT_THREADS 1
7-
#cmakedefine GIT_MSVC_CRTDBG 1
7+
#cmakedefine GIT_WIN32_LEAKCHECK 1
88

99
#cmakedefine GIT_ARCH_64 1
1010
#cmakedefine GIT_ARCH_32 1

0 commit comments

Comments
 (0)