coccinelle: make use of DIV_ROUND_UP() wherever appropriate
authorLennart Poettering <lennart@poettering.net>
Tue, 20 Mar 2018 19:36:09 +0000 (20:36 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 20 Mar 2018 19:59:02 +0000 (20:59 +0100)
Let's use our macros where we can

coccinelle/div-round-up.cocci [new file with mode: 0644]
src/basic/calendarspec.c
src/basic/exec-util.c
src/basic/hexdecoct.c
src/core/automount.c
src/firstboot/firstboot.c
src/journal/journal-file.c
src/shared/watchdog.c
src/udev/udevadm-control.c

diff --git a/coccinelle/div-round-up.cocci b/coccinelle/div-round-up.cocci
new file mode 100644 (file)
index 0000000..a0c6df9
--- /dev/null
@@ -0,0 +1,20 @@
+@@
+expression x, y;
+@@
+- ((x + y - 1) / y)
++ DIV_ROUND_UP(x, y)
+@@
+expression x, y;
+@@
+- ((x + (y - 1)) / y)
++ DIV_ROUND_UP(x, y)
+@@
+expression x, y;
+@@
+- (x + y - 1) / y
++ DIV_ROUND_UP(x, y)
+@@
+expression x, y;
+@@
+- (x + (y - 1)) / y
++ DIV_ROUND_UP(x, y)
index 648ac29..3abd26a 100644 (file)
@@ -1169,7 +1169,7 @@ static int find_matching_component(const CalendarSpec *spec, const CalendarCompo
                 } else if (c->repeat > 0) {
                         int k;
 
-                        k = start + c->repeat * ((*val - start + c->repeat - 1) / c->repeat);
+                        k = start + c->repeat * DIV_ROUND_UP(*val - start, c->repeat);
 
                         if ((!d_set || k < d) && (stop < 0 || k <= stop)) {
                                 d = k;
index e0057a7..49fb95e 100644 (file)
@@ -116,7 +116,7 @@ static int do_execute(
          * default action terminating the process, and turn on alarm(). */
 
         if (timeout != USEC_INFINITY)
-                alarm((timeout + USEC_PER_SEC - 1) / USEC_PER_SEC);
+                alarm(DIV_ROUND_UP(timeout, USEC_PER_SEC));
 
         STRV_FOREACH(path, paths) {
                 _cleanup_free_ char *t = NULL;
index fe7e495..0764521 100644 (file)
@@ -583,7 +583,7 @@ static int base64_append_width(
         if (len <= 0)
                 return len;
 
-        lines = (len + width - 1) / width;
+        lines = DIV_ROUND_UP(len, width);
 
         slen = strlen_ptr(sep);
         t = realloc(*prefix, plen + 1 + slen + (indent + width + 1) * lines);
index 01a6ff8..a8d7736 100644 (file)
@@ -429,7 +429,7 @@ static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, usec_t usec) {
                 param.timeout.timeout = 0;
         else
                 /* Convert to seconds, rounding up. */
-                param.timeout.timeout = (usec + USEC_PER_SEC - 1) / USEC_PER_SEC;
+                param.timeout.timeout = DIV_ROUND_UP(usec, USEC_PER_SEC);
 
         if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param) < 0)
                 return -errno;
index effa092..3d8a590 100644 (file)
@@ -130,7 +130,7 @@ static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned perc
         assert(n_columns > 0);
 
         n = strv_length(x);
-        per_column = (n + n_columns - 1) / n_columns;
+        per_column = DIV_ROUND_UP(n, n_columns);
 
         break_lines = lines();
         if (break_lines > 2)
index 5643c05..f35903a 100644 (file)
@@ -689,7 +689,7 @@ static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size)
         }
 
         /* Increase by larger blocks at once */
-        new_size = ((new_size+FILE_SIZE_INCREASE-1) / FILE_SIZE_INCREASE) * FILE_SIZE_INCREASE;
+        new_size = DIV_ROUND_UP(new_size, FILE_SIZE_INCREASE) * FILE_SIZE_INCREASE;
         if (f->metrics.max_size > 0 && new_size > f->metrics.max_size)
                 new_size = f->metrics.max_size;
 
index b0a422d..bf5c982 100644 (file)
@@ -54,7 +54,7 @@ static int update_timeout(void) {
                 int sec, flags;
                 char buf[FORMAT_TIMESPAN_MAX];
 
-                sec = (int) ((watchdog_timeout + USEC_PER_SEC - 1) / USEC_PER_SEC);
+                sec = (int) DIV_ROUND_UP(watchdog_timeout, USEC_PER_SEC);
                 r = ioctl(watchdog_fd, WDIOC_SETTIMEOUT, &sec);
                 if (r < 0)
                         return log_warning_errno(errno, "Failed to set timeout to %is: %m", sec);
index 9546a6e..c704279 100644 (file)
@@ -137,18 +137,17 @@ static int adm_control(struct udev *udev, int argc, char *argv[]) {
                         break;
                 }
                 case 't': {
+                        int r, seconds;
                         usec_t s;
-                        int seconds;
-                        int r;
 
                         r = parse_sec(optarg, &s);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to parse timeout value '%s'.", optarg);
 
-                        if (((s + USEC_PER_SEC - 1) / USEC_PER_SEC) > INT_MAX)
+                        if (DIV_ROUND_UP(s, USEC_PER_SEC) > INT_MAX)
                                 log_error("Timeout value is out of range.");
                         else {
-                                seconds = s != USEC_INFINITY ? (int) ((s + USEC_PER_SEC - 1) / USEC_PER_SEC) : INT_MAX;
+                                seconds = s != USEC_INFINITY ? (int) DIV_ROUND_UP(s, USEC_PER_SEC) : INT_MAX;
                                 timeout = seconds;
                                 rc = 0;
                         }