]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
955bd501 | 2 | |
11c3a366 | 3 | #include <errno.h> |
955bd501 KS |
4 | #include <stdlib.h> |
5 | #include <string.h> | |
6 | ||
b5efdb8a | 7 | #include "alloc-util.h" |
760877e9 | 8 | #include "sort-util.h" |
955bd501 KS |
9 | #include "strbuf.h" |
10 | ||
4693cfb3 | 11 | /* |
ab06eef8 | 12 | * Strbuf stores given strings in a single continuous allocated memory |
f1c0ece1 KS |
13 | * area. Identical strings are de-duplicated and return the same offset |
14 | * as the first string stored. If the tail of a string already exists | |
15 | * in the buffer, the tail is returned. | |
4693cfb3 | 16 | * |
f1c0ece1 KS |
17 | * A trie (http://en.wikipedia.org/wiki/Trie) is used to maintain the |
18 | * information about the stored strings. | |
4693cfb3 KS |
19 | * |
20 | * Example of udev rules: | |
21 | * $ ./udevadm test . | |
22 | * ... | |
23 | * read rules file: /usr/lib/udev/rules.d/99-systemd.rules | |
24 | * rules contain 196608 bytes tokens (16384 * 12 bytes), 39742 bytes strings | |
25 | * 23939 strings (207859 bytes), 20404 de-duplicated (171653 bytes), 3536 trie nodes used | |
26 | * ... | |
27 | */ | |
28 | ||
955bd501 KS |
29 | struct strbuf *strbuf_new(void) { |
30 | struct strbuf *str; | |
31 | ||
2fb076ad | 32 | str = new(struct strbuf, 1); |
955bd501 KS |
33 | if (!str) |
34 | return NULL; | |
2fb076ad ZJS |
35 | *str = (struct strbuf) { |
36 | .buf = new0(char, 1), | |
37 | .root = new0(struct strbuf_node, 1), | |
38 | .len = 1, | |
39 | .nodes_count = 1, | |
40 | }; | |
41 | if (!str->buf || !str->root) { | |
42 | free(str->buf); | |
43 | free(str->root); | |
44 | return mfree(str); | |
45 | } | |
955bd501 | 46 | |
955bd501 | 47 | return str; |
955bd501 KS |
48 | } |
49 | ||
2fb076ad | 50 | static struct strbuf_node* strbuf_node_cleanup(struct strbuf_node *node) { |
955bd501 KS |
51 | size_t i; |
52 | ||
53 | for (i = 0; i < node->children_count; i++) | |
54 | strbuf_node_cleanup(node->children[i].child); | |
55 | free(node->children); | |
2fb076ad | 56 | return mfree(node); |
955bd501 KS |
57 | } |
58 | ||
4693cfb3 | 59 | /* clean up trie data, leave only the string buffer */ |
955bd501 KS |
60 | void strbuf_complete(struct strbuf *str) { |
61 | if (!str) | |
62 | return; | |
63 | if (str->root) | |
2fb076ad | 64 | str->root = strbuf_node_cleanup(str->root); |
955bd501 KS |
65 | } |
66 | ||
4693cfb3 | 67 | /* clean up everything */ |
955bd501 | 68 | void strbuf_cleanup(struct strbuf *str) { |
f73fc95e YW |
69 | if (!str) |
70 | return; | |
71 | ||
2fb076ad | 72 | strbuf_complete(str); |
955bd501 KS |
73 | free(str->buf); |
74 | free(str); | |
75 | } | |
76 | ||
3c8bed4e ZJS |
77 | static 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 |
82 | static 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 |
108 | ssize_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; |
bc861c2e | 145 | child = typesafe_bsearch(&search, node->children, node->children_count, strbuf_children_cmp); |
955bd501 KS |
146 | if (!child) |
147 | break; | |
148 | node = child->child; | |
149 | } | |
150 | ||
151 | /* add new string */ | |
152 | buf_new = realloc(str->buf, str->len + len+1); | |
153 | if (!buf_new) | |
154 | return -ENOMEM; | |
155 | str->buf = buf_new; | |
156 | off = str->len; | |
157 | memcpy(str->buf + off, s, len); | |
158 | str->len += len; | |
159 | str->buf[str->len++] = '\0'; | |
160 | ||
161 | /* new node */ | |
2fb076ad | 162 | node_child = new(struct strbuf_node, 1); |
955bd501 KS |
163 | if (!node_child) |
164 | return -ENOMEM; | |
2fb076ad ZJS |
165 | *node_child = (struct strbuf_node) { |
166 | .value_off = off, | |
167 | .value_len = len, | |
168 | }; | |
955bd501 KS |
169 | |
170 | /* extend array, add new entry, sort for bisection */ | |
62d74c78 | 171 | child = reallocarray(node->children, node->children_count + 1, sizeof(struct strbuf_child_entry)); |
a9c307e5 ZJS |
172 | if (!child) { |
173 | free(node_child); | |
955bd501 | 174 | return -ENOMEM; |
a9c307e5 ZJS |
175 | } |
176 | ||
177 | str->nodes_count++; | |
178 | ||
955bd501 | 179 | node->children = child; |
3c8bed4e | 180 | bubbleinsert(node, c, node_child); |
955bd501 KS |
181 | |
182 | return off; | |
183 | } |