[U-Boot] [PATCH] lib/hashtable.c: add algorithm for small buffer import
Andreas Bießmann
andreas.devel at googlemail.com
Wed Sep 29 21:28:06 CEST 2010
This patch adds a new flag to influence the hashtable internal algorithm
for creation size when importing a buffer.
When importing a extremely small buffer (e.g. the default_environment)
the current algorithm cuts down the size of hash table to extremely
small size. In some cases this may render the device unusable until one
saves the environment to non volatile memory and restarts the device.
Signed-off-by: Andreas Bießmann <andreas.devel at googlemail.com>
---
In my case i had to import 5 key/value pairs from default_environment which
was about 30 byte buffer. These key/values fit in my hash table but the
ethernet driver would like to setenv() another key/value which returned with
error.
common/env_common.c | 2 +-
include/search.h | 4 +++-
lib/hashtable.c | 15 ++++++++++++---
3 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/common/env_common.c b/common/env_common.c
index a415ef8..bd6eae4 100644
--- a/common/env_common.c
+++ b/common/env_common.c
@@ -188,7 +188,7 @@ void set_default_env(const char *s)
}
if (himport((char *)default_environment,
- sizeof(default_environment), '\0', 0) == 0) {
+ sizeof(default_environment), '\0', H_ALG_SMALL_BUF) == 0) {
error("Environment import failed: errno = %d\n", errno);
}
gd->flags |= GD_FLG_ENV_READY;
diff --git a/include/search.h b/include/search.h
index fccc757..e6cd189 100644
--- a/include/search.h
+++ b/include/search.h
@@ -102,5 +102,7 @@ extern int himport_r(struct hsearch_data *__htab,
/* Flags for himport() / himport_r() */
#define H_NOCLEAR 1 /* do not clear hash table before importing */
-
+#define H_ALG_SMALL_BUF 2 /* use another algorithm for small buffers to
+ calculate hashtable size.
+ */
#endif /* search.h */
diff --git a/lib/hashtable.c b/lib/hashtable.c
index b747f1f..82a4d00 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -636,7 +636,7 @@ int himport_r(struct hsearch_data *htab,
}
/*
- * Create new hash table (if needed). The computation of the hash
+ * Create new hash table (if needed). The computation of the hash
* table size is based on heuristics: in a sample of some 70+
* existing systems we found an average size of 39+ bytes per entry
* in the environment (for the whole key=value pair). Assuming a
@@ -644,16 +644,25 @@ int himport_r(struct hsearch_data *htab,
* safety margin for any existing environment definitions and still
* allow for more than enough dynamic additions. Note that the
* "size" argument is supposed to give the maximum enviroment size
- * (CONFIG_ENV_SIZE). This heuristics will result in
+ * (CONFIG_ENV_SIZE). This heuristics will result in
* unreasonably large numbers (and thus memory footprint) for
* big flash environments (>8,000 entries for 64 KB
* envrionment size), so we clip it to a reasonable value
* (which can be overwritten in the board config file if
* needed).
+ *
+ * But in some cases it is necessary to have another algorithm to
+ * get the size of hash table. Especially for extremely small buffers
+ * there is the flag H_ALG_SMALL_BUF which takes another factor to
+ * calculate the hash table size.
*/
if (!htab->table) {
- int nent = size / 8;
+ int nent;
+ if (flag & H_ALG_SMALL_BUF)
+ nent = size / 2;
+ else
+ nent = size / 8;
if (nent > CONFIG_ENV_MAX_ENTRIES)
nent = CONFIG_ENV_MAX_ENTRIES;
--
1.7.3
More information about the U-Boot
mailing list