diff mbox series

[v4,3/5] kvargs: add get by key function

Message ID 1618064637-16413-4-git-send-email-xuemingl@nvidia.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers show
Series eal: enable global device syntax by default | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Xueming(Steven) Li April 10, 2021, 2:23 p.m. UTC
Adds a new function to get value of a specific key from kvargs list.

Signed-off-by: Xueming Li <xuemingl@nvidia.com>
Reviewed-by: Gaetan Rivet <grive@u256.net>
---
 lib/librte_kvargs/rte_kvargs.c | 20 ++++++++++++++++++++
 lib/librte_kvargs/rte_kvargs.h | 21 +++++++++++++++++++++
 lib/librte_kvargs/version.map  |  3 +++
 3 files changed, 44 insertions(+)

Comments

Olivier Matz April 12, 2021, 6:52 a.m. UTC | #1
Hi Xueming,

On Sat, Apr 10, 2021 at 02:23:55PM +0000, Xueming Li wrote:
> Adds a new function to get value of a specific key from kvargs list.
> 
> Signed-off-by: Xueming Li <xuemingl@nvidia.com>
> Reviewed-by: Gaetan Rivet <grive@u256.net>
> ---
>  lib/librte_kvargs/rte_kvargs.c | 20 ++++++++++++++++++++
>  lib/librte_kvargs/rte_kvargs.h | 21 +++++++++++++++++++++
>  lib/librte_kvargs/version.map  |  3 +++
>  3 files changed, 44 insertions(+)
> 
> diff --git a/lib/librte_kvargs/rte_kvargs.c b/lib/librte_kvargs/rte_kvargs.c
> index ffae8914cf..40e7670ab3 100644
> --- a/lib/librte_kvargs/rte_kvargs.c
> +++ b/lib/librte_kvargs/rte_kvargs.c
> @@ -203,6 +203,26 @@ rte_kvargs_free(struct rte_kvargs *kvlist)
>  	free(kvlist);
>  }
>  
> +/* Lookup a value in an rte_kvargs list by its key. */
> +const char *
> +rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key)
> +{
> +	unsigned int i;
> +
> +	if (!kvlist)
> +		return NULL;
> +	for (i = 0; i < kvlist->count; ++i) {
> +		/* Allows key to be NULL. */
> +		if (!key && !kvlist->pairs[i].key)
> +			return kvlist->pairs[i].value;

Is it possible that kvlist->pairs[i].key == NULL? In which case?


Thanks,
Olivier
Xueming(Steven) Li April 12, 2021, 12:07 p.m. UTC | #2
> -----Original Message-----
> From: Olivier Matz <olivier.matz@6wind.com>
> Sent: Monday, April 12, 2021 2:53 PM
> To: Xueming(Steven) Li <xuemingl@nvidia.com>
> Cc: NBU-Contact-Thomas Monjalon <thomas@monjalon.net>; Gaetan Rivet <gaetanr@nvidia.com>; dev@dpdk.org; Asaf Penso
> <asafp@nvidia.com>; Ray Kinsella <mdr@ashroe.eu>; Neil Horman <nhorman@tuxdriver.com>
> Subject: Re: [PATCH v4 3/5] kvargs: add get by key function
> 
> Hi Xueming,
> 
> On Sat, Apr 10, 2021 at 02:23:55PM +0000, Xueming Li wrote:
> > Adds a new function to get value of a specific key from kvargs list.
> >
> > Signed-off-by: Xueming Li <xuemingl@nvidia.com>
> > Reviewed-by: Gaetan Rivet <grive@u256.net>
> > ---
> >  lib/librte_kvargs/rte_kvargs.c | 20 ++++++++++++++++++++
> > lib/librte_kvargs/rte_kvargs.h | 21 +++++++++++++++++++++
> > lib/librte_kvargs/version.map  |  3 +++
> >  3 files changed, 44 insertions(+)
> >
> > diff --git a/lib/librte_kvargs/rte_kvargs.c
> > b/lib/librte_kvargs/rte_kvargs.c index ffae8914cf..40e7670ab3 100644
> > --- a/lib/librte_kvargs/rte_kvargs.c
> > +++ b/lib/librte_kvargs/rte_kvargs.c
> > @@ -203,6 +203,26 @@ rte_kvargs_free(struct rte_kvargs *kvlist)
> >  	free(kvlist);
> >  }
> >
> > +/* Lookup a value in an rte_kvargs list by its key. */ const char *
> > +rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key) {
> > +	unsigned int i;
> > +
> > +	if (!kvlist)
> > +		return NULL;
> > +	for (i = 0; i < kvlist->count; ++i) {
> > +		/* Allows key to be NULL. */
> > +		if (!key && !kvlist->pairs[i].key)
> > +			return kvlist->pairs[i].value;
> 
> Is it possible that kvlist->pairs[i].key == NULL? In which case?

Impossible, will remove this in next version, thanks.

> 
> 
> Thanks,
> Olivier
Thomas Monjalon April 12, 2021, 9:18 p.m. UTC | #3
12/04/2021 14:07, Xueming(Steven) Li:
> From: Olivier Matz <olivier.matz@6wind.com>
> > > +	if (!kvlist)
> > > +		return NULL;
> > > +	for (i = 0; i < kvlist->count; ++i) {
> > > +		/* Allows key to be NULL. */
> > > +		if (!key && !kvlist->pairs[i].key)
> > > +			return kvlist->pairs[i].value;
> > 
> > Is it possible that kvlist->pairs[i].key == NULL? In which case?
> 
> Impossible, will remove this in next version, thanks.

Please do explicit checks against NULL
to make clear that they are pointers, not booleans.
diff mbox series

Patch

diff --git a/lib/librte_kvargs/rte_kvargs.c b/lib/librte_kvargs/rte_kvargs.c
index ffae8914cf..40e7670ab3 100644
--- a/lib/librte_kvargs/rte_kvargs.c
+++ b/lib/librte_kvargs/rte_kvargs.c
@@ -203,6 +203,26 @@  rte_kvargs_free(struct rte_kvargs *kvlist)
 	free(kvlist);
 }
 
+/* Lookup a value in an rte_kvargs list by its key. */
+const char *
+rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key)
+{
+	unsigned int i;
+
+	if (!kvlist)
+		return NULL;
+	for (i = 0; i < kvlist->count; ++i) {
+		/* Allows key to be NULL. */
+		if (!key && !kvlist->pairs[i].key)
+			return kvlist->pairs[i].value;
+		if (!key || !kvlist->pairs[i].key)
+			continue;
+		if (!strcmp(kvlist->pairs[i].key, key))
+			return kvlist->pairs[i].value;
+	}
+	return NULL;
+}
+
 /*
  * Parse the arguments "key=value,key=value,..." string and return
  * an allocated structure that contains a key/value list. Also
diff --git a/lib/librte_kvargs/rte_kvargs.h b/lib/librte_kvargs/rte_kvargs.h
index eff598e08b..cb3ea99850 100644
--- a/lib/librte_kvargs/rte_kvargs.h
+++ b/lib/librte_kvargs/rte_kvargs.h
@@ -114,6 +114,27 @@  struct rte_kvargs *rte_kvargs_parse_delim(const char *args,
  */
 void rte_kvargs_free(struct rte_kvargs *kvlist);
 
+/**
+ * Get the value associated with a given key.
+ *
+ * If the key is NULL, the first value from the list is returned.
+ * If multiple key matches, the value of the first one is returned.
+ *
+ * The memory returned is allocated as part of the rte_kvargs structure,
+ * it must never be modified.
+ *
+ * @param kvlist
+ *   A list of rte_kvargs pair of 'key=value'.
+ * @param key
+ *   The matching key.
+
+ * @return
+ *   NULL if no key matches the input, a value associated with a matching
+ *   key otherwise.
+ */
+__rte_experimental
+const char *rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key);
+
 /**
  * Call a handler function for each key/value matching the key
  *
diff --git a/lib/librte_kvargs/version.map b/lib/librte_kvargs/version.map
index ed375bf4a3..fb9bed4f93 100644
--- a/lib/librte_kvargs/version.map
+++ b/lib/librte_kvargs/version.map
@@ -15,4 +15,7 @@  EXPERIMENTAL {
 	rte_kvargs_parse_delim;
 	rte_kvargs_strcmp;
 
+	# added in 21.05
+	rte_kvargs_get;
+
 };