Skip to content

Commit 9449518

Browse files
pks-tswisspol
authored andcommitted
win32: fix circular include deps with w32_crtdbg
The current order of declarations and includes between "common.h" and "w32_crtdbg_stacktrace.h" is rather complicated. Both header files make use of things defined in the other one and are thus circularly dependent on each other. This makes it currently impossible to compile the "w32_crtdbg_stacktrace.c" file when including "common.h" inside of "w32_crtdbg_stacktrace.h". We can disentangle the mess by moving declaration of the inline crtdbg functions into the "w32_crtdbg_stacktrace.h" file and adding additional includes inside of it, such that all required functions are available to it. This allows us to break the dependency cycle.
1 parent 0416bd2 commit 9449518

File tree

3 files changed

+107
-107
lines changed

3 files changed

+107
-107
lines changed

src/common.h

-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@
4747
# ifdef GIT_THREADS
4848
# include "win32/thread.h"
4949
# endif
50-
# if defined(GIT_MSVC_CRTDBG)
51-
# include "win32/w32_stack.h"
52-
# include "win32/w32_crtdbg_stacktrace.h"
53-
# endif
5450

5551
#else
5652

src/util.h

+26-103
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,6 @@
1010
#include "git2/buffer.h"
1111
#include "buffer.h"
1212

13-
#if defined(GIT_MSVC_CRTDBG)
14-
/* Enable MSVC CRTDBG memory leak reporting.
15-
*
16-
* We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC
17-
* documentation because all allocs/frees in libgit2 already go through
18-
* the "git__" routines defined in this file. Simply using the normal
19-
* reporting mechanism causes all leaks to be attributed to a routine
20-
* here in util.h (ie, the actual call to calloc()) rather than the
21-
* caller of git__calloc().
22-
*
23-
* Therefore, we declare a set of "git__crtdbg__" routines to replace
24-
* the corresponding "git__" routines and re-define the "git__" symbols
25-
* as macros. This allows us to get and report the file:line info of
26-
* the real caller.
27-
*
28-
* We DO NOT replace the "git__free" routine because it needs to remain
29-
* a function pointer because it is used as a function argument when
30-
* setting up various structure "destructors".
31-
*
32-
* We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes
33-
* "free" to be remapped to "_free_dbg" and this causes problems for
34-
* structures which define a field named "free".
35-
*
36-
* Finally, CRTDBG must be explicitly enabled and configured at program
37-
* startup. See tests/main.c for an example.
38-
*/
39-
#include <stdlib.h>
40-
#include <crtdbg.h>
41-
#include "win32/w32_crtdbg_stacktrace.h"
42-
#endif
43-
4413
#include "common.h"
4514
#include "strnlen.h"
4615

@@ -67,79 +36,33 @@
6736

6837
#if defined(GIT_MSVC_CRTDBG)
6938

70-
GIT_INLINE(void *) git__crtdbg__malloc(size_t len, const char *file, int line)
71-
{
72-
void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
73-
if (!ptr) giterr_set_oom();
74-
return ptr;
75-
}
76-
77-
GIT_INLINE(void *) git__crtdbg__calloc(size_t nelem, size_t elsize, const char *file, int line)
78-
{
79-
void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
80-
if (!ptr) giterr_set_oom();
81-
return ptr;
82-
}
83-
84-
GIT_INLINE(char *) git__crtdbg__strdup(const char *str, const char *file, int line)
85-
{
86-
char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
87-
if (!ptr) giterr_set_oom();
88-
return ptr;
89-
}
90-
91-
GIT_INLINE(char *) git__crtdbg__strndup(const char *str, size_t n, const char *file, int line)
92-
{
93-
size_t length = 0, alloclength;
94-
char *ptr;
95-
96-
length = p_strnlen(str, n);
97-
98-
if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
99-
!(ptr = git__crtdbg__malloc(alloclength, file, line)))
100-
return NULL;
101-
102-
if (length)
103-
memcpy(ptr, str, length);
104-
105-
ptr[length] = '\0';
106-
107-
return ptr;
108-
}
109-
110-
GIT_INLINE(char *) git__crtdbg__substrdup(const char *start, size_t n, const char *file, int line)
111-
{
112-
char *ptr;
113-
size_t alloclen;
114-
115-
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
116-
!(ptr = git__crtdbg__malloc(alloclen, file, line)))
117-
return NULL;
118-
119-
memcpy(ptr, start, n);
120-
ptr[n] = '\0';
121-
return ptr;
122-
}
123-
124-
GIT_INLINE(void *) git__crtdbg__realloc(void *ptr, size_t size, const char *file, int line)
125-
{
126-
void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
127-
if (!new_ptr) giterr_set_oom();
128-
return new_ptr;
129-
}
130-
131-
GIT_INLINE(void *) git__crtdbg__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
132-
{
133-
size_t newsize;
134-
135-
return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
136-
NULL : _realloc_dbg(ptr, newsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
137-
}
39+
/* Enable MSVC CRTDBG memory leak reporting.
40+
*
41+
* We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC
42+
* documentation because all allocs/frees in libgit2 already go through
43+
* the "git__" routines defined in this file. Simply using the normal
44+
* reporting mechanism causes all leaks to be attributed to a routine
45+
* here in util.h (ie, the actual call to calloc()) rather than the
46+
* caller of git__calloc().
47+
*
48+
* Therefore, we declare a set of "git__crtdbg__" routines to replace
49+
* the corresponding "git__" routines and re-define the "git__" symbols
50+
* as macros. This allows us to get and report the file:line info of
51+
* the real caller.
52+
*
53+
* We DO NOT replace the "git__free" routine because it needs to remain
54+
* a function pointer because it is used as a function argument when
55+
* setting up various structure "destructors".
56+
*
57+
* We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes
58+
* "free" to be remapped to "_free_dbg" and this causes problems for
59+
* structures which define a field named "free".
60+
*
61+
* Finally, CRTDBG must be explicitly enabled and configured at program
62+
* startup. See tests/main.c for an example.
63+
*/
13864

139-
GIT_INLINE(void *) git__crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
140-
{
141-
return git__crtdbg__reallocarray(NULL, nelem, elsize, file, line);
142-
}
65+
#include "win32/w32_crtdbg_stacktrace.h"
14366

14467
#define git__malloc(len) git__crtdbg__malloc(len, __FILE__, __LINE__)
14568
#define git__calloc(nelem, elsize) git__crtdbg__calloc(nelem, elsize, __FILE__, __LINE__)

src/win32/w32_crtdbg_stacktrace.h

+81
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
#if defined(GIT_MSVC_CRTDBG)
1111

12+
#include <stdlib.h>
13+
#include <crtdbg.h>
14+
15+
#include "git2/errors.h"
16+
#include "strnlen.h"
17+
1218
/**
1319
* Initialize our memory leak tracking and de-dup data structures.
1420
* This should ONLY be called by git_libgit2_init().
@@ -89,5 +95,80 @@ GIT_EXTERN(int) git_win32__crtdbg_stacktrace__dump(
8995
*/
9096
const char *git_win32__crtdbg_stacktrace(int skip, const char *file);
9197

98+
GIT_INLINE(void *) git__crtdbg__malloc(size_t len, const char *file, int line)
99+
{
100+
void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
101+
if (!ptr) giterr_set_oom();
102+
return ptr;
103+
}
104+
105+
GIT_INLINE(void *) git__crtdbg__calloc(size_t nelem, size_t elsize, const char *file, int line)
106+
{
107+
void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
108+
if (!ptr) giterr_set_oom();
109+
return ptr;
110+
}
111+
112+
GIT_INLINE(char *) git__crtdbg__strdup(const char *str, const char *file, int line)
113+
{
114+
char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
115+
if (!ptr) giterr_set_oom();
116+
return ptr;
117+
}
118+
119+
GIT_INLINE(char *) git__crtdbg__strndup(const char *str, size_t n, const char *file, int line)
120+
{
121+
size_t length = 0, alloclength;
122+
char *ptr;
123+
124+
length = p_strnlen(str, n);
125+
126+
if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
127+
!(ptr = git__crtdbg__malloc(alloclength, file, line)))
128+
return NULL;
129+
130+
if (length)
131+
memcpy(ptr, str, length);
132+
133+
ptr[length] = '\0';
134+
135+
return ptr;
136+
}
137+
138+
GIT_INLINE(char *) git__crtdbg__substrdup(const char *start, size_t n, const char *file, int line)
139+
{
140+
char *ptr;
141+
size_t alloclen;
142+
143+
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
144+
!(ptr = git__crtdbg__malloc(alloclen, file, line)))
145+
return NULL;
146+
147+
memcpy(ptr, start, n);
148+
ptr[n] = '\0';
149+
return ptr;
150+
}
151+
152+
GIT_INLINE(void *) git__crtdbg__realloc(void *ptr, size_t size, const char *file, int line)
153+
{
154+
void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
155+
if (!new_ptr) giterr_set_oom();
156+
return new_ptr;
157+
}
158+
159+
GIT_INLINE(void *) git__crtdbg__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
160+
{
161+
size_t newsize;
162+
163+
return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
164+
NULL : _realloc_dbg(ptr, newsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
165+
}
166+
167+
GIT_INLINE(void *) git__crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
168+
{
169+
return git__crtdbg__reallocarray(NULL, nelem, elsize, file, line);
170+
}
171+
172+
92173
#endif
93174
#endif

0 commit comments

Comments
 (0)