diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
commit | 3534b428629be185e096be99e3bd5fdfe32d5544 (patch) | |
tree | 210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libstddjb/alloc.c | |
download | skalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz |
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libstddjb/alloc.c')
-rw-r--r-- | src/libstddjb/alloc.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/libstddjb/alloc.c b/src/libstddjb/alloc.c new file mode 100644 index 0000000..e3f89dd --- /dev/null +++ b/src/libstddjb/alloc.c @@ -0,0 +1,49 @@ +/* ISC license. */ + +#include <errno.h> +#include <stdlib.h> +#include <skalibs/sysdeps.h> +#include <skalibs/alloc.h> +#include "alloc-internal.h" + +#ifdef DEBUG_ALLOC +# include "buffer.h" +# include "strerr2.h" +# include "lolstdio.h" +# define PLM(...) (bprintf(buffer_2, "%s: debug_alloc: ", PROG), bprintf(buffer_2, __VA_ARGS__), buffer_putflush(buffer_2, "\n", 1)) +#endif + +aligned_char_ref alloc (unsigned int n) +{ + register aligned_char_ref p = n ? (aligned_char_ref)malloc(n) : (aligned_char_ref)alloc_0 ; +#ifdef DEBUG_ALLOC + static unsigned int counter = 0 ; + PLM("alloc(%u): %p. Allocated: %u", n, p, ++counter) ; +#endif + return p ; +} + +void alloc_free (void *p) +{ + register int e = errno ; +#ifdef DEBUG_ALLOC + static unsigned int counter = 0 ; + PLM("alloc_free(%p). Freed: %u", p, ++counter) ; +#endif +#ifndef SKALIBS_HASMALLOC0 + if (p != alloc_0) +#endif + free(p) ; + errno = e ; +} + +int alloc_realloc (aligned_char_ref *x, unsigned int n) +{ + aligned_char_ref y = n ? (aligned_char_ref)realloc(*x, n) : (free(*x), alloc_0) ; +#ifdef DEBUG_ALLOC + PLM("alloc_realloc(&%p) -> new address = %p", *x, y) ; +#endif + if (!y) return 0 ; + *x = y ; + return 1 ; +} |