bug-gnulib
[Top][All Lists]
Advanced

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

aligned-malloc: Prepare for allocation-deallocation checking


From: Bruno Haible
Subject: aligned-malloc: Prepare for allocation-deallocation checking
Date: Sun, 08 Aug 2021 00:08:59 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-210-generic; KDE/5.18.0; x86_64; ; )

As mentioned in 
<https://lists.gnu.org/archive/html/bug-gnulib/2021-08/msg00092.html>,
I believe the attribute will someday be supported also on inline functions.


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'.

diff --git a/lib/aligned-malloc.h b/lib/aligned-malloc.h
index bcb7daa..c20f8e6 100644
--- a/lib/aligned-malloc.h
+++ b/lib/aligned-malloc.h
@@ -53,12 +53,27 @@
 #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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]