On Fri, Sep 27, 2024 at 4:48 PM Stephen Hemminger <step...@networkplumber.org> wrote: > > The allocation functions take a alignment argument that > can be useful to hint the compiler optimizer. > > This is supported by Gcc and Clang but only useful with > Gcc because Clang gives warning if alignment is 0. > > Recent versions of GCC have a malloc attribute that can > be used to find mismatches between allocation and free; > the typical problem caught is a pointer allocated with > rte_malloc() that is then incorrectly freed using free().
Interesting tool. > > Signed-off-by: Stephen Hemminger <step...@networkplumber.org> > --- > lib/eal/include/rte_common.h | 30 ++++++++++++++++++++++++++++++ > lib/eal/include/rte_malloc.h | 24 ++++++++++++++++-------- > 2 files changed, 46 insertions(+), 8 deletions(-) > > diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h > index eec0400dad..1b3781274d 100644 > --- a/lib/eal/include/rte_common.h > +++ b/lib/eal/include/rte_common.h > @@ -228,6 +228,36 @@ typedef uint16_t unaligned_uint16_t; > #define __rte_alloc_size(...) > #endif > > +/** > + * Tells the compiler that the function returns a value that points to > + * memory aligned by a function argument. > + * Not enabled on clang because it warns if align argument is zero. > + */ > +#if defined(RTE_CC_GCC) > +#define __rte_alloc_align(align_arg) \ > + __attribute__((alloc_align(align_arg))) > +#else > +#define __rte_alloc_align(...) > +#endif > + > +/** > + * Tells the compiler this is a function like malloc and that the pointer > + * returned cannot alias any other pointer (ie new memory). > + * > + * Also, with recent GCC versions also able to track that proper > + * dealloctor function is used for this pointer. > + */ > +#if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000) Even though it is probably equivalent, GCC_VERSION is set with RTE_CC_GCC. > +#define __rte_alloc_func(free_func) \ > + __attribute__((malloc, malloc(free_func))) I read that this malloc attribute can also make use of the arg index to assume the pointer is freed. Did you try this feature? Something like: @@ -248,14 +248,13 @@ typedef uint16_t unaligned_uint16_t; * dealloctor function is used for this pointer. */ #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000) -#define __rte_alloc_func(free_func) \ - __attribute__((malloc, malloc(free_func))) - +#define __rte_alloc_func(...) \ + __attribute__((malloc, malloc(__VA_ARGS__))) #elif defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) -#define __rte_alloc_func(free_func) \ +#define __rte_alloc_func(...) \ __attribute__((malloc)) #else -#define __rte_alloc_func(free_func) +#define __rte_alloc_func(...) #endif #define RTE_PRIORITY_LOG 101 > + > +#elif defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) > +#define __rte_alloc_func(free_func) \ > + __attribute__((malloc)) > +#else > +#define __rte_alloc_func(free_func) > +#endif > + > #define RTE_PRIORITY_LOG 101 > #define RTE_PRIORITY_BUS 110 > #define RTE_PRIORITY_CLASS 120 -- David Marchand