summaryrefslogtreecommitdiff
path: root/src/stdalloc.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2018-03-20 14:23:49 +0000
committerPatrick Steinhardt <ps@pks.im>2018-06-07 12:57:39 +0200
commit9865cd1696ac4a3f47991a9a1d79b17cef5edc89 (patch)
treee1ee4f20bceb1a8c5f98aceb41d98dcec3a3f3d1 /src/stdalloc.c
parent08b318c0645b27393a70c2289a65b949ea4b45ed (diff)
downloadlibgit2-9865cd1696ac4a3f47991a9a1d79b17cef5edc89.tar.gz
alloc: make memory allocators use function pointers
Currently, our memory allocators are being redirected to the correct implementation at compile time by simply using macros. In order to make them swappable at runtime, this commit reshuffles that by instead making use of a global "git_allocator" structure, whose pointers are set up to reference the allocator functions. Like this, it becomes easy to swap out allocators by simply setting these function pointers. In order to initialize a "git_allocator", our provided allocators "stdalloc" and "crtdbg" both provide an init function. This is being called to initialize a passed in allocator struct and set up its members correctly. No support is yet included to enable users of libgit2 to switch out the memory allocator at a global level.
Diffstat (limited to 'src/stdalloc.c')
-rw-r--r--src/stdalloc.c38
1 files changed, 26 insertions, 12 deletions
diff --git a/src/stdalloc.c b/src/stdalloc.c
index 8bc8b63ec..171c9ed65 100644
--- a/src/stdalloc.c
+++ b/src/stdalloc.c
@@ -7,7 +7,7 @@
#include "stdalloc.h"
-void *git__stdalloc__malloc(size_t len, const char *file, int line)
+static void *stdalloc__malloc(size_t len, const char *file, int line)
{
void *ptr = malloc(len);
@@ -18,7 +18,7 @@ void *git__stdalloc__malloc(size_t len, const char *file, int line)
return ptr;
}
-void *git__stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line)
+static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line)
{
void *ptr = calloc(nelem, elsize);
@@ -29,7 +29,7 @@ void *git__stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int l
return ptr;
}
-char *git__stdalloc__strdup(const char *str, const char *file, int line)
+static char *stdalloc__strdup(const char *str, const char *file, int line)
{
char *ptr = strdup(str);
@@ -40,7 +40,7 @@ char *git__stdalloc__strdup(const char *str, const char *file, int line)
return ptr;
}
-char *git__stdalloc__strndup(const char *str, size_t n, const char *file, int line)
+static char *stdalloc__strndup(const char *str, size_t n, const char *file, int line)
{
size_t length = 0, alloclength;
char *ptr;
@@ -48,7 +48,7 @@ char *git__stdalloc__strndup(const char *str, size_t n, const char *file, int li
length = p_strnlen(str, n);
if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
- !(ptr = git__stdalloc__malloc(alloclength, file, line)))
+ !(ptr = stdalloc__malloc(alloclength, file, line)))
return NULL;
if (length)
@@ -59,13 +59,13 @@ char *git__stdalloc__strndup(const char *str, size_t n, const char *file, int li
return ptr;
}
-char *git__stdalloc__substrdup(const char *start, size_t n, const char *file, int line)
+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 = git__stdalloc__malloc(alloclen, file, line)))
+ !(ptr = stdalloc__malloc(alloclen, file, line)))
return NULL;
memcpy(ptr, start, n);
@@ -73,7 +73,7 @@ char *git__stdalloc__substrdup(const char *start, size_t n, const char *file, in
return ptr;
}
-void *git__stdalloc__realloc(void *ptr, size_t size, const char *file, int line)
+static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line)
{
void *new_ptr = realloc(ptr, size);
@@ -84,7 +84,7 @@ void *git__stdalloc__realloc(void *ptr, size_t size, const char *file, int line)
return new_ptr;
}
-void *git__stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
+static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
{
size_t newsize;
@@ -95,12 +95,26 @@ void *git__stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const
NULL : realloc(ptr, newsize);
}
-void *git__stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
+static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
{
- return git__stdalloc__reallocarray(NULL, nelem, elsize, file, line);
+ return stdalloc__reallocarray(NULL, nelem, elsize, file, line);
}
-void git__stdalloc__free(void *ptr)
+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;
+}