1 /* $NetBSD: prop_dictionary.c,v 1.36 2010/09/24 22:51:52 rmind 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.
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)
40 #define __unused /* empty */
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
;
65 struct rb_node pdk_link
;
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
;
97 #define PD_F_IMMUTABLE 0x01 /* dictionary is immutable */
99 _PROP_POOL_INIT(_prop_dictionary_pool
, sizeof(struct _prop_dictionary
),
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
*,
110 static _prop_object_equals_rv_t
111 _prop_dictionary_equals(prop_object_t
, prop_object_t
,
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
);
118 _prop_dictionary_iterator_next_object_locked(void *);
120 _prop_dictionary_get_keysym(prop_dictionary_t
,
121 prop_dictionary_keysym_t
, bool);
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
*,
144 static _prop_object_equals_rv_t
145 _prop_dict_keysym_equals(prop_object_t
, prop_object_t
,
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.
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
);
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
;
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
),
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
)
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
);
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
);
228 _PROP_ASSERT(pdk
->pdk_size
<= PDK_SIZE_128
);
229 _PROP_POOL_PUT(_prop_dictionary_keysym128_pool
, pdk
);
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
;
246 _prop_dict_keysym_externalize(struct _prop_object_externalize_context
*ctx
,
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)
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.
278 return _PROP_OBJECT_EQUALS_TRUE
;
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
;
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
);
298 prop_object_retain(opdk
);
299 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex
);
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
);
317 pdk
= NULL
; /* key too long */
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
);
334 prop_object_retain(opdk
);
335 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex
);
336 _prop_dict_keysym_put(pdk
);
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
);
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
;
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
);
373 * If we are in emergency release mode,
374 * just let caller recurse down.
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. */
387 pdk
= pd
->pd_array
[pd
->pd_count
].pde_key
;
388 _PROP_ASSERT(pdk
!= NULL
);
390 prop_object_release(pdk
);
393 return (_PROP_OBJECT_FREE_RECURSE
);
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
);
407 _prop_dictionary_unlock(void)
409 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex
);
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);
421 pdk
= pd
->pd_array
[pd
->pd_count
].pde_key
;
422 _PROP_ASSERT(pdk
!= NULL
);
423 prop_object_release(pdk
);
427 _prop_dictionary_externalize(struct _prop_object_externalize_context
*ctx
,
430 prop_dictionary_t pd
= v
;
431 prop_dictionary_keysym_t pdk
;
432 struct _prop_object
*po
;
433 prop_object_iterator_t pi
;
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)
448 pi
= _prop_dictionary_iterator_locked(pd
);
453 _PROP_ASSERT(ctx
->poec_depth
!= 0);
455 while ((pdk
= _prop_dictionary_iterator_next_object_locked(pi
))
457 po
= _prop_dictionary_get_keysym(pd
, pdk
, true);
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
);
469 prop_object_iterator_release(pi
);
472 for (i
= 0; i
< ctx
->poec_depth
; i
++) {
473 if (_prop_object_externalize_append_char(ctx
, '\t') == false)
476 if (_prop_object_externalize_end_tag(ctx
, "dict") == false)
482 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
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
;
495 _prop_object_equals_rv_t rv
= _PROP_OBJECT_EQUALS_FALSE
;
498 return (_PROP_OBJECT_EQUALS_TRUE
);
500 _PROP_ASSERT(*stored_pointer1
== *stored_pointer2
);
502 idx
= (uintptr_t)*stored_pointer1
;
505 if ((uintptr_t)dict1
< (uintptr_t)dict2
) {
506 _PROP_RWLOCK_RDLOCK(dict1
->pd_rwlock
);
507 _PROP_RWLOCK_RDLOCK(dict2
->pd_rwlock
);
509 _PROP_RWLOCK_RDLOCK(dict2
->pd_rwlock
);
510 _PROP_RWLOCK_RDLOCK(dict1
->pd_rwlock
);
514 if (dict1
->pd_count
!= dict2
->pd_count
)
517 if (idx
== dict1
->pd_count
) {
518 rv
= _PROP_OBJECT_EQUALS_TRUE
;
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
))
534 return (_PROP_OBJECT_EQUALS_RECURSE
);
537 _PROP_RWLOCK_UNLOCK(dict1
->pd_rwlock
);
538 _PROP_RWLOCK_UNLOCK(dict2
->pd_rwlock
);
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
;
556 array
= _PROP_CALLOC(capacity
* sizeof(*array
), M_PROP_DICT
);
562 pd
= _PROP_POOL_GET(_prop_dictionary_pool
);
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
;
573 } else if (array
!= NULL
)
574 _PROP_FREE(array
, M_PROP_DICT
);
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
);
594 memcpy(array
, oarray
, pd
->pd_capacity
* sizeof(*array
));
595 pd
->pd_array
= array
;
596 pd
->pd_capacity
= capacity
;
599 _PROP_FREE(oarray
, M_PROP_DICT
);
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
;
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
);
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
));
652 pdi
->pdi_base
.pi_version
= pd
->pd_version
;
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.
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.
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).
696 prop_dictionary_copy(prop_dictionary_t opd
)
698 prop_dictionary_t pd
;
699 prop_dictionary_keysym_t pdk
;
703 if (! prop_object_is_dictionary(opd
))
706 _PROP_RWLOCK_RDLOCK(opd
->pd_rwlock
);
708 pd
= _prop_dictionary_alloc(opd
->pd_count
);
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
);
728 * prop_dictionary_copy_mutable --
729 * Like prop_dictionary_copy(), but the resulting dictionary is
733 prop_dictionary_copy_mutable(prop_dictionary_t opd
)
735 prop_dictionary_t pd
;
737 if (! prop_object_is_dictionary(opd
))
740 pd
= prop_dictionary_copy(opd
);
742 pd
->pd_flags
&= ~PD_F_IMMUTABLE
;
748 * prop_dictionary_make_immutable --
749 * Set the immutable flag on that dictionary.
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.
766 prop_dictionary_count(prop_dictionary_t pd
)
770 if (! prop_object_is_dictionary(pd
))
773 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
775 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
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
787 prop_dictionary_ensure_capacity(prop_dictionary_t pd
, unsigned int capacity
)
791 if (! prop_object_is_dictionary(pd
))
794 _PROP_RWLOCK_WRLOCK(pd
->pd_rwlock
);
795 if (capacity
> pd
->pd_capacity
)
796 rv
= _prop_dictionary_expand(pd
, capacity
);
799 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
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
))
811 pdi
= _PROP_CALLOC(sizeof(*pdi
), M_TEMP
);
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
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
);
840 * prop_dictionary_all_keys --
841 * Return an array containing a snapshot of all of the keys
845 prop_dictionary_all_keys(prop_dictionary_t pd
)
851 if (! prop_object_is_dictionary(pd
))
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
);
865 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
868 prop_object_release(array
);
874 static struct _prop_dict_entry
*
875 _prop_dict_lookup(prop_dictionary_t pd
, const char *key
,
878 struct _prop_dict_entry
*pde
;
879 unsigned int base
, idx
, distance
;
883 * Dictionary must be READ-LOCKED or WRITE-LOCKED.
886 for (idx
= 0, base
= 0, distance
= pd
->pd_count
; distance
!= 0;
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
);
897 if (res
> 0) { /* key > pdk_key: move right */
900 } /* else move left */
903 /* idx points to the slot we looked at last. */
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
))
919 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
920 pde
= _prop_dict_lookup(pd
, key
, NULL
);
922 _PROP_ASSERT(pde
->pde_objref
!= NULL
);
923 po
= pde
->pde_objref
;
926 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
930 * prop_dictionary_get --
931 * Return the object stored with specified key.
934 prop_dictionary_get(prop_dictionary_t pd
, const char *key
)
936 prop_object_t po
= NULL
;
938 if (! prop_object_is_dictionary(pd
))
941 _PROP_RWLOCK_RDLOCK(pd
->pd_rwlock
);
942 po
= _prop_dictionary_get(pd
, key
, true);
943 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
948 _prop_dictionary_get_keysym(prop_dictionary_t pd
, prop_dictionary_keysym_t pdk
,
952 if (! (prop_object_is_dictionary(pd
) &&
953 prop_object_is_dictionary_keysym(pdk
)))
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.
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.
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
;
983 if (! prop_object_is_dictionary(pd
))
986 _PROP_ASSERT(pd
->pd_count
<= pd
->pd_capacity
);
988 if (prop_dictionary_is_immutable(pd
))
991 _PROP_RWLOCK_WRLOCK(pd
->pd_rwlock
);
993 pde
= _prop_dict_lookup(pd
, key
, &idx
);
995 prop_object_t opo
= pde
->pde_objref
;
996 prop_object_retain(po
);
997 pde
->pde_objref
= po
;
998 prop_object_release(opo
);
1003 pdk
= _prop_dict_keysym_alloc(key
);
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
);
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
;
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
1035 * Because we're unsigned, we have to special case 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
;
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
;
1062 _PROP_RWLOCK_UNLOCK(pd
->pd_rwlock
);
1067 * prop_dictionary_set_keysym --
1068 * Replace the object in the dictionary at the location encoded by
1072 prop_dictionary_set_keysym(prop_dictionary_t pd
, prop_dictionary_keysym_t pdk
,
1076 if (! (prop_object_is_dictionary(pd
) &&
1077 prop_object_is_dictionary_keysym(pdk
)))
1080 return (prop_dictionary_set(pd
, pdk
->pdk_key
, po
));
1084 _prop_dictionary_remove(prop_dictionary_t pd
, struct _prop_dict_entry
*pde
,
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
]);
1099 memmove(&pd
->pd_array
[idx
- 1], &pd
->pd_array
[idx
],
1100 (pd
->pd_count
- idx
) * sizeof(*pde
));
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
1116 prop_dictionary_remove(prop_dictionary_t pd
, const char *key
)
1118 struct _prop_dict_entry
*pde
;
1121 if (! prop_object_is_dictionary(pd
))
1124 _PROP_RWLOCK_WRLOCK(pd
->pd_rwlock
);
1126 /* XXX Should this be a _PROP_ASSERT()? */
1127 if (prop_dictionary_is_immutable(pd
))
1130 pde
= _prop_dict_lookup(pd
, key
, &idx
);
1131 /* XXX Should this be a _PROP_ASSERT()? */
1135 _prop_dictionary_remove(pd
, pde
, idx
);
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.
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
)))
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.
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
))
1169 return (prop_object_equals(dict1
, dict2
));
1173 * prop_dictionary_keysym_cstring_nocopy --
1174 * Return an immutable reference to the keysym's value.
1177 prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pdk
)
1180 if (! prop_object_is_dictionary_keysym(pdk
))
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.
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
))
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.
1209 prop_dictionary_externalize(prop_dictionary_t pd
)
1211 struct _prop_object_externalize_context
*ctx
;
1214 ctx
= _prop_object_externalize_context_alloc();
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
);
1228 _prop_object_externalize_context_free(ctx
);
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
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 *);
1250 _prop_dictionary_internalize(prop_stack_t stack
, prop_object_t
*obj
,
1251 struct _prop_object_internalize_context
*ctx
)
1253 prop_dictionary_t dict
;
1256 /* We don't currently understand any attributes. */
1257 if (ctx
->poic_tagattr
!= NULL
)
1260 dict
= prop_dictionary_create();
1264 if (ctx
->poic_is_empty_element
) {
1269 tmpkey
= _PROP_MALLOC(PDK_MAXKEY
+ 1, M_TEMP
);
1270 if (tmpkey
== NULL
) {
1271 prop_object_release(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
);
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
);
1296 prop_object_release(child
);
1297 prop_object_release(dict
);
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
);
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
;
1318 /* Fetch the next tag. */
1319 if (_prop_object_internalize_find_tag(ctx
, NULL
, _PROP_TAG_TYPE_EITHER
) == false)
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
);
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
)
1335 if (_prop_object_internalize_decode_string(ctx
,
1336 tmpkey
, PDK_MAXKEY
, &keylen
,
1337 &ctx
->poic_cp
) == false)
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)
1347 /* ..and now the beginning of the value. */
1348 if (_prop_object_internalize_find_tag(ctx
, NULL
,
1349 _PROP_TAG_TYPE_START
) == false)
1353 * Key is found, now wait for value to be parsed.
1355 if (_prop_stack_push(stack
, *obj
,
1356 _prop_dictionary_internalize_continue
,
1361 _PROP_FREE(tmpkey
, M_TEMP
);
1362 prop_object_release(dict
);
1368 * prop_dictionary_internalize --
1369 * Create a dictionary by parsing the NUL-terminated XML-style
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.
1384 prop_dictionary_externalize_to_file(prop_dictionary_t dict
, const char *fname
)
1388 int save_errno
= 0; /* XXXGCC -Wuninitialized [mips, ...] */
1390 xml
= prop_dictionary_externalize(dict
);
1393 rv
= _prop_object_externalize_write_file(fname
, xml
, strlen(xml
),
1397 _PROP_FREE(xml
, M_TEMP
);
1405 * prop_dictionary_internalize_from_file --
1406 * Internalize a dictionary from a file.
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
);
1417 dict
= prop_dictionary_internalize(mf
->poimf_xml
);
1418 _prop_object_internalize_unmap_file(mf
);
1422 #endif /* !_KERNEL && !_STANDALONE */