[glib/ebassi/aligned-alloc] fixup! Add aligned memory allocators



commit 3e04f1554422540f784ba7e2f7435f71e0b0d743
Author: Philip Withnall <pwithnall endlessos org>
Date:   Fri Feb 11 13:33:13 2022 +0000

    fixup! Add aligned memory allocators

 glib/gmem.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
---
diff --git a/glib/gmem.c b/glib/gmem.c
index 49dade25c..17c4add0a 100644
--- a/glib/gmem.c
+++ b/glib/gmem.c
@@ -546,7 +546,8 @@ g_mem_profile (void)
  * g_aligned_alloc:
  * @n_blocks: the number of blocks to allocate
  * @n_block_bytes: the size of each block in bytes
- * @alignment: the alignment to be enforced, as a power of 2
+ * @alignment: the alignment to be enforced, which must be a power of 2 and a
+ *   multiple of `sizeof(void*)`
  *
  * This function is similar to g_malloc(), allocating (@n_blocks * @n_block_bytes)
  * bytes, but care is taken to align the allocated memory to with the given
@@ -574,6 +575,12 @@ g_aligned_alloc (gsize n_blocks,
                G_STRLOC, alignment);
     }
 
+  if (G_UNLIKELY ((alignment % sizeof (void *)) != 0))
+    {
+      g_error ("%s: alignment %"G_GSIZE_FORMAT" must be a multiple of %"G_GSIZE_FORMAT,
+               G_STRLOC, alignment, sizeof (void *));
+    }
+
   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
     {
       g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]