Skip to content

Commit

Permalink
env: Drop the ACTION typedef
Browse files Browse the repository at this point in the history
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
to. 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@chromium.org>
  • Loading branch information
sjg20 authored and trini committed Aug 11, 2019
1 parent d3716dd commit 3f0d680
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion api/api.c
Expand Up @@ -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, ENV_FIND, &match, &env_htab, 0);
if (i == 0) {
i = API_EINVAL;
goto done;
Expand Down
8 changes: 4 additions & 4 deletions cmd/nvedit.c
Expand Up @@ -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, ENV_FIND, &ep, &env_htab, flag);
if (ep == NULL)
return 0;
len = printf("%s=%s\n", ep->key, ep->data);
Expand Down Expand Up @@ -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, ENV_ENTER, &ep, &env_htab, env_flag);
free(value);
if (!ep) {
printf("## Error inserting \"%s\" variable, errno=%d\n",
Expand Down Expand Up @@ -668,7 +668,7 @@ char *env_get(const char *name)

e.key = name;
e.data = NULL;
hsearch_r(e, FIND, &ep, &env_htab, 0);
hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);

return ep ? ep->data : NULL;
}
Expand Down Expand Up @@ -1269,7 +1269,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, ENV_FIND, &ep, &env_htab, 0);

return (ep == NULL) ? 1 : 0;
}
Expand Down
6 changes: 3 additions & 3 deletions drivers/tee/sandbox.c
Expand Up @@ -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, ENV_FIND, &ep, &state->pstorage_htab, 0);
if (!ep)
return TEE_ERROR_ITEM_NOT_FOUND;

Expand All @@ -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, ENV_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, ENV_ENTER, &ep, &state->pstorage_htab, 0);
if (!ep)
return TEE_ERROR_OUT_OF_MEMORY;

Expand Down
2 changes: 1 addition & 1 deletion env/callback.c
Expand Up @@ -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, ENV_FIND, &ep, &env_htab, 0);

/* does the env variable actually exist? */
if (ep != NULL) {
Expand Down
2 changes: 1 addition & 1 deletion env/flags.c
Expand Up @@ -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, ENV_FIND, &ep, &env_htab, 0);

/* does the env variable actually exist? */
if (ep != NULL) {
Expand Down
16 changes: 8 additions & 8 deletions include/search.h
Expand Up @@ -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 {
ENV_FIND,
ENV_ENTER,
};

/** struct env_entry - An entry in the environment hashtable */
struct env_entry {
Expand Down Expand Up @@ -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 `ENV_FIND' return found entry or signal error by returning
* NULL. If __action is `ENV_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);

Expand Down
18 changes: 9 additions & 9 deletions lib/hashtable.c
Expand Up @@ -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 == ENV_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
Expand Down Expand Up @@ -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 ENV_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 == ENV_ENTER && item.data) {
/* check for permission */
if (htab->change_ok != NULL && htab->change_ok(
&htab->table[idx].entry, item.data,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 == ENV_ENTER) {
/*
* If table is full and another entry should be
* entered return with error.
Expand Down Expand Up @@ -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, ENV_FIND, &ep, htab, 0);
if (idx == 0) {
__set_errno(ESRCH);
return 0; /* not found */
Expand Down Expand Up @@ -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, ENV_ENTER, &rv, htab, flag);
if (rv == NULL)
printf("himport_r: can't insert \"%s=%s\" into hash table\n",
name, value);
Expand Down
8 changes: 4 additions & 4 deletions test/env/hashtable.c
Expand Up @@ -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, ENV_ENTER, &ritem, htab, 0));
}

return 0;
Expand All @@ -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, ENV_FIND, &ritem, htab, 0);
ut_assert(ritem);
ut_asserteq_str(key, ritem->key);
ut_asserteq_str(key, ritem->data);
Expand All @@ -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, ENV_ENTER, &ritem, htab, 0);
ritem = NULL;

hsearch_r(item, FIND, &ritem, htab, 0);
hsearch_r(item, ENV_FIND, &ritem, htab, 0);
ut_assert(ritem);
ut_asserteq_str(key, ritem->key);
ut_asserteq_str(key, ritem->data);
Expand Down

0 comments on commit 3f0d680

Please sign in to comment.