Skip to content

Commit

Permalink
Replace dt_strtab_hash() with str2hval() in string table handling
Browse files Browse the repository at this point in the history
We only need one single function that calculates a hash value for a
string.  Since we have a generic one (str2hval), the strtab code
should use that one instead of having its own custom function.

Signed-off-by: Kris Van Hees <kris.van.hees@oracle.com>
Reviewed-by: Eugene Loh <eugene.loh@oracle.com>
  • Loading branch information
kvanhees committed Jun 18, 2021
1 parent 4f8fa34 commit 0c33733
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
29 changes: 5 additions & 24 deletions libdtrace/dt_strtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <assert.h>

#include <dt_strtab.h>
#include <dt_string.h>
#include <dt_impl.h>

static int
Expand Down Expand Up @@ -105,28 +106,6 @@ dt_strtab_destroy(dt_strtab_t *sp)
free(sp);
}

ulong_t
dt_strtab_hash(const char *key, size_t *len)
{
ulong_t g, h = 0;
const char *p;
size_t n = 0;

for (p = key; *p != '\0'; p++, n++) {
h = (h << 4) + *p;

if ((g = (h & 0xf0000000)) != 0) {
h ^= (g >> 24);
h ^= g;
}
}

if (len != NULL)
*len = n;

return h;
}

static int
dt_strtab_compare(dt_strtab_t *sp, dt_strhash_t *hp,
const char *str, size_t len)
Expand Down Expand Up @@ -199,7 +178,8 @@ dt_strtab_index(dt_strtab_t *sp, const char *str)
if (str == NULL || str[0] == '\0')
return 0; /* The empty string is always at offset 0. */

h = dt_strtab_hash(str, &len) % sp->str_hashsz;
len = strlen(str);
h = str2hval(str, 0) % sp->str_hashsz;

for (hp = sp->str_hash[h]; hp != NULL; hp = hp->str_next) {
if (dt_strtab_compare(sp, hp, str, len + 1) == 0)
Expand All @@ -223,7 +203,8 @@ dt_strtab_insert(dt_strtab_t *sp, const char *str)
if ((off = dt_strtab_index(sp, str)) != -1)
return off;

h = dt_strtab_hash(str, &len) % sp->str_hashsz;
len = strlen(str);
h = str2hval(str, 0) % sp->str_hashsz;

/*
* Create a new hash bucket, initialize it, and insert it at the front
Expand Down
3 changes: 1 addition & 2 deletions libdtrace/dt_strtab.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Oracle Linux DTrace.
* Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
Expand Down Expand Up @@ -44,7 +44,6 @@ extern size_t dt_strtab_size(const dt_strtab_t *);
extern ssize_t dt_strtab_copystr(const char *, size_t, size_t, char *);
extern ssize_t dt_strtab_write(const dt_strtab_t *,
dt_strtab_write_f *, void *);
extern ulong_t dt_strtab_hash(const char *, size_t *);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions libdtrace/dt_symbol_modops.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Oracle Linux DTrace.
* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
Expand Down Expand Up @@ -119,7 +119,7 @@ BITIZE(dt_module_symname)(dt_module_t *dmp, const char *name,
if (dmp->dm_nsymelems == 0)
return NULL;

h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
h = str2hval(name, 0) % dmp->dm_nsymbuckets;

for (i = dmp->dm_symbuckets[h]; i != 0; i = dmsp->dms_next) {
dmsp = &dmp->dm_symchains[i];
Expand Down

0 comments on commit 0c33733

Please sign in to comment.