[PATCH 1/3] Export hash function

Prerna Saxena
2010-06-16T12:40:43Z

Rename tdb_hash() to qemu_hash().
Move definition from qdict.c to a new file qemu-misc.c for use by tracing
infrastructure.

Signed-off-by: Prerna Saxena <prerna@linux.vnet.ibm.com>
---

Makefile.objs | 2 +-
qdict.c | 24 ++++--------------------
qemu-common.h | 3 +++
qemu-misc.c | 24 ++++++++++++++++++++++++
4 files changed, 32 insertions(+), 21 deletions(-)
create mode 100644 qemu-misc.c

diff --git a/Makefile.objs b/Makefile.objs
index 7cb40ac..53e3a65 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -1,6 +1,6 @@
#######################################################################
# QObject
-qobject-obj-y = qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o
+qobject-obj-y = qint.o qstring.o qdict.o qemu-misc.o qlist.o qfloat.o qbool.o
qobject-obj-y += qjson.o json-lexer.o json-streamer.o json-parser.o
qobject-obj-y += qerror.o

diff --git a/qdict.c b/qdict.c
index 175bc17..d4588ef 100644
--- a/qdict.c
+++ b/qdict.c
@@ -53,22 +53,6 @@ QDict *qobject_to_qdict(const QObject *obj)
}

/**
- * tdb_hash(): based on the hash agorithm from gdbm, via tdb
- * (from module-init-tools)
- */
-static unsigned int tdb_hash(const char *name)
-{
- unsigned value; /* Used to compute the hash value. */
- unsigned i; /* Used to cycle through random values. */
-
- /* Set the initial value from the key size. */
- for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
- value = (value + (((const unsigned char *)name)[i] << (i*5 % 24)));
-
- return (1103515243 * value + 12345);
-}
-
-/**
* alloc_entry(): allocate a new QDictEntry
*/
static QDictEntry *alloc_entry(const char *key, QObject *value)
@@ -113,7 +97,7 @@ void qdict_put_obj(QDict *qdict, const char *key, QObject *value)
unsigned int hash;
QDictEntry *entry;

- hash = tdb_hash(key) % QDICT_HASH_SIZE;
+ hash = qemu_hash(key) % QDICT_HASH_SIZE;
entry = qdict_find(qdict, key, hash);
if (entry) {
/* replace key's value */
@@ -137,7 +121,7 @@ QObject *qdict_get(const QDict *qdict, const char *key)
{
QDictEntry *entry;

- entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE);
+ entry = qdict_find(qdict, key, qemu_hash(key) % QDICT_HASH_SIZE);
return (entry == NULL ? NULL : entry->value);
}

@@ -148,7 +132,7 @@ QObject *qdict_get(const QDict *qdict, const char *key)
*/
int qdict_haskey(const QDict *qdict, const char *key)
{
- unsigned int hash = tdb_hash(key) % QDICT_HASH_SIZE;
+ unsigned int hash = qemu_hash(key) % QDICT_HASH_SIZE;
return (qdict_find(qdict, key, hash) == NULL ? 0 : 1);
}

@@ -347,7 +331,7 @@ void qdict_del(QDict *qdict, const char *key)
{
QDictEntry *entry;

- entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE);
+ entry = qdict_find(qdict, key, qemu_hash(key) % QDICT_HASH_SIZE);
if (entry) {
QLIST_REMOVE(entry, next);
qentry_destroy(entry);
diff --git a/qemu-common.h b/qemu-common.h
index a4888e5..d225e45 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -17,6 +17,9 @@ typedef struct QEMUTimer QEMUTimer;
typedef struct QEMUFile QEMUFile;
typedef struct QEMUBH QEMUBH;

+/* Hash function definition */
+unsigned int qemu_hash(const char *name);
+
/* Hack around the mess dyngen-exec.h causes: We need QEMU_NORETURN in files that
cannot include the following headers without conflicts. This condition has
to be removed once dyngen is gone. */
diff --git a/qemu-misc.c b/qemu-misc.c
new file mode 100644
index 0000000..a69196a
--- /dev/null
+++ b/qemu-misc.c
@@ -0,0 +1,24 @@
+/*
+ * Definition of tdb_hash() moved here, from qdict.c. Renamed to qemu_hash()
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+#include "qemu-common.h"
+
+/**
+ * tdb_hash(): based on the hash agorithm from gdbm, via tdb
+ * (from module-init-tools). Renamed to qemu_hash().
+ */
+unsigned int qemu_hash(const char *name)
+{
+ unsigned value; /* Used to compute the hash value. */
+ unsigned i; /* Used to cycle through random values. */
+
+ /* Set the initial value from the key size. */
+ for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
+ value = (value + (((const unsigned char *)name)[i] << (i*5 % 24)));
+
+ return (1103515243 * value + 12345);
+}