about summary refs log tree commit diff
path: root/malloc/malloc.c
diff options
context:
space:
mode:
authorDJ Delorie <dj@redhat.com>2023-03-21 00:46:43 -0400
committerDJ Delorie <dj@redhat.com>2023-05-08 16:40:10 -0400
commitd1417176a35d27ffb8da0ffb1e33154163b6eeb2 (patch)
treeaa3e8cd61cadf5fec88e4cf57cfaf4b0e0ae6538 /malloc/malloc.c
parentcea74a4a24c36202309e8254f1f938e2166488f3 (diff)
downloadglibc-d1417176a35d27ffb8da0ffb1e33154163b6eeb2.tar.gz
glibc-d1417176a35d27ffb8da0ffb1e33154163b6eeb2.tar.xz
glibc-d1417176a35d27ffb8da0ffb1e33154163b6eeb2.zip
aligned_alloc: conform to C17
This patch adds the strict checking for power-of-two alignments
in aligned_alloc(), and updates the manual accordingly.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'malloc/malloc.c')
-rw-r--r--malloc/malloc.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/malloc/malloc.c b/malloc/malloc.c
index e33ed665db..5d8b61d66c 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3528,6 +3528,29 @@ __libc_memalign (size_t alignment, size_t bytes)
   void *address = RETURN_ADDRESS (0);
   return _mid_memalign (alignment, bytes, address);
 }
+libc_hidden_def (__libc_memalign)
+
+/* For ISO C17.  */
+void *
+weak_function
+aligned_alloc (size_t alignment, size_t bytes)
+{
+  if (!__malloc_initialized)
+    ptmalloc_init ();
+
+/* Similar to memalign, but starting with ISO C17 the standard
+   requires an error for alignments that are not supported by the
+   implementation.  Valid alignments for the current implementation
+   are non-negative powers of two.  */
+  if (!powerof2 (alignment) || alignment == 0)
+    {
+      __set_errno (EINVAL);
+      return 0;
+    }
+
+  void *address = RETURN_ADDRESS (0);
+  return _mid_memalign (alignment, bytes, address);
+}
 
 static void *
 _mid_memalign (size_t alignment, size_t bytes, void *address)
@@ -3618,9 +3641,6 @@ _mid_memalign (size_t alignment, size_t bytes, void *address)
           ar_ptr == arena_for_chunk (mem2chunk (p)));
   return tag_new_usable (p);
 }
-/* For ISO C11.  */
-weak_alias (__libc_memalign, aligned_alloc)
-libc_hidden_def (__libc_memalign)
 
 void *
 __libc_valloc (size_t bytes)