Using plain structure initialization is both shorter _and_ more clearer.
We get type safety for free.
#include "util.h"
typedef struct RateLimit {
- usec_t interval;
- usec_t begin;
- unsigned burst;
+ usec_t interval; /* Keep those two fields first so they can be initialized easily: */
+ unsigned burst; /* RateLimit rl = { INTERVAL, BURST }; */
unsigned num;
+ usec_t begin;
} RateLimit;
-#define RATELIMIT_DEFINE(_name, _interval, _burst) \
- RateLimit _name = { \
- .interval = (_interval), \
- .burst = (_burst), \
- .num = 0, \
- .begin = 0 \
- }
-
-#define RATELIMIT_INIT(v, _interval, _burst) \
- do { \
- RateLimit *_r = &(v); \
- _r->interval = (_interval); \
- _r->burst = (_burst); \
- _r->num = 0; \
- _r->begin = 0; \
- } while (false)
-
-#define RATELIMIT_RESET(v) \
- do { \
- RateLimit *_r = &(v); \
- _r->num = 0; \
- _r->begin = 0; \
- } while (false)
+static inline void ratelimit_reset(RateLimit *rl) {
+ rl->num = rl->begin = 0;
+}
bool ratelimit_below(RateLimit *r);
}
/* Reboot immediately if the user hits C-A-D more often than 7x per 2s */
- RATELIMIT_INIT(m->ctrl_alt_del_ratelimit, 2 * USEC_PER_SEC, 7);
+ m->ctrl_alt_del_ratelimit = (RateLimit) { .interval = 2 * USEC_PER_SEC, .burst = 7 };
r = manager_default_environment(m);
if (r < 0)
}
int manager_loop(Manager *m) {
+ RateLimit rl = { .interval = 1*USEC_PER_SEC, .burst = 50000 };
int r;
- RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000);
-
assert(m);
assert(m->objective == MANAGER_OK); /* Ensure manager_startup() has been called */
u->last_section_private = -1;
- RATELIMIT_INIT(u->start_limit, m->default_start_limit_interval, m->default_start_limit_burst);
- RATELIMIT_INIT(u->auto_stop_ratelimit, 10 * USEC_PER_SEC, 16);
+ u->start_limit = (RateLimit) { m->default_start_limit_interval, m->default_start_limit_burst };
+ u->auto_stop_ratelimit = (RateLimit) { 10 * USEC_PER_SEC, 16 };
for (CGroupIOAccountingMetric i = 0; i < _CGROUP_IO_ACCOUNTING_METRIC_MAX; i++)
u->io_accounting_last[i] = UINT64_MAX;
if (UNIT_VTABLE(u)->reset_failed)
UNIT_VTABLE(u)->reset_failed(u);
- RATELIMIT_RESET(u->start_limit);
+ ratelimit_reset(&u->start_limit);
u->start_limit_hit = false;
}
.on_finished = on_finished,
.userdata = userdata,
.last_percent = (unsigned) -1,
+ .progress_rate_limit = { 100 * USEC_PER_MSEC, 1 },
};
- RATELIMIT_INIT(e->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
-
if (event)
e->event = sd_event_ref(event);
else {
.userdata = userdata,
.quota_referenced = (uint64_t) -1,
.last_percent = (unsigned) -1,
+ .progress_rate_limit = { 100 * USEC_PER_MSEC, 1 },
};
- RATELIMIT_INIT(e->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
-
if (event)
e->event = sd_event_ref(event);
else {
(void) mkdir_parents_label(temp_path, 0700);
- RATELIMIT_INIT(progress.limit, 200*USEC_PER_MSEC, 1);
+ progress.limit = (RateLimit) { 200*USEC_PER_MSEC, 1 };
/* Hook into SIGINT/SIGTERM, so that we can cancel things then */
assert(sigaction(SIGINT, &sa, &old_sigint_sa) >= 0);
.userdata = userdata,
.last_percent = (unsigned) -1,
.image_root = TAKE_PTR(root),
+ .progress_rate_limit = { 100 * USEC_PER_MSEC, 1 },
};
- RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
-
if (event)
i->event = sd_event_ref(event);
else {
.userdata = userdata,
.last_percent = (unsigned) -1,
.image_root = TAKE_PTR(root),
+ .progress_rate_limit = { 100 * USEC_PER_MSEC, 1 },
};
- RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
-
if (event)
i->event = sd_event_ref(event);
else {
log_debug("New scope on link %s, protocol %s, family %s", l ? l->ifname : "*", dns_protocol_to_string(protocol), family == AF_UNSPEC ? "*" : af_to_name(family));
/* Enforce ratelimiting for the multicast protocols */
- RATELIMIT_INIT(s->ratelimit, MULTICAST_RATELIMIT_INTERVAL_USEC, MULTICAST_RATELIMIT_BURST);
+ s->ratelimit = (RateLimit) { MULTICAST_RATELIMIT_INTERVAL_USEC, MULTICAST_RATELIMIT_BURST };
*ret = s;
return 0;
static void test_ratelimit_below(void) {
int i;
- RATELIMIT_DEFINE(ratelimit, 1 * USEC_PER_SEC, 10);
+ RateLimit ratelimit = { 1 * USEC_PER_SEC, 10 };
for (i = 0; i < 10; i++)
assert_se(ratelimit_below(&ratelimit));
for (i = 0; i < 10; i++)
assert_se(ratelimit_below(&ratelimit));
- RATELIMIT_INIT(ratelimit, 0, 10);
+ ratelimit = (RateLimit) { 0, 10 };
for (i = 0; i < 10000; i++)
assert_se(ratelimit_below(&ratelimit));
}
m->server_socket = m->clock_watch_fd = -1;
- RATELIMIT_INIT(m->ratelimit, RATELIMIT_INTERVAL_USEC, RATELIMIT_BURST);
+ m->ratelimit = (RateLimit) { RATELIMIT_INTERVAL_USEC, RATELIMIT_BURST };
r = sd_event_default(&m->event);
if (r < 0)