]> Savannah Git Hosting - gnulib.git/commitdiff
aligned-malloc: Prepare for allocation-deallocation checking.
authorBruno Haible <bruno@clisp.org>
Sat, 7 Aug 2021 22:09:10 +0000 (00:09 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 7 Aug 2021 22:09:10 +0000 (00:09 +0200)
* lib/aligned-malloc.h (aligned_free): Move declaration up.
(aligned_malloc): Add comment that deallocation must happen through
'aligned_free'.

ChangeLog
lib/aligned-malloc.h

index 1f1230911433cc4189f259ea2d5fa9afd12d4b7c..be558220e8327f6cb95e1eb3361b6edd57292392 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2021-08-07  Bruno Haible  <bruno@clisp.org>
+
+       aligned-malloc: Prepare for allocation-deallocation checking.
+       * lib/aligned-malloc.h (aligned_free): Move declaration up.
+       (aligned_malloc): Add comment that deallocation must happen through
+       'aligned_free'.
+
 2021-08-07  Bruno Haible  <bruno@clisp.org>
 
        list, set, oset, map, omap: Prepare allocation-deallocation checking.
index bcb7daa5e0126b8d953afcd69587c292ea3bb7a7..c20f8e6f54ac0827b2f7dbb294b74aac9ce1f18d 100644 (file)
 #endif
 #if ((ALIGNMENT) <= MALLOC_ALIGNMENT) || HAVE_POSIX_MEMALIGN || HAVE_ALIGNED_ALLOC || HAVE_MEMALIGN
 
+# if defined aligned_free || __GNUC__ >= 11
+   /* The caller wants an inline function, not a macro,
+      or we can use GCC's -Wmismatched-dealloc warning.  */
+static inline void
+aligned_free (void *q)
+{
+  free (q);
+}
+# else
+#  define aligned_free free
+# endif
+
 # if (ALIGNMENT) <= MALLOC_ALIGNMENT
 /* Simply use malloc.  */
 
-#  ifdef aligned_malloc
-   /* The caller wants an inline function, not a macro.  */
-static inline void *
+#  if defined aligned_malloc || __GNUC__ >= 11
+   /* The caller wants an inline function, not a macro,
+      or GCC's -Wmismatched-dealloc warning might be in effect.  */
+static inline
+/*_GL_ATTRIBUTE_DEALLOC (aligned_free, 1)*/
+void *
 aligned_malloc (size_t size)
 {
   return malloc (size);
@@ -71,7 +86,9 @@ aligned_malloc (size_t size)
 /* Use posix_memalign.
    This is OK since ALIGNMENT > MALLOC_ALIGNMENT >= sizeof (void *).  */
 
-static inline void *
+static inline
+/*_GL_ATTRIBUTE_DEALLOC (aligned_free, 1)*/
+void *
 aligned_malloc (size_t size)
 {
   void *p;
@@ -85,7 +102,9 @@ aligned_malloc (size_t size)
 # elif HAVE_ALIGNED_ALLOC
 /* Use aligned_alloc.  */
 
-static inline void *
+static inline
+/*_GL_ATTRIBUTE_DEALLOC (aligned_free, 1)*/
+void *
 aligned_malloc (size_t size)
 {
   /* Round up SIZE to the next multiple of ALIGNMENT,
@@ -102,7 +121,9 @@ aligned_malloc (size_t size)
 # elif HAVE_MEMALIGN                    /* HP-UX, IRIX, Solaris <= 10 */
 /* Use memalign.  */
 
-static inline void *
+static inline
+/*_GL_ATTRIBUTE_DEALLOC (aligned_free, 1)*/
+void *
 aligned_malloc (size_t size)
 {
   return memalign ((ALIGNMENT), size);
@@ -110,21 +131,32 @@ aligned_malloc (size_t size)
 
 # endif
 
-# ifdef aligned_free
-   /* The caller wants an inline function, not a macro.  */
+#else
+/* Use malloc and waste a bit of memory.  */
+
 static inline void
 aligned_free (void *q)
 {
-  free (q);
+  if (q != NULL)
+    {
+      if ((uintptr_t) q & ((ALIGNMENT) - 1))
+        /* Argument not aligned as expected.  */
+        abort ();
+      else
+        {
+          void *p = ((void **) q)[-1];
+          if (!((uintptr_t) p <= (uintptr_t) q
+                && (uintptr_t) q - (uintptr_t) p >= MALLOC_ALIGNMENT
+                && (uintptr_t) q - (uintptr_t) p <= (ALIGNMENT)))
+            abort ();
+          free (p);
+        }
+    }
 }
-# else
-#  define aligned_free free
-# endif
-
-#else
-/* Use malloc and waste a bit of memory.  */
 
-static inline void *
+static inline
+/*_GL_ATTRIBUTE_DEALLOC (aligned_free, 1)*/
+void *
 aligned_malloc (size_t size)
 {
   size += (ALIGNMENT);
@@ -146,24 +178,4 @@ aligned_malloc (size_t size)
   return NULL;
 }
 
-static inline void
-aligned_free (void *q)
-{
-  if (q != NULL)
-    {
-      if ((uintptr_t) q & ((ALIGNMENT) - 1))
-        /* Argument not aligned as expected.  */
-        abort ();
-      else
-        {
-          void *p = ((void **) q)[-1];
-          if (!((uintptr_t) p <= (uintptr_t) q
-                && (uintptr_t) q - (uintptr_t) p >= MALLOC_ALIGNMENT
-                && (uintptr_t) q - (uintptr_t) p <= (ALIGNMENT)))
-            abort ();
-          free (p);
-        }
-    }
-}
-
 #endif