]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: use stack strbuf for index processing
authorTobias Stoeckmann <tobias@stoeckmann.org>
Mon, 24 Feb 2025 18:48:05 +0000 (19:48 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 7 Mar 2025 04:57:37 +0000 (22:57 -0600)
The strbuf content is never returned, so it's easy to switch to a
stack-based solution. It removes heap allocations and the need to
manually call strbuf_init and strbuf_release since these are covered
through DECLARE_STRBUF_WITH_STACK as well.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/296
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-index.c

index 40199a54d699a5b44fbe406364411d017f114214..ae784d6e2f78baa7a3b5c36f9a6311008d17ba62 100644 (file)
@@ -428,20 +428,18 @@ static void index_dump_node(struct index_node_f *node, struct strbuf *buf,
 
 void index_dump(struct index_file *in, int fd, bool alias_prefix)
 {
+       DECLARE_STRBUF_WITH_STACK(buf, 128);
        struct index_node_f *root;
-       struct strbuf buf;
        struct wrtbuf wbuf;
 
        root = index_readroot(in);
        if (root == NULL)
                return;
 
-       strbuf_init(&buf);
        wrtbuf_init(&wbuf, fd);
        if (!alias_prefix || strbuf_pushchars(&buf, "alias "))
                index_dump_node(root, &buf, &wbuf);
        wrtbuf_flush(&wbuf);
-       strbuf_release(&buf);
 }
 
 static char *index_search__node(struct index_node_f *node, const char *key, int i)
@@ -615,13 +613,11 @@ static void index_searchwild__node(struct index_node_f *node, struct strbuf *buf
  */
 struct index_value *index_searchwild(struct index_file *in, const char *key)
 {
+       DECLARE_STRBUF_WITH_STACK(buf, 128);
        struct index_node_f *root = index_readroot(in);
-       struct strbuf buf;
        struct index_value *out = NULL;
 
-       strbuf_init(&buf);
        index_searchwild__node(root, &buf, key, &out);
-       strbuf_release(&buf);
        return out;
 }
 
@@ -892,20 +888,18 @@ static void index_mm_dump_node(struct index_mm_node *node, struct strbuf *buf,
 
 void index_mm_dump(const struct index_mm *idx, int fd, bool alias_prefix)
 {
+       DECLARE_STRBUF_WITH_STACK(buf, 128);
        struct index_mm_node nbuf, *root;
-       struct strbuf buf;
        struct wrtbuf wbuf;
 
        root = index_mm_readroot(idx, &nbuf);
        if (root == NULL)
                return;
 
-       strbuf_init(&buf);
        wrtbuf_init(&wbuf, fd);
        if (!alias_prefix || strbuf_pushchars(&buf, "alias "))
                index_mm_dump_node(root, &buf, &wbuf);
        wrtbuf_flush(&wbuf);
-       strbuf_release(&buf);
 }
 
 static char *index_mm_search_node(struct index_mm_node *node, const char *key)
@@ -1074,13 +1068,11 @@ static void index_mm_searchwild_node(struct index_mm_node *node, struct strbuf *
  */
 struct index_value *index_mm_searchwild(const struct index_mm *idx, const char *key)
 {
+       DECLARE_STRBUF_WITH_STACK(buf, 128);
        struct index_mm_node nbuf, *root;
-       struct strbuf buf;
        struct index_value *out = NULL;
 
        root = index_mm_readroot(idx, &nbuf);
-       strbuf_init(&buf);
        index_mm_searchwild_node(root, &buf, key, &out);
-       strbuf_release(&buf);
        return out;
 }