coccinelle: make use of DIV_ROUND_UP() wherever appropriate

Let's use our macros where we can
This commit is contained in:
Lennart Poettering 2018-03-20 20:36:09 +01:00
parent 3ceae1bc14
commit be6b0c2165
9 changed files with 30 additions and 11 deletions

View File

@ -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)

View File

@ -1169,7 +1169,7 @@ static int find_matching_component(const CalendarSpec *spec, const CalendarCompo
} else if (c->repeat > 0) { } else if (c->repeat > 0) {
int k; 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)) { if ((!d_set || k < d) && (stop < 0 || k <= stop)) {
d = k; d = k;

View File

@ -116,7 +116,7 @@ static int do_execute(
* default action terminating the process, and turn on alarm(). */ * default action terminating the process, and turn on alarm(). */
if (timeout != USEC_INFINITY) 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) { STRV_FOREACH(path, paths) {
_cleanup_free_ char *t = NULL; _cleanup_free_ char *t = NULL;

View File

@ -583,7 +583,7 @@ static int base64_append_width(
if (len <= 0) if (len <= 0)
return len; return len;
lines = (len + width - 1) / width; lines = DIV_ROUND_UP(len, width);
slen = strlen_ptr(sep); slen = strlen_ptr(sep);
t = realloc(*prefix, plen + 1 + slen + (indent + width + 1) * lines); t = realloc(*prefix, plen + 1 + slen + (indent + width + 1) * lines);

View File

@ -429,7 +429,7 @@ static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, usec_t usec) {
param.timeout.timeout = 0; param.timeout.timeout = 0;
else else
/* Convert to seconds, rounding up. */ /* 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) if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param) < 0)
return -errno; return -errno;

View File

@ -130,7 +130,7 @@ static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned perc
assert(n_columns > 0); assert(n_columns > 0);
n = strv_length(x); n = strv_length(x);
per_column = (n + n_columns - 1) / n_columns; per_column = DIV_ROUND_UP(n, n_columns);
break_lines = lines(); break_lines = lines();
if (break_lines > 2) if (break_lines > 2)

View File

@ -689,7 +689,7 @@ static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size)
} }
/* Increase by larger blocks at once */ /* 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) if (f->metrics.max_size > 0 && new_size > f->metrics.max_size)
new_size = f->metrics.max_size; new_size = f->metrics.max_size;

View File

@ -54,7 +54,7 @@ static int update_timeout(void) {
int sec, flags; int sec, flags;
char buf[FORMAT_TIMESPAN_MAX]; 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); r = ioctl(watchdog_fd, WDIOC_SETTIMEOUT, &sec);
if (r < 0) if (r < 0)
return log_warning_errno(errno, "Failed to set timeout to %is: %m", sec); return log_warning_errno(errno, "Failed to set timeout to %is: %m", sec);

View File

@ -137,18 +137,17 @@ static int adm_control(struct udev *udev, int argc, char *argv[]) {
break; break;
} }
case 't': { case 't': {
int r, seconds;
usec_t s; usec_t s;
int seconds;
int r;
r = parse_sec(optarg, &s); r = parse_sec(optarg, &s);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to parse timeout value '%s'.", optarg); 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."); log_error("Timeout value is out of range.");
else { 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; timeout = seconds;
rc = 0; rc = 0;
} }