]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/strbuf.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / basic / strbuf.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
955bd501 2/***
96b2fb93 3 Copyright © 2012 Kay Sievers <kay@vrfy.org>
955bd501
KS
4***/
5
11c3a366 6#include <errno.h>
955bd501
KS
7#include <stdlib.h>
8#include <string.h>
9
b5efdb8a 10#include "alloc-util.h"
955bd501 11#include "strbuf.h"
d6c5d19b 12#include "util.h"
955bd501 13
4693cfb3 14/*
ab06eef8 15 * Strbuf stores given strings in a single continuous allocated memory
f1c0ece1
KS
16 * area. Identical strings are de-duplicated and return the same offset
17 * as the first string stored. If the tail of a string already exists
18 * in the buffer, the tail is returned.
4693cfb3 19 *
f1c0ece1
KS
20 * A trie (http://en.wikipedia.org/wiki/Trie) is used to maintain the
21 * information about the stored strings.
4693cfb3
KS
22 *
23 * Example of udev rules:
24 * $ ./udevadm test .
25 * ...
26 * read rules file: /usr/lib/udev/rules.d/99-systemd.rules
27 * rules contain 196608 bytes tokens (16384 * 12 bytes), 39742 bytes strings
28 * 23939 strings (207859 bytes), 20404 de-duplicated (171653 bytes), 3536 trie nodes used
29 * ...
30 */
31
955bd501
KS
32struct strbuf *strbuf_new(void) {
33 struct strbuf *str;
34
2fb076ad 35 str = new(struct strbuf, 1);
955bd501
KS
36 if (!str)
37 return NULL;
2fb076ad
ZJS
38 *str = (struct strbuf) {
39 .buf = new0(char, 1),
40 .root = new0(struct strbuf_node, 1),
41 .len = 1,
42 .nodes_count = 1,
43 };
44 if (!str->buf || !str->root) {
45 free(str->buf);
46 free(str->root);
47 return mfree(str);
48 }
955bd501 49
955bd501 50 return str;
955bd501
KS
51}
52
2fb076ad 53static struct strbuf_node* strbuf_node_cleanup(struct strbuf_node *node) {
955bd501
KS
54 size_t i;
55
56 for (i = 0; i < node->children_count; i++)
57 strbuf_node_cleanup(node->children[i].child);
58 free(node->children);
2fb076ad 59 return mfree(node);
955bd501
KS
60}
61
4693cfb3 62/* clean up trie data, leave only the string buffer */
955bd501
KS
63void strbuf_complete(struct strbuf *str) {
64 if (!str)
65 return;
66 if (str->root)
2fb076ad 67 str->root = strbuf_node_cleanup(str->root);
955bd501
KS
68}
69
4693cfb3 70/* clean up everything */
955bd501 71void strbuf_cleanup(struct strbuf *str) {
2fb076ad 72 strbuf_complete(str);
955bd501
KS
73 free(str->buf);
74 free(str);
75}
76
3c8bed4e
ZJS
77static int strbuf_children_cmp(const struct strbuf_child_entry *n1,
78 const struct strbuf_child_entry *n2) {
955bd501
KS
79 return n1->c - n2->c;
80}
81
3c8bed4e
ZJS
82static void bubbleinsert(struct strbuf_node *node,
83 uint8_t c,
84 struct strbuf_node *node_child) {
85
86 struct strbuf_child_entry new = {
87 .c = c,
88 .child = node_child,
89 };
90 int left = 0, right = node->children_count;
91
92 while (right > left) {
93 int middle = (right + left) / 2 ;
94 if (strbuf_children_cmp(&node->children[middle], &new) <= 0)
95 left = middle + 1;
96 else
97 right = middle;
98 }
99
100 memmove(node->children + left + 1, node->children + left,
101 sizeof(struct strbuf_child_entry) * (node->children_count - left));
102 node->children[left] = new;
103
313cefa1 104 node->children_count++;
3c8bed4e
ZJS
105}
106
4693cfb3 107/* add string, return the index/offset into the buffer */
955bd501
KS
108ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
109 uint8_t c;
110 struct strbuf_node *node;
111 size_t depth;
112 char *buf_new;
113 struct strbuf_child_entry *child;
114 struct strbuf_node *node_child;
115 ssize_t off;
116
117 if (!str->root)
118 return -EINVAL;
119
120 /* search string; start from last character to find possibly matching tails */
435ce146 121
955bd501 122 str->in_count++;
435ce146
ZJS
123 if (len == 0) {
124 str->dedup_count++;
125 return 0;
126 }
955bd501
KS
127 str->in_len += len;
128
129 node = str->root;
955bd501
KS
130 for (depth = 0; depth <= len; depth++) {
131 struct strbuf_child_entry search;
132
133 /* match against current node */
134 off = node->value_off + node->value_len - len;
135 if (depth == len || (node->value_len >= len && memcmp(str->buf + off, s, len) == 0)) {
136 str->dedup_len += len;
137 str->dedup_count++;
138 return off;
139 }
140
c45606eb
LR
141 c = s[len - 1 - depth];
142
955bd501 143 /* lookup child node */
955bd501 144 search.c = c;
d6c5d19b
ZJS
145 child = bsearch_safe(&search, node->children, node->children_count,
146 sizeof(struct strbuf_child_entry),
147 (__compar_fn_t) strbuf_children_cmp);
955bd501
KS
148 if (!child)
149 break;
150 node = child->child;
151 }
152
153 /* add new string */
154 buf_new = realloc(str->buf, str->len + len+1);
155 if (!buf_new)
156 return -ENOMEM;
157 str->buf = buf_new;
158 off = str->len;
159 memcpy(str->buf + off, s, len);
160 str->len += len;
161 str->buf[str->len++] = '\0';
162
163 /* new node */
2fb076ad 164 node_child = new(struct strbuf_node, 1);
955bd501
KS
165 if (!node_child)
166 return -ENOMEM;
2fb076ad
ZJS
167 *node_child = (struct strbuf_node) {
168 .value_off = off,
169 .value_len = len,
170 };
955bd501
KS
171
172 /* extend array, add new entry, sort for bisection */
62d74c78 173 child = reallocarray(node->children, node->children_count + 1, sizeof(struct strbuf_child_entry));
a9c307e5
ZJS
174 if (!child) {
175 free(node_child);
955bd501 176 return -ENOMEM;
a9c307e5
ZJS
177 }
178
179 str->nodes_count++;
180
955bd501 181 node->children = child;
3c8bed4e 182 bubbleinsert(node, c, node_child);
955bd501
KS
183
184 return off;
185}