aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJoe Hershberger2012-12-11 22:16:20 -0600
committerTom Rini2012-12-13 12:46:54 -0600
commit3d3b52f2586a8bf1c53496547062594fd4386454 (patch)
tree8524f556897ea5248de2d0a75c1755a53e7dacd5 /lib
parentc4e0057fa78ebb524b9241ad7245fcd1074ba414 (diff)
downloadu-boot-3d3b52f2586a8bf1c53496547062594fd4386454.tar.gz
u-boot-3d3b52f2586a8bf1c53496547062594fd4386454.tar.xz
u-boot-3d3b52f2586a8bf1c53496547062594fd4386454.zip
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>
Diffstat (limited to 'lib')
-rw-r--r--lib/hashtable.c71
1 files changed, 37 insertions, 34 deletions
diff --git a/lib/hashtable.c b/lib/hashtable.c
index f0056acf6c..f4d5795059 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -247,6 +247,34 @@ int hmatch_r(const char *match, int last_idx, ENTRY ** retval,
247 return 0; 247 return 0;
248} 248}
249 249
250/*
251 * Compare an existing entry with the desired key, and overwrite if the action
252 * is ENTER. This is simply a helper function for hsearch_r().
253 */
254static inline int _compare_and_overwrite_entry(ENTRY item, ACTION action,
255 ENTRY **retval, struct hsearch_data *htab, int flag,
256 unsigned int hval, unsigned int idx)
257{
258 if (htab->table[idx].used == hval
259 && strcmp(item.key, htab->table[idx].entry.key) == 0) {
260 /* Overwrite existing value? */
261 if ((action == ENTER) && (item.data != NULL)) {
262 free(htab->table[idx].entry.data);
263 htab->table[idx].entry.data = strdup(item.data);
264 if (!htab->table[idx].entry.data) {
265 __set_errno(ENOMEM);
266 *retval = NULL;
267 return 0;
268 }
269 }
270 /* return found entry */
271 *retval = &htab->table[idx].entry;
272 return idx;
273 }
274 /* keep searching */
275 return -1;
276}
277
250int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval, 278int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
251 struct hsearch_data *htab, int flag) 279 struct hsearch_data *htab, int flag)
252{ 280{
@@ -255,6 +283,7 @@ int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
255 unsigned int len = strlen(item.key); 283 unsigned int len = strlen(item.key);
256 unsigned int idx; 284 unsigned int idx;
257 unsigned int first_deleted = 0; 285 unsigned int first_deleted = 0;
286 int ret;
258 287
259 /* Compute an value for the given string. Perhaps use a better method. */ 288 /* Compute an value for the given string. Perhaps use a better method. */
260 hval = len; 289 hval = len;
@@ -286,23 +315,10 @@ int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
286 && !first_deleted) 315 && !first_deleted)
287 first_deleted = idx; 316 first_deleted = idx;
288 317
289 if (htab->table[idx].used == hval 318 ret = _compare_and_overwrite_entry(item, action, retval, htab,
290 && strcmp(item.key, htab->table[idx].entry.key) == 0) { 319 flag, hval, idx);
291 /* Overwrite existing value? */ 320 if (ret != -1)
292 if ((action == ENTER) && (item.data != NULL)) { 321 return ret;
293 free(htab->table[idx].entry.data);
294 htab->table[idx].entry.data =
295 strdup(item.data);
296 if (!htab->table[idx].entry.data) {
297 __set_errno(ENOMEM);
298 *retval = NULL;
299 return 0;
300 }
301 }
302 /* return found entry */
303 *retval = &htab->table[idx].entry;
304 return idx;
305 }
306 322
307 /* 323 /*
308 * Second hash function: 324 * Second hash function:
@@ -328,23 +344,10 @@ int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
328 break; 344 break;
329 345
330 /* If entry is found use it. */ 346 /* If entry is found use it. */
331 if ((htab->table[idx].used == hval) 347 ret = _compare_and_overwrite_entry(item, action, retval,
332 && strcmp(item.key, htab->table[idx].entry.key) == 0) { 348 htab, flag, hval, idx);
333 /* Overwrite existing value? */ 349 if (ret != -1)
334 if ((action == ENTER) && (item.data != NULL)) { 350 return ret;
335 free(htab->table[idx].entry.data);
336 htab->table[idx].entry.data =
337 strdup(item.data);
338 if (!htab->table[idx].entry.data) {
339 __set_errno(ENOMEM);
340 *retval = NULL;
341 return 0;
342 }
343 }
344 /* return found entry */
345 *retval = &htab->table[idx].entry;
346 return idx;
347 }
348 } 351 }
349 while (htab->table[idx].used); 352 while (htab->table[idx].used);
350 } 353 }