mirror of
https://github.com/Stichting-MINIX-Research-Foundation/u-boot.git
synced 2025-09-11 13:08:31 -04:00
env: Consolidate common code in hsearch_r()
The same chunk of code was replicated in two places and the following changes will make that chunk grow a bit, so combine into a static func. Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
This commit is contained in:
parent
c4e0057fa7
commit
3d3b52f258
@ -247,6 +247,34 @@ int hmatch_r(const char *match, int last_idx, ENTRY ** retval,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compare an existing entry with the desired key, and overwrite if the action
|
||||||
|
* is ENTER. This is simply a helper function for hsearch_r().
|
||||||
|
*/
|
||||||
|
static inline int _compare_and_overwrite_entry(ENTRY item, ACTION action,
|
||||||
|
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)) {
|
||||||
|
free(htab->table[idx].entry.data);
|
||||||
|
htab->table[idx].entry.data = strdup(item.data);
|
||||||
|
if (!htab->table[idx].entry.data) {
|
||||||
|
__set_errno(ENOMEM);
|
||||||
|
*retval = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* return found entry */
|
||||||
|
*retval = &htab->table[idx].entry;
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
/* keep searching */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
|
int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
|
||||||
struct hsearch_data *htab, int flag)
|
struct hsearch_data *htab, int flag)
|
||||||
{
|
{
|
||||||
@ -255,6 +283,7 @@ int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
|
|||||||
unsigned int len = strlen(item.key);
|
unsigned int len = strlen(item.key);
|
||||||
unsigned int idx;
|
unsigned int idx;
|
||||||
unsigned int first_deleted = 0;
|
unsigned int first_deleted = 0;
|
||||||
|
int ret;
|
||||||
|
|
||||||
/* Compute an value for the given string. Perhaps use a better method. */
|
/* Compute an value for the given string. Perhaps use a better method. */
|
||||||
hval = len;
|
hval = len;
|
||||||
@ -286,23 +315,10 @@ int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
|
|||||||
&& !first_deleted)
|
&& !first_deleted)
|
||||||
first_deleted = idx;
|
first_deleted = idx;
|
||||||
|
|
||||||
if (htab->table[idx].used == hval
|
ret = _compare_and_overwrite_entry(item, action, retval, htab,
|
||||||
&& strcmp(item.key, htab->table[idx].entry.key) == 0) {
|
flag, hval, idx);
|
||||||
/* Overwrite existing value? */
|
if (ret != -1)
|
||||||
if ((action == ENTER) && (item.data != NULL)) {
|
return ret;
|
||||||
free(htab->table[idx].entry.data);
|
|
||||||
htab->table[idx].entry.data =
|
|
||||||
strdup(item.data);
|
|
||||||
if (!htab->table[idx].entry.data) {
|
|
||||||
__set_errno(ENOMEM);
|
|
||||||
*retval = NULL;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* return found entry */
|
|
||||||
*retval = &htab->table[idx].entry;
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Second hash function:
|
* Second hash function:
|
||||||
@ -328,23 +344,10 @@ int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
/* If entry is found use it. */
|
/* If entry is found use it. */
|
||||||
if ((htab->table[idx].used == hval)
|
ret = _compare_and_overwrite_entry(item, action, retval,
|
||||||
&& strcmp(item.key, htab->table[idx].entry.key) == 0) {
|
htab, flag, hval, idx);
|
||||||
/* Overwrite existing value? */
|
if (ret != -1)
|
||||||
if ((action == ENTER) && (item.data != NULL)) {
|
return ret;
|
||||||
free(htab->table[idx].entry.data);
|
|
||||||
htab->table[idx].entry.data =
|
|
||||||
strdup(item.data);
|
|
||||||
if (!htab->table[idx].entry.data) {
|
|
||||||
__set_errno(ENOMEM);
|
|
||||||
*retval = NULL;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* return found entry */
|
|
||||||
*retval = &htab->table[idx].entry;
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
while (htab->table[idx].used);
|
while (htab->table[idx].used);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user