util: extract allocators into its own "alloc.h" header Our "util.h" header is a grabbag of various different functions, where many don't have a clear group they belong to. Our set of allocator functions though can be clearly singled out as a single group of functions that always belongs together. Furthermore, we will need to implement additional functions relating to our allocators subsystem when moving to pluggable allocators. Thus, we should just move these functions into their own "alloc" module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
diff --git a/src/alloc.h b/src/alloc.h
new file mode 100644
index 0000000..e2dfdbc
--- /dev/null
+++ b/src/alloc.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#ifndef INCLUDE_alloc_h__
+#define INCLUDE_alloc_h__
+
+#include "common.h"
+
+#if defined(GIT_MSVC_CRTDBG)
+
+/* Enable MSVC CRTDBG memory leak reporting.
+ *
+ * We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC
+ * documentation because all allocs/frees in libgit2 already go through
+ * the "git__" routines defined in this file. Simply using the normal
+ * reporting mechanism causes all leaks to be attributed to a routine
+ * here in util.h (ie, the actual call to calloc()) rather than the
+ * caller of git__calloc().
+ *
+ * Therefore, we declare a set of "git__crtdbg__" routines to replace
+ * the corresponding "git__" routines and re-define the "git__" symbols
+ * as macros. This allows us to get and report the file:line info of
+ * the real caller.
+ *
+ * We DO NOT replace the "git__free" routine because it needs to remain
+ * a function pointer because it is used as a function argument when
+ * setting up various structure "destructors".
+ *
+ * We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes
+ * "free" to be remapped to "_free_dbg" and this causes problems for
+ * structures which define a field named "free".
+ *
+ * Finally, CRTDBG must be explicitly enabled and configured at program
+ * startup. See tests/main.c for an example.
+ */
+
+#include "win32/w32_crtdbg_stacktrace.h"
+
+#define git__malloc(len) git__crtdbg__malloc(len, __FILE__, __LINE__)
+#define git__calloc(nelem, elsize) git__crtdbg__calloc(nelem, elsize, __FILE__, __LINE__)
+#define git__strdup(str) git__crtdbg__strdup(str, __FILE__, __LINE__)
+#define git__strndup(str, n) git__crtdbg__strndup(str, n, __FILE__, __LINE__)
+#define git__substrdup(str, n) git__crtdbg__substrdup(str, n, __FILE__, __LINE__)
+#define git__realloc(ptr, size) git__crtdbg__realloc(ptr, size, __FILE__, __LINE__)
+#define git__reallocarray(ptr, nelem, elsize) git__crtdbg__reallocarray(ptr, nelem, elsize, __FILE__, __LINE__)
+#define git__mallocarray(nelem, elsize) git__crtdbg__mallocarray(nelem, elsize, __FILE__, __LINE__)
+#define git__free git__crtdbg__free
+
+#else
+
+#include "stdalloc.h"
+
+#define git__malloc(len) git__stdalloc__malloc(len)
+#define git__calloc(nelem, elsize) git__stdalloc__calloc(nelem, elsize)
+#define git__strdup(str) git__stdalloc__strdup(str)
+#define git__strndup(str, n) git__stdalloc__strndup(str, n)
+#define git__substrdup(str, n) git__stdalloc__substrdup(str, n)
+#define git__realloc(ptr, size) git__stdalloc__realloc(ptr, size)
+#define git__reallocarray(ptr, nelem, elsize) git__stdalloc__reallocarray(ptr, nelem, elsize)
+#define git__mallocarray(nelem, elsize) git__stdalloc__mallocarray(nelem, elsize)
+#define git__free git__stdalloc__free
+
+#endif /* !MSVC_CTRDBG */
+
+#endif
diff --git a/src/util.h b/src/util.h
index 1d60223..b6f5b75 100644
--- a/src/util.h
+++ b/src/util.h
@@ -41,62 +41,6 @@
*/
#define CONST_STRLEN(x) ((sizeof(x)/sizeof(x[0])) - 1)
-#if defined(GIT_MSVC_CRTDBG)
-
-/* Enable MSVC CRTDBG memory leak reporting.
- *
- * We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC
- * documentation because all allocs/frees in libgit2 already go through
- * the "git__" routines defined in this file. Simply using the normal
- * reporting mechanism causes all leaks to be attributed to a routine
- * here in util.h (ie, the actual call to calloc()) rather than the
- * caller of git__calloc().
- *
- * Therefore, we declare a set of "git__crtdbg__" routines to replace
- * the corresponding "git__" routines and re-define the "git__" symbols
- * as macros. This allows us to get and report the file:line info of
- * the real caller.
- *
- * We DO NOT replace the "git__free" routine because it needs to remain
- * a function pointer because it is used as a function argument when
- * setting up various structure "destructors".
- *
- * We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes
- * "free" to be remapped to "_free_dbg" and this causes problems for
- * structures which define a field named "free".
- *
- * Finally, CRTDBG must be explicitly enabled and configured at program
- * startup. See tests/main.c for an example.
- */
-
-#include "win32/w32_crtdbg_stacktrace.h"
-
-#define git__malloc(len) git__crtdbg__malloc(len, __FILE__, __LINE__)
-#define git__calloc(nelem, elsize) git__crtdbg__calloc(nelem, elsize, __FILE__, __LINE__)
-#define git__strdup(str) git__crtdbg__strdup(str, __FILE__, __LINE__)
-#define git__strndup(str, n) git__crtdbg__strndup(str, n, __FILE__, __LINE__)
-#define git__substrdup(str, n) git__crtdbg__substrdup(str, n, __FILE__, __LINE__)
-#define git__realloc(ptr, size) git__crtdbg__realloc(ptr, size, __FILE__, __LINE__)
-#define git__reallocarray(ptr, nelem, elsize) git__crtdbg__reallocarray(ptr, nelem, elsize, __FILE__, __LINE__)
-#define git__mallocarray(nelem, elsize) git__crtdbg__mallocarray(nelem, elsize, __FILE__, __LINE__)
-#define git__free git__crtdbg__free
-
-#else
-
-#include "stdalloc.h"
-
-#define git__malloc(len) git__stdalloc__malloc(len)
-#define git__calloc(nelem, elsize) git__stdalloc__calloc(nelem, elsize)
-#define git__strdup(str) git__stdalloc__strdup(str)
-#define git__strndup(str, n) git__stdalloc__strndup(str, n)
-#define git__substrdup(str, n) git__stdalloc__substrdup(str, n)
-#define git__realloc(ptr, size) git__stdalloc__realloc(ptr, size)
-#define git__reallocarray(ptr, nelem, elsize) git__stdalloc__reallocarray(ptr, nelem, elsize)
-#define git__mallocarray(nelem, elsize) git__stdalloc__mallocarray(nelem, elsize)
-#define git__free git__stdalloc__free
-
-#endif /* !MSVC_CTRDBG */
-
#define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \
((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2)))
@@ -474,4 +418,6 @@ GIT_INLINE(double) git__timer(void)
extern int git__getenv(git_buf *out, const char *name);
+#include "alloc.h"
+
#endif