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/>.
29 * Strbuf stores given strings in a single continous allocated memory
30 * area. Identical strings are de-duplicated and return the same offset
31 * as the first string stored. If the tail of a string already exists
32 * in the buffer, the tail is returned.
34 * A trie (http://en.wikipedia.org/wiki/Trie) is used to maintain the
35 * information about the stored strings.
37 * Example of udev rules:
40 * read rules file: /usr/lib/udev/rules.d/99-systemd.rules
41 * rules contain 196608 bytes tokens (16384 * 12 bytes), 39742 bytes strings
42 * 23939 strings (207859 bytes), 20404 de-duplicated (171653 bytes), 3536 trie nodes used
46 struct strbuf *strbuf_new(void) {
49 str = new0(struct strbuf, 1);
53 str->buf = new0(char, 1);
58 str->root = new0(struct strbuf_node, 1);
70 static void strbuf_node_cleanup(struct strbuf_node *node) {
73 for (i = 0; i < node->children_count; i++)
74 strbuf_node_cleanup(node->children[i].child);
79 /* clean up trie data, leave only the string buffer */
80 void strbuf_complete(struct strbuf *str) {
84 strbuf_node_cleanup(str->root);
88 /* clean up everything */
89 void strbuf_cleanup(struct strbuf *str) {
93 strbuf_node_cleanup(str->root);
98 static int strbuf_children_cmp(const void *v1, const void *v2) {
99 const struct strbuf_child_entry *n1 = v1;
100 const struct strbuf_child_entry *n2 = v2;
102 return n1->c - n2->c;
105 /* add string, return the index/offset into the buffer */
106 ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
108 struct strbuf_node *node;
111 struct strbuf_child_entry *child;
112 struct strbuf_node *node_child;
118 /* search string; start from last character to find possibly matching tails */
126 for (depth = 0; depth <= len; depth++) {
127 struct strbuf_child_entry search;
129 /* match against current node */
130 off = node->value_off + node->value_len - len;
131 if (depth == len || (node->value_len >= len && memcmp(str->buf + off, s, len) == 0)) {
132 str->dedup_len += len;
137 /* lookup child node */
138 c = s[len - 1 - depth];
140 child = bsearch(&search, node->children, node->children_count, sizeof(struct strbuf_child_entry),
141 strbuf_children_cmp);
148 buf_new = realloc(str->buf, str->len + len+1);
153 memcpy(str->buf + off, s, len);
155 str->buf[str->len++] = '\0';
158 node_child = new0(struct strbuf_node, 1);
162 node_child->value_off = off;
163 node_child->value_len = len;
165 /* extend array, add new entry, sort for bisection */
166 child = realloc(node->children, (node->children_count + 1) * sizeof(struct strbuf_child_entry));
169 node->children = child;
170 node->children[node->children_count].c = c;
171 node->children[node->children_count].child = node_child;
172 node->children_count++;
173 qsort(node->children, node->children_count, sizeof(struct strbuf_child_entry), strbuf_children_cmp);