From 45077115b011ba7bd3740ef0aa482a7442cea9ae Mon Sep 17 00:00:00 2001 From: Mateusz Majewski Date: Tue, 1 Sep 2020 12:08:26 +0200 Subject: [PATCH] Use the hash from hash.c in the limiter We have a nicely tested hash implementation in hash.c. It makes little sense for the loglimiter to have its own hash. Change-Id: I09e8c8bb8e2dcc385a2063d794bf5c627fa12460 --- Makefile.am | 8 +++++--- include/loglimiter-internal.h | 2 +- src/shared/loglimiter.c | 23 +++++++---------------- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/Makefile.am b/Makefile.am index 9c47857..94ddad0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -185,6 +185,7 @@ dlogctl_SOURCES = \ src/shared/logconfig.c \ src/shared/parsers.c \ src/shared/ptrs_list.c \ + src/shared/hash.c \ src/shared/loglimiter.c \ src/logctl/logctl.c @@ -380,11 +381,11 @@ src_tests_test_common_pos_SOURCES = src/tests/test_common_pos.c src/shared/logco src_tests_test_common_pos_CFLAGS = $(check_CFLAGS) src_tests_test_common_pos_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=sendmsg,--wrap=recvmsg,--wrap=writev,--wrap=read,--wrap=poll,--wrap=fcntl,--wrap=fcntl64,--wrap=malloc,--wrap=calloc,--wrap=connect,--wrap=socket,--wrap=open,--wrap=open64,--wrap=ioctl -src_tests_limiter_pos_SOURCES = src/tests/limiter_pos.c src/shared/loglimiter.c src/shared/logconfig.c src/shared/logcommon.c src/shared/parsers.c src/shared/ptrs_list.c +src_tests_limiter_pos_SOURCES = src/tests/limiter_pos.c src/shared/hash.c src/shared/loglimiter.c src/shared/logconfig.c src/shared/logcommon.c src/shared/parsers.c src/shared/ptrs_list.c src_tests_limiter_pos_CFLAGS = $(check_CFLAGS) src_tests_limiter_pos_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=snprintf,--wrap=malloc,--wrap=time -src_tests_limiter_neg_SOURCES = src/tests/limiter_neg.c src/shared/loglimiter.c src/shared/logconfig.c src/shared/logcommon.c src/shared/parsers.c src/shared/ptrs_list.c +src_tests_limiter_neg_SOURCES = src/tests/limiter_neg.c src/shared/hash.c src/shared/loglimiter.c src/shared/logconfig.c src/shared/logcommon.c src/shared/parsers.c src/shared/ptrs_list.c src_tests_limiter_neg_CFLAGS = $(check_CFLAGS) src_tests_limiter_neg_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=snprintf,--wrap=malloc,--wrap=time @@ -484,7 +485,7 @@ src_tests_queued_entry_monotonic_pos_SOURCES = src/tests/queued_entry_pos.c src/ src_tests_queued_entry_monotonic_pos_CFLAGS = $(check_CFLAGS) -DUSE_ANDROID_MONOTONIC src_tests_queued_entry_monotonic_pos_LDFLAGS = $(AM_LDFLAGS) -src_tests_logctl_SOURCES = src/tests/logctl.c src/logctl/logctl.c src/shared/parsers.c src/shared/logconfig.c src/shared/loglimiter.c src/shared/logcommon.c src/shared/ptrs_list.c +src_tests_logctl_SOURCES = src/tests/logctl.c src/logctl/logctl.c src/shared/parsers.c src/shared/logconfig.c src/shared/hash.c src/shared/loglimiter.c src/shared/logcommon.c src/shared/ptrs_list.c src_tests_logctl_CFLAGS = $(check_CFLAGS) src_tests_logctl_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=calloc,--wrap=asprintf,--wrap=open,--wrap=open64,--wrap=fdopen,--wrap=fdopen64,--wrap=mkstemp,--wrap=mkstemp64,--wrap=fchmod,--wrap=rename @@ -615,6 +616,7 @@ src_tests_critical_log_CFLAGS = $(check_CFLAGS) -pthread src_tests_critical_log_LDFLAGS = $(AM_LDFLAGS) -lpthread -Wl,--wrap=execv,--wrap=clock_gettime src_tests_pid_limiter_SOURCES = src/tests/pid_limiter.c \ + src/shared/hash.c \ src/shared/loglimiter.c \ src/shared/logcommon.c \ src/shared/logconfig.c \ diff --git a/include/loglimiter-internal.h b/include/loglimiter-internal.h index 87a4570..ce69126 100644 --- a/include/loglimiter-internal.h +++ b/include/loglimiter-internal.h @@ -22,6 +22,6 @@ struct rule { }; int __log_limiter_initialize(struct rule *rules_table); -unsigned util_hash_key(const char* s, int c); +uint32_t util_hash_key(const char* s, int c); #endif // LOGLIMITER_INTERNAL_H_ diff --git a/src/shared/loglimiter.c b/src/shared/loglimiter.c index 5a3eb01..dc24a94 100644 --- a/src/shared/loglimiter.c +++ b/src/shared/loglimiter.c @@ -40,9 +40,7 @@ #include #include #include - -/* Some random big odd number to make hash more diverse */ -#define HASH_MAGIC_THINGY 5237231 +#include typedef int (*hash_cmp_func_t)(struct rule*, struct rule*); typedef int (*hash_match_func_t)(struct rule*, unsigned, const char*, int); @@ -208,22 +206,15 @@ static int util_prio_to_char(int prio) } } -/* The key is built from TAG and priority by DJB algorithm (Dan Bernstein). - * Key is only 31 bits long. Negative values are keep to hold NULL bucket */ -unsigned util_hash_key(const char* s, int c) +uint32_t util_hash_key(const char* s, int c) { - unsigned hash = (unsigned)c; - - hash = ((hash << 5) + hash) + c; - - if (s) - while ('\0' != (c = *s++)) - hash = ((hash << 5) + hash) + c; + if (!s) + return 0; - /* Makes the hash more diverse */ - hash *= HASH_MAGIC_THINGY; + const uint32_t hash = create_hash(s, strlen(s)); - return hash; + // Hardly random, but it doesn't have to be. + return hash ^ c; } /* Create hashmap, it's internal interface. */ -- 2.7.4