1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Kay Sievers <kay@vrfy.org>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #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
= new0(struct strbuf
, 1);
54 str
->buf
= new0(char, 1);
59 str
->root
= new0(struct strbuf_node
, 1);
71 static void strbuf_node_cleanup(struct strbuf_node
*node
) {
74 for (i
= 0; i
< node
->children_count
; i
++)
75 strbuf_node_cleanup(node
->children
[i
].child
);
80 /* clean up trie data, leave only the string buffer */
81 void strbuf_complete(struct strbuf
*str
) {
85 strbuf_node_cleanup(str
->root
);
89 /* clean up everything */
90 void strbuf_cleanup(struct strbuf
*str
) {
94 strbuf_node_cleanup(str
->root
);
99 static int strbuf_children_cmp(const struct strbuf_child_entry
*n1
,
100 const struct strbuf_child_entry
*n2
) {
101 return n1
->c
- n2
->c
;
104 static void bubbleinsert(struct strbuf_node
*node
,
106 struct strbuf_node
*node_child
) {
108 struct strbuf_child_entry
new = {
112 int left
= 0, right
= node
->children_count
;
114 while (right
> left
) {
115 int middle
= (right
+ left
) / 2 ;
116 if (strbuf_children_cmp(&node
->children
[middle
], &new) <= 0)
122 memmove(node
->children
+ left
+ 1, node
->children
+ left
,
123 sizeof(struct strbuf_child_entry
) * (node
->children_count
- left
));
124 node
->children
[left
] = new;
126 node
->children_count
++;
129 /* add string, return the index/offset into the buffer */
130 ssize_t
strbuf_add_string(struct strbuf
*str
, const char *s
, size_t len
) {
132 struct strbuf_node
*node
;
135 struct strbuf_child_entry
*child
;
136 struct strbuf_node
*node_child
;
142 /* search string; start from last character to find possibly matching tails */
150 for (depth
= 0; depth
<= len
; depth
++) {
151 struct strbuf_child_entry search
;
153 /* match against current node */
154 off
= node
->value_off
+ node
->value_len
- len
;
155 if (depth
== len
|| (node
->value_len
>= len
&& memcmp(str
->buf
+ off
, s
, len
) == 0)) {
156 str
->dedup_len
+= len
;
161 /* lookup child node */
162 c
= s
[len
- 1 - depth
];
164 child
= bsearch(&search
, node
->children
, node
->children_count
,
165 sizeof(struct strbuf_child_entry
),
166 (__compar_fn_t
) strbuf_children_cmp
);
173 buf_new
= realloc(str
->buf
, str
->len
+ len
+1);
178 memcpy(str
->buf
+ off
, s
, len
);
180 str
->buf
[str
->len
++] = '\0';
183 node_child
= new0(struct strbuf_node
, 1);
186 node_child
->value_off
= off
;
187 node_child
->value_len
= len
;
189 /* extend array, add new entry, sort for bisection */
190 child
= realloc(node
->children
, (node
->children_count
+ 1) * sizeof(struct strbuf_child_entry
));
198 node
->children
= child
;
199 bubbleinsert(node
, c
, node_child
);