use DIV_ROUND_UP

Convert code that divides and rounds up to use DIV_ROUND_UP to make the intent clearer and reduce the number of magic constants. Signed-off-by: Rene Scharfe <l.s.r@web.de> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jul 8, 2017 at 12:35 UTC 42c78a216e751cfa2720c8276c9e9f2b81640e6b
9 files changed +14 -15
builtin/gc.c
+1 -1
@@ -148,7 +148,7 @@ static int too_many_loose_objects(void)
148 if (!dir)
149 return 0;
150
151 - auto_threshold = (gc_auto_threshold + 255) / 256;
151 + auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
152 while ((ent = readdir(dir)) != NULL) {
153 if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
154 ent->d_name[38] != '\0')
builtin/grep.c
+1 -1
@@ -524,7 +524,7 @@ static void compile_submodule_options(const struct grep_opt *opt,
524 * submodule process has its own thread pool.
525 */
526 argv_array_pushf(&submodule_options, "--threads=%d",
527 - (num_threads + 1) / 2);
527 + DIV_ROUND_UP(num_threads, 2));
528
529 /* Add Pathspecs */
530 argv_array_push(&submodule_options, "--");
builtin/log.c
+1 -1
@@ -1302,7 +1302,7 @@ static struct commit *get_base_commit(const char *base_commit,
1302
1303 if (rev_nr % 2)
1304 rev[i] = rev[2 * i];
1305 - rev_nr = (rev_nr + 1) / 2;
1305 + rev_nr = DIV_ROUND_UP(rev_nr, 2);
1306 }
1307
1308 if (!in_merge_bases(base, rev[0]))
builtin/receive-pack.c
+1 -1
@@ -1805,7 +1805,7 @@ static const char *unpack_with_sideband(struct shallow_info *si)
1805 static void prepare_shallow_update(struct command *commands,
1806 struct shallow_info *si)
1807 {
1808 - int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1808 + int i, j, k, bitmap_size = DIV_ROUND_UP(si->ref->nr, 32);
1809
1810 ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
1811 assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
diff.c
+1 -1
@@ -2095,7 +2095,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
2095 * bytes per "line".
2096 * This is stupid and ugly, but very cheap...
2097 */
2098 - damage = (damage + 63) / 64;
2098 + damage = DIV_ROUND_UP(damage, 64);
2099 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
2100 dir.files[dir.nr].name = file->name;
2101 dir.files[dir.nr].changed = damage;
ewah/ewah_bitmap.c
+2 -2
@@ -210,8 +210,8 @@ size_t ewah_add(struct ewah_bitmap *self, eword_t word)
210 void ewah_set(struct ewah_bitmap *self, size_t i)
211 {
212 const size_t dist =
213 - (i + BITS_IN_EWORD) / BITS_IN_EWORD -
214 - (self->bit_size + BITS_IN_EWORD - 1) / BITS_IN_EWORD;
213 + DIV_ROUND_UP(i + 1, BITS_IN_EWORD) -
214 + DIV_ROUND_UP(self->bit_size, BITS_IN_EWORD);
215
216 assert(i >= self->bit_size);
217
imap-send.c
+1 -1
@@ -861,7 +861,7 @@ static char hexchar(unsigned int b)
861 return b < 10 ? '0' + b : 'a' + (b - 10);
862 }
863
864 -#define ENCODED_SIZE(n) (4*((n+2)/3))
864 +#define ENCODED_SIZE(n) (4 * DIV_ROUND_UP((n), 3))
865 static char *cram(const char *challenge_64, const char *user, const char *pass)
866 {
867 int i, resp_len, encoded_len, decoded_len;
sha1_name.c
+2 -3
@@ -479,10 +479,9 @@ int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
479 * We now know we have on the order of 2^len objects, which
480 * expects a collision at 2^(len/2). But we also care about hex
481 * chars, not bits, and there are 4 bits per hex. So all
482 - * together we need to divide by 2; but we also want to round
483 - * odd numbers up, hence adding one before dividing.
482 + * together we need to divide by 2 and round up.
483 */
485 - len = (len + 1) / 2;
484 + len = DIV_ROUND_UP(len, 2);
485 /*
486 * For very small repos, we stick with our regular fallback.
487 */
shallow.c
+4 -4
@@ -443,7 +443,7 @@ struct paint_info {
443
444 static uint32_t *paint_alloc(struct paint_info *info)
445 {
446 - unsigned nr = (info->nr_bits + 31) / 32;
446 + unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
447 unsigned size = nr * sizeof(uint32_t);
448 void *p;
449 if (!info->pool_count || size > info->end - info->free) {
@@ -471,7 +471,7 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,
471 {
472 unsigned int i, nr;
473 struct commit_list *head = NULL;
474 - int bitmap_nr = (info->nr_bits + 31) / 32;
474 + int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
475 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
476 struct commit *c = lookup_commit_reference_gently(sha1, 1);
477 uint32_t *tmp; /* to be freed before return */
@@ -611,7 +611,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
611 paint_down(&pi, ref->oid[i].hash, i);
612
613 if (used) {
614 - int bitmap_size = ((pi.nr_bits + 31) / 32) * sizeof(uint32_t);
614 + int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
615 memset(used, 0, sizeof(*used) * info->shallow->nr);
616 for (i = 0; i < nr_shallow; i++) {
617 const struct commit *c = lookup_commit(oid[shallow[i]].hash);
@@ -672,7 +672,7 @@ static void post_assign_shallow(struct shallow_info *info,
672 struct commit *c;
673 uint32_t **bitmap;
674 int dst, i, j;
675 - int bitmap_nr = (info->ref->nr + 31) / 32;
675 + int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
676 struct commit_array ca;
677
678 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");