Commit 9c30d6da4e44d98c3bcc1639e4ce062bbb241865

Ran Benita 2014-06-15T15:30:51

x11: don't iterate on empty batches If count % SIZE == 0 we did a useless iteration where start==stop. It's harmless but strange, so don't do that. Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/utils.h b/src/utils.h
index 0198f07..51d690e 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -187,6 +187,9 @@ unmap_file(const char *str, size_t size);
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 #define MAX3(a, b, c) MAX(MAX((a), (b)), (c))
 
+/* Round up @a so it's divisible by @b. */
+#define ROUNDUP(a, b) (((a) + (b) - 1) / (b) * (b))
+
 #if defined(HAVE_SECURE_GETENV)
 # define secure_getenv secure_getenv
 #elif defined(HAVE___SECURE_GETENV)
diff --git a/src/x11/util.c b/src/x11/util.c
index 47bb92a..c41f1d6 100644
--- a/src/x11/util.c
+++ b/src/x11/util.c
@@ -159,9 +159,10 @@ adopt_atoms(struct xkb_context *ctx, xcb_connection_t *conn,
 {
     enum { SIZE = 128 };
     xcb_get_atom_name_cookie_t cookies[SIZE];
+    const size_t num_batches = ROUNDUP(count, SIZE) / SIZE;
 
     /* Send and collect the atoms in batches of reasonable SIZE. */
-    for (size_t batch = 0; batch <= count / SIZE; batch++) {
+    for (size_t batch = 0; batch < num_batches; batch++) {
         const size_t start = batch * SIZE;
         const size_t stop = min((batch + 1) * SIZE, count);