diff options
| author | Patrick Steinhardt <ps@pks.im> | 2018-03-20 14:23:49 +0000 |
|---|---|---|
| committer | Patrick Steinhardt <ps@pks.im> | 2018-06-07 12:57:39 +0200 |
| commit | 9865cd1696ac4a3f47991a9a1d79b17cef5edc89 (patch) | |
| tree | e1ee4f20bceb1a8c5f98aceb41d98dcec3a3f3d1 /include/git2/sys | |
| parent | 08b318c0645b27393a70c2289a65b949ea4b45ed (diff) | |
| download | libgit2-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 'include/git2/sys')
| -rw-r--r-- | include/git2/sys/alloc.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/include/git2/sys/alloc.h b/include/git2/sys/alloc.h new file mode 100644 index 000000000..a0955ce8b --- /dev/null +++ b/include/git2/sys/alloc.h @@ -0,0 +1,77 @@ +/* + * 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_sys_git_alloc_h__ +#define INCLUDE_sys_git_alloc_h__ + +#include "git2/common.h" + +GIT_BEGIN_DECL + +/** + * An instance for a custom memory allocator + * + * Setting the pointers of this structure allows the developer to implement + * custom memory allocators. The global memory allocator can be set by using + * "GIT_OPT_SET_ALLOCATOR" with the `git_libgit2_opts` function. Keep in mind + * that all fields need to be set to a proper function. + */ +typedef struct { + /* Allocate `n` bytes of memory */ + void *(*gmalloc)(size_t n, const char *file, int line); + + /* + * Allocate memory for an array of `nelem` elements, where each element + * has a size of `elsize`. Returned memory shall be initialized to + * all-zeroes + */ + void *(*gcalloc)(size_t nelem, size_t elsize, const char *file, int line); + + /* Allocate memory for the string `str` and duplicate its contents. */ + char *(*gstrdup)(const char *str, const char *file, int line); + + /* + * Equivalent to the `gstrdup` function, but only duplicating at most + * `n + 1` bytes + */ + char *(*gstrndup)(const char *str, size_t n, const char *file, int line); + + /* + * Equivalent to `gstrndup`, but will always duplicate exactly `n` bytes + * of `str`. Thus, out of bounds reads at `str` may happen. + */ + char *(*gsubstrdup)(const char *str, size_t n, const char *file, int line); + + /* + * This function shall deallocate the old object `ptr` and return a + * pointer to a new object that has the size specified by `size`. In + * case `ptr` is `NULL`, a new array shall be allocated. + */ + void *(*grealloc)(void *ptr, size_t size, const char *file, int line); + + /* + * This function shall be equivalent to `grealloc`, but allocating + * `neleme * elsize` bytes. + */ + void *(*greallocarray)(void *ptr, size_t nelem, size_t elsize, const char *file, int line); + + /* + * This function shall allocate a new array of `nelem` elements, where + * each element has a size of `elsize` bytes. + */ + void *(*gmallocarray)(size_t nelem, size_t elsize, const char *file, int line); + + /* + * This function shall free the memory pointed to by `ptr`. In case + * `ptr` is `NULL`, this shall be a no-op. + */ + void (*gfree)(void *ptr); +} git_allocator; + +GIT_END_DECL + +#endif |
