Remove duplicated headers.
[portableproplib.git] / src / prop_dictionary.c
blobc27b089fd590aab30875e041e597bafadab1ecac
1 /* $NetBSD: prop_dictionary.c,v 1.36 2010/09/24 22:51:52 rmind 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 #define __unused /* empty */
41 #endif
44 * We implement these like arrays, but we keep them sorted by key.
45 * This allows us to binary-search as well as keep externalized output
46 * sane-looking for human eyes.
49 #define EXPAND_STEP 16
52 * prop_dictionary_keysym_t is allocated with space at the end to hold the
53 * key. This must be a regular object so that we can maintain sane iterator
54 * semantics -- we don't want to require that the caller release the result
55 * of prop_object_iterator_next().
57 * We'd like to have some small'ish keysym objects for up-to-16 characters
58 * in a key, some for up-to-32 characters in a key, and then a final bucket
59 * for up-to-128 characters in a key (not including NUL). Keys longer than
60 * 128 characters are not allowed.
62 struct _prop_dictionary_keysym {
63 struct _prop_object pdk_obj;
64 size_t pdk_size;
65 struct rb_node pdk_link;
66 char pdk_key[1];
67 /* actually variable length */
70 /* pdk_key[1] takes care of the NUL */
71 #define PDK_SIZE_16 (sizeof(struct _prop_dictionary_keysym) + 16)
72 #define PDK_SIZE_32 (sizeof(struct _prop_dictionary_keysym) + 32)
73 #define PDK_SIZE_128 (sizeof(struct _prop_dictionary_keysym) + 128)
75 #define PDK_MAXKEY 128
77 _PROP_POOL_INIT(_prop_dictionary_keysym16_pool, PDK_SIZE_16, "pdict16")
78 _PROP_POOL_INIT(_prop_dictionary_keysym32_pool, PDK_SIZE_32, "pdict32")
79 _PROP_POOL_INIT(_prop_dictionary_keysym128_pool, PDK_SIZE_128, "pdict128")
81 struct _prop_dict_entry {
82 prop_dictionary_keysym_t pde_key;
83 prop_object_t pde_objref;
86 struct _prop_dictionary {
87 struct _prop_object pd_obj;
88 _PROP_RWLOCK_DECL(pd_rwlock)
89 struct _prop_dict_entry *pd_array;
90 unsigned int pd_capacity;
91 unsigned int pd_count;
92 int pd_flags;
94 uint32_t pd_version;
97 #define PD_F_IMMUTABLE 0x01 /* dictionary is immutable */
99 _PROP_POOL_INIT(_prop_dictionary_pool, sizeof(struct _prop_dictionary),
100 "propdict")
101 _PROP_MALLOC_DEFINE(M_PROP_DICT, "prop dictionary",
102 "property dictionary container object")
104 static _prop_object_free_rv_t
105 _prop_dictionary_free(prop_stack_t, prop_object_t *);
106 static void _prop_dictionary_emergency_free(prop_object_t);
107 static bool _prop_dictionary_externalize(
108 struct _prop_object_externalize_context *,
109 void *);
110 static _prop_object_equals_rv_t
111 _prop_dictionary_equals(prop_object_t, prop_object_t,
112 void **, void **,
113 prop_object_t *, prop_object_t *);
114 static void _prop_dictionary_equals_finish(prop_object_t, prop_object_t);
115 static prop_object_iterator_t
116 _prop_dictionary_iterator_locked(prop_dictionary_t);
117 static prop_object_t
118 _prop_dictionary_iterator_next_object_locked(void *);
119 static prop_object_t
120 _prop_dictionary_get_keysym(prop_dictionary_t,
121 prop_dictionary_keysym_t, bool);
122 static prop_object_t
123 _prop_dictionary_get(prop_dictionary_t, const char *, bool);
125 static void _prop_dictionary_lock(void);
126 static void _prop_dictionary_unlock(void);
128 static const struct _prop_object_type _prop_object_type_dictionary = {
129 .pot_type = PROP_TYPE_DICTIONARY,
130 .pot_free = _prop_dictionary_free,
131 .pot_emergency_free = _prop_dictionary_emergency_free,
132 .pot_extern = _prop_dictionary_externalize,
133 .pot_equals = _prop_dictionary_equals,
134 .pot_equals_finish = _prop_dictionary_equals_finish,
135 .pot_lock = _prop_dictionary_lock,
136 .pot_unlock = _prop_dictionary_unlock,
139 static _prop_object_free_rv_t
140 _prop_dict_keysym_free(prop_stack_t, prop_object_t *);
141 static bool _prop_dict_keysym_externalize(
142 struct _prop_object_externalize_context *,
143 void *);
144 static _prop_object_equals_rv_t
145 _prop_dict_keysym_equals(prop_object_t, prop_object_t,
146 void **, void **,
147 prop_object_t *, prop_object_t *);
149 static const struct _prop_object_type _prop_object_type_dict_keysym = {
150 .pot_type = PROP_TYPE_DICT_KEYSYM,
151 .pot_free = _prop_dict_keysym_free,
152 .pot_extern = _prop_dict_keysym_externalize,
153 .pot_equals = _prop_dict_keysym_equals,
156 #define prop_object_is_dictionary(x) \
157 ((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_dictionary)
158 #define prop_object_is_dictionary_keysym(x) \
159 ((x) != NULL && (x)->pdk_obj.po_type == &_prop_object_type_dict_keysym)
161 #define prop_dictionary_is_immutable(x) \
162 (((x)->pd_flags & PD_F_IMMUTABLE) != 0)
164 struct _prop_dictionary_iterator {
165 struct _prop_object_iterator pdi_base;
166 unsigned int pdi_index;
170 * Dictionary key symbols are immutable, and we are likely to have many
171 * duplicated key symbols. So, to save memory, we unique'ify key symbols
172 * so we only have to have one copy of each string.
175 static int
176 /*ARGSUSED*/
177 _prop_dict_keysym_rb_compare_nodes(void *ctx __unused,
178 const void *n1, const void *n2)
180 const struct _prop_dictionary_keysym *pdk1 = n1;
181 const struct _prop_dictionary_keysym *pdk2 = n2;
183 return strcmp(pdk1->pdk_key, pdk2->pdk_key);
186 static int
187 /*ARGSUSED*/
188 _prop_dict_keysym_rb_compare_key(void *ctx __unused,
189 const void *n, const void *v)
191 const struct _prop_dictionary_keysym *pdk = n;
192 const char *cp = v;
194 return strcmp(pdk->pdk_key, cp);
197 static const rb_tree_ops_t _prop_dict_keysym_rb_tree_ops = {
198 .rbto_compare_nodes = _prop_dict_keysym_rb_compare_nodes,
199 .rbto_compare_key = _prop_dict_keysym_rb_compare_key,
200 .rbto_node_offset = offsetof(struct _prop_dictionary_keysym, pdk_link),
201 .rbto_context = NULL
204 static struct rb_tree _prop_dict_keysym_tree;
206 _PROP_ONCE_DECL(_prop_dict_init_once)
207 _PROP_MUTEX_DECL_STATIC(_prop_dict_keysym_tree_mutex)
209 static int
210 _prop_dict_init(void)
213 _PROP_MUTEX_INIT(_prop_dict_keysym_tree_mutex);
214 _prop_rb_tree_init(&_prop_dict_keysym_tree,
215 &_prop_dict_keysym_rb_tree_ops);
216 return 0;
219 static void
220 _prop_dict_keysym_put(prop_dictionary_keysym_t pdk)
223 if (pdk->pdk_size <= PDK_SIZE_16)
224 _PROP_POOL_PUT(_prop_dictionary_keysym16_pool, pdk);
225 else if (pdk->pdk_size <= PDK_SIZE_32)
226 _PROP_POOL_PUT(_prop_dictionary_keysym32_pool, pdk);
227 else {
228 _PROP_ASSERT(pdk->pdk_size <= PDK_SIZE_128);
229 _PROP_POOL_PUT(_prop_dictionary_keysym128_pool, pdk);
233 /* ARGSUSED */
234 static _prop_object_free_rv_t
235 _prop_dict_keysym_free(prop_stack_t stack, prop_object_t *obj)
237 prop_dictionary_keysym_t pdk = *obj;
239 _prop_rb_tree_remove_node(&_prop_dict_keysym_tree, pdk);
240 _prop_dict_keysym_put(pdk);
242 return _PROP_OBJECT_FREE_DONE;
245 static bool
246 _prop_dict_keysym_externalize(struct _prop_object_externalize_context *ctx,
247 void *v)
249 prop_dictionary_keysym_t pdk = v;
251 /* We externalize these as strings, and they're never empty. */
253 _PROP_ASSERT(pdk->pdk_key[0] != '\0');
255 if (_prop_object_externalize_start_tag(ctx, "string") == false ||
256 _prop_object_externalize_append_encoded_cstring(ctx,
257 pdk->pdk_key) == false ||
258 _prop_object_externalize_end_tag(ctx, "string") == false)
259 return (false);
261 return (true);
264 /* ARGSUSED */
265 static _prop_object_equals_rv_t
266 _prop_dict_keysym_equals(prop_object_t v1, prop_object_t v2,
267 void **stored_pointer1, void **stored_pointer2,
268 prop_object_t *next_obj1, prop_object_t *next_obj2)
270 prop_dictionary_keysym_t pdk1 = v1;
271 prop_dictionary_keysym_t pdk2 = v2;
274 * There is only ever one copy of a keysym at any given time,
275 * so we can reduce this to a simple pointer equality check.
277 if (pdk1 == pdk2)
278 return _PROP_OBJECT_EQUALS_TRUE;
279 else
280 return _PROP_OBJECT_EQUALS_FALSE;
283 static prop_dictionary_keysym_t
284 _prop_dict_keysym_alloc(const char *key)
286 prop_dictionary_keysym_t opdk, pdk, rpdk;
287 size_t size;
289 _PROP_ONCE_RUN(_prop_dict_init_once, _prop_dict_init);
292 * Check to see if this already exists in the tree. If it does,
293 * we just retain it and return it.
295 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
296 opdk = _prop_rb_tree_find(&_prop_dict_keysym_tree, key);
297 if (opdk != NULL) {
298 prop_object_retain(opdk);
299 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
300 return (opdk);
302 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
305 * Not in the tree. Create it now.
308 size = sizeof(*pdk) + strlen(key) /* pdk_key[1] covers the NUL */;
310 if (size <= PDK_SIZE_16)
311 pdk = _PROP_POOL_GET(_prop_dictionary_keysym16_pool);
312 else if (size <= PDK_SIZE_32)
313 pdk = _PROP_POOL_GET(_prop_dictionary_keysym32_pool);
314 else if (size <= PDK_SIZE_128)
315 pdk = _PROP_POOL_GET(_prop_dictionary_keysym128_pool);
316 else
317 pdk = NULL; /* key too long */
319 if (pdk == NULL)
320 return (NULL);
322 _prop_object_init(&pdk->pdk_obj, &_prop_object_type_dict_keysym);
324 strcpy(pdk->pdk_key, key);
325 pdk->pdk_size = size;
328 * We dropped the mutex when we allocated the new object, so
329 * we have to check again if it is in the tree.
331 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
332 opdk = _prop_rb_tree_find(&_prop_dict_keysym_tree, key);
333 if (opdk != NULL) {
334 prop_object_retain(opdk);
335 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
336 _prop_dict_keysym_put(pdk);
337 return (opdk);
339 rpdk = _prop_rb_tree_insert_node(&_prop_dict_keysym_tree, pdk);
340 _PROP_ASSERT(rpdk == pdk);
341 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
342 return (pdk);
345 static _prop_object_free_rv_t
346 _prop_dictionary_free(prop_stack_t stack, prop_object_t *obj)
348 prop_dictionary_t pd = *obj;
349 prop_dictionary_keysym_t pdk;
350 prop_object_t po;
352 _PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
353 _PROP_ASSERT((pd->pd_capacity == 0 && pd->pd_array == NULL) ||
354 (pd->pd_capacity != 0 && pd->pd_array != NULL));
356 /* The empty dictorinary is easy, handle that first. */
357 if (pd->pd_count == 0) {
358 if (pd->pd_array != NULL)
359 _PROP_FREE(pd->pd_array, M_PROP_DICT);
361 _PROP_RWLOCK_DESTROY(pd->pd_rwlock);
363 _PROP_POOL_PUT(_prop_dictionary_pool, pd);
365 return (_PROP_OBJECT_FREE_DONE);
368 po = pd->pd_array[pd->pd_count - 1].pde_objref;
369 _PROP_ASSERT(po != NULL);
371 if (stack == NULL) {
373 * If we are in emergency release mode,
374 * just let caller recurse down.
376 *obj = po;
377 return (_PROP_OBJECT_FREE_FAILED);
380 /* Otherwise, try to push the current object on the stack. */
381 if (!_prop_stack_push(stack, pd, NULL, NULL, NULL)) {
382 /* Push failed, entering emergency release mode. */
383 return (_PROP_OBJECT_FREE_FAILED);
385 /* Object pushed on stack, caller will release it. */
386 --pd->pd_count;
387 pdk = pd->pd_array[pd->pd_count].pde_key;
388 _PROP_ASSERT(pdk != NULL);
390 prop_object_release(pdk);
392 *obj = po;
393 return (_PROP_OBJECT_FREE_RECURSE);
397 static void
398 _prop_dictionary_lock(void)
401 /* XXX: once necessary or paranoia? */
402 _PROP_ONCE_RUN(_prop_dict_init_once, _prop_dict_init);
403 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
406 static void
407 _prop_dictionary_unlock(void)
409 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
412 static void
413 _prop_dictionary_emergency_free(prop_object_t obj)
415 prop_dictionary_t pd = obj;
416 prop_dictionary_keysym_t pdk;
418 _PROP_ASSERT(pd->pd_count != 0);
419 --pd->pd_count;
421 pdk = pd->pd_array[pd->pd_count].pde_key;
422 _PROP_ASSERT(pdk != NULL);
423 prop_object_release(pdk);
426 static bool
427 _prop_dictionary_externalize(struct _prop_object_externalize_context *ctx,
428 void *v)
430 prop_dictionary_t pd = v;
431 prop_dictionary_keysym_t pdk;
432 struct _prop_object *po;
433 prop_object_iterator_t pi;
434 unsigned int i;
435 bool rv = false;
437 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
439 if (pd->pd_count == 0) {
440 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
441 return (_prop_object_externalize_empty_tag(ctx, "dict"));
444 if (_prop_object_externalize_start_tag(ctx, "dict") == false ||
445 _prop_object_externalize_append_char(ctx, '\n') == false)
446 goto out;
448 pi = _prop_dictionary_iterator_locked(pd);
449 if (pi == NULL)
450 goto out;
452 ctx->poec_depth++;
453 _PROP_ASSERT(ctx->poec_depth != 0);
455 while ((pdk = _prop_dictionary_iterator_next_object_locked(pi))
456 != NULL) {
457 po = _prop_dictionary_get_keysym(pd, pdk, true);
458 if (po == NULL ||
459 _prop_object_externalize_start_tag(ctx, "key") == false ||
460 _prop_object_externalize_append_encoded_cstring(ctx,
461 pdk->pdk_key) == false ||
462 _prop_object_externalize_end_tag(ctx, "key") == false ||
463 (*po->po_type->pot_extern)(ctx, po) == false) {
464 prop_object_iterator_release(pi);
465 goto out;
469 prop_object_iterator_release(pi);
471 ctx->poec_depth--;
472 for (i = 0; i < ctx->poec_depth; i++) {
473 if (_prop_object_externalize_append_char(ctx, '\t') == false)
474 goto out;
476 if (_prop_object_externalize_end_tag(ctx, "dict") == false)
477 goto out;
479 rv = true;
481 out:
482 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
483 return (rv);
486 /* ARGSUSED */
487 static _prop_object_equals_rv_t
488 _prop_dictionary_equals(prop_object_t v1, prop_object_t v2,
489 void **stored_pointer1, void **stored_pointer2,
490 prop_object_t *next_obj1, prop_object_t *next_obj2)
492 prop_dictionary_t dict1 = v1;
493 prop_dictionary_t dict2 = v2;
494 uintptr_t idx;
495 _prop_object_equals_rv_t rv = _PROP_OBJECT_EQUALS_FALSE;
497 if (dict1 == dict2)
498 return (_PROP_OBJECT_EQUALS_TRUE);
500 _PROP_ASSERT(*stored_pointer1 == *stored_pointer2);
502 idx = (uintptr_t)*stored_pointer1;
504 if (idx == 0) {
505 if ((uintptr_t)dict1 < (uintptr_t)dict2) {
506 _PROP_RWLOCK_RDLOCK(dict1->pd_rwlock);
507 _PROP_RWLOCK_RDLOCK(dict2->pd_rwlock);
508 } else {
509 _PROP_RWLOCK_RDLOCK(dict2->pd_rwlock);
510 _PROP_RWLOCK_RDLOCK(dict1->pd_rwlock);
514 if (dict1->pd_count != dict2->pd_count)
515 goto out;
517 if (idx == dict1->pd_count) {
518 rv = _PROP_OBJECT_EQUALS_TRUE;
519 goto out;
522 _PROP_ASSERT(idx < dict1->pd_count);
524 *stored_pointer1 = (void *)(idx + 1);
525 *stored_pointer2 = (void *)(idx + 1);
527 *next_obj1 = &dict1->pd_array[idx].pde_objref;
528 *next_obj2 = &dict2->pd_array[idx].pde_objref;
530 if (!prop_dictionary_keysym_equals(dict1->pd_array[idx].pde_key,
531 dict2->pd_array[idx].pde_key))
532 goto out;
534 return (_PROP_OBJECT_EQUALS_RECURSE);
536 out:
537 _PROP_RWLOCK_UNLOCK(dict1->pd_rwlock);
538 _PROP_RWLOCK_UNLOCK(dict2->pd_rwlock);
539 return (rv);
542 static void
543 _prop_dictionary_equals_finish(prop_object_t v1, prop_object_t v2)
545 _PROP_RWLOCK_UNLOCK(((prop_dictionary_t)v1)->pd_rwlock);
546 _PROP_RWLOCK_UNLOCK(((prop_dictionary_t)v2)->pd_rwlock);
549 static prop_dictionary_t
550 _prop_dictionary_alloc(unsigned int capacity)
552 prop_dictionary_t pd;
553 struct _prop_dict_entry *array;
555 if (capacity != 0) {
556 array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
557 if (array == NULL)
558 return (NULL);
559 } else
560 array = NULL;
562 pd = _PROP_POOL_GET(_prop_dictionary_pool);
563 if (pd != NULL) {
564 _prop_object_init(&pd->pd_obj, &_prop_object_type_dictionary);
566 _PROP_RWLOCK_INIT(pd->pd_rwlock);
567 pd->pd_array = array;
568 pd->pd_capacity = capacity;
569 pd->pd_count = 0;
570 pd->pd_flags = 0;
572 pd->pd_version = 0;
573 } else if (array != NULL)
574 _PROP_FREE(array, M_PROP_DICT);
576 return (pd);
579 static bool
580 _prop_dictionary_expand(prop_dictionary_t pd, unsigned int capacity)
582 struct _prop_dict_entry *array, *oarray;
585 * Dictionary must be WRITE-LOCKED.
588 oarray = pd->pd_array;
590 array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
591 if (array == NULL)
592 return (false);
593 if (oarray != NULL)
594 memcpy(array, oarray, pd->pd_capacity * sizeof(*array));
595 pd->pd_array = array;
596 pd->pd_capacity = capacity;
598 if (oarray != NULL)
599 _PROP_FREE(oarray, M_PROP_DICT);
601 return (true);
604 static prop_object_t
605 _prop_dictionary_iterator_next_object_locked(void *v)
607 struct _prop_dictionary_iterator *pdi = v;
608 prop_dictionary_t pd = pdi->pdi_base.pi_obj;
609 prop_dictionary_keysym_t pdk = NULL;
611 _PROP_ASSERT(prop_object_is_dictionary(pd));
613 if (pd->pd_version != pdi->pdi_base.pi_version)
614 goto out; /* dictionary changed during iteration */
616 _PROP_ASSERT(pdi->pdi_index <= pd->pd_count);
618 if (pdi->pdi_index == pd->pd_count)
619 goto out; /* we've iterated all objects */
621 pdk = pd->pd_array[pdi->pdi_index].pde_key;
622 pdi->pdi_index++;
624 out:
625 return (pdk);
628 static prop_object_t
629 _prop_dictionary_iterator_next_object(void *v)
631 struct _prop_dictionary_iterator *pdi = v;
632 prop_dictionary_t pd __unused = pdi->pdi_base.pi_obj;
633 prop_dictionary_keysym_t pdk;
635 _PROP_ASSERT(prop_object_is_dictionary(pd));
637 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
638 pdk = _prop_dictionary_iterator_next_object_locked(pdi);
639 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
640 return (pdk);
643 static void
644 _prop_dictionary_iterator_reset_locked(void *v)
646 struct _prop_dictionary_iterator *pdi = v;
647 prop_dictionary_t pd = pdi->pdi_base.pi_obj;
649 _PROP_ASSERT(prop_object_is_dictionary(pd));
651 pdi->pdi_index = 0;
652 pdi->pdi_base.pi_version = pd->pd_version;
655 static void
656 _prop_dictionary_iterator_reset(void *v)
658 struct _prop_dictionary_iterator *pdi = v;
659 prop_dictionary_t pd __unused = pdi->pdi_base.pi_obj;
661 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
662 _prop_dictionary_iterator_reset_locked(pdi);
663 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
667 * prop_dictionary_create --
668 * Create a dictionary.
670 prop_dictionary_t
671 prop_dictionary_create(void)
674 return (_prop_dictionary_alloc(0));
678 * prop_dictionary_create_with_capacity --
679 * Create a dictionary with the capacity to store N objects.
681 prop_dictionary_t
682 prop_dictionary_create_with_capacity(unsigned int capacity)
685 return (_prop_dictionary_alloc(capacity));
689 * prop_dictionary_copy --
690 * Copy a dictionary. The new dictionary has an initial capacity equal
691 * to the number of objects stored int the original dictionary. The new
692 * dictionary contains refrences to the original dictionary's objects,
693 * not copies of those objects (i.e. a shallow copy).
695 prop_dictionary_t
696 prop_dictionary_copy(prop_dictionary_t opd)
698 prop_dictionary_t pd;
699 prop_dictionary_keysym_t pdk;
700 prop_object_t po;
701 unsigned int idx;
703 if (! prop_object_is_dictionary(opd))
704 return (NULL);
706 _PROP_RWLOCK_RDLOCK(opd->pd_rwlock);
708 pd = _prop_dictionary_alloc(opd->pd_count);
709 if (pd != NULL) {
710 for (idx = 0; idx < opd->pd_count; idx++) {
711 pdk = opd->pd_array[idx].pde_key;
712 po = opd->pd_array[idx].pde_objref;
714 prop_object_retain(pdk);
715 prop_object_retain(po);
717 pd->pd_array[idx].pde_key = pdk;
718 pd->pd_array[idx].pde_objref = po;
720 pd->pd_count = opd->pd_count;
721 pd->pd_flags = opd->pd_flags;
723 _PROP_RWLOCK_UNLOCK(opd->pd_rwlock);
724 return (pd);
728 * prop_dictionary_copy_mutable --
729 * Like prop_dictionary_copy(), but the resulting dictionary is
730 * mutable.
732 prop_dictionary_t
733 prop_dictionary_copy_mutable(prop_dictionary_t opd)
735 prop_dictionary_t pd;
737 if (! prop_object_is_dictionary(opd))
738 return (NULL);
740 pd = prop_dictionary_copy(opd);
741 if (pd != NULL)
742 pd->pd_flags &= ~PD_F_IMMUTABLE;
744 return (pd);
748 * prop_dictionary_make_immutable --
749 * Set the immutable flag on that dictionary.
751 void
752 prop_dictionary_make_immutable(prop_dictionary_t pd)
755 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
756 if (prop_dictionary_is_immutable(pd) == false)
757 pd->pd_flags |= PD_F_IMMUTABLE;
758 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
762 * prop_dictionary_count --
763 * Return the number of objects stored in the dictionary.
765 unsigned int
766 prop_dictionary_count(prop_dictionary_t pd)
768 unsigned int rv;
770 if (! prop_object_is_dictionary(pd))
771 return (0);
773 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
774 rv = pd->pd_count;
775 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
777 return (rv);
781 * prop_dictionary_ensure_capacity --
782 * Ensure that the dictionary has the capacity to store the specified
783 * total number of objects (including the objects already stored in
784 * the dictionary).
786 bool
787 prop_dictionary_ensure_capacity(prop_dictionary_t pd, unsigned int capacity)
789 bool rv;
791 if (! prop_object_is_dictionary(pd))
792 return (false);
794 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
795 if (capacity > pd->pd_capacity)
796 rv = _prop_dictionary_expand(pd, capacity);
797 else
798 rv = true;
799 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
800 return (rv);
803 static prop_object_iterator_t
804 _prop_dictionary_iterator_locked(prop_dictionary_t pd)
806 struct _prop_dictionary_iterator *pdi;
808 if (! prop_object_is_dictionary(pd))
809 return (NULL);
811 pdi = _PROP_CALLOC(sizeof(*pdi), M_TEMP);
812 if (pdi == NULL)
813 return (NULL);
814 pdi->pdi_base.pi_next_object = _prop_dictionary_iterator_next_object;
815 pdi->pdi_base.pi_reset = _prop_dictionary_iterator_reset;
816 prop_object_retain(pd);
817 pdi->pdi_base.pi_obj = pd;
818 _prop_dictionary_iterator_reset_locked(pdi);
820 return (&pdi->pdi_base);
824 * prop_dictionary_iterator --
825 * Return an iterator for the dictionary. The dictionary is retained by
826 * the iterator.
828 prop_object_iterator_t
829 prop_dictionary_iterator(prop_dictionary_t pd)
831 prop_object_iterator_t pi;
833 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
834 pi = _prop_dictionary_iterator_locked(pd);
835 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
836 return (pi);
840 * prop_dictionary_all_keys --
841 * Return an array containing a snapshot of all of the keys
842 * in the dictionary.
844 prop_array_t
845 prop_dictionary_all_keys(prop_dictionary_t pd)
847 prop_array_t array;
848 unsigned int idx;
849 bool rv = true;
851 if (! prop_object_is_dictionary(pd))
852 return (NULL);
854 /* There is no pressing need to lock the dictionary for this. */
855 array = prop_array_create_with_capacity(pd->pd_count);
857 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
859 for (idx = 0; idx < pd->pd_count; idx++) {
860 rv = prop_array_add(array, pd->pd_array[idx].pde_key);
861 if (rv == false)
862 break;
865 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
867 if (rv == false) {
868 prop_object_release(array);
869 array = NULL;
871 return (array);
874 static struct _prop_dict_entry *
875 _prop_dict_lookup(prop_dictionary_t pd, const char *key,
876 unsigned int *idxp)
878 struct _prop_dict_entry *pde;
879 unsigned int base, idx, distance;
880 int res;
883 * Dictionary must be READ-LOCKED or WRITE-LOCKED.
886 for (idx = 0, base = 0, distance = pd->pd_count; distance != 0;
887 distance >>= 1) {
888 idx = base + (distance >> 1);
889 pde = &pd->pd_array[idx];
890 _PROP_ASSERT(pde->pde_key != NULL);
891 res = strcmp(key, pde->pde_key->pdk_key);
892 if (res == 0) {
893 if (idxp != NULL)
894 *idxp = idx;
895 return (pde);
897 if (res > 0) { /* key > pdk_key: move right */
898 base = idx + 1;
899 distance--;
900 } /* else move left */
903 /* idx points to the slot we looked at last. */
904 if (idxp != NULL)
905 *idxp = idx;
906 return (NULL);
909 static prop_object_t
910 _prop_dictionary_get(prop_dictionary_t pd, const char *key, bool locked)
912 const struct _prop_dict_entry *pde;
913 prop_object_t po = NULL;
915 if (! prop_object_is_dictionary(pd))
916 return (NULL);
918 if (!locked)
919 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
920 pde = _prop_dict_lookup(pd, key, NULL);
921 if (pde != NULL) {
922 _PROP_ASSERT(pde->pde_objref != NULL);
923 po = pde->pde_objref;
925 if (!locked)
926 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
927 return (po);
930 * prop_dictionary_get --
931 * Return the object stored with specified key.
933 prop_object_t
934 prop_dictionary_get(prop_dictionary_t pd, const char *key)
936 prop_object_t po = NULL;
938 if (! prop_object_is_dictionary(pd))
939 return (NULL);
941 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
942 po = _prop_dictionary_get(pd, key, true);
943 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
944 return (po);
947 static prop_object_t
948 _prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
949 bool locked)
952 if (! (prop_object_is_dictionary(pd) &&
953 prop_object_is_dictionary_keysym(pdk)))
954 return (NULL);
956 return (_prop_dictionary_get(pd, pdk->pdk_key, locked));
960 * prop_dictionary_get_keysym --
961 * Return the object stored at the location encoded by the keysym.
963 prop_object_t
964 prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk)
967 return (_prop_dictionary_get_keysym(pd, pdk, false));
971 * prop_dictionary_set --
972 * Store a reference to an object at with the specified key.
973 * If the key already exisit, the original object is released.
975 bool
976 prop_dictionary_set(prop_dictionary_t pd, const char *key, prop_object_t po)
978 struct _prop_dict_entry *pde;
979 prop_dictionary_keysym_t pdk;
980 unsigned int idx;
981 bool rv = false;
983 if (! prop_object_is_dictionary(pd))
984 return (false);
986 _PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
988 if (prop_dictionary_is_immutable(pd))
989 return (false);
991 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
993 pde = _prop_dict_lookup(pd, key, &idx);
994 if (pde != NULL) {
995 prop_object_t opo = pde->pde_objref;
996 prop_object_retain(po);
997 pde->pde_objref = po;
998 prop_object_release(opo);
999 rv = true;
1000 goto out;
1003 pdk = _prop_dict_keysym_alloc(key);
1004 if (pdk == NULL)
1005 goto out;
1007 if (pd->pd_count == pd->pd_capacity &&
1008 _prop_dictionary_expand(pd,
1009 pd->pd_capacity + EXPAND_STEP) == false) {
1010 prop_object_release(pdk);
1011 goto out;
1014 /* At this point, the store will succeed. */
1015 prop_object_retain(po);
1017 if (pd->pd_count == 0) {
1018 pd->pd_array[0].pde_key = pdk;
1019 pd->pd_array[0].pde_objref = po;
1020 pd->pd_count++;
1021 pd->pd_version++;
1022 rv = true;
1023 goto out;
1026 pde = &pd->pd_array[idx];
1027 _PROP_ASSERT(pde->pde_key != NULL);
1029 if (strcmp(key, pde->pde_key->pdk_key) < 0) {
1031 * key < pdk_key: insert to the left. This is the same as
1032 * inserting to the right, except we decrement the current
1033 * index first.
1035 * Because we're unsigned, we have to special case 0
1036 * (grumble).
1038 if (idx == 0) {
1039 memmove(&pd->pd_array[1], &pd->pd_array[0],
1040 pd->pd_count * sizeof(*pde));
1041 pd->pd_array[0].pde_key = pdk;
1042 pd->pd_array[0].pde_objref = po;
1043 pd->pd_count++;
1044 pd->pd_version++;
1045 rv = true;
1046 goto out;
1048 idx--;
1051 memmove(&pd->pd_array[idx + 2], &pd->pd_array[idx + 1],
1052 (pd->pd_count - (idx + 1)) * sizeof(*pde));
1053 pd->pd_array[idx + 1].pde_key = pdk;
1054 pd->pd_array[idx + 1].pde_objref = po;
1055 pd->pd_count++;
1057 pd->pd_version++;
1059 rv = true;
1061 out:
1062 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
1063 return (rv);
1067 * prop_dictionary_set_keysym --
1068 * Replace the object in the dictionary at the location encoded by
1069 * the keysym.
1071 bool
1072 prop_dictionary_set_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
1073 prop_object_t po)
1076 if (! (prop_object_is_dictionary(pd) &&
1077 prop_object_is_dictionary_keysym(pdk)))
1078 return (false);
1080 return (prop_dictionary_set(pd, pdk->pdk_key, po));
1083 static void
1084 _prop_dictionary_remove(prop_dictionary_t pd, struct _prop_dict_entry *pde,
1085 unsigned int idx)
1087 prop_dictionary_keysym_t pdk = pde->pde_key;
1088 prop_object_t po = pde->pde_objref;
1091 * Dictionary must be WRITE-LOCKED.
1094 _PROP_ASSERT(pd->pd_count != 0);
1095 _PROP_ASSERT(idx < pd->pd_count);
1096 _PROP_ASSERT(pde == &pd->pd_array[idx]);
1098 idx++;
1099 memmove(&pd->pd_array[idx - 1], &pd->pd_array[idx],
1100 (pd->pd_count - idx) * sizeof(*pde));
1101 pd->pd_count--;
1102 pd->pd_version++;
1105 prop_object_release(pdk);
1107 prop_object_release(po);
1111 * prop_dictionary_remove --
1112 * Remove the reference to an object with the specified key from
1113 * the dictionary.
1115 void
1116 prop_dictionary_remove(prop_dictionary_t pd, const char *key)
1118 struct _prop_dict_entry *pde;
1119 unsigned int idx;
1121 if (! prop_object_is_dictionary(pd))
1122 return;
1124 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
1126 /* XXX Should this be a _PROP_ASSERT()? */
1127 if (prop_dictionary_is_immutable(pd))
1128 goto out;
1130 pde = _prop_dict_lookup(pd, key, &idx);
1131 /* XXX Should this be a _PROP_ASSERT()? */
1132 if (pde == NULL)
1133 goto out;
1135 _prop_dictionary_remove(pd, pde, idx);
1136 out:
1137 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
1141 * prop_dictionary_remove_keysym --
1142 * Remove a reference to an object stored in the dictionary at the
1143 * location encoded by the keysym.
1145 void
1146 prop_dictionary_remove_keysym(prop_dictionary_t pd,
1147 prop_dictionary_keysym_t pdk)
1150 if (! (prop_object_is_dictionary(pd) &&
1151 prop_object_is_dictionary_keysym(pdk)))
1152 return;
1154 prop_dictionary_remove(pd, pdk->pdk_key);
1158 * prop_dictionary_equals --
1159 * Return true if the two dictionaries are equivalent. Note we do a
1160 * by-value comparison of the objects in the dictionary.
1162 bool
1163 prop_dictionary_equals(prop_dictionary_t dict1, prop_dictionary_t dict2)
1165 if (!prop_object_is_dictionary(dict1) ||
1166 !prop_object_is_dictionary(dict2))
1167 return (false);
1169 return (prop_object_equals(dict1, dict2));
1173 * prop_dictionary_keysym_cstring_nocopy --
1174 * Return an immutable reference to the keysym's value.
1176 const char *
1177 prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pdk)
1180 if (! prop_object_is_dictionary_keysym(pdk))
1181 return (NULL);
1183 return (pdk->pdk_key);
1187 * prop_dictionary_keysym_equals --
1188 * Return true if the two dictionary key symbols are equivalent.
1189 * Note: We do not compare the object references.
1191 bool
1192 prop_dictionary_keysym_equals(prop_dictionary_keysym_t pdk1,
1193 prop_dictionary_keysym_t pdk2)
1195 if (!prop_object_is_dictionary_keysym(pdk1) ||
1196 !prop_object_is_dictionary_keysym(pdk2))
1197 return (false);
1199 return (prop_object_equals(pdk1, pdk2));
1203 * prop_dictionary_externalize --
1204 * Externalize a dictionary, returning a NUL-terminated buffer
1205 * containing the XML-style representation. The buffer is allocated
1206 * with the M_TEMP memory type.
1208 char *
1209 prop_dictionary_externalize(prop_dictionary_t pd)
1211 struct _prop_object_externalize_context *ctx;
1212 char *cp;
1214 ctx = _prop_object_externalize_context_alloc();
1215 if (ctx == NULL)
1216 return (NULL);
1218 if (_prop_object_externalize_header(ctx) == false ||
1219 (*pd->pd_obj.po_type->pot_extern)(ctx, pd) == false ||
1220 _prop_object_externalize_footer(ctx) == false) {
1221 /* We are responsible for releasing the buffer. */
1222 _PROP_FREE(ctx->poec_buf, M_TEMP);
1223 _prop_object_externalize_context_free(ctx);
1224 return (NULL);
1227 cp = ctx->poec_buf;
1228 _prop_object_externalize_context_free(ctx);
1230 return (cp);
1234 * _prop_dictionary_internalize --
1235 * Parse a <dict>...</dict> and return the object created from the
1236 * external representation.
1238 * Internal state in via rec_data is the storage area for the last processed
1239 * key.
1240 * _prop_dictionary_internalize_body is the upper half of the parse loop.
1241 * It is responsible for parsing the key directly and storing it in the area
1242 * referenced by rec_data.
1243 * _prop_dictionary_internalize_cont is the lower half and called with the value
1244 * associated with the key.
1246 static bool _prop_dictionary_internalize_body(prop_stack_t,
1247 prop_object_t *, struct _prop_object_internalize_context *, char *);
1249 bool
1250 _prop_dictionary_internalize(prop_stack_t stack, prop_object_t *obj,
1251 struct _prop_object_internalize_context *ctx)
1253 prop_dictionary_t dict;
1254 char *tmpkey;
1256 /* We don't currently understand any attributes. */
1257 if (ctx->poic_tagattr != NULL)
1258 return (true);
1260 dict = prop_dictionary_create();
1261 if (dict == NULL)
1262 return (true);
1264 if (ctx->poic_is_empty_element) {
1265 *obj = dict;
1266 return (true);
1269 tmpkey = _PROP_MALLOC(PDK_MAXKEY + 1, M_TEMP);
1270 if (tmpkey == NULL) {
1271 prop_object_release(dict);
1272 return (true);
1275 *obj = dict;
1277 * Opening tag is found, storage for key allocated and
1278 * now continue to the first element.
1280 return _prop_dictionary_internalize_body(stack, obj, ctx, tmpkey);
1283 static bool
1284 _prop_dictionary_internalize_continue(prop_stack_t stack, prop_object_t *obj,
1285 struct _prop_object_internalize_context *ctx, void *data, prop_object_t child)
1287 prop_dictionary_t dict = *obj;
1288 char *tmpkey = data;
1290 _PROP_ASSERT(tmpkey != NULL);
1292 if (child == NULL ||
1293 prop_dictionary_set(dict, tmpkey, child) == false) {
1294 _PROP_FREE(tmpkey, M_TEMP);
1295 if (child != NULL)
1296 prop_object_release(child);
1297 prop_object_release(dict);
1298 *obj = NULL;
1299 return (true);
1302 prop_object_release(child);
1305 * key, value was added, now continue looking for the next key
1306 * or the closing tag.
1308 return _prop_dictionary_internalize_body(stack, obj, ctx, tmpkey);
1311 static bool
1312 _prop_dictionary_internalize_body(prop_stack_t stack, prop_object_t *obj,
1313 struct _prop_object_internalize_context *ctx, char *tmpkey)
1315 prop_dictionary_t dict = *obj;
1316 size_t keylen;
1318 /* Fetch the next tag. */
1319 if (_prop_object_internalize_find_tag(ctx, NULL, _PROP_TAG_TYPE_EITHER) == false)
1320 goto bad;
1322 /* Check to see if this is the end of the dictionary. */
1323 if (_PROP_TAG_MATCH(ctx, "dict") &&
1324 ctx->poic_tag_type == _PROP_TAG_TYPE_END) {
1325 _PROP_FREE(tmpkey, M_TEMP);
1326 return (true);
1329 /* Ok, it must be a non-empty key start tag. */
1330 if (!_PROP_TAG_MATCH(ctx, "key") ||
1331 ctx->poic_tag_type != _PROP_TAG_TYPE_START ||
1332 ctx->poic_is_empty_element)
1333 goto bad;
1335 if (_prop_object_internalize_decode_string(ctx,
1336 tmpkey, PDK_MAXKEY, &keylen,
1337 &ctx->poic_cp) == false)
1338 goto bad;
1340 _PROP_ASSERT(keylen <= PDK_MAXKEY);
1341 tmpkey[keylen] = '\0';
1343 if (_prop_object_internalize_find_tag(ctx, "key",
1344 _PROP_TAG_TYPE_END) == false)
1345 goto bad;
1347 /* ..and now the beginning of the value. */
1348 if (_prop_object_internalize_find_tag(ctx, NULL,
1349 _PROP_TAG_TYPE_START) == false)
1350 goto bad;
1353 * Key is found, now wait for value to be parsed.
1355 if (_prop_stack_push(stack, *obj,
1356 _prop_dictionary_internalize_continue,
1357 tmpkey, NULL))
1358 return (false);
1360 bad:
1361 _PROP_FREE(tmpkey, M_TEMP);
1362 prop_object_release(dict);
1363 *obj = NULL;
1364 return (true);
1368 * prop_dictionary_internalize --
1369 * Create a dictionary by parsing the NUL-terminated XML-style
1370 * representation.
1372 prop_dictionary_t
1373 prop_dictionary_internalize(const char *xml)
1375 return _prop_generic_internalize(xml, "dict");
1378 #if !defined(_KERNEL) && !defined(_STANDALONE)
1380 * prop_dictionary_externalize_to_file --
1381 * Externalize a dictionary to the specified file.
1383 bool
1384 prop_dictionary_externalize_to_file(prop_dictionary_t dict, const char *fname)
1386 char *xml;
1387 bool rv;
1388 int save_errno = 0; /* XXXGCC -Wuninitialized [mips, ...] */
1390 xml = prop_dictionary_externalize(dict);
1391 if (xml == NULL)
1392 return (false);
1393 rv = _prop_object_externalize_write_file(fname, xml, strlen(xml),
1394 false);
1395 if (rv == false)
1396 save_errno = errno;
1397 _PROP_FREE(xml, M_TEMP);
1398 if (rv == false)
1399 errno = save_errno;
1401 return (rv);
1405 * prop_dictionary_internalize_from_file --
1406 * Internalize a dictionary from a file.
1408 prop_dictionary_t
1409 prop_dictionary_internalize_from_file(const char *fname)
1411 struct _prop_object_internalize_mapped_file *mf;
1412 prop_dictionary_t dict;
1414 mf = _prop_object_internalize_map_file(fname);
1415 if (mf == NULL)
1416 return (NULL);
1417 dict = prop_dictionary_internalize(mf->poimf_xml);
1418 _prop_object_internalize_unmap_file(mf);
1420 return (dict);
1422 #endif /* !_KERNEL && !_STANDALONE */