summaryrefslogtreecommitdiff
path: root/src/allocators
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-02-21 12:27:42 +0100
committerPatrick Steinhardt <ps@pks.im>2019-02-21 13:23:54 +0100
commit48727e5d3bc0922a2073b717a6ee449e9bb0ddd7 (patch)
tree03673aa6ffa59ff4b8cf9dee8c4dd6d216af8def /src/allocators
parentb63396b75fe26afa3b27259c0d13dd7bc13b53ba (diff)
downloadlibgit2-48727e5d3bc0922a2073b717a6ee449e9bb0ddd7.tar.gz
allocators: extract crtdbg allocator into its own file
The Windows-specific crtdbg allocator is currently mixed into the crtdbg stacktracing compilation unit, making it harder to find than necessary. Extract it and move it into the new "allocators/" subdirectory to improve discoverability. This change means that the crtdbg compilation unit is now compiled unconditionally, whereas it has previously only been compiled on Windows platforms. Thus we now have additional guards around the code so that it will only be compiled if GIT_MSVC_CRTDBG is defined. This also allows us to move over the fallback-implementation of `git_win32_crtdbg_init_allocator` into the same compilation unit.
Diffstat (limited to 'src/allocators')
-rw-r--r--src/allocators/win32_crtdbg.c116
-rw-r--r--src/allocators/win32_crtdbg.h17
2 files changed, 133 insertions, 0 deletions
diff --git a/src/allocators/win32_crtdbg.c b/src/allocators/win32_crtdbg.c
new file mode 100644
index 000000000..d1539d54b
--- /dev/null
+++ b/src/allocators/win32_crtdbg.c
@@ -0,0 +1,116 @@
+/*
+ * 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.
+ */
+
+#include "win32_crtdbg.h"
+
+#if defined(GIT_MSVC_CRTDBG)
+
+#include "win32/w32_crtdbg_stacktrace.h"
+
+static void *crtdbg__malloc(size_t len, const char *file, int line)
+{
+ void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
+ if (!ptr) git_error_set_oom();
+ return ptr;
+}
+
+static void *crtdbg__calloc(size_t nelem, size_t elsize, const char *file, int line)
+{
+ void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
+ if (!ptr) git_error_set_oom();
+ return ptr;
+}
+
+static char *crtdbg__strdup(const char *str, const char *file, int line)
+{
+ char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
+ if (!ptr) git_error_set_oom();
+ return ptr;
+}
+
+static char *crtdbg__strndup(const char *str, size_t n, const char *file, int line)
+{
+ size_t length = 0, alloclength;
+ char *ptr;
+
+ length = p_strnlen(str, n);
+
+ if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
+ !(ptr = crtdbg__malloc(alloclength, file, line)))
+ return NULL;
+
+ if (length)
+ memcpy(ptr, str, length);
+
+ ptr[length] = '\0';
+
+ return ptr;
+}
+
+static char *crtdbg__substrdup(const char *start, size_t n, const char *file, int line)
+{
+ char *ptr;
+ size_t alloclen;
+
+ if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
+ !(ptr = crtdbg__malloc(alloclen, file, line)))
+ return NULL;
+
+ memcpy(ptr, start, n);
+ ptr[n] = '\0';
+ return ptr;
+}
+
+static void *crtdbg__realloc(void *ptr, size_t size, const char *file, int line)
+{
+ void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
+ if (!new_ptr) git_error_set_oom();
+ return new_ptr;
+}
+
+static void *crtdbg__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
+{
+ size_t newsize;
+
+ return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
+ NULL : _realloc_dbg(ptr, newsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
+}
+
+static void *crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
+{
+ return crtdbg__reallocarray(NULL, nelem, elsize, file, line);
+}
+
+static void crtdbg__free(void *ptr)
+{
+ free(ptr);
+}
+
+int git_win32_crtdbg_init_allocator(git_allocator *allocator)
+{
+ allocator->gmalloc = crtdbg__malloc;
+ allocator->gcalloc = crtdbg__calloc;
+ allocator->gstrdup = crtdbg__strdup;
+ allocator->gstrndup = crtdbg__strndup;
+ allocator->gsubstrdup = crtdbg__substrdup;
+ allocator->grealloc = crtdbg__realloc;
+ allocator->greallocarray = crtdbg__reallocarray;
+ allocator->gmallocarray = crtdbg__mallocarray;
+ allocator->gfree = crtdbg__free;
+ return 0;
+}
+
+#else
+
+int git_win32_crtdbg_init_allocator(git_allocator *allocator)
+{
+ GIT_UNUSED(allocator);
+ git_error_set(GIT_EINVALID, "crtdbg memory allocator not available");
+ return -1;
+}
+
+#endif
diff --git a/src/allocators/win32_crtdbg.h b/src/allocators/win32_crtdbg.h
new file mode 100644
index 000000000..754c6b6fb
--- /dev/null
+++ b/src/allocators/win32_crtdbg.h
@@ -0,0 +1,17 @@
+/*
+ * 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_allocators_crtdbg_h
+#define INCLUDE_allocators_crtdbg_h
+
+#include "common.h"
+
+#include "alloc.h"
+
+int git_win32_crtdbg_init_allocator(git_allocator *allocator);
+
+#endif