From 34d4e6f32affb0d0b23c26c2df6837424f6b020b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 12 Jun 2024 03:24:30 +0900 Subject: [PATCH] strbuf: several cleanups for strbuf_add_string() - add missing assertions, - use GREEDY_REALLOC() at one more place, - etc. Before: ``` $ sudo time valgrind --leak-check=full ./systemd-hwdb update ==112572== Memcheck, a memory error detector ==112572== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. ==112572== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info ==112572== Command: ./systemd-hwdb update ==112572== ==112572== ==112572== HEAP SUMMARY: ==112572== in use at exit: 0 bytes in 0 blocks ==112572== total heap usage: 1,320,113 allocs, 1,320,113 frees, 70,614,501 bytes allocated ==112572== ==112572== All heap blocks were freed -- no leaks are possible ==112572== ==112572== For lists of detected and suppressed errors, rerun with: -s ==112572== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 21.94user 0.19system 0:22.23elapsed 99%CPU (0avgtext+0avgdata 229876maxresident)k 0inputs+25264outputs (0major+57275minor)pagefaults 0swaps ``` After: ``` $ sudo time valgrind --leak-check=full ./systemd-hwdb update [sudo] password for watanabe: ==114732== Memcheck, a memory error detector ==114732== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. ==114732== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info ==114732== Command: ./systemd-hwdb update ==114732== ==114732== ==114732== HEAP SUMMARY: ==114732== in use at exit: 0 bytes in 0 blocks ==114732== total heap usage: 1,276,406 allocs, 1,276,406 frees, 68,500,491 bytes allocated ==114732== ==114732== All heap blocks were freed -- no leaks are possible ==114732== ==114732== For lists of detected and suppressed errors, rerun with: -s ==114732== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 21.91user 0.24system 0:22.26elapsed 99%CPU (0avgtext+0avgdata 233584maxresident)k 0inputs+25168outputs (0major+58237minor)pagefaults 0swaps ``` --- src/basic/strbuf.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/basic/strbuf.c b/src/basic/strbuf.c index 6d43955bb1..c81ba8a487 100644 --- a/src/basic/strbuf.c +++ b/src/basic/strbuf.c @@ -107,10 +107,11 @@ static void bubbleinsert(struct strbuf_node *node, /* add string, return the index/offset into the buffer */ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) { uint8_t c; - struct strbuf_child_entry *child; - struct strbuf_node *node; ssize_t off; + assert(str); + assert(s || len == 0); + if (!str->root) return -EINVAL; @@ -123,10 +124,8 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) { } str->in_len += len; - node = str->root; + struct strbuf_node *node = str->root; for (size_t depth = 0; depth <= len; depth++) { - struct strbuf_child_entry search; - /* match against current node */ off = node->value_off + node->value_len - len; if (depth == len || (node->value_len >= len && memcmp(str->buf + off, s, len) == 0)) { @@ -138,7 +137,7 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) { c = s[len - 1 - depth]; /* lookup child node */ - search.c = c; + struct strbuf_child_entry *child, search = { .c = c }; child = typesafe_bsearch(&search, node->children, node->children_count, strbuf_children_cmp); if (!child) break; @@ -165,13 +164,11 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) { }; /* extend array, add new entry, sort for bisection */ - child = reallocarray(node->children, node->children_count + 1, sizeof(struct strbuf_child_entry)); - if (!child) + if (!GREEDY_REALLOC(node->children, node->children_count + 1)) return -ENOMEM; str->nodes_count++; - node->children = child; bubbleinsert(node, c, TAKE_PTR(node_child)); return off;