From b63396b75fe26afa3b27259c0d13dd7bc13b53ba Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 21 Feb 2019 12:13:59 +0100 Subject: allocators: move standard allocator into subdirectory Right now, our two allocator implementations are scattered around the tree in "stdalloc.h" and "win32/w32_crtdbg_stacktrace.h". Start grouping them together in a single directory "allocators/", similar to how e.g. our streams are organized. --- src/CMakeLists.txt | 1 + src/alloc.c | 2 +- src/allocators/stdalloc.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++ src/allocators/stdalloc.h | 17 +++++++ src/stdalloc.c | 119 ---------------------------------------------- src/stdalloc.h | 17 ------- 6 files changed, 138 insertions(+), 137 deletions(-) create mode 100644 src/allocators/stdalloc.c create mode 100644 src/allocators/stdalloc.h delete mode 100644 src/stdalloc.c delete mode 100644 src/stdalloc.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5ad8b1447..b056c9a9b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -428,6 +428,7 @@ ELSE() FILE(GLOB SRC_OS unix/*.c unix/*.h) ENDIF() FILE(GLOB SRC_GIT2 *.c *.h + allocators/*.c allocators/*.h streams/*.c streams/*.h transports/*.c transports/*.h xdiff/*.c xdiff/*.h) diff --git a/src/alloc.c b/src/alloc.c index 8d4a19476..5ff41517b 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -10,7 +10,7 @@ #if defined(GIT_MSVC_CRTDBG) # include "win32/w32_crtdbg_stacktrace.h" #else -# include "stdalloc.h" +# include "allocators/stdalloc.h" #endif git_allocator git__allocator; diff --git a/src/allocators/stdalloc.c b/src/allocators/stdalloc.c new file mode 100644 index 000000000..c4938e32b --- /dev/null +++ b/src/allocators/stdalloc.c @@ -0,0 +1,119 @@ +/* + * 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 "stdalloc.h" + +static void *stdalloc__malloc(size_t len, const char *file, int line) +{ + void *ptr = malloc(len); + + GIT_UNUSED(file); + GIT_UNUSED(line); + + if (!ptr) git_error_set_oom(); + return ptr; +} + +static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line) +{ + void *ptr = calloc(nelem, elsize); + + GIT_UNUSED(file); + GIT_UNUSED(line); + + if (!ptr) git_error_set_oom(); + return ptr; +} + +static char *stdalloc__strdup(const char *str, const char *file, int line) +{ + char *ptr = strdup(str); + + GIT_UNUSED(file); + GIT_UNUSED(line); + + if (!ptr) git_error_set_oom(); + return ptr; +} + +static char *stdalloc__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 = stdalloc__malloc(alloclength, file, line))) + return NULL; + + if (length) + memcpy(ptr, str, length); + + ptr[length] = '\0'; + + return ptr; +} + +static char *stdalloc__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 = stdalloc__malloc(alloclen, file, line))) + return NULL; + + memcpy(ptr, start, n); + ptr[n] = '\0'; + return ptr; +} + +static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line) +{ + void *new_ptr = realloc(ptr, size); + + GIT_UNUSED(file); + GIT_UNUSED(line); + + if (!new_ptr) git_error_set_oom(); + return new_ptr; +} + +static void *stdalloc__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 stdalloc__realloc(ptr, newsize, file, line); +} + +static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line) +{ + return stdalloc__reallocarray(NULL, nelem, elsize, file, line); +} + +static void stdalloc__free(void *ptr) +{ + free(ptr); +} + +int git_stdalloc_init_allocator(git_allocator *allocator) +{ + allocator->gmalloc = stdalloc__malloc; + allocator->gcalloc = stdalloc__calloc; + allocator->gstrdup = stdalloc__strdup; + allocator->gstrndup = stdalloc__strndup; + allocator->gsubstrdup = stdalloc__substrdup; + allocator->grealloc = stdalloc__realloc; + allocator->greallocarray = stdalloc__reallocarray; + allocator->gmallocarray = stdalloc__mallocarray; + allocator->gfree = stdalloc__free; + return 0; +} diff --git a/src/allocators/stdalloc.h b/src/allocators/stdalloc.h new file mode 100644 index 000000000..fa23fe6e3 --- /dev/null +++ b/src/allocators/stdalloc.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_stdalloc_h__ +#define INCLUDE_allocators_stdalloc_h__ + +#include "common.h" + +#include "alloc.h" + +int git_stdalloc_init_allocator(git_allocator *allocator); + +#endif diff --git a/src/stdalloc.c b/src/stdalloc.c deleted file mode 100644 index c4938e32b..000000000 --- a/src/stdalloc.c +++ /dev/null @@ -1,119 +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 "stdalloc.h" - -static void *stdalloc__malloc(size_t len, const char *file, int line) -{ - void *ptr = malloc(len); - - GIT_UNUSED(file); - GIT_UNUSED(line); - - if (!ptr) git_error_set_oom(); - return ptr; -} - -static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line) -{ - void *ptr = calloc(nelem, elsize); - - GIT_UNUSED(file); - GIT_UNUSED(line); - - if (!ptr) git_error_set_oom(); - return ptr; -} - -static char *stdalloc__strdup(const char *str, const char *file, int line) -{ - char *ptr = strdup(str); - - GIT_UNUSED(file); - GIT_UNUSED(line); - - if (!ptr) git_error_set_oom(); - return ptr; -} - -static char *stdalloc__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 = stdalloc__malloc(alloclength, file, line))) - return NULL; - - if (length) - memcpy(ptr, str, length); - - ptr[length] = '\0'; - - return ptr; -} - -static char *stdalloc__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 = stdalloc__malloc(alloclen, file, line))) - return NULL; - - memcpy(ptr, start, n); - ptr[n] = '\0'; - return ptr; -} - -static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line) -{ - void *new_ptr = realloc(ptr, size); - - GIT_UNUSED(file); - GIT_UNUSED(line); - - if (!new_ptr) git_error_set_oom(); - return new_ptr; -} - -static void *stdalloc__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 stdalloc__realloc(ptr, newsize, file, line); -} - -static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line) -{ - return stdalloc__reallocarray(NULL, nelem, elsize, file, line); -} - -static void stdalloc__free(void *ptr) -{ - free(ptr); -} - -int git_stdalloc_init_allocator(git_allocator *allocator) -{ - allocator->gmalloc = stdalloc__malloc; - allocator->gcalloc = stdalloc__calloc; - allocator->gstrdup = stdalloc__strdup; - allocator->gstrndup = stdalloc__strndup; - allocator->gsubstrdup = stdalloc__substrdup; - allocator->grealloc = stdalloc__realloc; - allocator->greallocarray = stdalloc__reallocarray; - allocator->gmallocarray = stdalloc__mallocarray; - allocator->gfree = stdalloc__free; - return 0; -} diff --git a/src/stdalloc.h b/src/stdalloc.h deleted file mode 100644 index a8927a507..000000000 --- a/src/stdalloc.h +++ /dev/null @@ -1,17 +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. - */ - -#ifndef INCLUDE_stdalloc_h__ -#define INCLUDE_stdalloc_h__ - -#include "alloc.h" - -#include "common.h" - -int git_stdalloc_init_allocator(git_allocator *allocator); - -#endif -- cgit v1.2.1 From 48727e5d3bc0922a2073b717a6ee449e9bb0ddd7 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 21 Feb 2019 12:27:42 +0100 Subject: 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. --- src/alloc.c | 16 +----- src/allocators/win32_crtdbg.c | 116 ++++++++++++++++++++++++++++++++++++++ src/allocators/win32_crtdbg.h | 17 ++++++ src/win32/w32_crtdbg_stacktrace.c | 93 ------------------------------ src/win32/w32_crtdbg_stacktrace.h | 2 - 5 files changed, 135 insertions(+), 109 deletions(-) create mode 100644 src/allocators/win32_crtdbg.c create mode 100644 src/allocators/win32_crtdbg.h diff --git a/src/alloc.c b/src/alloc.c index 5ff41517b..51c4d8029 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -7,11 +7,8 @@ #include "alloc.h" -#if defined(GIT_MSVC_CRTDBG) -# include "win32/w32_crtdbg_stacktrace.h" -#else -# include "allocators/stdalloc.h" -#endif +#include "allocators/stdalloc.h" +#include "allocators/win32_crtdbg.h" git_allocator git__allocator; @@ -44,12 +41,3 @@ int git_allocator_setup(git_allocator *allocator) memcpy(&git__allocator, allocator, sizeof(*allocator)); return 0; } - -#if !defined(GIT_MSVC_CRTDBG) -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.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 diff --git a/src/win32/w32_crtdbg_stacktrace.c b/src/win32/w32_crtdbg_stacktrace.c index 49b54f134..cdb5ac1a5 100644 --- a/src/win32/w32_crtdbg_stacktrace.c +++ b/src/win32/w32_crtdbg_stacktrace.c @@ -71,99 +71,6 @@ static bool g_limit_reached = false; /* had allocs after we filled row table */ static unsigned int g_checkpoint_id = 0; /* to better label leak checkpoints */ static bool g_transient_leaks_since_mark = false; /* payload for hook */ -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; -} - /** * Compare function for bsearch on g_cs_index table. */ diff --git a/src/win32/w32_crtdbg_stacktrace.h b/src/win32/w32_crtdbg_stacktrace.h index 7a3deeff4..d65154bab 100644 --- a/src/win32/w32_crtdbg_stacktrace.h +++ b/src/win32/w32_crtdbg_stacktrace.h @@ -43,8 +43,6 @@ * startup. See tests/main.c for an example. */ -int git_win32_crtdbg_init_allocator(git_allocator *allocator); - /** * Initialize our memory leak tracking and de-dup data structures. * This should ONLY be called by git_libgit2_init(). -- cgit v1.2.1 From 765ff6e029a422d910ec65c38fd16ed90caff7b5 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 21 Feb 2019 12:35:48 +0100 Subject: allocators: make crtdbg allocator reuse its own realloc In commit 6e0dfc6ff (Make stdalloc__reallocarray call stdalloc__realloc, 2019-02-16), we have changed the stdalloc allocator to reuse `stdalloc__realloc` to implement `stdalloc__reallocarray`. This commit is making the same change for the Windows-specific crtdbg allocator to avoid code duplication. --- src/allocators/win32_crtdbg.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/allocators/win32_crtdbg.c b/src/allocators/win32_crtdbg.c index d1539d54b..1187e2fcd 100644 --- a/src/allocators/win32_crtdbg.c +++ b/src/allocators/win32_crtdbg.c @@ -76,8 +76,10 @@ static void *crtdbg__reallocarray(void *ptr, size_t nelem, size_t elsize, const { 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); + 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) -- cgit v1.2.1