* [RFC] kvargs: add free attribute annotation
@ 2024-10-31 23:47 Stephen Hemminger
0 siblings, 0 replies; only message in thread
From: Stephen Hemminger @ 2024-10-31 23:47 UTC (permalink / raw)
To: dev; +Cc: Stephen Hemminger
Pointers created from parsing kvargs must be freed via
rte_kvargs_free. Use compiler attributes to catch misuse.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
lib/kvargs/rte_kvargs.c | 10 ++++++++--
lib/kvargs/rte_kvargs.h | 40 ++++++++++++++++++++++++----------------
2 files changed, 32 insertions(+), 18 deletions(-)
diff --git a/lib/kvargs/rte_kvargs.c b/lib/kvargs/rte_kvargs.c
index 1d355516bc..cdfb7318f1 100644
--- a/lib/kvargs/rte_kvargs.c
+++ b/lib/kvargs/rte_kvargs.c
@@ -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);
diff --git a/lib/kvargs/rte_kvargs.h b/lib/kvargs/rte_kvargs.h
index 73fa1e621b..11a78a109b 100644
--- a/lib/kvargs/rte_kvargs.h
+++ b/lib/kvargs/rte_kvargs.h
@@ -21,6 +21,8 @@
* ethernet devices at initialization for arguments parsing.
*/
+#include <rte_common.h>
+
#ifdef __cplusplus
extern "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.
--
2.45.2
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2024-10-31 23:48 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-31 23:47 [RFC] kvargs: add free attribute annotation Stephen Hemminger
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).