From: Lucas De Marchi Date: Tue, 12 Nov 2024 14:36:54 +0000 (-0600) Subject: strbuf: Extract realloc X-Git-Tag: v34~94 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7e2c1eb58ad77beb938743d6a6b57b0f54c78cf;p=thirdparty%2Fkmod.git strbuf: Extract realloc Extract the realloc from the size calculation so it can be shared with other upcoming callers. Signed-off-by: Lucas De Marchi Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/239 --- diff --git a/shared/strbuf.c b/shared/strbuf.c index f89a97d7..290d1db3 100644 --- a/shared/strbuf.c +++ b/shared/strbuf.c @@ -14,27 +14,31 @@ #define BUF_STEP 128 -static bool buf_grow(struct strbuf *buf, size_t newsize) +static bool buf_realloc(struct strbuf *buf, size_t sz) { void *tmp; - size_t sz; - - if (newsize <= buf->size) - return true; - - if (newsize % BUF_STEP == 0) - sz = newsize; - else - sz = ((newsize / BUF_STEP) + 1) * BUF_STEP; tmp = realloc(buf->bytes, sz); if (sz > 0 && tmp == NULL) return false; + buf->bytes = tmp; buf->size = sz; + return true; } +static bool buf_grow(struct strbuf *buf, size_t newsize) +{ + if (newsize <= buf->size) + return true; + + if (newsize % BUF_STEP) + newsize = ((newsize / BUF_STEP) + 1) * BUF_STEP; + + return buf_realloc(buf, newsize); +} + void strbuf_init(struct strbuf *buf) { buf->bytes = NULL;