File: | proplist.c |
Warning: | line 184, column 17 This statement is never executed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | |
3 | /*** |
4 | This file is part of libkanberra. |
5 | |
6 | Copyright 2008 Lennart Poettering |
7 | |
8 | libkanberra is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU Lesser General Public License as |
10 | published by the Free Software Foundation, either version 2.1 of the |
11 | License, or (at your option) any later version. |
12 | |
13 | libkanberra 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. |
17 | |
18 | You should have received a copy of the GNU Lesser General Public |
19 | License along with libkanberra. If not, see |
20 | <http://www.gnu.org/licenses/>. |
21 | ***/ |
22 | |
23 | #ifdef HAVE_CONFIG_H1 |
24 | #include <config.h> |
25 | #endif |
26 | |
27 | #include <stdarg.h> |
28 | |
29 | #include "kanberra.h" |
30 | #include "proplist.h" |
31 | #include "macro.h" |
32 | #include "malloc.h" |
33 | |
34 | static unsigned calc_hash(const char *c) { |
35 | unsigned hash = 0; |
36 | |
37 | for (; *c; c++) |
38 | hash = 31 * hash + (unsigned) *c; |
39 | |
40 | return hash; |
41 | } |
42 | |
43 | /** |
44 | * ka_proplist_create: |
45 | * @p: A pointer where to fill in a pointer for the new property list. |
46 | * |
47 | * Allocate a new empty property list. |
48 | * |
49 | * Returns: 0 on success, negative error code on error. |
50 | */ |
51 | int ka_proplist_create(ka_proplist **_p) { |
52 | ka_proplist *p; |
53 | ka_return_val_if_fail(_p, KA_ERROR_INVALID)do { if ((__builtin_expect((!(_p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "_p" , "proplist.c", 53, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
54 | |
55 | if (!(p = ka_new0(ka_proplist, 1)((ka_proplist*) calloc(1, (sizeof(ka_proplist)*(1)))))) |
56 | return KA_ERROR_OOM; |
57 | |
58 | if (!(p->mutex = ka_mutex_new())) { |
59 | ka_freefree(p); |
60 | return KA_ERROR_OOM; |
61 | } |
62 | |
63 | *_p = p; |
64 | |
65 | return KA_SUCCESS; |
66 | } |
67 | |
68 | static int _unset(ka_proplist *p, const char *key) { |
69 | ka_prop *prop, *nprop; |
70 | unsigned i; |
71 | |
72 | ka_return_val_if_fail(p, KA_ERROR_INVALID)do { if ((__builtin_expect((!(p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "p" , "proplist.c", 72, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
73 | ka_return_val_if_fail(key, KA_ERROR_INVALID)do { if ((__builtin_expect((!(key)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "key" , "proplist.c", 73, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
74 | |
75 | i = calc_hash(key) % N_HASHTABLE31; |
76 | |
77 | nprop = NULL((void*)0); |
78 | for (prop = p->prop_hashtable[i]; prop; nprop = prop, prop = prop->next_in_slot) |
79 | if (strcmp(prop->key, key) == 0) |
80 | break; |
81 | |
82 | if (prop) { |
83 | if (nprop) |
84 | nprop->next_in_slot = prop->next_in_slot; |
85 | else |
86 | p->prop_hashtable[i] = prop->next_in_slot; |
87 | |
88 | if (prop->prev_item) |
89 | prop->prev_item->next_item = prop->next_item; |
90 | else |
91 | p->first_item = prop->next_item; |
92 | |
93 | if (prop->next_item) |
94 | prop->next_item->prev_item = prop->prev_item; |
95 | |
96 | ka_freefree(prop->key); |
97 | ka_freefree(prop); |
98 | } |
99 | |
100 | return KA_SUCCESS; |
101 | } |
102 | |
103 | /** |
104 | * ka_proplist_sets: |
105 | * @p: The property list to add this key/value pair to |
106 | * @key: The key for this key/value pair |
107 | * @value: The value for this key/value pair |
108 | * |
109 | * Add a new string key/value pair to the property list. |
110 | * |
111 | * Returns: 0 on success, negative error code on error. |
112 | */ |
113 | |
114 | int ka_proplist_sets(ka_proplist *p, const char *key, const char *value) { |
115 | ka_return_val_if_fail(p, KA_ERROR_INVALID)do { if ((__builtin_expect((!(p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "p" , "proplist.c", 115, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
116 | ka_return_val_if_fail(key, KA_ERROR_INVALID)do { if ((__builtin_expect((!(key)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "key" , "proplist.c", 116, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
117 | ka_return_val_if_fail(value, KA_ERROR_INVALID)do { if ((__builtin_expect((!(value)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "value" , "proplist.c", 117, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
118 | |
119 | return ka_proplist_set(p, key, value, strlen(value)+1); |
120 | } |
121 | |
122 | /** |
123 | * ka_proplist_setf: |
124 | * @p: The property list to add this key/value pair to |
125 | * @key: The key for this key/value pair |
126 | * @format: The format string for the value for this key/value pair |
127 | * @...: The parameters for the format string |
128 | * |
129 | * Much like ka_proplist_sets(): add a new string key/value pair to |
130 | * the property list. Takes a standard C format string plus arguments |
131 | * and formats a string of it. |
132 | * |
133 | * Returns: 0 on success, negative error code on error. |
134 | */ |
135 | |
136 | int ka_proplist_setf(ka_proplist *p, const char *key, const char *format, ...) { |
137 | int ret; |
138 | char *k; |
139 | ka_prop *prop; |
140 | size_t size = 100; |
141 | unsigned h; |
142 | |
143 | ka_return_val_if_fail(p, KA_ERROR_INVALID)do { if ((__builtin_expect((!(p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "p" , "proplist.c", 143, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
144 | ka_return_val_if_fail(key, KA_ERROR_INVALID)do { if ((__builtin_expect((!(key)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "key" , "proplist.c", 144, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
145 | ka_return_val_if_fail(format, KA_ERROR_INVALID)do { if ((__builtin_expect((!(format)),0))) { if (ka_debug()) fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n" , "format" , "proplist.c", 145, __PRETTY_FUNCTION__); return ( KA_ERROR_INVALID); } } while((0)); |
146 | |
147 | if (!(k = ka_strdupstrdup(key))) |
148 | return KA_ERROR_OOM; |
149 | |
150 | for (;;) { |
151 | va_list ap; |
152 | int r; |
153 | |
154 | if (!(prop = ka_mallocmalloc(KA_ALIGN(sizeof(ka_prop))(ka_align(sizeof(ka_prop))) + size))) { |
155 | ka_freefree(k); |
156 | return KA_ERROR_OOM; |
157 | } |
158 | |
159 | |
160 | va_start(ap, format)__builtin_va_start(ap, format); |
161 | r = vsnprintf(KA_PROP_DATA(prop)((void*) ((char*) (prop) + (ka_align(sizeof(ka_prop))))), size, format, ap); |
162 | va_end(ap)__builtin_va_end(ap); |
163 | |
164 | ((char*) KA_PROP_DATA(prop)((void*) ((char*) (prop) + (ka_align(sizeof(ka_prop))))))[size-1] = 0; |
165 | |
166 | if (r > -1 && (size_t) r < size) { |
167 | prop->nbytes = (size_t) r+1; |
168 | break; |
169 | } |
170 | |
171 | if (r > -1) /* glibc 2.1 */ |
172 | size = (size_t) r+1; |
173 | else /* glibc 2.0 */ |
174 | size *= 2; |
175 | |
176 | ka_freefree(prop); |
177 | } |
178 | |
179 | prop->key = k; |
180 | |
181 | ka_mutex_lock(p->mutex); |
182 | |
183 | if ((ret = _unset(p, key)) < 0) { |
184 | ka_freefree(prop); |
This statement is never executed | |
185 | ka_freefree(k); |
186 | goto finish; |
187 | } |
188 | |
189 | h = calc_hash(key) % N_HASHTABLE31; |
190 | |
191 | prop->next_in_slot = p->prop_hashtable[h]; |
192 | p->prop_hashtable[h] = prop; |
193 | |
194 | prop->prev_item = NULL((void*)0); |
195 | if ((prop->next_item = p->first_item)) |
196 | prop->next_item->prev_item = prop; |
197 | p->first_item = prop; |
198 | |
199 | finish: |
200 | |
201 | ka_mutex_unlock(p->mutex); |
202 | |
203 | return ret; |
204 | } |
205 | |
206 | /** |
207 | * ka_proplist_set: |
208 | * @p: The property list to add this key/value pair to |
209 | * @key: The key for this key/value pair |
210 | * @data: The binary value for this key value pair |
211 | * @nbytes: The size of thebinary value for this key value pair. |
212 | * |
213 | * Add a new binary key/value pair to the property list. |
214 | * |
215 | * Returns: 0 on success, negative error code on error. |
216 | */ |
217 | |
218 | int ka_proplist_set(ka_proplist *p, const char *key, const void *data, size_t nbytes) { |
219 | int ret; |
220 | char *k; |
221 | ka_prop *prop; |
222 | unsigned h; |
223 | |
224 | ka_return_val_if_fail(p, KA_ERROR_INVALID)do { if ((__builtin_expect((!(p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "p" , "proplist.c", 224, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
225 | ka_return_val_if_fail(key, KA_ERROR_INVALID)do { if ((__builtin_expect((!(key)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "key" , "proplist.c", 225, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
226 | ka_return_val_if_fail(!nbytes || data, KA_ERROR_INVALID)do { if ((__builtin_expect((!(!nbytes || data)),0))) { if (ka_debug ()) fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n" , "!nbytes || data" , "proplist.c", 226, __PRETTY_FUNCTION__) ; return (KA_ERROR_INVALID); } } while((0)); |
227 | |
228 | if (!(k = ka_strdupstrdup(key))) |
229 | return KA_ERROR_OOM; |
230 | |
231 | if (!(prop = ka_mallocmalloc(KA_ALIGN(sizeof(ka_prop))(ka_align(sizeof(ka_prop))) + nbytes))) { |
232 | ka_freefree(k); |
233 | return KA_ERROR_OOM; |
234 | } |
235 | |
236 | prop->key = k; |
237 | prop->nbytes = nbytes; |
238 | memcpy(KA_PROP_DATA(prop)((void*) ((char*) (prop) + (ka_align(sizeof(ka_prop))))), data, nbytes); |
239 | |
240 | ka_mutex_lock(p->mutex); |
241 | |
242 | if ((ret = _unset(p, key)) < 0) { |
243 | ka_freefree(prop); |
244 | ka_freefree(k); |
245 | goto finish; |
246 | } |
247 | |
248 | h = calc_hash(key) % N_HASHTABLE31; |
249 | |
250 | prop->next_in_slot = p->prop_hashtable[h]; |
251 | p->prop_hashtable[h] = prop; |
252 | |
253 | prop->prev_item = NULL((void*)0); |
254 | if ((prop->next_item = p->first_item)) |
255 | prop->next_item->prev_item = prop; |
256 | p->first_item = prop; |
257 | |
258 | finish: |
259 | |
260 | ka_mutex_unlock(p->mutex); |
261 | |
262 | return ret; |
263 | } |
264 | |
265 | /* Not exported, not self-locking */ |
266 | ka_prop* ka_proplist_get_unlocked(ka_proplist *p, const char *key) { |
267 | ka_prop *prop; |
268 | unsigned i; |
269 | |
270 | ka_return_val_if_fail(p, NULL)do { if ((__builtin_expect((!(p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "p" , "proplist.c", 270, __PRETTY_FUNCTION__); return (((void*)0 )); } } while((0)); |
271 | ka_return_val_if_fail(key, NULL)do { if ((__builtin_expect((!(key)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "key" , "proplist.c", 271, __PRETTY_FUNCTION__); return (((void*)0 )); } } while((0)); |
272 | |
273 | i = calc_hash(key) % N_HASHTABLE31; |
274 | |
275 | for (prop = p->prop_hashtable[i]; prop; prop = prop->next_in_slot) |
276 | if (strcmp(prop->key, key) == 0) |
277 | return prop; |
278 | |
279 | return NULL((void*)0); |
280 | } |
281 | |
282 | /* Not exported, not self-locking */ |
283 | const char* ka_proplist_gets_unlocked(ka_proplist *p, const char *key) { |
284 | ka_prop *prop; |
285 | |
286 | ka_return_val_if_fail(p, NULL)do { if ((__builtin_expect((!(p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "p" , "proplist.c", 286, __PRETTY_FUNCTION__); return (((void*)0 )); } } while((0)); |
287 | ka_return_val_if_fail(key, NULL)do { if ((__builtin_expect((!(key)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "key" , "proplist.c", 287, __PRETTY_FUNCTION__); return (((void*)0 )); } } while((0)); |
288 | |
289 | if (!(prop = ka_proplist_get_unlocked(p, key))) |
290 | return NULL((void*)0); |
291 | |
292 | if (!memchr(KA_PROP_DATA(prop)((void*) ((char*) (prop) + (ka_align(sizeof(ka_prop))))), 0, prop->nbytes)) |
293 | return NULL((void*)0); |
294 | |
295 | return KA_PROP_DATA(prop)((void*) ((char*) (prop) + (ka_align(sizeof(ka_prop))))); |
296 | } |
297 | |
298 | /** |
299 | * ka_proplist_destroy: |
300 | * @p: The property list to destroy |
301 | * |
302 | * Destroys a property list that was created with ka_proplist_create() earlier. |
303 | * |
304 | * Returns: 0 on success, negative error code on error. |
305 | */ |
306 | |
307 | int ka_proplist_destroy(ka_proplist *p) { |
308 | ka_prop *prop, *nprop; |
309 | |
310 | ka_return_val_if_fail(p, KA_ERROR_INVALID)do { if ((__builtin_expect((!(p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "p" , "proplist.c", 310, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
311 | |
312 | for (prop = p->first_item; prop; prop = nprop) { |
313 | nprop = prop->next_item; |
314 | ka_freefree(prop->key); |
315 | ka_freefree(prop); |
316 | } |
317 | |
318 | ka_mutex_free(p->mutex); |
319 | |
320 | ka_freefree(p); |
321 | |
322 | return KA_SUCCESS; |
323 | } |
324 | |
325 | static int merge_into(ka_proplist *a, ka_proplist *b) { |
326 | int ret = KA_SUCCESS; |
327 | ka_prop *prop; |
328 | |
329 | ka_return_val_if_fail(a, KA_ERROR_INVALID)do { if ((__builtin_expect((!(a)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "a" , "proplist.c", 329, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
330 | ka_return_val_if_fail(b, KA_ERROR_INVALID)do { if ((__builtin_expect((!(b)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "b" , "proplist.c", 330, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
331 | |
332 | ka_mutex_lock(b->mutex); |
333 | |
334 | for (prop = b->first_item; prop; prop = prop->next_item) |
335 | if ((ret = ka_proplist_set(a, prop->key, KA_PROP_DATA(prop)((void*) ((char*) (prop) + (ka_align(sizeof(ka_prop))))), prop->nbytes)) < 0) |
336 | break; |
337 | |
338 | ka_mutex_unlock(b->mutex); |
339 | |
340 | return ret; |
341 | } |
342 | |
343 | int ka_proplist_merge(ka_proplist **_a, ka_proplist *b, ka_proplist *c) { |
344 | ka_proplist *a; |
345 | int ret; |
346 | |
347 | ka_return_val_if_fail(_a, KA_ERROR_INVALID)do { if ((__builtin_expect((!(_a)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "_a" , "proplist.c", 347, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
348 | ka_return_val_if_fail(b, KA_ERROR_INVALID)do { if ((__builtin_expect((!(b)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "b" , "proplist.c", 348, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
349 | ka_return_val_if_fail(c, KA_ERROR_INVALID)do { if ((__builtin_expect((!(c)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "c" , "proplist.c", 349, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
350 | |
351 | if ((ret = ka_proplist_create(&a)) < 0) |
352 | return ret; |
353 | |
354 | if ((ret = merge_into(a, b)) < 0 || |
355 | (ret = merge_into(a, c)) < 0) { |
356 | ka_proplist_destroy(a); |
357 | return ret; |
358 | } |
359 | |
360 | *_a = a; |
361 | return KA_SUCCESS; |
362 | } |
363 | |
364 | ka_bool_t ka_proplist_contains(ka_proplist *p, const char *key) { |
365 | ka_bool_t b; |
366 | |
367 | ka_return_val_if_fail(p, FALSE)do { if ((__builtin_expect((!(p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "p" , "proplist.c", 367, __PRETTY_FUNCTION__); return ((0)); } } while((0)); |
368 | ka_return_val_if_fail(key, FALSE)do { if ((__builtin_expect((!(key)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "key" , "proplist.c", 368, __PRETTY_FUNCTION__); return ((0)); } } while((0)); |
369 | |
370 | ka_mutex_lock(p->mutex); |
371 | b = !!ka_proplist_get_unlocked(p, key); |
372 | ka_mutex_unlock(p->mutex); |
373 | |
374 | return b; |
375 | } |
376 | |
377 | int ka_proplist_merge_ap(ka_proplist *p, va_list ap) { |
378 | int ret; |
379 | |
380 | ka_return_val_if_fail(p, KA_ERROR_INVALID)do { if ((__builtin_expect((!(p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "p" , "proplist.c", 380, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
381 | |
382 | for (;;) { |
383 | const char *key, *value; |
384 | |
385 | if (!(key = va_arg(ap, const char*)__builtin_va_arg(ap, const char*))) |
386 | break; |
387 | |
388 | if (!(value = va_arg(ap, const char*)__builtin_va_arg(ap, const char*))) |
389 | return KA_ERROR_INVALID; |
390 | |
391 | if ((ret = ka_proplist_sets(p, key, value)) < 0) |
392 | return ret; |
393 | } |
394 | |
395 | return KA_SUCCESS; |
396 | } |
397 | |
398 | int ka_proplist_from_ap(ka_proplist **_p, va_list ap) { |
399 | int ret; |
400 | ka_proplist *p; |
401 | |
402 | ka_return_val_if_fail(_p, KA_ERROR_INVALID)do { if ((__builtin_expect((!(_p)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "_p" , "proplist.c", 402, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
403 | |
404 | if ((ret = ka_proplist_create(&p)) < 0) |
405 | return ret; |
406 | |
407 | if ((ret = ka_proplist_merge_ap(p, ap)) < 0) |
408 | goto fail; |
409 | |
410 | *_p = p; |
411 | |
412 | return KA_SUCCESS; |
413 | |
414 | fail: |
415 | ka_assert_se(ka_proplist_destroy(p) == KA_SUCCESS)do { if ((__builtin_expect((!(ka_proplist_destroy(p) == KA_SUCCESS )),0))) { fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s(). Aborting.\n" , "ka_proplist_destroy(p) == KA_SUCCESS" , "proplist.c", 415, __PRETTY_FUNCTION__); abort(); } } while ((0)); |
416 | |
417 | return ret; |
418 | } |