|
10 | 10 | #include "git2/buffer.h"
|
11 | 11 | #include "buffer.h"
|
12 | 12 |
|
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 |
| - |
44 | 13 | #include "common.h"
|
45 | 14 | #include "strnlen.h"
|
46 | 15 |
|
|
67 | 36 |
|
68 | 37 | #if defined(GIT_MSVC_CRTDBG)
|
69 | 38 |
|
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 | + */ |
138 | 64 |
|
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" |
143 | 66 |
|
144 | 67 | #define git__malloc(len) git__crtdbg__malloc(len, __FILE__, __LINE__)
|
145 | 68 | #define git__calloc(nelem, elsize) git__crtdbg__calloc(nelem, elsize, __FILE__, __LINE__)
|
|
0 commit comments