diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-03-26 10:26:39 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-03-26 15:28:03 +0200 |
commit | 2fb076ad43772b808813f88dd26b300336b63671 (patch) | |
tree | 39a5cc2493dac45c8bd2c64fab83f15c55c88af6 /src/basic/strbuf.c | |
parent | 2e38df53494178022aeee885e339273fecf80e40 (diff) | |
download | systemd-2fb076ad43772b808813f88dd26b300336b63671.tar.gz |
basic/strbuf: use structured initalizers to shorten code
Diffstat (limited to 'src/basic/strbuf.c')
-rw-r--r-- | src/basic/strbuf.c | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/src/basic/strbuf.c b/src/basic/strbuf.c index b1b1af9cd4..5a416ce286 100644 --- a/src/basic/strbuf.c +++ b/src/basic/strbuf.c @@ -47,33 +47,31 @@ struct strbuf *strbuf_new(void) { struct strbuf *str; - str = new0(struct strbuf, 1); + str = new(struct strbuf, 1); if (!str) return NULL; + *str = (struct strbuf) { + .buf = new0(char, 1), + .root = new0(struct strbuf_node, 1), + .len = 1, + .nodes_count = 1, + }; + if (!str->buf || !str->root) { + free(str->buf); + free(str->root); + return mfree(str); + } - str->buf = new0(char, 1); - if (!str->buf) - goto err; - str->len = 1; - - str->root = new0(struct strbuf_node, 1); - if (!str->root) - goto err; - str->nodes_count = 1; return str; -err: - free(str->buf); - free(str->root); - return mfree(str); } -static void strbuf_node_cleanup(struct strbuf_node *node) { +static struct strbuf_node* strbuf_node_cleanup(struct strbuf_node *node) { size_t i; for (i = 0; i < node->children_count; i++) strbuf_node_cleanup(node->children[i].child); free(node->children); - free(node); + return mfree(node); } /* clean up trie data, leave only the string buffer */ @@ -81,16 +79,12 @@ void strbuf_complete(struct strbuf *str) { if (!str) return; if (str->root) - strbuf_node_cleanup(str->root); - str->root = NULL; + str->root = strbuf_node_cleanup(str->root); } /* clean up everything */ void strbuf_cleanup(struct strbuf *str) { - if (!str) - return; - if (str->root) - strbuf_node_cleanup(str->root); + strbuf_complete(str); free(str->buf); free(str); } @@ -179,11 +173,13 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) { str->buf[str->len++] = '\0'; /* new node */ - node_child = new0(struct strbuf_node, 1); + node_child = new(struct strbuf_node, 1); if (!node_child) return -ENOMEM; - node_child->value_off = off; - node_child->value_len = len; + *node_child = (struct strbuf_node) { + .value_off = off, + .value_len = len, + }; /* extend array, add new entry, sort for bisection */ child = reallocarray(node->children, node->children_count + 1, sizeof(struct strbuf_child_entry)); |