]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
strbuf: Extract realloc
authorLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 12 Nov 2024 14:36:54 +0000 (08:36 -0600)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Sun, 17 Nov 2024 21:35:13 +0000 (15:35 -0600)
Extract the realloc from the size calculation so it can be shared with
other upcoming callers.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
shared/strbuf.c

index f89a97d70de76f3ea173bdb909e6e757e57d33b5..290d1db37d59bc8bcc46586e53c46d19482f889a 100644 (file)
 
 #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;