aboutsummaryrefslogtreecommitdiffstats
path: root/src/malloc/aligned_alloc.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@member.fsf.org>2016-01-01 18:14:07 +0100
committerMattias Andrée <maandree@member.fsf.org>2016-01-01 18:14:07 +0100
commite9dc4ff30eedfc944340f6c594b1ba36442f1020 (patch)
tree6c9cd3447f3b21f87fb18cafa381d8c2f5e427d7 /src/malloc/aligned_alloc.c
parentdoc (diff)
downloadslibc-e9dc4ff30eedfc944340f6c594b1ba36442f1020.tar.gz
slibc-e9dc4ff30eedfc944340f6c594b1ba36442f1020.tar.bz2
slibc-e9dc4ff30eedfc944340f6c594b1ba36442f1020.tar.xz
partially split malloc.c
Signed-off-by: Mattias Andrée <maandree@member.fsf.org>
Diffstat (limited to 'src/malloc/aligned_alloc.c')
-rw-r--r--src/malloc/aligned_alloc.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/malloc/aligned_alloc.c b/src/malloc/aligned_alloc.c
new file mode 100644
index 0000000..a562ca0
--- /dev/null
+++ b/src/malloc/aligned_alloc.c
@@ -0,0 +1,53 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015, 2016 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stdlib.h>
+
+
+
+/**
+ * This function is identical to `memalign`,
+ * except it can be freed with `free`.
+ *
+ * Variant of `malloc` that returns an address with a
+ * specified alignment.
+ *
+ * It is unspecified how the function works. This implemention
+ * will allocate a bit of extra memory and shift the returned
+ * pointer so that it is aligned.
+ *
+ * @etymology (Alig)ned memory (alloc)ation.
+ *
+ * @param boundary The alignment.
+ * @param size The number of bytes to allocated.
+ * @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.
+ * @throws EINVAL If `boundary` is not a power of two.
+ *
+ * @since Always.
+ */
+void* aligned_alloc(size_t boundary, size_t size)
+{
+ return memalign(boundary, size);
+}
+