]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
shared/strbuf: introduce and use ALIGN
authorEmil Velikov <emil.l.velikov@gmail.com>
Sun, 2 Aug 2026 12:26:13 +0000 (13:26 +0100)
committerLucas De Marchi <demarchi@kernel.org>
Mon, 10 Aug 2026 13:49:47 +0000 (08:49 -0500)
Introduce the simple macro and use it to (better) illustrate the strbuf
size calculation.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/451
Signed-off-by: Lucas De Marchi <demarchi@kernel.org>
shared/macro.h
shared/strbuf.c

index 83e9d9de5db9177e7522eb12c6017a8319366301..46861ff7036fa834e458695f6990a5218a7c5aa8 100644 (file)
@@ -31,6 +31,9 @@
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
 
+#define _ALIGN_MASK(x, _MASK) (((x) + (_MASK)) & ~(_MASK))
+#define ALIGN(x, B) _ALIGN_MASK(x, (typeof(x))(B) - 1)
+
 #define XSTRINGIFY(x) #x
 #define STRINGIFY(x) XSTRINGIFY(x)
 
index 4c3b55c2a5c3f143bcceaed17ec775112d2c43a6..76aab073f1811a4ca5b13f37baa1aac3f9fafcae 100644 (file)
@@ -10,8 +10,9 @@
 #include <string.h>
 #include <sys/param.h>
 
-#include "util.h"
+#include "macro.h"
 #include "strbuf.h"
+#include "util.h"
 
 #define BUF_STEP 128
 
@@ -42,8 +43,7 @@ static bool strbuf_reserve_extra(struct strbuf *buf, size_t n)
            n > SIZE_MAX - BUF_STEP)
                return false;
 
-       if (n % BUF_STEP)
-               n = ((n / BUF_STEP) + 1) * BUF_STEP;
+       n = ALIGN(n, BUF_STEP);
 
        return buf_realloc(buf, n);
 }