diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2024-05-11 13:44:53 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2024-05-12 03:32:14 +0200 |
commit | bab044b94bd8252dfd0e2c7479a7b24d0b3d43da (patch) | |
tree | 979f380e40618dbbf2762fc1687cc65bc7f96c72 /src/journal | |
parent | journal-rate-limit: rename variables (diff) | |
download | systemd-bab044b94bd8252dfd0e2c7479a7b24d0b3d43da.tar.xz systemd-bab044b94bd8252dfd0e2c7479a7b24d0b3d43da.zip |
journal-rate-limit: introduce cleanup function for JournalRateLimitGroup
This also makes journal_ratelimit_group_new() use structured initializer.
Diffstat (limited to 'src/journal')
-rw-r--r-- | src/journal/journald-rate-limit.c | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/src/journal/journald-rate-limit.c b/src/journal/journald-rate-limit.c index 69f19bdc36..e4e3728125 100644 --- a/src/journal/journald-rate-limit.c +++ b/src/journal/journald-rate-limit.c @@ -92,6 +92,8 @@ static JournalRateLimitGroup* journal_ratelimit_group_free(JournalRateLimitGroup return mfree(g); } +DEFINE_TRIVIAL_CLEANUP_FUNC(JournalRateLimitGroup*, journal_ratelimit_group_free); + void journal_ratelimit_free(JournalRateLimit *r) { assert(r); @@ -121,28 +123,30 @@ static void journal_ratelimit_vacuum(JournalRateLimit *r, usec_t ts) { journal_ratelimit_group_free(r->lru_tail); } -static JournalRateLimitGroup* journal_ratelimit_group_new( +static int journal_ratelimit_group_new( JournalRateLimit *rl, const char *id, usec_t interval, - usec_t ts) { + usec_t ts, + JournalRateLimitGroup **ret) { - JournalRateLimitGroup *g; + _cleanup_(journal_ratelimit_group_freep) JournalRateLimitGroup *g = NULL; assert(rl); assert(id); + assert(ret); - g = new0(JournalRateLimitGroup, 1); + g = new(JournalRateLimitGroup, 1); if (!g) - return NULL; + return -ENOMEM; - g->id = strdup(id); + *g = (JournalRateLimitGroup) { + .id = strdup(id), + .hash = siphash24_string(id, rl->hash_key), + .interval = interval, + }; if (!g->id) - goto fail; - - g->hash = siphash24_string(g->id, rl->hash_key); - - g->interval = interval; + return -ENOMEM; journal_ratelimit_vacuum(rl, ts); @@ -153,11 +157,9 @@ static JournalRateLimitGroup* journal_ratelimit_group_new( rl->n_groups++; g->parent = rl; - return g; -fail: - journal_ratelimit_group_free(g); - return NULL; + *ret = TAKE_PTR(g); + return 0; } static unsigned burst_modulate(unsigned burst, uint64_t available) { @@ -201,6 +203,7 @@ int journal_ratelimit_test( unsigned burst; uint64_t h; usec_t ts; + int r; assert(id); @@ -226,9 +229,9 @@ int journal_ratelimit_test( } if (!found) { - found = journal_ratelimit_group_new(rl, id, rl_interval, ts); - if (!found) - return -ENOMEM; + r = journal_ratelimit_group_new(rl, id, rl_interval, ts, &found); + if (r < 0) + return r; } else found->interval = rl_interval; |