aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--include/malloc.h6
-rw-r--r--include/stddef.h2
-rw-r--r--src/malloc.c32
3 files changed, 35 insertions, 5 deletions
diff --git a/include/malloc.h b/include/malloc.h
index 28f937c..66adcbd 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -37,6 +37,8 @@
/**
* Create a new memory allocation on the heap.
* The allocation will not be initialised.
+ * The returned pointer has an alignment usable
+ * for any compiler-independent intrinsic data type.
*
* @param size The size of the allocation.
* @return Pointer to the beginning of the new allocation.
@@ -99,7 +101,9 @@ void* zalloc(size_t)
/**
* 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.
+ * size, copies the data, and frees the old allocation. The returned
+ * pointer has an alignment usable for any compiler-independent intrinsic
+ * data type, if a new pointer is returned.
*
* On error, `ptr` is not freed.
*
diff --git a/include/stddef.h b/include/stddef.h
index e33cfed..50e9ba1 100644
--- a/include/stddef.h
+++ b/include/stddef.h
@@ -82,7 +82,7 @@
#define __NEED_ptrdiff_t
#define __NEED_wchar_t
#define __NEED_size_t
-#if defined(__C11__)
+#if defined(__C11__) || defined(__BUILDING_SLIBC)
# define __NEED_max_align_t
#endif
diff --git a/src/malloc.c b/src/malloc.c
index 528c49f..cd22ccd 100644
--- a/src/malloc.c
+++ b/src/malloc.c
@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
+#include <stddef.h>
#include <slibc-alloc.h>
#include <strings.h>
/* TODO #include <sys/mman.h> */
@@ -34,6 +35,7 @@
/**
* Create a new memory allocation on the heap.
* The allocation will not be initialised.
+ * The returned pointer is unaligned.
*
* @param size The size of the allocation.
* @return Pointer to the beginning of the new allocation.
@@ -45,7 +47,7 @@
*
* @throws ENOMEM The process cannot allocate more memory.
*/
-void* malloc(size_t size)
+static void* unaligned_malloc(size_t size)
{
char* ptr;
size_t full_size;
@@ -65,6 +67,28 @@ void* malloc(size_t size)
/**
+ * Create a new memory allocation on the heap.
+ * The allocation will not be initialised.
+ * The returned pointer has an alignment usable
+ * for any compiler-independent intrinsic data type.
+ *
+ * @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* malloc(size_t size)
+{
+ return memalign(sizeof(max_align_t), size);
+}
+
+
+/**
* Variant of `malloc` that clears the allocation with zeroes.
*
* `p = calloc(n, m)` is equivalent to
@@ -125,7 +149,9 @@ void* zalloc(size_t 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.
+ * size, copies the data, and frees the old allocation. The returned
+ * pointer has an alignment usable for any compiler-independent intrinsic
+ * data type, if a new pointer is returned.
*
* On error, `ptr` is not freed.
*
@@ -216,7 +242,7 @@ void* memalign(size_t boundary, size_t size)
if (__builtin_uaddl_overflow(boundary - 1, size, &full_size))
return errno = ENOMEM, NULL;
- ptr = malloc(full_size);
+ ptr = unaligned_malloc(full_size);
if (ptr == NULL)
return NULL;