From: Tobias Stoeckmann Date: Mon, 24 Feb 2025 18:48:05 +0000 (+0100) Subject: libkmod: use stack strbuf for index processing X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5c38d457a093853be002a950c8266cc2c4839db8;p=thirdparty%2Fkmod.git libkmod: use stack strbuf for index processing 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 Link: https://github.com/kmod-project/kmod/pull/296 Signed-off-by: Lucas De Marchi --- diff --git a/libkmod/libkmod-index.c b/libkmod/libkmod-index.c index 40199a54..ae784d6e 100644 --- a/libkmod/libkmod-index.c +++ b/libkmod/libkmod-index.c @@ -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; }