On Fri, 27 Sep 2024 18:09:22 -0400 David Marchand <david.march...@redhat.com> wrote:
> 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? Yes, but all DPDK functions use first arg, so not really that relevant