[U-Boot] [PATCH 33/39] env: Drop the ACTION typedef

Simon Glass sjg at chromium.org
Sun Jul 28 14:03:50 UTC 2019


Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
too. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 api/api.c             |  2 +-
 cmd/nvedit.c          |  8 ++++----
 drivers/tee/sandbox.c |  6 +++---
 env/callback.c        |  2 +-
 env/flags.c           |  2 +-
 include/search.h      | 16 ++++++++--------
 lib/hashtable.c       | 18 +++++++++---------
 test/env/hashtable.c  |  8 ++++----
 8 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/api/api.c b/api/api.c
index a0fc62ca9e..5e7c8149bf 100644
--- a/api/api.c
+++ b/api/api.c
@@ -514,7 +514,7 @@ static int API_env_enum(va_list ap)
 		if (s != NULL)
 			*s = 0;
 		search.key = var;
-		i = hsearch_r(search, FIND, &match, &env_htab, 0);
+		i = hsearch_r(search, ENVA_FIND, &match, &env_htab, 0);
 		if (i == 0) {
 			i = API_EINVAL;
 			goto done;
diff --git a/cmd/nvedit.c b/cmd/nvedit.c
index d6a86abb03..3d244951b0 100644
--- a/cmd/nvedit.c
+++ b/cmd/nvedit.c
@@ -98,7 +98,7 @@ static int env_print(char *name, int flag)
 
 		e.key = name;
 		e.data = NULL;
-		hsearch_r(e, FIND, &ep, &env_htab, flag);
+		hsearch_r(e, ENVA_FIND, &ep, &env_htab, flag);
 		if (ep == NULL)
 			return 0;
 		len = printf("%s=%s\n", ep->key, ep->data);
@@ -288,7 +288,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
 
 	e.key	= name;
 	e.data	= value;
-	hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
+	hsearch_r(e, ENVA_ENTER, &ep, &env_htab, env_flag);
 	free(value);
 	if (!ep) {
 		printf("## Error inserting \"%s\" variable, errno=%d\n",
@@ -680,7 +680,7 @@ char *env_get(const char *name)
 
 		e.key	= name;
 		e.data	= NULL;
-		hsearch_r(e, FIND, &ep, &env_htab, 0);
+		hsearch_r(e, ENVA_FIND, &ep, &env_htab, 0);
 
 		return ep ? ep->data : NULL;
 	}
@@ -1281,7 +1281,7 @@ static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
 
 	e.key = argv[1];
 	e.data = NULL;
-	hsearch_r(e, FIND, &ep, &env_htab, 0);
+	hsearch_r(e, ENVA_FIND, &ep, &env_htab, 0);
 
 	return (ep == NULL) ? 1 : 0;
 }
diff --git a/drivers/tee/sandbox.c b/drivers/tee/sandbox.c
index 4bbcf74967..0561957067 100644
--- a/drivers/tee/sandbox.c
+++ b/drivers/tee/sandbox.c
@@ -174,7 +174,7 @@ static u32 ta_avb_invoke_func(struct udevice *dev, u32 func, uint num_params,
 
 		e.key = name;
 		e.data = NULL;
-		hsearch_r(e, FIND, &ep, &state->pstorage_htab, 0);
+		hsearch_r(e, ENVA_FIND, &ep, &state->pstorage_htab, 0);
 		if (!ep)
 			return TEE_ERROR_ITEM_NOT_FOUND;
 
@@ -198,13 +198,13 @@ static u32 ta_avb_invoke_func(struct udevice *dev, u32 func, uint num_params,
 
 		e.key = name;
 		e.data = NULL;
-		hsearch_r(e, FIND, &ep, &state->pstorage_htab, 0);
+		hsearch_r(e, ENVA_FIND, &ep, &state->pstorage_htab, 0);
 		if (ep)
 			hdelete_r(e.key, &state->pstorage_htab, 0);
 
 		e.key = name;
 		e.data = value;
-		hsearch_r(e, ENTER, &ep, &state->pstorage_htab, 0);
+		hsearch_r(e, ENVA_ENTER, &ep, &state->pstorage_htab, 0);
 		if (!ep)
 			return TEE_ERROR_OUT_OF_MEMORY;
 
diff --git a/env/callback.c b/env/callback.c
index d539da93aa..9bfa744921 100644
--- a/env/callback.c
+++ b/env/callback.c
@@ -98,7 +98,7 @@ static int set_callback(const char *name, const char *value, void *priv)
 	e.key	= name;
 	e.data	= NULL;
 	e.callback = NULL;
-	hsearch_r(e, FIND, &ep, &env_htab, 0);
+	hsearch_r(e, ENVA_FIND, &ep, &env_htab, 0);
 
 	/* does the env variable actually exist? */
 	if (ep != NULL) {
diff --git a/env/flags.c b/env/flags.c
index fdbad7bf33..388c2f184b 100644
--- a/env/flags.c
+++ b/env/flags.c
@@ -458,7 +458,7 @@ static int set_flags(const char *name, const char *value, void *priv)
 	e.key	= name;
 	e.data	= NULL;
 	e.callback = NULL;
-	hsearch_r(e, FIND, &ep, &env_htab, 0);
+	hsearch_r(e, ENVA_FIND, &ep, &env_htab, 0);
 
 	/* does the env variable actually exist? */
 	if (ep != NULL) {
diff --git a/include/search.h b/include/search.h
index c99648f80b..fa7758480b 100644
--- a/include/search.h
+++ b/include/search.h
@@ -19,11 +19,11 @@
 
 #define __set_errno(val) do { errno = val; } while (0)
 
-/* Action which shall be performed in the call to hsearch.  */
-typedef enum {
-	FIND,
-	ENTER
-} ACTION;
+/* enum env_action: action which shall be performed in the call to hsearch */
+enum env_action {
+	ENVA_FIND,
+	ENVA_ENTER,
+};
 
 /** struct env_entry - An entry in the environment hashtable */
 struct env_entry {
@@ -64,11 +64,11 @@ extern void hdestroy_r(struct hsearch_data *__htab);
 
 /*
  * Search for entry matching __item.key in internal hash table.  If
- * ACTION is `FIND' return found entry or signal error by returning
- * NULL.  If ACTION is `ENTER' replace existing data (if any) with
+ * __action is `ENVA_FIND' return found entry or signal error by returning
+ * NULL.  If __action is `ENVA_ENTER' replace existing data (if any) with
  * __item.data.
  * */
-extern int hsearch_r(struct env_entry __item, ACTION __action,
+extern int hsearch_r(struct env_entry __item, enum env_action __action,
 		     struct env_entry **__retval, struct hsearch_data *__htab,
 		     int __flag);
 
diff --git a/lib/hashtable.c b/lib/hashtable.c
index 1093d8adaa..6ff0de3e79 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -194,7 +194,7 @@ void hdestroy_r(struct hsearch_data *htab)
  *   data any more.
  * - The standard implementation does not provide a way to update an
  *   existing entry.  This version will create a new entry or update an
- *   existing one when both "action == ENTER" and "item.data != NULL".
+ *   existing one when both "action == ENVA_ENTER" and "item.data != NULL".
  * - Instead of returning 1 on success, we return the index into the
  *   internal hash table, which is also guaranteed to be positive.
  *   This allows us direct access to the found hash table slot for
@@ -223,17 +223,17 @@ int hmatch_r(const char *match, int last_idx, struct env_entry **retval,
 
 /*
  * Compare an existing entry with the desired key, and overwrite if the action
- * is ENTER.  This is simply a helper function for hsearch_r().
+ * is ENVA_ENTER.  This is simply a helper function for hsearch_r().
  */
 static inline int _compare_and_overwrite_entry(struct env_entry item,
-		ACTION action, struct env_entry **retval,
+		enum env_action action, struct env_entry **retval,
 		struct hsearch_data *htab, int flag, unsigned int hval,
 		unsigned int idx)
 {
 	if (htab->table[idx].used == hval
 	    && strcmp(item.key, htab->table[idx].entry.key) == 0) {
 		/* Overwrite existing value? */
-		if ((action == ENTER) && (item.data != NULL)) {
+		if (action == ENVA_ENTER && item.data) {
 			/* check for permission */
 			if (htab->change_ok != NULL && htab->change_ok(
 			    &htab->table[idx].entry, item.data,
@@ -272,8 +272,8 @@ static inline int _compare_and_overwrite_entry(struct env_entry item,
 	return -1;
 }
 
-int hsearch_r(struct env_entry item, ACTION action, struct env_entry **retval,
-	      struct hsearch_data *htab, int flag)
+int hsearch_r(struct env_entry item, enum env_action action,
+	      struct env_entry **retval, struct hsearch_data *htab, int flag)
 {
 	unsigned int hval;
 	unsigned int count;
@@ -354,7 +354,7 @@ int hsearch_r(struct env_entry item, ACTION action, struct env_entry **retval,
 	}
 
 	/* An empty bucket has been found. */
-	if (action == ENTER) {
+	if (action == ENVA_ENTER) {
 		/*
 		 * If table is full and another entry should be
 		 * entered return with error.
@@ -456,7 +456,7 @@ int hdelete_r(const char *key, struct hsearch_data *htab, int flag)
 
 	e.key = (char *)key;
 
-	idx = hsearch_r(e, FIND, &ep, htab, 0);
+	idx = hsearch_r(e, ENVA_FIND, &ep, htab, 0);
 	if (idx == 0) {
 		__set_errno(ESRCH);
 		return 0;	/* not found */
@@ -931,7 +931,7 @@ int himport_r(struct hsearch_data *htab,
 		e.key = name;
 		e.data = value;
 
-		hsearch_r(e, ENTER, &rv, htab, flag);
+		hsearch_r(e, ENVA_ENTER, &rv, htab, flag);
 		if (rv == NULL)
 			printf("himport_r: can't insert \"%s=%s\" into hash table\n",
 				name, value);
diff --git a/test/env/hashtable.c b/test/env/hashtable.c
index bad276bd10..f9f3807fca 100644
--- a/test/env/hashtable.c
+++ b/test/env/hashtable.c
@@ -28,7 +28,7 @@ static int htab_fill(struct unit_test_state *uts,
 		item.data = key;
 		item.flags = 0;
 		item.key = key;
-		ut_asserteq(1, hsearch_r(item, ENTER, &ritem, htab, 0));
+		ut_asserteq(1, hsearch_r(item, ENVA_ENTER, &ritem, htab, 0));
 	}
 
 	return 0;
@@ -48,7 +48,7 @@ static int htab_check_fill(struct unit_test_state *uts,
 		item.flags = 0;
 		item.data = key;
 		item.key = key;
-		hsearch_r(item, FIND, &ritem, htab, 0);
+		hsearch_r(item, ENVA_FIND, &ritem, htab, 0);
 		ut_assert(ritem);
 		ut_asserteq_str(key, ritem->key);
 		ut_asserteq_str(key, ritem->data);
@@ -71,10 +71,10 @@ static int htab_create_delete(struct unit_test_state *uts,
 		item.flags = 0;
 		item.data = key;
 		item.key = key;
-		hsearch_r(item, ENTER, &ritem, htab, 0);
+		hsearch_r(item, ENVA_ENTER, &ritem, htab, 0);
 		ritem = NULL;
 
-		hsearch_r(item, FIND, &ritem, htab, 0);
+		hsearch_r(item, ENVA_FIND, &ritem, htab, 0);
 		ut_assert(ritem);
 		ut_asserteq_str(key, ritem->key);
 		ut_asserteq_str(key, ritem->data);
-- 
2.22.0.709.g102302147b-goog



More information about the U-Boot mailing list