DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [RFC] kvargs: add free attribute annotation
Date: Thu, 31 Oct 2024 16:47:49 -0700	[thread overview]
Message-ID: <20241031234809.213517-1-stephen@networkplumber.org> (raw)

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


                 reply	other threads:[~2024-10-31 23:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241031234809.213517-1-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).