Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / common / lib / libprop / prop_dictionary.c
blob3aece35827e6d8de53fea63efcae9acdf6827a86
1 /* $NetBSD: prop_dictionary.c,v 1.34 2009/01/03 18:31:33 pooka Exp $ */
3 /*-
4 * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <prop/prop_array.h>
33 #include <prop/prop_dictionary.h>
34 #include <prop/prop_string.h>
35 #include "prop_object_impl.h"
36 #include "prop_rb_impl.h"
38 #if !defined(_KERNEL) && !defined(_STANDALONE)
39 #include <errno.h>
40 #endif
43 * We implement these like arrays, but we keep them sorted by key.
44 * This allows us to binary-search as well as keep externalized output
45 * sane-looking for human eyes.
48 #define EXPAND_STEP 16
51 * prop_dictionary_keysym_t is allocated with space at the end to hold the
52 * key. This must be a regular object so that we can maintain sane iterator
53 * semantics -- we don't want to require that the caller release the result
54 * of prop_object_iterator_next().
56 * We'd like to have some small'ish keysym objects for up-to-16 characters
57 * in a key, some for up-to-32 characters in a key, and then a final bucket
58 * for up-to-128 characters in a key (not including NUL). Keys longer than
59 * 128 characters are not allowed.
61 struct _prop_dictionary_keysym {
62 struct _prop_object pdk_obj;
63 size_t pdk_size;
64 struct rb_node pdk_link;
65 char pdk_key[1];
66 /* actually variable length */
69 #define RBNODE_TO_PDK(n) \
70 ((struct _prop_dictionary_keysym *) \
71 ((uintptr_t)n - offsetof(struct _prop_dictionary_keysym, pdk_link)))
73 /* pdk_key[1] takes care of the NUL */
74 #define PDK_SIZE_16 (sizeof(struct _prop_dictionary_keysym) + 16)
75 #define PDK_SIZE_32 (sizeof(struct _prop_dictionary_keysym) + 32)
76 #define PDK_SIZE_128 (sizeof(struct _prop_dictionary_keysym) + 128)
78 #define PDK_MAXKEY 128
80 _PROP_POOL_INIT(_prop_dictionary_keysym16_pool, PDK_SIZE_16, "pdict16")
81 _PROP_POOL_INIT(_prop_dictionary_keysym32_pool, PDK_SIZE_32, "pdict32")
82 _PROP_POOL_INIT(_prop_dictionary_keysym128_pool, PDK_SIZE_128, "pdict128")
84 struct _prop_dict_entry {
85 prop_dictionary_keysym_t pde_key;
86 prop_object_t pde_objref;
89 struct _prop_dictionary {
90 struct _prop_object pd_obj;
91 _PROP_RWLOCK_DECL(pd_rwlock)
92 struct _prop_dict_entry *pd_array;
93 unsigned int pd_capacity;
94 unsigned int pd_count;
95 int pd_flags;
97 uint32_t pd_version;
100 #define PD_F_IMMUTABLE 0x01 /* dictionary is immutable */
102 _PROP_POOL_INIT(_prop_dictionary_pool, sizeof(struct _prop_dictionary),
103 "propdict")
104 _PROP_MALLOC_DEFINE(M_PROP_DICT, "prop dictionary",
105 "property dictionary container object")
107 static _prop_object_free_rv_t
108 _prop_dictionary_free(prop_stack_t, prop_object_t *);
109 static void _prop_dictionary_emergency_free(prop_object_t);
110 static bool _prop_dictionary_externalize(
111 struct _prop_object_externalize_context *,
112 void *);
113 static _prop_object_equals_rv_t
114 _prop_dictionary_equals(prop_object_t, prop_object_t,
115 void **, void **,
116 prop_object_t *, prop_object_t *);
117 static void _prop_dictionary_equals_finish(prop_object_t, prop_object_t);
118 static prop_object_iterator_t
119 _prop_dictionary_iterator_locked(prop_dictionary_t);
120 static prop_object_t
121 _prop_dictionary_iterator_next_object_locked(void *);
122 static prop_object_t
123 _prop_dictionary_get_keysym(prop_dictionary_t,
124 prop_dictionary_keysym_t, bool);
125 static prop_object_t
126 _prop_dictionary_get(prop_dictionary_t, const char *, bool);
128 static void _prop_dictionary_lock(void);
129 static void _prop_dictionary_unlock(void);
131 static const struct _prop_object_type _prop_object_type_dictionary = {
132 .pot_type = PROP_TYPE_DICTIONARY,
133 .pot_free = _prop_dictionary_free,
134 .pot_emergency_free = _prop_dictionary_emergency_free,
135 .pot_extern = _prop_dictionary_externalize,
136 .pot_equals = _prop_dictionary_equals,
137 .pot_equals_finish = _prop_dictionary_equals_finish,
138 .pot_lock = _prop_dictionary_lock,
139 .pot_unlock = _prop_dictionary_unlock,
142 static _prop_object_free_rv_t
143 _prop_dict_keysym_free(prop_stack_t, prop_object_t *);
144 static bool _prop_dict_keysym_externalize(
145 struct _prop_object_externalize_context *,
146 void *);
147 static _prop_object_equals_rv_t
148 _prop_dict_keysym_equals(prop_object_t, prop_object_t,
149 void **, void **,
150 prop_object_t *, prop_object_t *);
152 static const struct _prop_object_type _prop_object_type_dict_keysym = {
153 .pot_type = PROP_TYPE_DICT_KEYSYM,
154 .pot_free = _prop_dict_keysym_free,
155 .pot_extern = _prop_dict_keysym_externalize,
156 .pot_equals = _prop_dict_keysym_equals,
159 #define prop_object_is_dictionary(x) \
160 ((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_dictionary)
161 #define prop_object_is_dictionary_keysym(x) \
162 ((x) != NULL && (x)->pdk_obj.po_type == &_prop_object_type_dict_keysym)
164 #define prop_dictionary_is_immutable(x) \
165 (((x)->pd_flags & PD_F_IMMUTABLE) != 0)
167 struct _prop_dictionary_iterator {
168 struct _prop_object_iterator pdi_base;
169 unsigned int pdi_index;
173 * Dictionary key symbols are immutable, and we are likely to have many
174 * duplicated key symbols. So, to save memory, we unique'ify key symbols
175 * so we only have to have one copy of each string.
178 static int
179 _prop_dict_keysym_rb_compare_nodes(const struct rb_node *n1,
180 const struct rb_node *n2)
182 const prop_dictionary_keysym_t pdk1 = RBNODE_TO_PDK(n1);
183 const prop_dictionary_keysym_t pdk2 = RBNODE_TO_PDK(n2);
185 return (strcmp(pdk1->pdk_key, pdk2->pdk_key));
188 static int
189 _prop_dict_keysym_rb_compare_key(const struct rb_node *n,
190 const void *v)
192 const prop_dictionary_keysym_t pdk = RBNODE_TO_PDK(n);
193 const char *cp = v;
195 return (strcmp(pdk->pdk_key, cp));
198 static const struct rb_tree_ops _prop_dict_keysym_rb_tree_ops = {
199 .rbto_compare_nodes = _prop_dict_keysym_rb_compare_nodes,
200 .rbto_compare_key = _prop_dict_keysym_rb_compare_key,
203 static struct rb_tree _prop_dict_keysym_tree;
205 _PROP_ONCE_DECL(_prop_dict_init_once)
206 _PROP_MUTEX_DECL_STATIC(_prop_dict_keysym_tree_mutex)
208 static int
209 _prop_dict_init(void)
212 _PROP_MUTEX_INIT(_prop_dict_keysym_tree_mutex);
213 _prop_rb_tree_init(&_prop_dict_keysym_tree,
214 &_prop_dict_keysym_rb_tree_ops);
215 return 0;
218 static void
219 _prop_dict_keysym_put(prop_dictionary_keysym_t pdk)
222 if (pdk->pdk_size <= PDK_SIZE_16)
223 _PROP_POOL_PUT(_prop_dictionary_keysym16_pool, pdk);
224 else if (pdk->pdk_size <= PDK_SIZE_32)
225 _PROP_POOL_PUT(_prop_dictionary_keysym32_pool, pdk);
226 else {
227 _PROP_ASSERT(pdk->pdk_size <= PDK_SIZE_128);
228 _PROP_POOL_PUT(_prop_dictionary_keysym128_pool, pdk);
232 /* ARGSUSED */
233 static _prop_object_free_rv_t
234 _prop_dict_keysym_free(prop_stack_t stack, prop_object_t *obj)
236 prop_dictionary_keysym_t pdk = *obj;
238 _prop_rb_tree_remove_node(&_prop_dict_keysym_tree, &pdk->pdk_link);
239 _prop_dict_keysym_put(pdk);
241 return _PROP_OBJECT_FREE_DONE;
244 static bool
245 _prop_dict_keysym_externalize(struct _prop_object_externalize_context *ctx,
246 void *v)
248 prop_dictionary_keysym_t pdk = v;
250 /* We externalize these as strings, and they're never empty. */
252 _PROP_ASSERT(pdk->pdk_key[0] != '\0');
254 if (_prop_object_externalize_start_tag(ctx, "string") == false ||
255 _prop_object_externalize_append_encoded_cstring(ctx,
256 pdk->pdk_key) == false ||
257 _prop_object_externalize_end_tag(ctx, "string") == false)
258 return (false);
260 return (true);
263 /* ARGSUSED */
264 static _prop_object_equals_rv_t
265 _prop_dict_keysym_equals(prop_object_t v1, prop_object_t v2,
266 void **stored_pointer1, void **stored_pointer2,
267 prop_object_t *next_obj1, prop_object_t *next_obj2)
269 prop_dictionary_keysym_t pdk1 = v1;
270 prop_dictionary_keysym_t pdk2 = v2;
273 * There is only ever one copy of a keysym at any given time,
274 * so we can reduce this to a simple pointer equality check.
276 if (pdk1 == pdk2)
277 return _PROP_OBJECT_EQUALS_TRUE;
278 else
279 return _PROP_OBJECT_EQUALS_FALSE;
282 static prop_dictionary_keysym_t
283 _prop_dict_keysym_alloc(const char *key)
285 prop_dictionary_keysym_t opdk, pdk;
286 const struct rb_node *n;
287 size_t size;
288 bool rv;
290 _PROP_ONCE_RUN(_prop_dict_init_once, _prop_dict_init);
293 * Check to see if this already exists in the tree. If it does,
294 * we just retain it and return it.
296 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
297 n = _prop_rb_tree_find(&_prop_dict_keysym_tree, key);
298 if (n != NULL) {
299 opdk = RBNODE_TO_PDK(n);
300 prop_object_retain(opdk);
301 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
302 return (opdk);
304 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
307 * Not in the tree. Create it now.
310 size = sizeof(*pdk) + strlen(key) /* pdk_key[1] covers the NUL */;
312 if (size <= PDK_SIZE_16)
313 pdk = _PROP_POOL_GET(_prop_dictionary_keysym16_pool);
314 else if (size <= PDK_SIZE_32)
315 pdk = _PROP_POOL_GET(_prop_dictionary_keysym32_pool);
316 else if (size <= PDK_SIZE_128)
317 pdk = _PROP_POOL_GET(_prop_dictionary_keysym128_pool);
318 else
319 pdk = NULL; /* key too long */
321 if (pdk == NULL)
322 return (NULL);
324 _prop_object_init(&pdk->pdk_obj, &_prop_object_type_dict_keysym);
326 strcpy(pdk->pdk_key, key);
327 pdk->pdk_size = size;
330 * We dropped the mutex when we allocated the new object, so
331 * we have to check again if it is in the tree.
333 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
334 n = _prop_rb_tree_find(&_prop_dict_keysym_tree, key);
335 if (n != NULL) {
336 opdk = RBNODE_TO_PDK(n);
337 prop_object_retain(opdk);
338 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
339 _prop_dict_keysym_put(pdk);
340 return (opdk);
342 rv = _prop_rb_tree_insert_node(&_prop_dict_keysym_tree, &pdk->pdk_link);
343 _PROP_ASSERT(rv == true);
344 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
345 return (pdk);
348 static _prop_object_free_rv_t
349 _prop_dictionary_free(prop_stack_t stack, prop_object_t *obj)
351 prop_dictionary_t pd = *obj;
352 prop_dictionary_keysym_t pdk;
353 prop_object_t po;
355 _PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
356 _PROP_ASSERT((pd->pd_capacity == 0 && pd->pd_array == NULL) ||
357 (pd->pd_capacity != 0 && pd->pd_array != NULL));
359 /* The empty dictorinary is easy, handle that first. */
360 if (pd->pd_count == 0) {
361 if (pd->pd_array != NULL)
362 _PROP_FREE(pd->pd_array, M_PROP_DICT);
364 _PROP_RWLOCK_DESTROY(pd->pd_rwlock);
366 _PROP_POOL_PUT(_prop_dictionary_pool, pd);
368 return (_PROP_OBJECT_FREE_DONE);
371 po = pd->pd_array[pd->pd_count - 1].pde_objref;
372 _PROP_ASSERT(po != NULL);
374 if (stack == NULL) {
376 * If we are in emergency release mode,
377 * just let caller recurse down.
379 *obj = po;
380 return (_PROP_OBJECT_FREE_FAILED);
383 /* Otherwise, try to push the current object on the stack. */
384 if (!_prop_stack_push(stack, pd, NULL, NULL, NULL)) {
385 /* Push failed, entering emergency release mode. */
386 return (_PROP_OBJECT_FREE_FAILED);
388 /* Object pushed on stack, caller will release it. */
389 --pd->pd_count;
390 pdk = pd->pd_array[pd->pd_count].pde_key;
391 _PROP_ASSERT(pdk != NULL);
393 prop_object_release(pdk);
395 *obj = po;
396 return (_PROP_OBJECT_FREE_RECURSE);
400 static void
401 _prop_dictionary_lock(void)
404 /* XXX: once necessary or paranoia? */
405 _PROP_ONCE_RUN(_prop_dict_init_once, _prop_dict_init);
406 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
409 static void
410 _prop_dictionary_unlock(void)
412 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
415 static void
416 _prop_dictionary_emergency_free(prop_object_t obj)
418 prop_dictionary_t pd = obj;
419 prop_dictionary_keysym_t pdk;
421 _PROP_ASSERT(pd->pd_count != 0);
422 --pd->pd_count;
424 pdk = pd->pd_array[pd->pd_count].pde_key;
425 _PROP_ASSERT(pdk != NULL);
426 prop_object_release(pdk);
429 static bool
430 _prop_dictionary_externalize(struct _prop_object_externalize_context *ctx,
431 void *v)
433 prop_dictionary_t pd = v;
434 prop_dictionary_keysym_t pdk;
435 struct _prop_object *po;
436 prop_object_iterator_t pi;
437 unsigned int i;
438 bool rv = false;
440 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
442 if (pd->pd_count == 0) {
443 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
444 return (_prop_object_externalize_empty_tag(ctx, "dict"));
447 if (_prop_object_externalize_start_tag(ctx, "dict") == false ||
448 _prop_object_externalize_append_char(ctx, '\n') == false)
449 goto out;
451 pi = _prop_dictionary_iterator_locked(pd);
452 if (pi == NULL)
453 goto out;
455 ctx->poec_depth++;
456 _PROP_ASSERT(ctx->poec_depth != 0);
458 while ((pdk = _prop_dictionary_iterator_next_object_locked(pi))
459 != NULL) {
460 po = _prop_dictionary_get_keysym(pd, pdk, true);
461 if (po == NULL ||
462 _prop_object_externalize_start_tag(ctx, "key") == false ||
463 _prop_object_externalize_append_encoded_cstring(ctx,
464 pdk->pdk_key) == false ||
465 _prop_object_externalize_end_tag(ctx, "key") == false ||
466 (*po->po_type->pot_extern)(ctx, po) == false) {
467 prop_object_iterator_release(pi);
468 goto out;
472 prop_object_iterator_release(pi);
474 ctx->poec_depth--;
475 for (i = 0; i < ctx->poec_depth; i++) {
476 if (_prop_object_externalize_append_char(ctx, '\t') == false)
477 goto out;
479 if (_prop_object_externalize_end_tag(ctx, "dict") == false)
480 goto out;
482 rv = true;
484 out:
485 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
486 return (rv);
489 /* ARGSUSED */
490 static _prop_object_equals_rv_t
491 _prop_dictionary_equals(prop_object_t v1, prop_object_t v2,
492 void **stored_pointer1, void **stored_pointer2,
493 prop_object_t *next_obj1, prop_object_t *next_obj2)
495 prop_dictionary_t dict1 = v1;
496 prop_dictionary_t dict2 = v2;
497 uintptr_t idx;
498 _prop_object_equals_rv_t rv = _PROP_OBJECT_EQUALS_FALSE;
500 if (dict1 == dict2)
501 return (_PROP_OBJECT_EQUALS_TRUE);
503 _PROP_ASSERT(*stored_pointer1 == *stored_pointer2);
505 idx = (uintptr_t)*stored_pointer1;
507 if (idx == 0) {
508 if ((uintptr_t)dict1 < (uintptr_t)dict2) {
509 _PROP_RWLOCK_RDLOCK(dict1->pd_rwlock);
510 _PROP_RWLOCK_RDLOCK(dict2->pd_rwlock);
511 } else {
512 _PROP_RWLOCK_RDLOCK(dict2->pd_rwlock);
513 _PROP_RWLOCK_RDLOCK(dict1->pd_rwlock);
517 if (dict1->pd_count != dict2->pd_count)
518 goto out;
520 if (idx == dict1->pd_count) {
521 rv = _PROP_OBJECT_EQUALS_TRUE;
522 goto out;
525 _PROP_ASSERT(idx < dict1->pd_count);
527 *stored_pointer1 = (void *)(idx + 1);
528 *stored_pointer2 = (void *)(idx + 1);
530 *next_obj1 = &dict1->pd_array[idx].pde_objref;
531 *next_obj2 = &dict2->pd_array[idx].pde_objref;
533 if (!prop_dictionary_keysym_equals(dict1->pd_array[idx].pde_key,
534 dict2->pd_array[idx].pde_key))
535 goto out;
537 return (_PROP_OBJECT_EQUALS_RECURSE);
539 out:
540 _PROP_RWLOCK_UNLOCK(dict1->pd_rwlock);
541 _PROP_RWLOCK_UNLOCK(dict2->pd_rwlock);
542 return (rv);
545 static void
546 _prop_dictionary_equals_finish(prop_object_t v1, prop_object_t v2)
548 _PROP_RWLOCK_UNLOCK(((prop_dictionary_t)v1)->pd_rwlock);
549 _PROP_RWLOCK_UNLOCK(((prop_dictionary_t)v2)->pd_rwlock);
552 static prop_dictionary_t
553 _prop_dictionary_alloc(unsigned int capacity)
555 prop_dictionary_t pd;
556 struct _prop_dict_entry *array;
558 if (capacity != 0) {
559 array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
560 if (array == NULL)
561 return (NULL);
562 } else
563 array = NULL;
565 pd = _PROP_POOL_GET(_prop_dictionary_pool);
566 if (pd != NULL) {
567 _prop_object_init(&pd->pd_obj, &_prop_object_type_dictionary);
569 _PROP_RWLOCK_INIT(pd->pd_rwlock);
570 pd->pd_array = array;
571 pd->pd_capacity = capacity;
572 pd->pd_count = 0;
573 pd->pd_flags = 0;
575 pd->pd_version = 0;
576 } else if (array != NULL)
577 _PROP_FREE(array, M_PROP_DICT);
579 return (pd);
582 static bool
583 _prop_dictionary_expand(prop_dictionary_t pd, unsigned int capacity)
585 struct _prop_dict_entry *array, *oarray;
588 * Dictionary must be WRITE-LOCKED.
591 oarray = pd->pd_array;
593 array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
594 if (array == NULL)
595 return (false);
596 if (oarray != NULL)
597 memcpy(array, oarray, pd->pd_capacity * sizeof(*array));
598 pd->pd_array = array;
599 pd->pd_capacity = capacity;
601 if (oarray != NULL)
602 _PROP_FREE(oarray, M_PROP_DICT);
604 return (true);
607 static prop_object_t
608 _prop_dictionary_iterator_next_object_locked(void *v)
610 struct _prop_dictionary_iterator *pdi = v;
611 prop_dictionary_t pd = pdi->pdi_base.pi_obj;
612 prop_dictionary_keysym_t pdk = NULL;
614 _PROP_ASSERT(prop_object_is_dictionary(pd));
616 if (pd->pd_version != pdi->pdi_base.pi_version)
617 goto out; /* dictionary changed during iteration */
619 _PROP_ASSERT(pdi->pdi_index <= pd->pd_count);
621 if (pdi->pdi_index == pd->pd_count)
622 goto out; /* we've iterated all objects */
624 pdk = pd->pd_array[pdi->pdi_index].pde_key;
625 pdi->pdi_index++;
627 out:
628 return (pdk);
631 static prop_object_t
632 _prop_dictionary_iterator_next_object(void *v)
634 struct _prop_dictionary_iterator *pdi = v;
635 prop_dictionary_t pd __unused = pdi->pdi_base.pi_obj;
636 prop_dictionary_keysym_t pdk;
638 _PROP_ASSERT(prop_object_is_dictionary(pd));
640 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
641 pdk = _prop_dictionary_iterator_next_object_locked(pdi);
642 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
643 return (pdk);
646 static void
647 _prop_dictionary_iterator_reset_locked(void *v)
649 struct _prop_dictionary_iterator *pdi = v;
650 prop_dictionary_t pd = pdi->pdi_base.pi_obj;
652 _PROP_ASSERT(prop_object_is_dictionary(pd));
654 pdi->pdi_index = 0;
655 pdi->pdi_base.pi_version = pd->pd_version;
658 static void
659 _prop_dictionary_iterator_reset(void *v)
661 struct _prop_dictionary_iterator *pdi = v;
662 prop_dictionary_t pd __unused = pdi->pdi_base.pi_obj;
664 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
665 _prop_dictionary_iterator_reset_locked(pdi);
666 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
670 * prop_dictionary_create --
671 * Create a dictionary.
673 prop_dictionary_t
674 prop_dictionary_create(void)
677 return (_prop_dictionary_alloc(0));
681 * prop_dictionary_create_with_capacity --
682 * Create a dictionary with the capacity to store N objects.
684 prop_dictionary_t
685 prop_dictionary_create_with_capacity(unsigned int capacity)
688 return (_prop_dictionary_alloc(capacity));
692 * prop_dictionary_copy --
693 * Copy a dictionary. The new dictionary has an initial capacity equal
694 * to the number of objects stored int the original dictionary. The new
695 * dictionary contains refrences to the original dictionary's objects,
696 * not copies of those objects (i.e. a shallow copy).
698 prop_dictionary_t
699 prop_dictionary_copy(prop_dictionary_t opd)
701 prop_dictionary_t pd;
702 prop_dictionary_keysym_t pdk;
703 prop_object_t po;
704 unsigned int idx;
706 if (! prop_object_is_dictionary(opd))
707 return (NULL);
709 _PROP_RWLOCK_RDLOCK(opd->pd_rwlock);
711 pd = _prop_dictionary_alloc(opd->pd_count);
712 if (pd != NULL) {
713 for (idx = 0; idx < opd->pd_count; idx++) {
714 pdk = opd->pd_array[idx].pde_key;
715 po = opd->pd_array[idx].pde_objref;
717 prop_object_retain(pdk);
718 prop_object_retain(po);
720 pd->pd_array[idx].pde_key = pdk;
721 pd->pd_array[idx].pde_objref = po;
723 pd->pd_count = opd->pd_count;
724 pd->pd_flags = opd->pd_flags;
726 _PROP_RWLOCK_UNLOCK(opd->pd_rwlock);
727 return (pd);
731 * prop_dictionary_copy_mutable --
732 * Like prop_dictionary_copy(), but the resulting dictionary is
733 * mutable.
735 prop_dictionary_t
736 prop_dictionary_copy_mutable(prop_dictionary_t opd)
738 prop_dictionary_t pd;
740 if (! prop_object_is_dictionary(opd))
741 return (NULL);
743 pd = prop_dictionary_copy(opd);
744 if (pd != NULL)
745 pd->pd_flags &= ~PD_F_IMMUTABLE;
747 return (pd);
751 * prop_dictionary_make_immutable --
752 * Set the immutable flag on that dictionary.
754 void
755 prop_dictionary_make_immutable(prop_dictionary_t pd)
758 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
759 if (prop_dictionary_is_immutable(pd) == false)
760 pd->pd_flags |= PD_F_IMMUTABLE;
761 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
765 * prop_dictionary_count --
766 * Return the number of objects stored in the dictionary.
768 unsigned int
769 prop_dictionary_count(prop_dictionary_t pd)
771 unsigned int rv;
773 if (! prop_object_is_dictionary(pd))
774 return (0);
776 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
777 rv = pd->pd_count;
778 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
780 return (rv);
784 * prop_dictionary_ensure_capacity --
785 * Ensure that the dictionary has the capacity to store the specified
786 * total number of objects (including the objects already stored in
787 * the dictionary).
789 bool
790 prop_dictionary_ensure_capacity(prop_dictionary_t pd, unsigned int capacity)
792 bool rv;
794 if (! prop_object_is_dictionary(pd))
795 return (false);
797 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
798 if (capacity > pd->pd_capacity)
799 rv = _prop_dictionary_expand(pd, capacity);
800 else
801 rv = true;
802 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
803 return (rv);
806 static prop_object_iterator_t
807 _prop_dictionary_iterator_locked(prop_dictionary_t pd)
809 struct _prop_dictionary_iterator *pdi;
811 if (! prop_object_is_dictionary(pd))
812 return (NULL);
814 pdi = _PROP_CALLOC(sizeof(*pdi), M_TEMP);
815 if (pdi == NULL)
816 return (NULL);
817 pdi->pdi_base.pi_next_object = _prop_dictionary_iterator_next_object;
818 pdi->pdi_base.pi_reset = _prop_dictionary_iterator_reset;
819 prop_object_retain(pd);
820 pdi->pdi_base.pi_obj = pd;
821 _prop_dictionary_iterator_reset_locked(pdi);
823 return (&pdi->pdi_base);
827 * prop_dictionary_iterator --
828 * Return an iterator for the dictionary. The dictionary is retained by
829 * the iterator.
831 prop_object_iterator_t
832 prop_dictionary_iterator(prop_dictionary_t pd)
834 prop_object_iterator_t pi;
836 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
837 pi = _prop_dictionary_iterator_locked(pd);
838 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
839 return (pi);
843 * prop_dictionary_all_keys --
844 * Return an array containing a snapshot of all of the keys
845 * in the dictionary.
847 prop_array_t
848 prop_dictionary_all_keys(prop_dictionary_t pd)
850 prop_array_t array;
851 unsigned int idx;
852 bool rv = true;
854 if (! prop_object_is_dictionary(pd))
855 return (NULL);
857 /* There is no pressing need to lock the dictionary for this. */
858 array = prop_array_create_with_capacity(pd->pd_count);
860 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
862 for (idx = 0; idx < pd->pd_count; idx++) {
863 rv = prop_array_add(array, pd->pd_array[idx].pde_key);
864 if (rv == false)
865 break;
868 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
870 if (rv == false) {
871 prop_object_release(array);
872 array = NULL;
874 return (array);
877 static struct _prop_dict_entry *
878 _prop_dict_lookup(prop_dictionary_t pd, const char *key,
879 unsigned int *idxp)
881 struct _prop_dict_entry *pde;
882 unsigned int base, idx, distance;
883 int res;
886 * Dictionary must be READ-LOCKED or WRITE-LOCKED.
889 for (idx = 0, base = 0, distance = pd->pd_count; distance != 0;
890 distance >>= 1) {
891 idx = base + (distance >> 1);
892 pde = &pd->pd_array[idx];
893 _PROP_ASSERT(pde->pde_key != NULL);
894 res = strcmp(key, pde->pde_key->pdk_key);
895 if (res == 0) {
896 if (idxp != NULL)
897 *idxp = idx;
898 return (pde);
900 if (res > 0) { /* key > pdk_key: move right */
901 base = idx + 1;
902 distance--;
903 } /* else move left */
906 /* idx points to the slot we looked at last. */
907 if (idxp != NULL)
908 *idxp = idx;
909 return (NULL);
912 static prop_object_t
913 _prop_dictionary_get(prop_dictionary_t pd, const char *key, bool locked)
915 const struct _prop_dict_entry *pde;
916 prop_object_t po = NULL;
918 if (! prop_object_is_dictionary(pd))
919 return (NULL);
921 if (!locked)
922 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
923 pde = _prop_dict_lookup(pd, key, NULL);
924 if (pde != NULL) {
925 _PROP_ASSERT(pde->pde_objref != NULL);
926 po = pde->pde_objref;
928 if (!locked)
929 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
930 return (po);
933 * prop_dictionary_get --
934 * Return the object stored with specified key.
936 prop_object_t
937 prop_dictionary_get(prop_dictionary_t pd, const char *key)
939 prop_object_t po = NULL;
941 if (! prop_object_is_dictionary(pd))
942 return (NULL);
944 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
945 po = _prop_dictionary_get(pd, key, true);
946 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
947 return (po);
950 static prop_object_t
951 _prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
952 bool locked)
955 if (! (prop_object_is_dictionary(pd) &&
956 prop_object_is_dictionary_keysym(pdk)))
957 return (NULL);
959 return (_prop_dictionary_get(pd, pdk->pdk_key, locked));
963 * prop_dictionary_get_keysym --
964 * Return the object stored at the location encoded by the keysym.
966 prop_object_t
967 prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk)
970 return (_prop_dictionary_get_keysym(pd, pdk, false));
974 * prop_dictionary_set --
975 * Store a reference to an object at with the specified key.
976 * If the key already exisit, the original object is released.
978 bool
979 prop_dictionary_set(prop_dictionary_t pd, const char *key, prop_object_t po)
981 struct _prop_dict_entry *pde;
982 prop_dictionary_keysym_t pdk;
983 unsigned int idx;
984 bool rv = false;
986 if (! prop_object_is_dictionary(pd))
987 return (false);
989 _PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
991 if (prop_dictionary_is_immutable(pd))
992 return (false);
994 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
996 pde = _prop_dict_lookup(pd, key, &idx);
997 if (pde != NULL) {
998 prop_object_t opo = pde->pde_objref;
999 prop_object_retain(po);
1000 pde->pde_objref = po;
1001 prop_object_release(opo);
1002 rv = true;
1003 goto out;
1006 pdk = _prop_dict_keysym_alloc(key);
1007 if (pdk == NULL)
1008 goto out;
1010 if (pd->pd_count == pd->pd_capacity &&
1011 _prop_dictionary_expand(pd,
1012 pd->pd_capacity + EXPAND_STEP) == false) {
1013 prop_object_release(pdk);
1014 goto out;
1017 /* At this point, the store will succeed. */
1018 prop_object_retain(po);
1020 if (pd->pd_count == 0) {
1021 pd->pd_array[0].pde_key = pdk;
1022 pd->pd_array[0].pde_objref = po;
1023 pd->pd_count++;
1024 pd->pd_version++;
1025 rv = true;
1026 goto out;
1029 pde = &pd->pd_array[idx];
1030 _PROP_ASSERT(pde->pde_key != NULL);
1032 if (strcmp(key, pde->pde_key->pdk_key) < 0) {
1034 * key < pdk_key: insert to the left. This is the same as
1035 * inserting to the right, except we decrement the current
1036 * index first.
1038 * Because we're unsigned, we have to special case 0
1039 * (grumble).
1041 if (idx == 0) {
1042 memmove(&pd->pd_array[1], &pd->pd_array[0],
1043 pd->pd_count * sizeof(*pde));
1044 pd->pd_array[0].pde_key = pdk;
1045 pd->pd_array[0].pde_objref = po;
1046 pd->pd_count++;
1047 pd->pd_version++;
1048 rv = true;
1049 goto out;
1051 idx--;
1054 memmove(&pd->pd_array[idx + 2], &pd->pd_array[idx + 1],
1055 (pd->pd_count - (idx + 1)) * sizeof(*pde));
1056 pd->pd_array[idx + 1].pde_key = pdk;
1057 pd->pd_array[idx + 1].pde_objref = po;
1058 pd->pd_count++;
1060 pd->pd_version++;
1062 rv = true;
1064 out:
1065 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
1066 return (rv);
1070 * prop_dictionary_set_keysym --
1071 * Replace the object in the dictionary at the location encoded by
1072 * the keysym.
1074 bool
1075 prop_dictionary_set_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
1076 prop_object_t po)
1079 if (! (prop_object_is_dictionary(pd) &&
1080 prop_object_is_dictionary_keysym(pdk)))
1081 return (false);
1083 return (prop_dictionary_set(pd, pdk->pdk_key, po));
1086 static void
1087 _prop_dictionary_remove(prop_dictionary_t pd, struct _prop_dict_entry *pde,
1088 unsigned int idx)
1090 prop_dictionary_keysym_t pdk = pde->pde_key;
1091 prop_object_t po = pde->pde_objref;
1094 * Dictionary must be WRITE-LOCKED.
1097 _PROP_ASSERT(pd->pd_count != 0);
1098 _PROP_ASSERT(idx < pd->pd_count);
1099 _PROP_ASSERT(pde == &pd->pd_array[idx]);
1101 idx++;
1102 memmove(&pd->pd_array[idx - 1], &pd->pd_array[idx],
1103 (pd->pd_count - idx) * sizeof(*pde));
1104 pd->pd_count--;
1105 pd->pd_version++;
1108 prop_object_release(pdk);
1110 prop_object_release(po);
1114 * prop_dictionary_remove --
1115 * Remove the reference to an object with the specified key from
1116 * the dictionary.
1118 void
1119 prop_dictionary_remove(prop_dictionary_t pd, const char *key)
1121 struct _prop_dict_entry *pde;
1122 unsigned int idx;
1124 if (! prop_object_is_dictionary(pd))
1125 return;
1127 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
1129 /* XXX Should this be a _PROP_ASSERT()? */
1130 if (prop_dictionary_is_immutable(pd))
1131 goto out;
1133 pde = _prop_dict_lookup(pd, key, &idx);
1134 /* XXX Should this be a _PROP_ASSERT()? */
1135 if (pde == NULL)
1136 goto out;
1138 _prop_dictionary_remove(pd, pde, idx);
1139 out:
1140 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
1144 * prop_dictionary_remove_keysym --
1145 * Remove a reference to an object stored in the dictionary at the
1146 * location encoded by the keysym.
1148 void
1149 prop_dictionary_remove_keysym(prop_dictionary_t pd,
1150 prop_dictionary_keysym_t pdk)
1153 if (! (prop_object_is_dictionary(pd) &&
1154 prop_object_is_dictionary_keysym(pdk)))
1155 return;
1157 prop_dictionary_remove(pd, pdk->pdk_key);
1161 * prop_dictionary_equals --
1162 * Return true if the two dictionaries are equivalent. Note we do a
1163 * by-value comparison of the objects in the dictionary.
1165 bool
1166 prop_dictionary_equals(prop_dictionary_t dict1, prop_dictionary_t dict2)
1168 if (!prop_object_is_dictionary(dict1) ||
1169 !prop_object_is_dictionary(dict2))
1170 return (false);
1172 return (prop_object_equals(dict1, dict2));
1176 * prop_dictionary_keysym_cstring_nocopy --
1177 * Return an immutable reference to the keysym's value.
1179 const char *
1180 prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pdk)
1183 if (! prop_object_is_dictionary_keysym(pdk))
1184 return (NULL);
1186 return (pdk->pdk_key);
1190 * prop_dictionary_keysym_equals --
1191 * Return true if the two dictionary key symbols are equivalent.
1192 * Note: We do not compare the object references.
1194 bool
1195 prop_dictionary_keysym_equals(prop_dictionary_keysym_t pdk1,
1196 prop_dictionary_keysym_t pdk2)
1198 if (!prop_object_is_dictionary_keysym(pdk1) ||
1199 !prop_object_is_dictionary_keysym(pdk2))
1200 return (false);
1202 return (prop_object_equals(pdk1, pdk2));
1206 * prop_dictionary_externalize --
1207 * Externalize a dictionary, returning a NUL-terminated buffer
1208 * containing the XML-style representation. The buffer is allocated
1209 * with the M_TEMP memory type.
1211 char *
1212 prop_dictionary_externalize(prop_dictionary_t pd)
1214 struct _prop_object_externalize_context *ctx;
1215 char *cp;
1217 ctx = _prop_object_externalize_context_alloc();
1218 if (ctx == NULL)
1219 return (NULL);
1221 if (_prop_object_externalize_header(ctx) == false ||
1222 (*pd->pd_obj.po_type->pot_extern)(ctx, pd) == false ||
1223 _prop_object_externalize_footer(ctx) == false) {
1224 /* We are responsible for releasing the buffer. */
1225 _PROP_FREE(ctx->poec_buf, M_TEMP);
1226 _prop_object_externalize_context_free(ctx);
1227 return (NULL);
1230 cp = ctx->poec_buf;
1231 _prop_object_externalize_context_free(ctx);
1233 return (cp);
1237 * _prop_dictionary_internalize --
1238 * Parse a <dict>...</dict> and return the object created from the
1239 * external representation.
1241 * Internal state in via rec_data is the storage area for the last processed
1242 * key.
1243 * _prop_dictionary_internalize_body is the upper half of the parse loop.
1244 * It is responsible for parsing the key directly and storing it in the area
1245 * referenced by rec_data.
1246 * _prop_dictionary_internalize_cont is the lower half and called with the value
1247 * associated with the key.
1249 static bool _prop_dictionary_internalize_body(prop_stack_t,
1250 prop_object_t *, struct _prop_object_internalize_context *, char *);
1252 bool
1253 _prop_dictionary_internalize(prop_stack_t stack, prop_object_t *obj,
1254 struct _prop_object_internalize_context *ctx)
1256 prop_dictionary_t dict;
1257 char *tmpkey;
1259 /* We don't currently understand any attributes. */
1260 if (ctx->poic_tagattr != NULL)
1261 return (true);
1263 dict = prop_dictionary_create();
1264 if (dict == NULL)
1265 return (true);
1267 if (ctx->poic_is_empty_element) {
1268 *obj = dict;
1269 return (true);
1272 tmpkey = _PROP_MALLOC(PDK_MAXKEY + 1, M_TEMP);
1273 if (tmpkey == NULL) {
1274 prop_object_release(dict);
1275 return (true);
1278 *obj = dict;
1280 * Opening tag is found, storage for key allocated and
1281 * now continue to the first element.
1283 return _prop_dictionary_internalize_body(stack, obj, ctx, tmpkey);
1286 static bool
1287 _prop_dictionary_internalize_continue(prop_stack_t stack, prop_object_t *obj,
1288 struct _prop_object_internalize_context *ctx, void *data, prop_object_t child)
1290 prop_dictionary_t dict = *obj;
1291 char *tmpkey = data;
1293 _PROP_ASSERT(tmpkey != NULL);
1295 if (child == NULL ||
1296 prop_dictionary_set(dict, tmpkey, child) == false) {
1297 _PROP_FREE(tmpkey, M_TEMP);
1298 if (child != NULL)
1299 prop_object_release(child);
1300 prop_object_release(dict);
1301 *obj = NULL;
1302 return (true);
1305 prop_object_release(child);
1308 * key, value was added, now continue looking for the next key
1309 * or the closing tag.
1311 return _prop_dictionary_internalize_body(stack, obj, ctx, tmpkey);
1314 static bool
1315 _prop_dictionary_internalize_body(prop_stack_t stack, prop_object_t *obj,
1316 struct _prop_object_internalize_context *ctx, char *tmpkey)
1318 prop_dictionary_t dict = *obj;
1319 size_t keylen;
1321 /* Fetch the next tag. */
1322 if (_prop_object_internalize_find_tag(ctx, NULL, _PROP_TAG_TYPE_EITHER) == false)
1323 goto bad;
1325 /* Check to see if this is the end of the dictionary. */
1326 if (_PROP_TAG_MATCH(ctx, "dict") &&
1327 ctx->poic_tag_type == _PROP_TAG_TYPE_END) {
1328 _PROP_FREE(tmpkey, M_TEMP);
1329 return (true);
1332 /* Ok, it must be a non-empty key start tag. */
1333 if (!_PROP_TAG_MATCH(ctx, "key") ||
1334 ctx->poic_tag_type != _PROP_TAG_TYPE_START ||
1335 ctx->poic_is_empty_element)
1336 goto bad;
1338 if (_prop_object_internalize_decode_string(ctx,
1339 tmpkey, PDK_MAXKEY, &keylen,
1340 &ctx->poic_cp) == false)
1341 goto bad;
1343 _PROP_ASSERT(keylen <= PDK_MAXKEY);
1344 tmpkey[keylen] = '\0';
1346 if (_prop_object_internalize_find_tag(ctx, "key",
1347 _PROP_TAG_TYPE_END) == false)
1348 goto bad;
1350 /* ..and now the beginning of the value. */
1351 if (_prop_object_internalize_find_tag(ctx, NULL,
1352 _PROP_TAG_TYPE_START) == false)
1353 goto bad;
1356 * Key is found, now wait for value to be parsed.
1358 if (_prop_stack_push(stack, *obj,
1359 _prop_dictionary_internalize_continue,
1360 tmpkey, NULL))
1361 return (false);
1363 bad:
1364 _PROP_FREE(tmpkey, M_TEMP);
1365 prop_object_release(dict);
1366 *obj = NULL;
1367 return (true);
1371 * prop_dictionary_internalize --
1372 * Create a dictionary by parsing the NUL-terminated XML-style
1373 * representation.
1375 prop_dictionary_t
1376 prop_dictionary_internalize(const char *xml)
1378 return _prop_generic_internalize(xml, "dict");
1381 #if !defined(_KERNEL) && !defined(_STANDALONE)
1383 * prop_dictionary_externalize_to_file --
1384 * Externalize a dictionary to the specified file.
1386 bool
1387 prop_dictionary_externalize_to_file(prop_dictionary_t dict, const char *fname)
1389 char *xml;
1390 bool rv;
1391 int save_errno = 0; /* XXXGCC -Wuninitialized [mips, ...] */
1393 xml = prop_dictionary_externalize(dict);
1394 if (xml == NULL)
1395 return (false);
1396 rv = _prop_object_externalize_write_file(fname, xml, strlen(xml));
1397 if (rv == false)
1398 save_errno = errno;
1399 _PROP_FREE(xml, M_TEMP);
1400 if (rv == false)
1401 errno = save_errno;
1403 return (rv);
1407 * prop_dictionary_internalize_from_file --
1408 * Internalize a dictionary from a file.
1410 prop_dictionary_t
1411 prop_dictionary_internalize_from_file(const char *fname)
1413 struct _prop_object_internalize_mapped_file *mf;
1414 prop_dictionary_t dict;
1416 mf = _prop_object_internalize_map_file(fname);
1417 if (mf == NULL)
1418 return (NULL);
1419 dict = prop_dictionary_internalize(mf->poimf_xml);
1420 _prop_object_internalize_unmap_file(mf);
1422 return (dict);
1424 #endif /* !_KERNEL && !_STANDALONE */