diff options
| author | Patrick Steinhardt <ps@pks.im> | 2018-03-14 10:34:59 +0000 |
|---|---|---|
| committer | Patrick Steinhardt <ps@pks.im> | 2018-06-07 12:57:39 +0200 |
| commit | c47f71551714006ee1aba26c49fdd1f187251da2 (patch) | |
| tree | 11f42083246877bac56fa1ccc37811f9ac0767cc /src/stdalloc.h | |
| parent | 496b0df2ca2a115d2e5a7099f2b8fd6d7409fcb2 (diff) | |
| download | libgit2-c47f71551714006ee1aba26c49fdd1f187251da2.tar.gz | |
util: extract `stdalloc` allocator into its own module
Right now, the standard allocator is being declared as part of the
"util.h" header as a set of inline functions. As with the crtdbg
allocator functions, these inline functions make it hard to convert to
function pointers for our allocators.
Create a new "stdalloc" module containing our standard allocations
functions to split these out. Convert the existing allocators to macros
which make use of the stdalloc functions.
Diffstat (limited to 'src/stdalloc.h')
| -rw-r--r-- | src/stdalloc.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/stdalloc.h b/src/stdalloc.h new file mode 100644 index 000000000..17c207210 --- /dev/null +++ b/src/stdalloc.h @@ -0,0 +1,40 @@ +/* + * 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 "common.h" + +/* + * Custom memory allocation wrappers + * that set error code and error message + * on allocation failure + */ +void *git__stdalloc__malloc(size_t len); +void *git__stdalloc__calloc(size_t nelem, size_t elsize); +char *git__stdalloc__strdup(const char *str); +char *git__stdalloc__strndup(const char *str, size_t n); +/* NOTE: This doesn't do null or '\0' checking. Watch those boundaries! */ +char *git__stdalloc__substrdup(const char *start, size_t n); +void *git__stdalloc__realloc(void *ptr, size_t size); + +/** + * Similar to `git__stdalloc__realloc`, except that it is suitable for reallocing an + * array to a new number of elements of `nelem`, each of size `elsize`. + * The total size calculation is checked for overflow. + */ +void *git__stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize); + +/** + * Similar to `git__stdalloc__calloc`, except that it does not zero memory. + */ +void *git__stdalloc__mallocarray(size_t nelem, size_t elsize); + +void git__stdalloc__free(void *ptr); + +#endif |
