From f912f24e28558a6cc078240d5b97e2cc76b67868 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 14 Nov 2015 23:10:00 +0100 Subject: add mallocz MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/malloc.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/malloc.c') diff --git a/src/malloc.c b/src/malloc.c index cd22ccd..864a8f5 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -121,6 +121,31 @@ void* calloc(size_t elem_count, size_t elem_size) } +/** + * Variant of `malloc` that conditionally clears the allocation with zeroes. + * + * This is a Plan 9 from Bell Labs extension. + * + * @param size The size of the allocation. + * @param clear Clear the allocation unless this value is zero. + * @return Pointer to the beginning of the new allocation. + * If `size` is zero, this function will either return + * `NULL` (that is what this implement does) or return + * a unique pointer that can later be freed with `free`. + * `NULL` is returned on error, and `errno` is set to + * indicate the error. + * + * @throws ENOMEM The process cannot allocate more memory. + */ +void* mallocz(size_t size, int clear) +{ + void* ptr = memalign(sizeof(max_align_t), size); + if ((ptr != NULL) && clear) + explicit_bzero(ptr, size); + return ptr; +} + + /** * Variant of `malloc` that clears the allocation with zeroes. * -- cgit v1.2.3-70-g09d2