From f85d1f90700759f5951e9440254dc3933487c325 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Fri, 10 Jul 2020 00:02:48 -0400 Subject: [PATCH] [core] splaytree_djbhash() in splaytree.h (reuse) rename and move hashme() from stat_cache.c to splaytree_djbhash() in splaytree.h for code reuse --- src/splaytree.h | 24 ++++++++++++++++++++++++ src/stat_cache.c | 25 +++---------------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/splaytree.h b/src/splaytree.h index cc5fe9b1..d7fe1a2d 100644 --- a/src/splaytree.h +++ b/src/splaytree.h @@ -22,4 +22,28 @@ splay_tree * splaytree_size(splay_tree *t); /* a special version of NULL which was a real node with size 0. */ +__attribute_pure__ +static inline uint32_t djbhash(const char *str, const uint32_t len); + +__attribute_pure__ +static inline int32_t splaytree_djbhash(const char *str, const uint32_t len); + + +/* the famous DJB hash function for strings */ +static inline uint32_t djbhash(const char *str, const uint32_t len) +{ + const unsigned char * const s = (const unsigned char *)str; + uint32_t hash = 5381; + for (uint32_t i = 0; i < len; ++i) hash = ((hash << 5) + hash) ^ s[i]; + return hash; +} + + +static inline int32_t splaytree_djbhash(const char *str, const uint32_t len) +{ + /* strip highest bit of hash value for splaytree */ + return (int32_t)(djbhash(str,len) & ~(((uint32_t)1) << 31)); +} + + #endif diff --git a/src/stat_cache.c b/src/stat_cache.c index f8fe0429..a31d87eb 100644 --- a/src/stat_cache.c +++ b/src/stat_cache.c @@ -55,30 +55,11 @@ typedef struct stat_cache { static stat_cache sc; -/* the famous DJB hash function for strings */ -__attribute_pure__ -static uint32_t djbhash(const char *str, const uint32_t len) -{ - const unsigned char * const s = (const unsigned char *)str; - uint32_t hash = 5381; - for (uint32_t i = 0; i < len; ++i) hash = ((hash << 5) + hash) ^ s[i]; - return hash; -} - - -__attribute_pure__ -static int32_t hashme(const char *str, const uint32_t len) -{ - /* strip highest bit of hash value for splaytree */ - return (int32_t)(djbhash(str,len) & ~(((uint32_t)1) << 31)); -} - - static void * stat_cache_sptree_find(splay_tree ** const sptree, const char * const name, uint32_t len) { - const int ndx = hashme(name, len); + const int ndx = splaytree_djbhash(name, len); *sptree = splaytree_splay(*sptree, ndx); return (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL; } @@ -428,7 +409,7 @@ static fam_dir_entry * fam_dir_monitor(stat_cache_fam *scf, char *fn, uint32_t d while (fn[--dirlen] != '/') ; if (0 == dirlen) dirlen = 1; /*(should not happen for file)*/ } - int dir_ndx = hashme(fn, dirlen); + int dir_ndx = splaytree_djbhash(fn, dirlen); fam_dir_entry *fam_dir = NULL; scf->dirs = splaytree_splay(scf->dirs, dir_ndx); @@ -955,7 +936,7 @@ stat_cache_entry * stat_cache_get_entry(const buffer *name) { const time_t cur_ts = log_epoch_secs; - file_ndx = hashme(name->ptr, len); + file_ndx = splaytree_djbhash(name->ptr, len); splay_tree * const sptree = sc.files = splaytree_splay(sc.files, file_ndx); if (sptree && (sptree->key == file_ndx)) {