]> git.ipfire.org Git - thirdparty/systemd.git/blame - libudev/libudev-list.c
libudev: list - use binary search for list lookup
[thirdparty/systemd.git] / libudev / libudev-list.c
CommitLineData
e345e267
KS
1/*
2 * libudev - interface to udev device information
3 *
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5 *
4061ab9f
KS
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
e345e267
KS
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <stddef.h>
15#include <unistd.h>
16#include <errno.h>
17#include <string.h>
18
19#include "libudev.h"
20#include "libudev-private.h"
21
ce1d6d7f
KS
22/**
23 * SECTION:libudev-list
24 * @short_description: list operation
25 *
26 * Libudev list operations.
27 */
28
1e511322
KS
29/**
30 * udev_list_entry:
31 *
ce1d6d7f
KS
32 * Opaque object representing one entry in a list. An entry contains
33 * contains a name, and optionally a value.
1e511322 34 */
0de33a61 35struct udev_list_entry {
8cd2e972 36 struct udev_list_node node;
869c9031 37 struct udev_list *list;
e345e267
KS
38 char *name;
39 char *value;
8958da13 40 int num;
e345e267
KS
41};
42
8958da13 43/* the list's head points to itself if empty */
869c9031 44void udev_list_node_init(struct udev_list_node *list)
e345e267
KS
45{
46 list->next = list;
47 list->prev = list;
48}
49
869c9031 50int udev_list_node_is_empty(struct udev_list_node *list)
e345e267
KS
51{
52 return list->next == list;
53}
54
3feeb77c 55static void udev_list_node_insert_between(struct udev_list_node *new,
9dcf7ec8
KS
56 struct udev_list_node *prev,
57 struct udev_list_node *next)
e345e267 58{
e345e267
KS
59 next->prev = new;
60 new->next = next;
e345e267
KS
61 new->prev = prev;
62 prev->next = new;
63}
64
9dcf7ec8
KS
65void udev_list_node_append(struct udev_list_node *new, struct udev_list_node *list)
66{
67 udev_list_node_insert_between(new, list->prev, list);
68}
69
70void udev_list_node_remove(struct udev_list_node *entry)
e345e267 71{
8cd2e972
KS
72 struct udev_list_node *prev = entry->prev;
73 struct udev_list_node *next = entry->next;
e345e267
KS
74
75 next->prev = prev;
76 prev->next = next;
0de33a61
KS
77
78 entry->prev = NULL;
79 entry->next = NULL;
e345e267
KS
80}
81
0de33a61 82/* return list entry which embeds this node */
8cd2e972 83static struct udev_list_entry *list_node_to_entry(struct udev_list_node *node)
0de33a61
KS
84{
85 char *list;
e345e267 86
0de33a61
KS
87 list = (char *)node;
88 list -= offsetof(struct udev_list_entry, node);
89 return (struct udev_list_entry *)list;
90}
91
869c9031 92void udev_list_init(struct udev *udev, struct udev_list *list, bool unique)
0de33a61 93{
869c9031
KS
94 memset(list, 0x00, sizeof(struct udev_list));
95 list->udev = udev;
96 list->unique = unique;
97 udev_list_node_init(&list->node);
0de33a61
KS
98}
99
869c9031
KS
100/* insert entry into a list as the last element */
101void udev_list_entry_append(struct udev_list_entry *new, struct udev_list *list)
1e78dcbe 102{
869c9031
KS
103 /* inserting before the list head make the node the last node in the list */
104 udev_list_node_insert_between(&new->node, list->node.prev, &list->node);
105 new->list = list;
1e78dcbe
KS
106}
107
0de33a61 108/* insert entry into a list, before a given existing entry */
1e78dcbe 109void udev_list_entry_insert_before(struct udev_list_entry *new, struct udev_list_entry *entry)
0de33a61 110{
3feeb77c 111 udev_list_node_insert_between(&new->node, entry->node.prev, &entry->node);
0de33a61
KS
112 new->list = entry->list;
113}
e345e267 114
869c9031
KS
115/* binary search in sorted array */
116static int list_search(struct udev_list *list, const char *name)
0de33a61 117{
869c9031
KS
118 unsigned int first, last;
119
120 first = 0;
121 last = list->entries_cur;
122 while (first < last) {
123 unsigned int i;
124 int cmp;
125
126 i = (first + last)/2;
127 cmp = strcmp(name, list->entries[i]->name);
128 if (cmp < 0)
129 last = i;
130 else if (cmp > 0)
131 first = i+1;
132 else
133 return i;
8958da13 134 }
e345e267 135
869c9031
KS
136 /* not found, return negative insertion-index+1 */
137 return -(first+1);
138}
139
140struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value)
141{
142 struct udev_list_entry *entry;
143 int i = 0;
144
145 if (list->unique) {
146 /* lookup existing name or insertion-index */
147 i = list_search(list, name);
148 if (i >= 0) {
149 entry = list->entries[i];
150
151 dbg(list->udev, "'%s' is already in the list\n", name);
152 free(entry->value);
153 if (value == NULL) {
154 entry->value = NULL;
155 dbg(list->udev, "'%s' value unset\n", name);
156 return entry;
157 }
158 entry->value = strdup(value);
159 if (entry->value == NULL)
160 return NULL;
161 dbg(list->udev, "'%s' value replaced with '%s'\n", name, value);
162 return entry;
e345e267 163 }
8958da13 164 }
e345e267 165
869c9031
KS
166 /* add new name */
167 entry = calloc(1, sizeof(struct udev_list_entry));
168 if (entry == NULL)
e345e267 169 return NULL;
869c9031
KS
170 entry->name = strdup(name);
171 if (entry->name == NULL) {
172 free(entry);
e345e267
KS
173 return NULL;
174 }
175 if (value != NULL) {
869c9031
KS
176 entry->value = strdup(value);
177 if (entry->value == NULL) {
178 free(entry->name);
179 free(entry);
e345e267
KS
180 return NULL;
181 }
182 }
869c9031
KS
183 udev_list_entry_append(entry, list);
184
185 if (list->unique) {
186 /* allocate or enlarge sorted array if needed */
187 if (list->entries_cur >= list->entries_max) {
188 unsigned int add;
189
190 add = list->entries_max;
191 if (add < 1)
192 add = 64;
193 list->entries = realloc(list->entries, (list->entries_max + add) * sizeof(struct udev_list_entry *));
194 if (list->entries == NULL) {
195 free(entry->name);
196 free(entry->value);
197 return NULL;
198 }
199 list->entries_max += add;
200 }
8958da13 201
869c9031
KS
202 /* insert into sorted array */
203 i = (-i)-1;
204 memmove(&list->entries[i+1], &list->entries[i],
205 (list->entries_cur - i) * sizeof(struct udev_list_entry *));
206 list->entries[i] = entry;
207 list->entries_cur++;
208 }
8958da13 209
869c9031
KS
210 dbg(list->udev, "'%s=%s' added\n", entry->name, entry->value);
211 return entry;
e345e267
KS
212}
213
1e78dcbe 214void udev_list_entry_delete(struct udev_list_entry *entry)
e345e267 215{
869c9031
KS
216 if (entry->list->entries != NULL) {
217 int i;
218 struct udev_list *list = entry->list;
219
220 /* remove entry from sorted array */
221 i = list_search(list, entry->name);
222 if (i >= 0) {
223 memmove(&list->entries[i], &list->entries[i+1],
224 ((list->entries_cur-1) - i) * sizeof(struct udev_list_entry *));
225 list->entries_cur--;
226 }
227 }
228
3feeb77c 229 udev_list_node_remove(&entry->node);
2c09b3b4
KS
230 free(entry->name);
231 free(entry->value);
232 free(entry);
e345e267
KS
233}
234
869c9031 235void udev_list_cleanup(struct udev_list *list)
e345e267 236{
0de33a61
KS
237 struct udev_list_entry *entry_loop;
238 struct udev_list_entry *entry_tmp;
239
869c9031
KS
240 free(list->entries);
241 list->entries = NULL;
242 list->entries_cur = 0;
243 list->entries_max = 0;
daa849db 244 udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
1e78dcbe 245 udev_list_entry_delete(entry_loop);
be7f7f57
KS
246}
247
869c9031 248struct udev_list_entry *udev_list_get_entry(struct udev_list *list)
e345e267 249{
869c9031 250 if (udev_list_node_is_empty(&list->node))
e345e267 251 return NULL;
869c9031 252 return list_node_to_entry(list->node.next);
e345e267
KS
253}
254
1e511322
KS
255/**
256 * udev_list_entry_get_next:
257 * @list_entry: current entry
258 *
259 * Returns: the next entry from the list, #NULL is no more entries are found.
260 */
666fcf03 261UDEV_EXPORT struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
e345e267 262{
8cd2e972 263 struct udev_list_node *next;
e345e267 264
0de33a61
KS
265 if (list_entry == NULL)
266 return NULL;
e345e267 267 next = list_entry->node.next;
0de33a61 268 /* empty list or no more entries */
869c9031 269 if (next == &list_entry->list->node)
e345e267 270 return NULL;
0de33a61 271 return list_node_to_entry(next);
e345e267
KS
272}
273
1e511322
KS
274/**
275 * udev_list_entry_get_by_name:
276 * @list_entry: current entry
277 * @name: name string to match
278 *
279 * Returns: the entry where @name matched, #NULL if no matching entry is found.
280 */
666fcf03 281UDEV_EXPORT struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name)
bc8184ed 282{
869c9031 283 int i;
bc8184ed 284
869c9031
KS
285 if (list_entry == NULL)
286 return NULL;
287
288 if (!list_entry->list->unique)
289 return NULL;
290
291 i = list_search(list_entry->list, name);
292 if (i < 0)
293 return NULL;
294 return list_entry->list->entries[i];
bc8184ed
KS
295}
296
1e511322
KS
297/**
298 * udev_list_entry_get_name:
299 * @list_entry: current entry
300 *
301 * Returns: the name string of this entry.
302 */
666fcf03 303UDEV_EXPORT const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
e345e267
KS
304{
305 if (list_entry == NULL)
306 return NULL;
307 return list_entry->name;
308}
309
1e511322
KS
310/**
311 * udev_list_entry_get_value:
312 * @list_entry: current entry
313 *
314 * Returns: the value string of this entry.
315 */
666fcf03 316UDEV_EXPORT const char *udev_list_entry_get_value(struct udev_list_entry *list_entry)
e345e267
KS
317{
318 if (list_entry == NULL)
319 return NULL;
320 return list_entry->value;
321}
df1dcc09 322
8958da13 323int udev_list_entry_get_num(struct udev_list_entry *list_entry)
df1dcc09
KS
324{
325 if (list_entry == NULL)
326 return -EINVAL;
8958da13 327 return list_entry->num;
df1dcc09
KS
328}
329
8958da13 330void udev_list_entry_set_num(struct udev_list_entry *list_entry, int num)
df1dcc09
KS
331{
332 if (list_entry == NULL)
333 return;
8958da13 334 list_entry->num = num;
df1dcc09 335}