aboutsummaryrefslogtreecommitdiffstats
path: root/src/malloc.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-10-11 20:12:00 +0200
committerMattias Andrée <maandree@operamail.com>2015-10-11 20:12:00 +0200
commit1bb64dbff8220c4ffc8ea79384d44bc1d4925643 (patch)
tree55ce1dc27c5540ba1b8b75dd1a52636adeac25c5 /src/malloc.c
parentbeginning of scanf functions (diff)
downloadslibc-1bb64dbff8220c4ffc8ea79384d44bc1d4925643.tar.gz
slibc-1bb64dbff8220c4ffc8ea79384d44bc1d4925643.tar.bz2
slibc-1bb64dbff8220c4ffc8ea79384d44bc1d4925643.tar.xz
add zalloc
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/malloc.c')
-rw-r--r--src/malloc.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/malloc.c b/src/malloc.c
index 7789070..6219fce 100644
--- a/src/malloc.c
+++ b/src/malloc.c
@@ -91,6 +91,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
* size, copies the data, and frees the old allocation.