kvargs: add free attribute annotation
RFC 1/1 kvargs: add free attribute annotation
Pointers created from parsing kvargs must be freed via rte_kvargs_free. Use compiler attributes to catch misuse.
Signed-off-by Stephen Hemminger <[email protected]>
--- lib/kvargs/rte_kvargs.c | 10 ++++++++-- lib/kvargs/rte_kvargs.h | 40 ++++++++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 18 deletions(-)
lib/kvargs/rte_kvargs.c +8 -2
@@ -249,6 +249,13 @@ rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key)return rte_kvargs_get_with_value (kvlist, key,NULL ); } +/* Helper function to get a pointer with kvargs_free attribute */ +static __rte_dealloc_kvargs_free struct rte_kvargs * +rte_kvargs_alloc (void ) +{ +return calloc (1 ,sizeof (struct rte_kvargs )); +} +/* * Parse the arguments "key=value,key=value,..." string and return * an allocated structure that contains a key/value list. Also
@@ -259,10 +266,9 @@ rte_kvargs_parse(const char *args, const char * const valid_keys[]) {struct rte_kvargs *kvlist; - kvlist =malloc (sizeof (*kvlist)); + kvlist =rte_kvargs_alloc ();if (kvlist ==NULL )return NULL ; -memset (kvlist,0 ,sizeof (*kvlist));if (rte_kvargs_tokenize (kvlist, args) <0 ) {rte_kvargs_free (kvlist);
lib/kvargs/rte_kvargs.h +24 -16
@@ -21,6 +21,8 @@* ethernet devices at initialization for arguments parsing. */ +#include <rte_common.h> +#ifdef __cplusplusextern "C" {#endif
@@ -62,6 +64,22 @@ struct rte_kvargs {struct rte_kvargs_pair pairs[RTE_KVARGS_MAX ];/**< list of key/values */ }; +/** +* Functions that expect return value to be freed with rte_kvargs_free() +*/ +#define __rte_dealloc_kvargs_free __rte_dealloc(rte_kvargs_free, 1) + +/** +* Free a rte_kvargs structure +* +* Free a rte_kvargs structure previously allocated with +* rte_kvargs_parse(). +* +* @param kvlist +* The rte_kvargs structure. No error if NULL. +*/ +void rte_kvargs_free (struct rte_kvargs *kvlist); +/** * Allocate a rte_kvargs and store key/value associations from a string *
@@ -80,8 +98,9 @@ struct rte_kvargs {* - A pointer to an allocated rte_kvargs structure on success * - NULL on error */ -struct rte_kvargs *rte_kvargs_parse (const char *args, -const char *const valid_keys[]); +struct rte_kvargs * +rte_kvargs_parse (const char *args,const char *const valid_keys[]) + __rte_malloc __rte_dealloc_kvargs_free;/** * Allocate a rte_kvargs and store key/value associations from a string.
@@ -108,20 +127,9 @@ struct rte_kvargs *rte_kvargs_parse(const char *args,* - A pointer to an allocated rte_kvargs structure on success * - NULL on error */ -struct rte_kvargs *rte_kvargs_parse_delim (const char *args, -const char *const valid_keys[], -const char *valid_ends); - -/** -* Free a rte_kvargs structure -* -* Free a rte_kvargs structure previously allocated with -* rte_kvargs_parse(). -* -* @param kvlist -* The rte_kvargs structure. No error if NULL. -*/ -void rte_kvargs_free (struct rte_kvargs *kvlist); +struct rte_kvargs * +rte_kvargs_parse_delim (const char *args,const char *const valid_keys[],const char *valid_ends) + __rte_malloc __rte_dealloc_kvargs_free;/** * Get the value associated with a given key.
Signature · 1 line
2.45.2