From 2d2ab37b554c567e26f3396affe67d73ebf1f38b Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 8 Aug 2021 00:09:10 +0200 Subject: [PATCH] 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'. --- ChangeLog | 7 ++++ lib/aligned-malloc.h | 84 +++++++++++++++++++++++++------------------- 2 files changed, 55 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1f12309114..be558220e8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2021-08-07 Bruno Haible + + 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 list, set, oset, map, omap: Prepare allocation-deallocation checking. diff --git a/lib/aligned-malloc.h b/lib/aligned-malloc.h index bcb7daa5e0..c20f8e6f54 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 -- 2.39.5