diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2020-07-12 18:18:12 +0100 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-11-21 21:53:30 +0000 |
| commit | 51825c4f39af069d413fdb6cc661e53c4b63ad47 (patch) | |
| tree | 1a37cc03c75b3c8ba98ba2838c8677c77de2a2ff /src/allocators | |
| parent | 31654a34c9e6a457ef581892ba8775e3f5235f62 (diff) | |
| download | libgit2-51825c4f39af069d413fdb6cc661e53c4b63ad47.tar.gz | |
alloc: rename the win32 leakcheck allocator
The win32 leakchecking system is now named win32_leakcheck. Update the
allocator to match.
Diffstat (limited to 'src/allocators')
| -rw-r--r-- | src/allocators/win32_crtdbg.c | 118 | ||||
| -rw-r--r-- | src/allocators/win32_leakcheck.c | 118 | ||||
| -rw-r--r-- | src/allocators/win32_leakcheck.h (renamed from src/allocators/win32_crtdbg.h) | 6 |
3 files changed, 121 insertions, 121 deletions
diff --git a/src/allocators/win32_crtdbg.c b/src/allocators/win32_crtdbg.c deleted file mode 100644 index c542b0c54..000000000 --- a/src/allocators/win32_crtdbg.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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_leakcheck.h" - -static void *crtdbg__malloc(size_t len, const char *file, int line) -{ - void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32_leakcheck_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_leakcheck_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_leakcheck_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_leakcheck_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; - - if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) - return NULL; - - return crtdbg__realloc(ptr, newsize, 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_leakcheck.c b/src/allocators/win32_leakcheck.c new file mode 100644 index 000000000..3be45cae6 --- /dev/null +++ b/src/allocators/win32_leakcheck.c @@ -0,0 +1,118 @@ +/* + * 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_leakcheck.h" + +#if defined(GIT_MSVC_CRTDBG) + +#include "win32/w32_leakcheck.h" + +static void *leakcheck_malloc(size_t len, const char *file, int line) +{ + void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line); + if (!ptr) git_error_set_oom(); + return ptr; +} + +static void *leakcheck_calloc(size_t nelem, size_t elsize, const char *file, int line) +{ + void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line); + if (!ptr) git_error_set_oom(); + return ptr; +} + +static char *leakcheck_strdup(const char *str, const char *file, int line) +{ + char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line); + if (!ptr) git_error_set_oom(); + return ptr; +} + +static char *leakcheck_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 = leakcheck_malloc(alloclength, file, line))) + return NULL; + + if (length) + memcpy(ptr, str, length); + + ptr[length] = '\0'; + + return ptr; +} + +static char *leakcheck_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 = leakcheck_malloc(alloclen, file, line))) + return NULL; + + memcpy(ptr, start, n); + ptr[n] = '\0'; + return ptr; +} + +static void *leakcheck_realloc(void *ptr, size_t size, const char *file, int line) +{ + void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line); + if (!new_ptr) git_error_set_oom(); + return new_ptr; +} + +static void *leakcheck_reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line) +{ + size_t newsize; + + if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) + return NULL; + + return leakcheck_realloc(ptr, newsize, file, line); +} + +static void *leakcheck_mallocarray(size_t nelem, size_t elsize, const char *file, int line) +{ + return leakcheck_reallocarray(NULL, nelem, elsize, file, line); +} + +static void leakcheck_free(void *ptr) +{ + free(ptr); +} + +int git_win32_leakcheck_init_allocator(git_allocator *allocator) +{ + allocator->gmalloc = leakcheck_malloc; + allocator->gcalloc = leakcheck_calloc; + allocator->gstrdup = leakcheck_strdup; + allocator->gstrndup = leakcheck_strndup; + allocator->gsubstrdup = leakcheck_substrdup; + allocator->grealloc = leakcheck_realloc; + allocator->greallocarray = leakcheck_reallocarray; + allocator->gmallocarray = leakcheck_mallocarray; + allocator->gfree = leakcheck_free; + return 0; +} + +#else + +int git_win32_leakcheck_init_allocator(git_allocator *allocator) +{ + GIT_UNUSED(allocator); + git_error_set(GIT_EINVALID, "leakcheck memory allocator not available"); + return -1; +} + +#endif diff --git a/src/allocators/win32_crtdbg.h b/src/allocators/win32_leakcheck.h index 754c6b6fb..089690f90 100644 --- a/src/allocators/win32_crtdbg.h +++ b/src/allocators/win32_leakcheck.h @@ -5,13 +5,13 @@ * a Linking Exception. For full terms see the included COPYING file. */ -#ifndef INCLUDE_allocators_crtdbg_h -#define INCLUDE_allocators_crtdbg_h +#ifndef INCLUDE_allocators_win32_leakcheck_h +#define INCLUDE_allocators_win32_leakcheck_h #include "common.h" #include "alloc.h" -int git_win32_crtdbg_init_allocator(git_allocator *allocator); +int git_win32_leakcheck_init_allocator(git_allocator *allocator); #endif |
