From 8aee94558b01383867b3d9f93a19ba47e51c063d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 18 Oct 2015 04:28:03 +0200 Subject: the pointer returned by malloc is aligned MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/malloc.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'src/malloc.c') 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 . */ #include +#include #include #include /* TODO #include */ @@ -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; @@ -64,6 +66,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. * @@ -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; -- cgit v1.2.3-70-g09d2