diff --git a/coccinelle/div-round-up.cocci b/coccinelle/div-round-up.cocci new file mode 100644 index 0000000000..a0c6df9801 --- /dev/null +++ b/coccinelle/div-round-up.cocci @@ -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) diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c index 648ac29af3..3abd26a78a 100644 --- a/src/basic/calendarspec.c +++ b/src/basic/calendarspec.c @@ -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; diff --git a/src/basic/exec-util.c b/src/basic/exec-util.c index e0057a7572..49fb95e382 100644 --- a/src/basic/exec-util.c +++ b/src/basic/exec-util.c @@ -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; diff --git a/src/basic/hexdecoct.c b/src/basic/hexdecoct.c index fe7e4954ef..0764521b6f 100644 --- a/src/basic/hexdecoct.c +++ b/src/basic/hexdecoct.c @@ -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); diff --git a/src/core/automount.c b/src/core/automount.c index 01a6ff806e..a8d773686b 100644 --- a/src/core/automount.c +++ b/src/core/automount.c @@ -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, ¶m) < 0) return -errno; diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index effa092ec9..3d8a59029d 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -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) diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index 5643c0578d..f35903a302 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -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; diff --git a/src/shared/watchdog.c b/src/shared/watchdog.c index b0a422da84..bf5c98227b 100644 --- a/src/shared/watchdog.c +++ b/src/shared/watchdog.c @@ -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); diff --git a/src/udev/udevadm-control.c b/src/udev/udevadm-control.c index 9546a6ebaf..c704279512 100644 --- a/src/udev/udevadm-control.c +++ b/src/udev/udevadm-control.c @@ -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; }