From 1bb64dbff8220c4ffc8ea79384d44bc1d4925643 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 11 Oct 2015 20:12:00 +0200 Subject: add zalloc 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') diff --git a/src/malloc.c b/src/malloc.c index 7789070..6219fce 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -90,6 +90,31 @@ void* calloc(size_t elem_count, size_t elem_size) } +/** + * Variant of `malloc` that clears the allocation with zeroes. + * + * `zalloc(n)` is equivalent to `calloc(1, n)`, or equivalently, + * `calloc(n, m)` is equivalent to `zalloc(n * m)` assumming `n * m` + * does not overflow (in which case `calloc(n, m)` returns `ENOMEM`.) + * + * This is a klibc extension. + * + * @param size The size of the allocation. + * @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* zalloc(size_t size) +{ + return calloc(1, size); +} + + /** * Variant of `malloc` that extends, or shrinks, an existing allocation, * if beneficial and possible, or creates a new allocation with the new -- cgit v1.2.3-70-g09d2