From: Emil Velikov Date: Sun, 2 Aug 2026 12:26:13 +0000 (+0100) Subject: shared/strbuf: introduce and use ALIGN X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ea30e6681fe729eba4623649aa8651fb5ce5c7a7;p=thirdparty%2Fkmod.git shared/strbuf: introduce and use ALIGN Introduce the simple macro and use it to (better) illustrate the strbuf size calculation. Signed-off-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/451 Signed-off-by: Lucas De Marchi --- diff --git a/shared/macro.h b/shared/macro.h index 83e9d9d..46861ff 100644 --- a/shared/macro.h +++ b/shared/macro.h @@ -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) diff --git a/shared/strbuf.c b/shared/strbuf.c index 4c3b55c..76aab07 100644 --- a/shared/strbuf.c +++ b/shared/strbuf.c @@ -10,8 +10,9 @@ #include #include -#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); }