siphash24: add helper for calculating the hash value for a string

Let's shorten some code.
This commit is contained in:
Lennart Poettering 2018-10-16 13:56:14 +02:00
parent 43d3c94e64
commit a53f90ca24
2 changed files with 7 additions and 8 deletions

View File

@ -3,6 +3,7 @@
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
struct siphash {
@ -21,3 +22,7 @@ void siphash24_compress(const void *in, size_t inlen, struct siphash *state);
uint64_t siphash24_finalize(struct siphash *state);
uint64_t siphash24(const void *in, size_t inlen, const uint8_t k[16]);
static inline uint64_t siphash24_string(const char *s, const uint8_t k[16]) {
return siphash24(s, strlen(s) + 1, k);
}

View File

@ -128,7 +128,6 @@ static void journal_rate_limit_vacuum(JournalRateLimit *r, usec_t ts) {
static JournalRateLimitGroup* journal_rate_limit_group_new(JournalRateLimit *r, const char *id, usec_t ts) {
JournalRateLimitGroup *g;
struct siphash state;
assert(r);
assert(id);
@ -141,9 +140,7 @@ static JournalRateLimitGroup* journal_rate_limit_group_new(JournalRateLimit *r,
if (!g->id)
goto fail;
siphash24_init(&state, r->hash_key);
string_hash_func(g->id, &state);
g->hash = siphash24_finalize(&state);
g->hash = siphash24_string(g->id, r->hash_key);
journal_rate_limit_vacuum(r, ts);
@ -193,7 +190,6 @@ int journal_rate_limit_test(JournalRateLimit *r, const char *id, int priority, u
uint64_t h;
JournalRateLimitGroup *g;
JournalRateLimitPool *p;
struct siphash state;
unsigned burst;
usec_t ts;
@ -216,9 +212,7 @@ int journal_rate_limit_test(JournalRateLimit *r, const char *id, int priority, u
ts = now(CLOCK_MONOTONIC);
siphash24_init(&state, r->hash_key);
string_hash_func(id, &state);
h = siphash24_finalize(&state);
h = siphash24_string(id, r->hash_key);
g = r->buckets[h % BUCKETS_MAX];
LIST_FOREACH(bucket, g, g)