]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strbuf: several cleanups for strbuf_add_string()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 11 Jun 2024 18:24:30 +0000 (03:24 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 12 Jun 2024 09:17:46 +0000 (18:17 +0900)
- 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

index 6d43955bb1856b71866364e2e4e0719a4681b4a5..c81ba8a4878840d060dac44b2d0ed72a845d1036 100644 (file)
@@ -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;