1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Kay Sievers <kay@vrfy.org>
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "alloc-util.h"
30 * Strbuf stores given strings in a single continuous allocated memory
31 * area. Identical strings are de-duplicated and return the same offset
32 * as the first string stored. If the tail of a string already exists
33 * in the buffer, the tail is returned.
35 * A trie (http://en.wikipedia.org/wiki/Trie) is used to maintain the
36 * information about the stored strings.
38 * Example of udev rules:
41 * read rules file: /usr/lib/udev/rules.d/99-systemd.rules
42 * rules contain 196608 bytes tokens (16384 * 12 bytes), 39742 bytes strings
43 * 23939 strings (207859 bytes), 20404 de-duplicated (171653 bytes), 3536 trie nodes used
47 struct strbuf
*strbuf_new(void) {
50 str
= new(struct strbuf
, 1);
53 *str
= (struct strbuf
) {
55 .root
= new0(struct strbuf_node
, 1),
59 if (!str
->buf
|| !str
->root
) {
68 static struct strbuf_node
* strbuf_node_cleanup(struct strbuf_node
*node
) {
71 for (i
= 0; i
< node
->children_count
; i
++)
72 strbuf_node_cleanup(node
->children
[i
].child
);
77 /* clean up trie data, leave only the string buffer */
78 void strbuf_complete(struct strbuf
*str
) {
82 str
->root
= strbuf_node_cleanup(str
->root
);
85 /* clean up everything */
86 void strbuf_cleanup(struct strbuf
*str
) {
92 static int strbuf_children_cmp(const struct strbuf_child_entry
*n1
,
93 const struct strbuf_child_entry
*n2
) {
97 static void bubbleinsert(struct strbuf_node
*node
,
99 struct strbuf_node
*node_child
) {
101 struct strbuf_child_entry
new = {
105 int left
= 0, right
= node
->children_count
;
107 while (right
> left
) {
108 int middle
= (right
+ left
) / 2 ;
109 if (strbuf_children_cmp(&node
->children
[middle
], &new) <= 0)
115 memmove(node
->children
+ left
+ 1, node
->children
+ left
,
116 sizeof(struct strbuf_child_entry
) * (node
->children_count
- left
));
117 node
->children
[left
] = new;
119 node
->children_count
++;
122 /* add string, return the index/offset into the buffer */
123 ssize_t
strbuf_add_string(struct strbuf
*str
, const char *s
, size_t len
) {
125 struct strbuf_node
*node
;
128 struct strbuf_child_entry
*child
;
129 struct strbuf_node
*node_child
;
135 /* search string; start from last character to find possibly matching tails */
142 for (depth
= 0; depth
<= len
; depth
++) {
143 struct strbuf_child_entry search
;
145 /* match against current node */
146 off
= node
->value_off
+ node
->value_len
- len
;
147 if (depth
== len
|| (node
->value_len
>= len
&& memcmp(str
->buf
+ off
, s
, len
) == 0)) {
148 str
->dedup_len
+= len
;
153 c
= s
[len
- 1 - depth
];
155 /* lookup child node */
157 child
= bsearch_safe(&search
, node
->children
, node
->children_count
,
158 sizeof(struct strbuf_child_entry
),
159 (__compar_fn_t
) strbuf_children_cmp
);
166 buf_new
= realloc(str
->buf
, str
->len
+ len
+1);
171 memcpy(str
->buf
+ off
, s
, len
);
173 str
->buf
[str
->len
++] = '\0';
176 node_child
= new(struct strbuf_node
, 1);
179 *node_child
= (struct strbuf_node
) {
184 /* extend array, add new entry, sort for bisection */
185 child
= reallocarray(node
->children
, node
->children_count
+ 1, sizeof(struct strbuf_child_entry
));
193 node
->children
= child
;
194 bubbleinsert(node
, c
, node_child
);