Browse Source

[core] splaytree_djbhash() in splaytree.h (reuse)

rename and move hashme() from stat_cache.c to splaytree_djbhash() in
splaytree.h for code reuse
personal/stbuehler/ci-build
Glenn Strauss 1 year ago
parent
commit
f85d1f9070
  1. 24
      src/splaytree.h
  2. 25
      src/stat_cache.c

24
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

25
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)) {

Loading…
Cancel
Save