1 /* $NetBSD: prop_dictionary.c,v 1.32 2008/08/03 04:00:12 thorpej Exp $ */
4 * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
33 #include "prop_object_impl.h"
34 #include "prop_rb_impl.h"
39 * We implement these like arrays, but we keep them sorted by key.
40 * This allows us to binary-search as well as keep externalized output
41 * sane-looking for human eyes.
44 #define EXPAND_STEP 16
47 * prop_dictionary_keysym_t is allocated with space at the end to hold the
48 * key. This must be a regular object so that we can maintain sane iterator
49 * semantics -- we don't want to require that the caller release the result
50 * of prop_object_iterator_next().
52 * We'd like to have some small'ish keysym objects for up-to-16 characters
53 * in a key, some for up-to-32 characters in a key, and then a final bucket
54 * for up-to-128 characters in a key (not including NUL). Keys longer than
55 * 128 characters are not allowed.
57 struct _prop_dictionary_keysym
{
58 struct _prop_object pdk_obj
;
60 struct rb_node pdk_link
;
62 /* actually variable length */
65 #define RBNODE_TO_PDK(n) \
66 ((struct _prop_dictionary_keysym *) \
67 ((uintptr_t)n - offsetof(struct _prop_dictionary_keysym, pdk_link)))
69 /* pdk_key[1] takes care of the NUL */
70 #define PDK_SIZE_16 (sizeof(struct _prop_dictionary_keysym) + 16)
71 #define PDK_SIZE_32 (sizeof(struct _prop_dictionary_keysym) + 32)
72 #define PDK_SIZE_128 (sizeof(struct _prop_dictionary_keysym) + 128)
74 #define PDK_MAXKEY 128
76 _PROP_POOL_INIT(_prop_dictionary_keysym16_pool
, PDK_SIZE_16
, "pdict16")
77 _PROP_POOL_INIT(_prop_dictionary_keysym32_pool
, PDK_SIZE_32
, "pdict32")
78 _PROP_POOL_INIT(_prop_dictionary_keysym128_pool
, PDK_SIZE_128
, "pdict128")
80 struct _prop_dict_entry
{
81 prop_dictionary_keysym_t pde_key
;
82 prop_object_t pde_objref
;
85 struct _prop_dictionary
{
86 struct _prop_object pd_obj
;
87 _PROP_RWLOCK_DECL(pd_rwlock
)
88 struct _prop_dict_entry
*pd_array
;
89 unsigned int pd_capacity
;
90 unsigned int pd_count
;
96 #define PD_F_IMMUTABLE 0x01 /* dictionary is immutable */
98 _PROP_POOL_INIT(_prop_dictionary_pool
, sizeof(struct _prop_dictionary
),
100 _PROP_MALLOC_DEFINE(M_PROP_DICT
, "prop dictionary",
101 "property dictionary container object")
103 static _prop_object_free_rv_t
104 _prop_dictionary_free(prop_stack_t
, prop_object_t
*);
105 static void _prop_dictionary_emergency_free(prop_object_t
);
106 static bool _prop_dictionary_externalize(
107 struct _prop_object_externalize_context
*,
109 static _prop_object_equals_rv_t
110 _prop_dictionary_equals(prop_object_t
, prop_object_t
,
112 prop_object_t
*, prop_object_t
*);
113 static void _prop_dictionary_equals_finish(prop_object_t
, prop_object_t
);
114 static prop_object_iterator_t
115 _prop_dictionary_iterator_locked(prop_dictionary_t
);
117 _prop_dictionary_iterator_next_object_locked(void *);
119 _prop_dictionary_get_keysym(prop_dictionary_t
,
120 prop_dictionary_keysym_t
, bool);
122 _prop_dictionary_get(prop_dictionary_t
, const char *, bool);
124 static const struct _prop_object_type _prop_object_type_dictionary
= {
125 .pot_type
= PROP_TYPE_DICTIONARY
,
126 .pot_free
= _prop_dictionary_free
,
127 .pot_emergency_free
= _prop_dictionary_emergency_free
,
128 .pot_extern
= _prop_dictionary_externalize
,
129 .pot_equals
= _prop_dictionary_equals
,
130 .pot_equals_finish
= _prop_dictionary_equals_finish
,
133 static _prop_object_free_rv_t
134 _prop_dict_keysym_free(prop_stack_t
, prop_object_t
*);
135 static bool _prop_dict_keysym_externalize(
136 struct _prop_object_externalize_context
*,
138 static _prop_object_equals_rv_t
139 _prop_dict_keysym_equals(prop_object_t
, prop_object_t
,
141 prop_object_t
*, prop_object_t
*);
143 static const struct _prop_object_type _prop_object_type_dict_keysym
= {
144 .pot_type
= PROP_TYPE_DICT_KEYSYM
,
145 .pot_free
= _prop_dict_keysym_free
,
146 .pot_extern
= _prop_dict_keysym_externalize
,
147 .pot_equals
= _prop_dict_keysym_equals
,
150 #define prop_object_is_dictionary(x) \
151 ((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_dictionary)
152 #define prop_object_is_dictionary_keysym(x) \
153 ((x) != NULL && (x)->pdk_obj.po_type == &_prop_object_type_dict_keysym)
155 #define prop_dictionary_is_immutable(x) \
156 (((x)->pd_flags & PD_F_IMMUTABLE) != 0)
158 struct _prop_dictionary_iterator
{
159 struct _prop_object_iterator pdi_base
;
160 unsigned int pdi_index
;
164 * Dictionary key symbols are immutable, and we are likely to have many
165 * duplicated key symbols. So, to save memory, we unique'ify key symbols
166 * so we only have to have one copy of each string.
170 _prop_dict_keysym_rb_compare_nodes(const struct rb_node
*n1
,
171 const struct rb_node
*n2
)
173 const prop_dictionary_keysym_t pdk1
= RBNODE_TO_PDK(n1
);
174 const prop_dictionary_keysym_t pdk2
= RBNODE_TO_PDK(n2
);
176 return (strcmp(pdk1
->pdk_key
, pdk2
->pdk_key
));
180 _prop_dict_keysym_rb_compare_key(const struct rb_node
*n
,
183 const prop_dictionary_keysym_t pdk
= RBNODE_TO_PDK(n
);
186 return (strcmp(pdk
->pdk_key
, cp
));
189 static const struct rb_tree_ops _prop_dict_keysym_rb_tree_ops
= {
190 .rbto_compare_nodes
= _prop_dict_keysym_rb_compare_nodes
,
191 .rbto_compare_key
= _prop_dict_keysym_rb_compare_key
,
194 static struct rb_tree _prop_dict_keysym_tree
;
195 static bool _prop_dict_keysym_tree_initialized
;
197 _PROP_MUTEX_DECL_STATIC(_prop_dict_keysym_tree_mutex
)
200 _prop_dict_keysym_put(prop_dictionary_keysym_t pdk
)
203 if (pdk
->pdk_size
<= PDK_SIZE_16
)
204 _PROP_POOL_PUT(_prop_dictionary_keysym16_pool
, pdk
);
205 else if (pdk
->pdk_size
<= PDK_SIZE_32
)
206 _PROP_POOL_PUT(_prop_dictionary_keysym32_pool
, pdk
);
208 _PROP_ASSERT(pdk
->pdk_size
<= PDK_SIZE_128
);
209 _PROP_POOL_PUT(_prop_dictionary_keysym128_pool
, pdk
);
214 static _prop_object_free_rv_t
215 _prop_dict_keysym_free(prop_stack_t stack
, prop_object_t
*obj
)
217 prop_dictionary_keysym_t pdk
= *obj
;
219 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex
);
220 _prop_rb_tree_remove_node(&_prop_dict_keysym_tree
, &pdk
->pdk_link
);
221 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex
);
223 _prop_dict_keysym_put(pdk
);
225 return _PROP_OBJECT_FREE_DONE
;
229 _prop_dict_keysym_externalize(struct _prop_object_externalize_context
*ctx
,
232 prop_dictionary_keysym_t pdk
= v
;
234 /* We externalize these as strings, and they're never empty. */
236 _PROP_ASSERT(pdk
->pdk_key
[0] != '\0');
238 if (_prop_object_externalize_start_tag(ctx
, "string") == false ||
239 _prop_object_externalize_append_encoded_cstring(ctx
,
240 pdk
->pdk_key
) == false ||
241 _prop_object_externalize_end_tag(ctx
, "string") == false)
248 static _prop_object_equals_rv_t
249 _prop_dict_keysym_equals(prop_object_t v1
, prop_object_t v2
,
250 void **stored_pointer1
, void **stored_pointer2
,
251 prop_object_t
*next_obj1
, prop_object_t
*next_obj2
)
253 prop_dictionary_keysym_t pdk1
= v1
;
254 prop_dictionary_keysym_t pdk2
= v2
;
257 * There is only ever one copy of a keysym at any given time,
258 * so we can reduce this to a simple pointer equality check.
261 return _PROP_OBJECT_EQUALS_TRUE
;
263 return _PROP_OBJECT_EQUALS_FALSE
;
266 static prop_dictionary_keysym_t
267 _prop_dict_keysym_alloc(const char *key
)
269 prop_dictionary_keysym_t opdk
, pdk
;
270 const struct rb_node
*n
;
275 * Check to see if this already exists in the tree. If it does,
276 * we just retain it and return it.
278 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex
);
279 if (! _prop_dict_keysym_tree_initialized
) {
280 _prop_rb_tree_init(&_prop_dict_keysym_tree
,
281 &_prop_dict_keysym_rb_tree_ops
);
282 _prop_dict_keysym_tree_initialized
= true;
284 n
= _prop_rb_tree_find(&_prop_dict_keysym_tree
, key
);
286 opdk
= RBNODE_TO_PDK(n
);
287 prop_object_retain(opdk
);
288 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex
);
292 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex
);
295 * Not in the tree. Create it now.
298 size
= sizeof(*pdk
) + strlen(key
) /* pdk_key[1] covers the NUL */;
300 if (size
<= PDK_SIZE_16
)
301 pdk
= _PROP_POOL_GET(_prop_dictionary_keysym16_pool
);
302 else if (size
<= PDK_SIZE_32
)
303 pdk
= _PROP_POOL_GET(_prop_dictionary_keysym32_pool
);
304 else if (size
<= PDK_SIZE_128
)
305 pdk
= _PROP_POOL_GET(_prop_dictionary_keysym128_pool
);
307 pdk
= NULL
; /* key too long */
312 _prop_object_init(&pdk
->pdk_obj
, &_prop_object_type_dict_keysym
);
314 strcpy(pdk
->pdk_key
, key
);
315 pdk
->pdk_size
= size
;
318 * We dropped the mutex when we allocated the new object, so
319 * we have to check again if it is in the tree.
321 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex
);
322 n
= _prop_rb_tree_find(&_prop_dict_keysym_tree
, key
);
324 opdk
= RBNODE_TO_PDK(n
);
325 prop_object_retain(opdk
);
326 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex
);
327 _prop_dict_keysym_put(pdk
);
330 rv
= _prop_rb_tree_insert_node(&_prop_dict_keysym_tree
, &pdk
->pdk_link
);
331 _PROP_ASSERT(rv
== true);
332 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex
);
336 static _prop_object_free_rv_t
337 _prop_dictionary_free(prop_stack_t stack
, prop_object_t
*obj
)
339 prop_dictionary_t pd
= *obj
;
340 prop_dictionary_keysym_t pdk
;
343 _PROP_ASSERT(pd
->pd_count
<= pd
->pd_capacity
);
344 _PROP_ASSERT((pd
->pd_capacity
== 0 && pd
->pd_array
== NULL
) ||
345 (pd
->pd_capacity
!= 0 && pd
->pd_array
!= NULL
));
347 /* The empty dictorinary is easy, handle that first. */
348 if (pd
->pd_count
== 0) {
349 if (pd
->pd_array
!= NULL
)
350 _PROP_FREE(pd
->pd_array
, M_PROP_DICT
);
352 _PROP_RWLOCK_DESTROY(pd
->pd_rwlock
);
354 _PROP_POOL_PUT(_prop_dictionary_pool
, pd
);
356 return (_PROP_OBJECT_FREE_DONE
);
359 po
= pd
->pd_array
[pd
->pd_count
- 1].pde_objref
;
360 _PROP_ASSERT(po
!= NULL
);
364 * If we are in emergency release mode,
365 * just let caller recurse down.
368 return (_PROP_OBJECT_FREE_FAILED
);
371 /* Otherwise, try to push the current object on the stack. */
372 if (!_prop_stack_push(stack
, pd
, NULL
, NULL
, NULL
)) {
373 /* Push failed, entering emergency release mode. */
374 return (_PROP_OBJECT_FREE_FAILED
);
376 /* Object pushed on stack, caller will release it. */
378 pdk
= pd
->pd_array
[pd
->pd_count
].pde_key
;
379 _PROP_ASSERT(pdk
!= NULL
);
380 prop_object_release(pdk
);
382 return (_PROP_OBJECT_FREE_RECURSE
);
386 _prop_dictionary_emergency_free(prop_object_t obj
)
388 prop_dictionary_t pd
= obj
;
389 prop_dictionary_keysym_t pdk
;
391 _PROP_ASSERT(pd
->pd_count
!= 0);
394 pdk
= pd
->pd_array
[pd
->pd_count
].pde_key
;
395 _PROP_ASSERT(pdk
!= NULL
);
396 prop_object_release(pdk
);
400 _prop_dictionary_externalize(struct _prop_object_externalize_context
*ctx
,
403 prop_dictionary_t pd
= v
;
404 prop_dictionary_keysym_t pdk
;
405 struct _prop_object
*po
;
406 prop_object_iterator_t pi
;
410 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
412 if (pd
->pd_count
== 0) {
413 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
414 return (_prop_object_externalize_empty_tag(ctx
, "dict"));
417 if (_prop_object_externalize_start_tag(ctx
, "dict") == false ||
418 _prop_object_externalize_append_char(ctx
, '\n') == false)
421 pi
= _prop_dictionary_iterator_locked(pd
);
426 _PROP_ASSERT(ctx
->poec_depth
!= 0);
428 while ((pdk
= _prop_dictionary_iterator_next_object_locked(pi
))
430 po
= _prop_dictionary_get_keysym(pd
, pdk
, true);
432 _prop_object_externalize_start_tag(ctx
, "key") == false ||
433 _prop_object_externalize_append_encoded_cstring(ctx
,
434 pdk
->pdk_key
) == false ||
435 _prop_object_externalize_end_tag(ctx
, "key") == false ||
436 (*po
->po_type
->pot_extern
)(ctx
, po
) == false) {
437 prop_object_iterator_release(pi
);
442 prop_object_iterator_release(pi
);
445 for (i
= 0; i
< ctx
->poec_depth
; i
++) {
446 if (_prop_object_externalize_append_char(ctx
, '\t') == false)
449 if (_prop_object_externalize_end_tag(ctx
, "dict") == false)
455 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
460 static _prop_object_equals_rv_t
461 _prop_dictionary_equals(prop_object_t v1
, prop_object_t v2
,
462 void **stored_pointer1
, void **stored_pointer2
,
463 prop_object_t
*next_obj1
, prop_object_t
*next_obj2
)
465 prop_dictionary_t dict1
= v1
;
466 prop_dictionary_t dict2
= v2
;
468 _prop_object_equals_rv_t rv
= _PROP_OBJECT_EQUALS_FALSE
;
471 return (_PROP_OBJECT_EQUALS_TRUE
);
473 _PROP_ASSERT(*stored_pointer1
== *stored_pointer2
);
475 idx
= (uintptr_t)*stored_pointer1
;
478 if ((uintptr_t)dict1
< (uintptr_t)dict2
) {
479 _PROP_RWLOCK_RDLOCK(dict1
->pd_rwlock
);
480 _PROP_RWLOCK_RDLOCK(dict2
->pd_rwlock
);
482 _PROP_RWLOCK_RDLOCK(dict2
->pd_rwlock
);
483 _PROP_RWLOCK_RDLOCK(dict1
->pd_rwlock
);
487 if (dict1
->pd_count
!= dict2
->pd_count
)
490 if (idx
== dict1
->pd_count
) {
491 rv
= _PROP_OBJECT_EQUALS_TRUE
;
495 _PROP_ASSERT(idx
< dict1
->pd_count
);
497 *stored_pointer1
= (void *)(idx
+ 1);
498 *stored_pointer2
= (void *)(idx
+ 1);
500 *next_obj1
= &dict1
->pd_array
[idx
].pde_objref
;
501 *next_obj2
= &dict2
->pd_array
[idx
].pde_objref
;
503 if (!prop_dictionary_keysym_equals(dict1
->pd_array
[idx
].pde_key
,
504 dict2
->pd_array
[idx
].pde_key
))
507 return (_PROP_OBJECT_EQUALS_RECURSE
);
510 _PROP_RWLOCK_UNLOCK(dict1
->pd_rwlock
);
511 _PROP_RWLOCK_UNLOCK(dict2
->pd_rwlock
);
516 _prop_dictionary_equals_finish(prop_object_t v1
, prop_object_t v2
)
518 _PROP_RWLOCK_UNLOCK(((prop_dictionary_t
)v1
)->pd_rwlock
);
519 _PROP_RWLOCK_UNLOCK(((prop_dictionary_t
)v2
)->pd_rwlock
);
522 static prop_dictionary_t
523 _prop_dictionary_alloc(unsigned int capacity
)
525 prop_dictionary_t pd
;
526 struct _prop_dict_entry
*array
;
529 array
= _PROP_CALLOC(capacity
* sizeof(*array
), M_PROP_DICT
);
535 pd
= _PROP_POOL_GET(_prop_dictionary_pool
);
537 _prop_object_init(&pd
->pd_obj
, &_prop_object_type_dictionary
);
539 _PROP_RWLOCK_INIT(pd
->pd_rwlock
);
540 pd
->pd_array
= array
;
541 pd
->pd_capacity
= capacity
;
546 } else if (array
!= NULL
)
547 _PROP_FREE(array
, M_PROP_DICT
);
553 _prop_dictionary_expand(prop_dictionary_t pd
, unsigned int capacity
)
555 struct _prop_dict_entry
*array
, *oarray
;
558 * Dictionary must be WRITE-LOCKED.
561 oarray
= pd
->pd_array
;
563 array
= _PROP_CALLOC(capacity
* sizeof(*array
), M_PROP_DICT
);
567 memcpy(array
, oarray
, pd
->pd_capacity
* sizeof(*array
));
568 pd
->pd_array
= array
;
569 pd
->pd_capacity
= capacity
;
572 _PROP_FREE(oarray
, M_PROP_DICT
);
578 _prop_dictionary_iterator_next_object_locked(void *v
)
580 struct _prop_dictionary_iterator
*pdi
= v
;
581 prop_dictionary_t pd
= pdi
->pdi_base
.pi_obj
;
582 prop_dictionary_keysym_t pdk
= NULL
;
584 _PROP_ASSERT(prop_object_is_dictionary(pd
));
586 if (pd
->pd_version
!= pdi
->pdi_base
.pi_version
)
587 goto out
; /* dictionary changed during iteration */
589 _PROP_ASSERT(pdi
->pdi_index
<= pd
->pd_count
);
591 if (pdi
->pdi_index
== pd
->pd_count
)
592 goto out
; /* we've iterated all objects */
594 pdk
= pd
->pd_array
[pdi
->pdi_index
].pde_key
;
602 _prop_dictionary_iterator_next_object(void *v
)
604 struct _prop_dictionary_iterator
*pdi
= v
;
605 prop_dictionary_t pd
= pdi
->pdi_base
.pi_obj
;
606 prop_dictionary_keysym_t pdk
;
608 _PROP_ASSERT(prop_object_is_dictionary(pd
));
610 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
611 pdk
= _prop_dictionary_iterator_next_object_locked(pdi
);
612 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
617 _prop_dictionary_iterator_reset_locked(void *v
)
619 struct _prop_dictionary_iterator
*pdi
= v
;
620 prop_dictionary_t pd
= pdi
->pdi_base
.pi_obj
;
622 _PROP_ASSERT(prop_object_is_dictionary(pd
));
625 pdi
->pdi_base
.pi_version
= pd
->pd_version
;
629 _prop_dictionary_iterator_reset(void *v
)
631 struct _prop_dictionary_iterator
*pdi
= v
;
632 prop_dictionary_t pd
= pdi
->pdi_base
.pi_obj
;
634 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
635 _prop_dictionary_iterator_reset_locked(pdi
);
636 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
640 * prop_dictionary_create --
641 * Create a dictionary.
644 prop_dictionary_create(void)
647 return (_prop_dictionary_alloc(0));
651 * prop_dictionary_create_with_capacity --
652 * Create a dictionary with the capacity to store N objects.
655 prop_dictionary_create_with_capacity(unsigned int capacity
)
658 return (_prop_dictionary_alloc(capacity
));
662 * prop_dictionary_copy --
663 * Copy a dictionary. The new dictionary has an initial capacity equal
664 * to the number of objects stored int the original dictionary. The new
665 * dictionary contains refrences to the original dictionary's objects,
666 * not copies of those objects (i.e. a shallow copy).
669 prop_dictionary_copy(prop_dictionary_t opd
)
671 prop_dictionary_t pd
;
672 prop_dictionary_keysym_t pdk
;
676 if (! prop_object_is_dictionary(opd
))
679 _PROP_RWLOCK_RDLOCK(opd
->pd_rwlock
);
681 pd
= _prop_dictionary_alloc(opd
->pd_count
);
683 for (idx
= 0; idx
< opd
->pd_count
; idx
++) {
684 pdk
= opd
->pd_array
[idx
].pde_key
;
685 po
= opd
->pd_array
[idx
].pde_objref
;
687 prop_object_retain(pdk
);
688 prop_object_retain(po
);
690 pd
->pd_array
[idx
].pde_key
= pdk
;
691 pd
->pd_array
[idx
].pde_objref
= po
;
693 pd
->pd_count
= opd
->pd_count
;
694 pd
->pd_flags
= opd
->pd_flags
;
696 _PROP_RWLOCK_UNLOCK(opd
->pd_rwlock
);
701 * prop_dictionary_copy_mutable --
702 * Like prop_dictionary_copy(), but the resulting dictionary is
706 prop_dictionary_copy_mutable(prop_dictionary_t opd
)
708 prop_dictionary_t pd
;
710 if (! prop_object_is_dictionary(opd
))
713 pd
= prop_dictionary_copy(opd
);
715 pd
->pd_flags
&= ~PD_F_IMMUTABLE
;
721 * prop_dictionary_make_immutable --
722 * Set the immutable flag on that dictionary.
725 prop_dictionary_make_immutable(prop_dictionary_t pd
)
728 _PROP_RWLOCK_WRLOCK(pd
->pd_rwlock
);
729 if (prop_dictionary_is_immutable(pd
) == false)
730 pd
->pd_flags
|= PD_F_IMMUTABLE
;
731 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
735 * prop_dictionary_count --
736 * Return the number of objects stored in the dictionary.
739 prop_dictionary_count(prop_dictionary_t pd
)
743 if (! prop_object_is_dictionary(pd
))
746 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
748 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
754 * prop_dictionary_ensure_capacity --
755 * Ensure that the dictionary has the capacity to store the specified
756 * total number of objects (including the objects already stored in
760 prop_dictionary_ensure_capacity(prop_dictionary_t pd
, unsigned int capacity
)
764 if (! prop_object_is_dictionary(pd
))
767 _PROP_RWLOCK_WRLOCK(pd
->pd_rwlock
);
768 if (capacity
> pd
->pd_capacity
)
769 rv
= _prop_dictionary_expand(pd
, capacity
);
772 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
776 static prop_object_iterator_t
777 _prop_dictionary_iterator_locked(prop_dictionary_t pd
)
779 struct _prop_dictionary_iterator
*pdi
;
781 if (! prop_object_is_dictionary(pd
))
784 pdi
= _PROP_CALLOC(sizeof(*pdi
), M_TEMP
);
787 pdi
->pdi_base
.pi_next_object
= _prop_dictionary_iterator_next_object
;
788 pdi
->pdi_base
.pi_reset
= _prop_dictionary_iterator_reset
;
789 prop_object_retain(pd
);
790 pdi
->pdi_base
.pi_obj
= pd
;
791 _prop_dictionary_iterator_reset_locked(pdi
);
793 return (&pdi
->pdi_base
);
797 * prop_dictionary_iterator --
798 * Return an iterator for the dictionary. The dictionary is retained by
801 prop_object_iterator_t
802 prop_dictionary_iterator(prop_dictionary_t pd
)
804 prop_object_iterator_t pi
;
806 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
807 pi
= _prop_dictionary_iterator_locked(pd
);
808 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
813 * prop_dictionary_all_keys --
814 * Return an array containing a snapshot of all of the keys
818 prop_dictionary_all_keys(prop_dictionary_t pd
)
824 if (! prop_object_is_dictionary(pd
))
827 /* There is no pressing need to lock the dictionary for this. */
828 array
= prop_array_create_with_capacity(pd
->pd_count
);
830 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
832 for (idx
= 0; idx
< pd
->pd_count
; idx
++) {
833 rv
= prop_array_add(array
, pd
->pd_array
[idx
].pde_key
);
838 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
841 prop_object_release(array
);
847 static struct _prop_dict_entry
*
848 _prop_dict_lookup(prop_dictionary_t pd
, const char *key
,
851 struct _prop_dict_entry
*pde
;
852 unsigned int base
, idx
, distance
;
856 * Dictionary must be READ-LOCKED or WRITE-LOCKED.
859 for (idx
= 0, base
= 0, distance
= pd
->pd_count
; distance
!= 0;
861 idx
= base
+ (distance
>> 1);
862 pde
= &pd
->pd_array
[idx
];
863 _PROP_ASSERT(pde
->pde_key
!= NULL
);
864 res
= strcmp(key
, pde
->pde_key
->pdk_key
);
870 if (res
> 0) { /* key > pdk_key: move right */
873 } /* else move left */
876 /* idx points to the slot we looked at last. */
883 _prop_dictionary_get(prop_dictionary_t pd
, const char *key
, bool locked
)
885 const struct _prop_dict_entry
*pde
;
886 prop_object_t po
= NULL
;
888 if (! prop_object_is_dictionary(pd
))
892 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
893 pde
= _prop_dict_lookup(pd
, key
, NULL
);
895 _PROP_ASSERT(pde
->pde_objref
!= NULL
);
896 po
= pde
->pde_objref
;
899 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
903 * prop_dictionary_get --
904 * Return the object stored with specified key.
907 prop_dictionary_get(prop_dictionary_t pd
, const char *key
)
911 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
912 po
= _prop_dictionary_get(pd
, key
, true);
913 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
918 _prop_dictionary_get_keysym(prop_dictionary_t pd
, prop_dictionary_keysym_t pdk
,
922 if (! (prop_object_is_dictionary(pd
) &&
923 prop_object_is_dictionary_keysym(pdk
)))
926 return (_prop_dictionary_get(pd
, pdk
->pdk_key
, locked
));
930 * prop_dictionary_get_keysym --
931 * Return the object stored at the location encoded by the keysym.
934 prop_dictionary_get_keysym(prop_dictionary_t pd
, prop_dictionary_keysym_t pdk
)
937 return (_prop_dictionary_get_keysym(pd
, pdk
, false));
941 * prop_dictionary_set --
942 * Store a reference to an object at with the specified key.
943 * If the key already exisit, the original object is released.
946 prop_dictionary_set(prop_dictionary_t pd
, const char *key
, prop_object_t po
)
948 struct _prop_dict_entry
*pde
;
949 prop_dictionary_keysym_t pdk
;
953 if (! prop_object_is_dictionary(pd
))
956 _PROP_ASSERT(pd
->pd_count
<= pd
->pd_capacity
);
958 if (prop_dictionary_is_immutable(pd
))
961 _PROP_RWLOCK_WRLOCK(pd
->pd_rwlock
);
963 pde
= _prop_dict_lookup(pd
, key
, &idx
);
965 prop_object_t opo
= pde
->pde_objref
;
966 prop_object_retain(po
);
967 pde
->pde_objref
= po
;
968 prop_object_release(opo
);
973 pdk
= _prop_dict_keysym_alloc(key
);
977 if (pd
->pd_count
== pd
->pd_capacity
&&
978 _prop_dictionary_expand(pd
,
979 pd
->pd_capacity
+ EXPAND_STEP
) == false) {
980 prop_object_release(pdk
);
984 /* At this point, the store will succeed. */
985 prop_object_retain(po
);
987 if (pd
->pd_count
== 0) {
988 pd
->pd_array
[0].pde_key
= pdk
;
989 pd
->pd_array
[0].pde_objref
= po
;
996 pde
= &pd
->pd_array
[idx
];
997 _PROP_ASSERT(pde
->pde_key
!= NULL
);
999 if (strcmp(key
, pde
->pde_key
->pdk_key
) < 0) {
1001 * key < pdk_key: insert to the left. This is the same as
1002 * inserting to the right, except we decrement the current
1005 * Because we're unsigned, we have to special case 0
1009 memmove(&pd
->pd_array
[1], &pd
->pd_array
[0],
1010 pd
->pd_count
* sizeof(*pde
));
1011 pd
->pd_array
[0].pde_key
= pdk
;
1012 pd
->pd_array
[0].pde_objref
= po
;
1021 memmove(&pd
->pd_array
[idx
+ 2], &pd
->pd_array
[idx
+ 1],
1022 (pd
->pd_count
- (idx
+ 1)) * sizeof(*pde
));
1023 pd
->pd_array
[idx
+ 1].pde_key
= pdk
;
1024 pd
->pd_array
[idx
+ 1].pde_objref
= po
;
1032 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
1037 * prop_dictionary_set_keysym --
1038 * Replace the object in the dictionary at the location encoded by
1042 prop_dictionary_set_keysym(prop_dictionary_t pd
, prop_dictionary_keysym_t pdk
,
1046 if (! (prop_object_is_dictionary(pd
) &&
1047 prop_object_is_dictionary_keysym(pdk
)))
1050 return (prop_dictionary_set(pd
, pdk
->pdk_key
, po
));
1054 _prop_dictionary_remove(prop_dictionary_t pd
, struct _prop_dict_entry
*pde
,
1057 prop_dictionary_keysym_t pdk
= pde
->pde_key
;
1058 prop_object_t po
= pde
->pde_objref
;
1061 * Dictionary must be WRITE-LOCKED.
1064 _PROP_ASSERT(pd
->pd_count
!= 0);
1065 _PROP_ASSERT(idx
< pd
->pd_count
);
1066 _PROP_ASSERT(pde
== &pd
->pd_array
[idx
]);
1069 memmove(&pd
->pd_array
[idx
- 1], &pd
->pd_array
[idx
],
1070 (pd
->pd_count
- idx
) * sizeof(*pde
));
1074 prop_object_release(pdk
);
1075 prop_object_release(po
);
1079 * prop_dictionary_remove --
1080 * Remove the reference to an object with the specified key from
1084 prop_dictionary_remove(prop_dictionary_t pd
, const char *key
)
1086 struct _prop_dict_entry
*pde
;
1089 if (! prop_object_is_dictionary(pd
))
1092 _PROP_RWLOCK_WRLOCK(pd
->pd_rwlock
);
1094 /* XXX Should this be a _PROP_ASSERT()? */
1095 if (prop_dictionary_is_immutable(pd
))
1098 pde
= _prop_dict_lookup(pd
, key
, &idx
);
1099 /* XXX Should this be a _PROP_ASSERT()? */
1103 _prop_dictionary_remove(pd
, pde
, idx
);
1105 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
1109 * prop_dictionary_remove_keysym --
1110 * Remove a reference to an object stored in the dictionary at the
1111 * location encoded by the keysym.
1114 prop_dictionary_remove_keysym(prop_dictionary_t pd
,
1115 prop_dictionary_keysym_t pdk
)
1118 if (! (prop_object_is_dictionary(pd
) &&
1119 prop_object_is_dictionary_keysym(pdk
)))
1122 prop_dictionary_remove(pd
, pdk
->pdk_key
);
1126 * prop_dictionary_equals --
1127 * Return true if the two dictionaries are equivalent. Note we do a
1128 * by-value comparison of the objects in the dictionary.
1131 prop_dictionary_equals(prop_dictionary_t dict1
, prop_dictionary_t dict2
)
1133 if (!prop_object_is_dictionary(dict1
) ||
1134 !prop_object_is_dictionary(dict2
))
1137 return (prop_object_equals(dict1
, dict2
));
1141 * prop_dictionary_keysym_cstring_nocopy --
1142 * Return an immutable reference to the keysym's value.
1145 prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pdk
)
1148 if (! prop_object_is_dictionary_keysym(pdk
))
1151 return (pdk
->pdk_key
);
1155 * prop_dictionary_keysym_equals --
1156 * Return true if the two dictionary key symbols are equivalent.
1157 * Note: We do not compare the object references.
1160 prop_dictionary_keysym_equals(prop_dictionary_keysym_t pdk1
,
1161 prop_dictionary_keysym_t pdk2
)
1163 if (!prop_object_is_dictionary_keysym(pdk1
) ||
1164 !prop_object_is_dictionary_keysym(pdk2
))
1167 return (prop_object_equals(pdk1
, pdk2
));
1171 * prop_dictionary_externalize --
1172 * Externalize a dictionary, returning a NUL-terminated buffer
1173 * containing the XML-style representation. The buffer is allocated
1174 * with the M_TEMP memory type.
1177 prop_dictionary_externalize(prop_dictionary_t pd
)
1179 struct _prop_object_externalize_context
*ctx
;
1182 ctx
= _prop_object_externalize_context_alloc();
1186 if (_prop_object_externalize_header(ctx
) == false ||
1187 (*pd
->pd_obj
.po_type
->pot_extern
)(ctx
, pd
) == false ||
1188 _prop_object_externalize_footer(ctx
) == false) {
1189 /* We are responsible for releasing the buffer. */
1190 _PROP_FREE(ctx
->poec_buf
, M_TEMP
);
1191 _prop_object_externalize_context_free(ctx
);
1196 _prop_object_externalize_context_free(ctx
);
1202 * _prop_dictionary_internalize --
1203 * Parse a <dict>...</dict> and return the object created from the
1204 * external representation.
1206 * Internal state in via rec_data is the storage area for the last processed
1208 * _prop_dictionary_internalize_body is the upper half of the parse loop.
1209 * It is responsible for parsing the key directly and storing it in the area
1210 * referenced by rec_data.
1211 * _prop_dictionary_internalize_cont is the lower half and called with the value
1212 * associated with the key.
1214 static bool _prop_dictionary_internalize_body(prop_stack_t
,
1215 prop_object_t
*, struct _prop_object_internalize_context
*, char *);
1218 _prop_dictionary_internalize(prop_stack_t stack
, prop_object_t
*obj
,
1219 struct _prop_object_internalize_context
*ctx
)
1221 prop_dictionary_t dict
;
1224 /* We don't currently understand any attributes. */
1225 if (ctx
->poic_tagattr
!= NULL
)
1228 dict
= prop_dictionary_create();
1232 if (ctx
->poic_is_empty_element
) {
1237 tmpkey
= _PROP_MALLOC(PDK_MAXKEY
+ 1, M_TEMP
);
1238 if (tmpkey
== NULL
) {
1239 prop_object_release(dict
);
1245 * Opening tag is found, storage for key allocated and
1246 * now continue to the first element.
1248 return _prop_dictionary_internalize_body(stack
, obj
, ctx
, tmpkey
);
1252 _prop_dictionary_internalize_continue(prop_stack_t stack
, prop_object_t
*obj
,
1253 struct _prop_object_internalize_context
*ctx
, void *data
, prop_object_t child
)
1255 prop_dictionary_t dict
= *obj
;
1256 char *tmpkey
= data
;
1258 _PROP_ASSERT(tmpkey
!= NULL
);
1260 if (child
== NULL
||
1261 prop_dictionary_set(dict
, tmpkey
, child
) == false) {
1262 _PROP_FREE(tmpkey
, M_TEMP
);
1264 prop_object_release(child
);
1265 prop_object_release(dict
);
1270 prop_object_release(child
);
1273 * key, value was added, now continue looking for the next key
1274 * or the closing tag.
1276 return _prop_dictionary_internalize_body(stack
, obj
, ctx
, tmpkey
);
1280 _prop_dictionary_internalize_body(prop_stack_t stack
, prop_object_t
*obj
,
1281 struct _prop_object_internalize_context
*ctx
, char *tmpkey
)
1283 prop_dictionary_t dict
= *obj
;
1286 /* Fetch the next tag. */
1287 if (_prop_object_internalize_find_tag(ctx
, NULL
, _PROP_TAG_TYPE_EITHER
) == false)
1290 /* Check to see if this is the end of the dictionary. */
1291 if (_PROP_TAG_MATCH(ctx
, "dict") &&
1292 ctx
->poic_tag_type
== _PROP_TAG_TYPE_END
) {
1293 _PROP_FREE(tmpkey
, M_TEMP
);
1297 /* Ok, it must be a non-empty key start tag. */
1298 if (!_PROP_TAG_MATCH(ctx
, "key") ||
1299 ctx
->poic_tag_type
!= _PROP_TAG_TYPE_START
||
1300 ctx
->poic_is_empty_element
)
1303 if (_prop_object_internalize_decode_string(ctx
,
1304 tmpkey
, PDK_MAXKEY
, &keylen
,
1305 &ctx
->poic_cp
) == false)
1308 _PROP_ASSERT(keylen
<= PDK_MAXKEY
);
1309 tmpkey
[keylen
] = '\0';
1311 if (_prop_object_internalize_find_tag(ctx
, "key",
1312 _PROP_TAG_TYPE_END
) == false)
1315 /* ..and now the beginning of the value. */
1316 if (_prop_object_internalize_find_tag(ctx
, NULL
,
1317 _PROP_TAG_TYPE_START
) == false)
1321 * Key is found, now wait for value to be parsed.
1323 if (_prop_stack_push(stack
, *obj
,
1324 _prop_dictionary_internalize_continue
,
1329 _PROP_FREE(tmpkey
, M_TEMP
);
1330 prop_object_release(dict
);
1336 * prop_dictionary_internalize --
1337 * Create a dictionary by parsing the NUL-terminated XML-style
1341 prop_dictionary_internalize(const char *xml
)
1343 return _prop_generic_internalize(xml
, "dict");
1347 * prop_dictionary_externalize_to_file --
1348 * Externalize a dictionary to the specified file.
1351 prop_dictionary_externalize_to_file(prop_dictionary_t dict
, const char *fname
)
1355 int save_errno
= 0; /* XXXGCC -Wuninitialized [mips, ...] */
1357 xml
= prop_dictionary_externalize(dict
);
1360 rv
= _prop_object_externalize_write_file(fname
, xml
, strlen(xml
));
1363 _PROP_FREE(xml
, M_TEMP
);
1371 * prop_dictionary_internalize_from_file --
1372 * Internalize a dictionary from a file.
1375 prop_dictionary_internalize_from_file(const char *fname
)
1377 struct _prop_object_internalize_mapped_file
*mf
;
1378 prop_dictionary_t dict
;
1380 mf
= _prop_object_internalize_map_file(fname
);
1383 dict
= prop_dictionary_internalize(mf
->poimf_xml
);
1384 _prop_object_internalize_unmap_file(mf
);