]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Drop RATELIMIT macros
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 19 Sep 2019 15:41:20 +0000 (17:41 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 20 Sep 2019 14:05:53 +0000 (16:05 +0200)
Using plain structure initialization is both shorter _and_ more clearer.
We get type safety for free.

src/basic/ratelimit.h
src/core/manager.c
src/core/unit.c
src/import/export-raw.c
src/import/export-tar.c
src/import/import-fs.c
src/import/import-raw.c
src/import/import-tar.c
src/resolve/resolved-dns-scope.c
src/test/test-ratelimit.c
src/timesync/timesyncd-manager.c

index de91def28d0c9ae35e3326b53df7288221d98a2b..79e33b6a62ea87bf3db26e7d51120542f10c9397 100644 (file)
@@ -7,34 +7,14 @@
 #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);
index d9114bb0c597a4ef9c72f41ed4cda23584ba2ce1..dac4248356b2b02731520e47285af1fa6c99139a 100644 (file)
@@ -815,7 +815,7 @@ int manager_new(UnitFileScope scope, ManagerTestRunFlags test_run_flags, Manager
         }
 
         /* 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)
@@ -2855,10 +2855,9 @@ static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t use
 }
 
 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 */
 
index 87a5976dcc202d30797cfa5ba7a038623491a69d..7d6ee792adaf486cf73820a49bdbe1dbcfcf4a43 100644 (file)
@@ -123,8 +123,8 @@ Unit *unit_new(Manager *m, size_t size) {
 
         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;
@@ -4000,7 +4000,7 @@ void unit_reset_failed(Unit *u) {
         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;
 }
 
index c1c946cd2be1800385516daec9066a188dcf4844..57b4334a65e15c62a80630f27daa766d5ef45692 100644 (file)
@@ -96,10 +96,9 @@ int raw_export_new(
                 .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 {
index ed546769f340c611eeb5882fa145f6b775424aaf..b66b5ee37cc71aff25888e27de52994c51515fbc 100644 (file)
@@ -99,10 +99,9 @@ int tar_export_new(
                 .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 {
index f8f3a23206ff2097c4d9e7c34301d918baea5591..468303a6dce70773752467be2c3df405dbdb80d7 100644 (file)
@@ -169,7 +169,7 @@ static int import_fs(int argc, char *argv[], void *userdata) {
 
         (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);
index b90583260357b47aaadc903136acc6b297805bec..dc73387bc22bf5a5aebbcd872805fd2373f3008f 100644 (file)
@@ -111,10 +111,9 @@ int raw_import_new(
                 .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 {
index f3eff82a2990b735b6237ef11bf44c7f8cd2fb7f..c2ece7af740ad23b1c4ee818bf04b4bf654ea9bc 100644 (file)
@@ -119,10 +119,9 @@ int tar_import_new(
                 .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 {
index eb304e52e55e15c1db5172eeb981bfffaeb571ef..ddcf164ebd48ebd6ab50edc5a42a8557c036c3d5 100644 (file)
@@ -70,7 +70,7 @@ int dns_scope_new(Manager *m, DnsScope **ret, Link *l, DnsProtocol protocol, int
         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;
index 9f344e115212dd710bd1386b594c7660ef743ea3..56a6fa2d76617d714f366a933457fec704575f3a 100644 (file)
@@ -8,7 +8,7 @@
 
 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));
@@ -17,7 +17,7 @@ static void test_ratelimit_below(void) {
         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));
 }
index 3c3a7fe69aec049ca3fac7ccb0b27a174fd81f9c..604c7e01be9738051c5d6aa28378b84876d94f02 100644 (file)
@@ -1094,7 +1094,7 @@ int manager_new(Manager **ret) {
 
         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)