summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c4e0057)
raw | patch | inline | side by side (parent: c4e0057)
author | Joe Hershberger <joe.hershberger@ni.com> | |
Wed, 12 Dec 2012 04:16:20 +0000 (22:16 -0600) | ||
committer | Tom Rini <trini@ti.com> | |
Thu, 13 Dec 2012 18:46:54 +0000 (11:46 -0700) |
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>
changes will make that chunk grow a bit, so combine into a static func.
Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
lib/hashtable.c | patch | blob | history |
diff --git a/lib/hashtable.c b/lib/hashtable.c
index f0056acf6cda69a8febdda925aaccc2bcdba6acb..f4d57950591366128b5c06c339bdec076970b61d 100644 (file)
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
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,
struct hsearch_data *htab, int flag)
{
unsigned int len = strlen(item.key);
unsigned int idx;
unsigned int first_deleted = 0;
+ int ret;
/* Compute an value for the given string. Perhaps use a better method. */
hval = len;
&& !first_deleted)
first_deleted = 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;
- }
+ ret = _compare_and_overwrite_entry(item, action, retval, htab,
+ flag, hval, idx);
+ if (ret != -1)
+ return ret;
/*
* Second hash function:
break;
/* If entry is found use it. */
- 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;
- }
+ ret = _compare_and_overwrite_entry(item, action, retval,
+ htab, flag, hval, idx);
+ if (ret != -1)
+ return ret;
}
while (htab->table[idx].used);
}