Home | History | Annotate | Line # | Download | only in libprop
prop_dictionary.c revision 1.16.2.2
      1  1.16.2.2  thorpej /*	$NetBSD: prop_dictionary.c,v 1.16.2.2 2006/10/26 05:02:13 thorpej Exp $	*/
      2  1.16.2.2  thorpej 
      3  1.16.2.2  thorpej /*-
      4  1.16.2.2  thorpej  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.16.2.2  thorpej  * All rights reserved.
      6  1.16.2.2  thorpej  *
      7  1.16.2.2  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.16.2.2  thorpej  * by Jason R. Thorpe.
      9  1.16.2.2  thorpej  *
     10  1.16.2.2  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.16.2.2  thorpej  * modification, are permitted provided that the following conditions
     12  1.16.2.2  thorpej  * are met:
     13  1.16.2.2  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.16.2.2  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.16.2.2  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.16.2.2  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.16.2.2  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.16.2.2  thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.16.2.2  thorpej  *    must display the following acknowledgement:
     20  1.16.2.2  thorpej  *      This product includes software developed by the NetBSD
     21  1.16.2.2  thorpej  *      Foundation, Inc. and its contributors.
     22  1.16.2.2  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.16.2.2  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.16.2.2  thorpej  *    from this software without specific prior written permission.
     25  1.16.2.2  thorpej  *
     26  1.16.2.2  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.16.2.2  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.16.2.2  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.16.2.2  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.16.2.2  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.16.2.2  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.16.2.2  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.16.2.2  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.16.2.2  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.16.2.2  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.16.2.2  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.16.2.2  thorpej  */
     38  1.16.2.2  thorpej 
     39  1.16.2.2  thorpej #include <prop/prop_array.h>
     40  1.16.2.2  thorpej #include <prop/prop_dictionary.h>
     41  1.16.2.2  thorpej #include <prop/prop_string.h>
     42  1.16.2.2  thorpej #include "prop_object_impl.h"
     43  1.16.2.2  thorpej #include "prop_rb_impl.h"
     44  1.16.2.2  thorpej 
     45  1.16.2.2  thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
     46  1.16.2.2  thorpej #include <errno.h>
     47  1.16.2.2  thorpej #endif
     48  1.16.2.2  thorpej 
     49  1.16.2.2  thorpej /*
     50  1.16.2.2  thorpej  * We implement these like arrays, but we keep them sorted by key.
     51  1.16.2.2  thorpej  * This allows us to binary-search as well as keep externalized output
     52  1.16.2.2  thorpej  * sane-looking for human eyes.
     53  1.16.2.2  thorpej  */
     54  1.16.2.2  thorpej 
     55  1.16.2.2  thorpej #define	EXPAND_STEP		16
     56  1.16.2.2  thorpej 
     57  1.16.2.2  thorpej /*
     58  1.16.2.2  thorpej  * prop_dictionary_keysym_t is allocated with space at the end to hold the
     59  1.16.2.2  thorpej  * key.  This must be a regular object so that we can maintain sane iterator
     60  1.16.2.2  thorpej  * semantics -- we don't want to require that the caller release the result
     61  1.16.2.2  thorpej  * of prop_object_iterator_next().
     62  1.16.2.2  thorpej  *
     63  1.16.2.2  thorpej  * We'd like to have some small'ish keysym objects for up-to-16 characters
     64  1.16.2.2  thorpej  * in a key, some for up-to-32 characters in a key, and then a final bucket
     65  1.16.2.2  thorpej  * for up-to-128 characters in a key (not including NUL).  Keys longer than
     66  1.16.2.2  thorpej  * 128 characters are not allowed.
     67  1.16.2.2  thorpej  */
     68  1.16.2.2  thorpej struct _prop_dictionary_keysym {
     69  1.16.2.2  thorpej 	struct _prop_object		pdk_obj;
     70  1.16.2.2  thorpej 	size_t				pdk_size;
     71  1.16.2.2  thorpej 	struct rb_node			pdk_link;
     72  1.16.2.2  thorpej 	char 				pdk_key[1];
     73  1.16.2.2  thorpej 	/* actually variable length */
     74  1.16.2.2  thorpej };
     75  1.16.2.2  thorpej 
     76  1.16.2.2  thorpej #define	RBNODE_TO_PDK(n)						\
     77  1.16.2.2  thorpej 	((struct _prop_dictionary_keysym *)				\
     78  1.16.2.2  thorpej 	 ((uintptr_t)n - offsetof(struct _prop_dictionary_keysym, pdk_link)))
     79  1.16.2.2  thorpej 
     80  1.16.2.2  thorpej 	/* pdk_key[1] takes care of the NUL */
     81  1.16.2.2  thorpej #define	PDK_SIZE_16		(sizeof(struct _prop_dictionary_keysym) + 16)
     82  1.16.2.2  thorpej #define	PDK_SIZE_32		(sizeof(struct _prop_dictionary_keysym) + 32)
     83  1.16.2.2  thorpej #define	PDK_SIZE_128		(sizeof(struct _prop_dictionary_keysym) + 128)
     84  1.16.2.2  thorpej 
     85  1.16.2.2  thorpej #define	PDK_MAXKEY		128
     86  1.16.2.2  thorpej 
     87  1.16.2.2  thorpej _PROP_POOL_INIT(_prop_dictionary_keysym16_pool, PDK_SIZE_16, "pdict16")
     88  1.16.2.2  thorpej _PROP_POOL_INIT(_prop_dictionary_keysym32_pool, PDK_SIZE_32, "pdict32")
     89  1.16.2.2  thorpej _PROP_POOL_INIT(_prop_dictionary_keysym128_pool, PDK_SIZE_128, "pdict128")
     90  1.16.2.2  thorpej 
     91  1.16.2.2  thorpej struct _prop_dict_entry {
     92  1.16.2.2  thorpej 	prop_dictionary_keysym_t	pde_key;
     93  1.16.2.2  thorpej 	prop_object_t			pde_objref;
     94  1.16.2.2  thorpej };
     95  1.16.2.2  thorpej 
     96  1.16.2.2  thorpej struct _prop_dictionary {
     97  1.16.2.2  thorpej 	struct _prop_object	pd_obj;
     98  1.16.2.2  thorpej 	_PROP_RWLOCK_DECL(pd_rwlock)
     99  1.16.2.2  thorpej 	struct _prop_dict_entry	*pd_array;
    100  1.16.2.2  thorpej 	unsigned int		pd_capacity;
    101  1.16.2.2  thorpej 	unsigned int		pd_count;
    102  1.16.2.2  thorpej 	int			pd_flags;
    103  1.16.2.2  thorpej 
    104  1.16.2.2  thorpej 	uint32_t		pd_version;
    105  1.16.2.2  thorpej };
    106  1.16.2.2  thorpej 
    107  1.16.2.2  thorpej #define	PD_F_IMMUTABLE		0x01	/* dictionary is immutable */
    108  1.16.2.2  thorpej 
    109  1.16.2.2  thorpej _PROP_POOL_INIT(_prop_dictionary_pool, sizeof(struct _prop_dictionary),
    110  1.16.2.2  thorpej 		"propdict")
    111  1.16.2.2  thorpej _PROP_MALLOC_DEFINE(M_PROP_DICT, "prop dictionary",
    112  1.16.2.2  thorpej 		    "property dictionary container object")
    113  1.16.2.2  thorpej 
    114  1.16.2.2  thorpej static void		_prop_dictionary_free(void *);
    115  1.16.2.2  thorpej static boolean_t	_prop_dictionary_externalize(
    116  1.16.2.2  thorpej 				struct _prop_object_externalize_context *,
    117  1.16.2.2  thorpej 				void *);
    118  1.16.2.2  thorpej static boolean_t	_prop_dictionary_equals(void *, void *);
    119  1.16.2.2  thorpej 
    120  1.16.2.2  thorpej static const struct _prop_object_type _prop_object_type_dictionary = {
    121  1.16.2.2  thorpej 	.pot_type	=	PROP_TYPE_DICTIONARY,
    122  1.16.2.2  thorpej 	.pot_free	=	_prop_dictionary_free,
    123  1.16.2.2  thorpej 	.pot_extern	=	_prop_dictionary_externalize,
    124  1.16.2.2  thorpej 	.pot_equals	=	_prop_dictionary_equals,
    125  1.16.2.2  thorpej };
    126  1.16.2.2  thorpej 
    127  1.16.2.2  thorpej static void		_prop_dict_keysym_free(void *);
    128  1.16.2.2  thorpej static boolean_t	_prop_dict_keysym_externalize(
    129  1.16.2.2  thorpej 				struct _prop_object_externalize_context *,
    130  1.16.2.2  thorpej 				void *);
    131  1.16.2.2  thorpej static boolean_t	_prop_dict_keysym_equals(void *, void *);
    132  1.16.2.2  thorpej 
    133  1.16.2.2  thorpej static const struct _prop_object_type _prop_object_type_dict_keysym = {
    134  1.16.2.2  thorpej 	.pot_type	=	PROP_TYPE_DICT_KEYSYM,
    135  1.16.2.2  thorpej 	.pot_free	=	_prop_dict_keysym_free,
    136  1.16.2.2  thorpej 	.pot_extern	=	_prop_dict_keysym_externalize,
    137  1.16.2.2  thorpej 	.pot_equals	=	_prop_dict_keysym_equals,
    138  1.16.2.2  thorpej };
    139  1.16.2.2  thorpej 
    140  1.16.2.2  thorpej #define	prop_object_is_dictionary(x)		\
    141  1.16.2.2  thorpej 	((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_dictionary)
    142  1.16.2.2  thorpej #define	prop_object_is_dictionary_keysym(x)	\
    143  1.16.2.2  thorpej 	((x) != NULL && (x)->pdk_obj.po_type == &_prop_object_type_dict_keysym)
    144  1.16.2.2  thorpej 
    145  1.16.2.2  thorpej #define	prop_dictionary_is_immutable(x)		\
    146  1.16.2.2  thorpej 				(((x)->pd_flags & PD_F_IMMUTABLE) != 0)
    147  1.16.2.2  thorpej 
    148  1.16.2.2  thorpej struct _prop_dictionary_iterator {
    149  1.16.2.2  thorpej 	struct _prop_object_iterator pdi_base;
    150  1.16.2.2  thorpej 	unsigned int		pdi_index;
    151  1.16.2.2  thorpej };
    152  1.16.2.2  thorpej 
    153  1.16.2.2  thorpej /*
    154  1.16.2.2  thorpej  * Dictionary key symbols are immutable, and we are likely to have many
    155  1.16.2.2  thorpej  * duplicated key symbols.  So, to save memory, we unique'ify key symbols
    156  1.16.2.2  thorpej  * so we only have to have one copy of each string.
    157  1.16.2.2  thorpej  */
    158  1.16.2.2  thorpej 
    159  1.16.2.2  thorpej static int
    160  1.16.2.2  thorpej _prop_dict_keysym_rb_compare_nodes(const struct rb_node *n1,
    161  1.16.2.2  thorpej 				   const struct rb_node *n2)
    162  1.16.2.2  thorpej {
    163  1.16.2.2  thorpej 	const prop_dictionary_keysym_t pdk1 = RBNODE_TO_PDK(n1);
    164  1.16.2.2  thorpej 	const prop_dictionary_keysym_t pdk2 = RBNODE_TO_PDK(n2);
    165  1.16.2.2  thorpej 
    166  1.16.2.2  thorpej 	return (strcmp(pdk1->pdk_key, pdk2->pdk_key));
    167  1.16.2.2  thorpej }
    168  1.16.2.2  thorpej 
    169  1.16.2.2  thorpej static int
    170  1.16.2.2  thorpej _prop_dict_keysym_rb_compare_key(const struct rb_node *n,
    171  1.16.2.2  thorpej 				 const void *v)
    172  1.16.2.2  thorpej {
    173  1.16.2.2  thorpej 	const prop_dictionary_keysym_t pdk = RBNODE_TO_PDK(n);
    174  1.16.2.2  thorpej 	const char *cp = v;
    175  1.16.2.2  thorpej 
    176  1.16.2.2  thorpej 	return (strcmp(pdk->pdk_key, cp));
    177  1.16.2.2  thorpej }
    178  1.16.2.2  thorpej 
    179  1.16.2.2  thorpej static const struct rb_tree_ops _prop_dict_keysym_rb_tree_ops = {
    180  1.16.2.2  thorpej 	.rbto_compare_nodes = _prop_dict_keysym_rb_compare_nodes,
    181  1.16.2.2  thorpej 	.rbto_compare_key   = _prop_dict_keysym_rb_compare_key,
    182  1.16.2.2  thorpej };
    183  1.16.2.2  thorpej 
    184  1.16.2.2  thorpej static struct rb_tree _prop_dict_keysym_tree;
    185  1.16.2.2  thorpej static boolean_t _prop_dict_keysym_tree_initialized;
    186  1.16.2.2  thorpej 
    187  1.16.2.2  thorpej _PROP_MUTEX_DECL_STATIC(_prop_dict_keysym_tree_mutex)
    188  1.16.2.2  thorpej 
    189  1.16.2.2  thorpej static void
    190  1.16.2.2  thorpej _prop_dict_keysym_put(prop_dictionary_keysym_t pdk)
    191  1.16.2.2  thorpej {
    192  1.16.2.2  thorpej 
    193  1.16.2.2  thorpej 	if (pdk->pdk_size <= PDK_SIZE_16)
    194  1.16.2.2  thorpej 		_PROP_POOL_PUT(_prop_dictionary_keysym16_pool, pdk);
    195  1.16.2.2  thorpej 	else if (pdk->pdk_size <= PDK_SIZE_32)
    196  1.16.2.2  thorpej 		_PROP_POOL_PUT(_prop_dictionary_keysym32_pool, pdk);
    197  1.16.2.2  thorpej 	else {
    198  1.16.2.2  thorpej 		_PROP_ASSERT(pdk->pdk_size <= PDK_SIZE_128);
    199  1.16.2.2  thorpej 		_PROP_POOL_PUT(_prop_dictionary_keysym128_pool, pdk);
    200  1.16.2.2  thorpej 	}
    201  1.16.2.2  thorpej }
    202  1.16.2.2  thorpej 
    203  1.16.2.2  thorpej static void
    204  1.16.2.2  thorpej _prop_dict_keysym_free(void *v)
    205  1.16.2.2  thorpej {
    206  1.16.2.2  thorpej 	prop_dictionary_keysym_t pdk = v;
    207  1.16.2.2  thorpej 
    208  1.16.2.2  thorpej 	_PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
    209  1.16.2.2  thorpej 	_prop_rb_tree_remove_node(&_prop_dict_keysym_tree, &pdk->pdk_link);
    210  1.16.2.2  thorpej 	_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    211  1.16.2.2  thorpej 
    212  1.16.2.2  thorpej 	_prop_dict_keysym_put(pdk);
    213  1.16.2.2  thorpej }
    214  1.16.2.2  thorpej 
    215  1.16.2.2  thorpej static boolean_t
    216  1.16.2.2  thorpej _prop_dict_keysym_externalize(struct _prop_object_externalize_context *ctx,
    217  1.16.2.2  thorpej 			     void *v)
    218  1.16.2.2  thorpej {
    219  1.16.2.2  thorpej 	prop_dictionary_keysym_t pdk = v;
    220  1.16.2.2  thorpej 
    221  1.16.2.2  thorpej 	/* We externalize these as strings, and they're never empty. */
    222  1.16.2.2  thorpej 
    223  1.16.2.2  thorpej 	_PROP_ASSERT(pdk->pdk_key[0] != '\0');
    224  1.16.2.2  thorpej 
    225  1.16.2.2  thorpej 	if (_prop_object_externalize_start_tag(ctx, "string") == FALSE ||
    226  1.16.2.2  thorpej 	    _prop_object_externalize_append_encoded_cstring(ctx,
    227  1.16.2.2  thorpej 						pdk->pdk_key) == FALSE ||
    228  1.16.2.2  thorpej 	    _prop_object_externalize_end_tag(ctx, "string") == FALSE)
    229  1.16.2.2  thorpej 		return (FALSE);
    230  1.16.2.2  thorpej 
    231  1.16.2.2  thorpej 	return (TRUE);
    232  1.16.2.2  thorpej }
    233  1.16.2.2  thorpej 
    234  1.16.2.2  thorpej static boolean_t
    235  1.16.2.2  thorpej _prop_dict_keysym_equals(void *v1, void *v2)
    236  1.16.2.2  thorpej {
    237  1.16.2.2  thorpej 	prop_dictionary_keysym_t pdk1 = v1;
    238  1.16.2.2  thorpej 	prop_dictionary_keysym_t pdk2 = v2;
    239  1.16.2.2  thorpej 
    240  1.16.2.2  thorpej 	if (! (prop_object_is_dictionary_keysym(pdk1) &&
    241  1.16.2.2  thorpej 	       prop_object_is_dictionary_keysym(pdk2)))
    242  1.16.2.2  thorpej 		return (FALSE);
    243  1.16.2.2  thorpej 
    244  1.16.2.2  thorpej 	/*
    245  1.16.2.2  thorpej 	 * There is only ever one copy of a keysym at any given time,
    246  1.16.2.2  thorpej 	 * so we can reduce this to a simple pointer equality check.
    247  1.16.2.2  thorpej 	 */
    248  1.16.2.2  thorpej 	return (pdk1 == pdk2);
    249  1.16.2.2  thorpej }
    250  1.16.2.2  thorpej 
    251  1.16.2.2  thorpej static prop_dictionary_keysym_t
    252  1.16.2.2  thorpej _prop_dict_keysym_alloc(const char *key)
    253  1.16.2.2  thorpej {
    254  1.16.2.2  thorpej 	prop_dictionary_keysym_t opdk, pdk;
    255  1.16.2.2  thorpej 	const struct rb_node *n;
    256  1.16.2.2  thorpej 	size_t size;
    257  1.16.2.2  thorpej 
    258  1.16.2.2  thorpej 	/*
    259  1.16.2.2  thorpej 	 * Check to see if this already exists in the tree.  If it does,
    260  1.16.2.2  thorpej 	 * we just retain it and return it.
    261  1.16.2.2  thorpej 	 */
    262  1.16.2.2  thorpej 	_PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
    263  1.16.2.2  thorpej 	if (! _prop_dict_keysym_tree_initialized) {
    264  1.16.2.2  thorpej 		_prop_rb_tree_init(&_prop_dict_keysym_tree,
    265  1.16.2.2  thorpej 				   &_prop_dict_keysym_rb_tree_ops);
    266  1.16.2.2  thorpej 		_prop_dict_keysym_tree_initialized = TRUE;
    267  1.16.2.2  thorpej 	} else {
    268  1.16.2.2  thorpej 		n = _prop_rb_tree_find(&_prop_dict_keysym_tree, key);
    269  1.16.2.2  thorpej 		if (n != NULL) {
    270  1.16.2.2  thorpej 			opdk = RBNODE_TO_PDK(n);
    271  1.16.2.2  thorpej 			prop_object_retain(opdk);
    272  1.16.2.2  thorpej 			_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    273  1.16.2.2  thorpej 			return (opdk);
    274  1.16.2.2  thorpej 		}
    275  1.16.2.2  thorpej 	}
    276  1.16.2.2  thorpej 	_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    277  1.16.2.2  thorpej 
    278  1.16.2.2  thorpej 	/*
    279  1.16.2.2  thorpej 	 * Not in the tree.  Create it now.
    280  1.16.2.2  thorpej 	 */
    281  1.16.2.2  thorpej 
    282  1.16.2.2  thorpej 	size = sizeof(*pdk) + strlen(key) /* pdk_key[1] covers the NUL */;
    283  1.16.2.2  thorpej 
    284  1.16.2.2  thorpej 	if (size <= PDK_SIZE_16)
    285  1.16.2.2  thorpej 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym16_pool);
    286  1.16.2.2  thorpej 	else if (size <= PDK_SIZE_32)
    287  1.16.2.2  thorpej 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym32_pool);
    288  1.16.2.2  thorpej 	else if (size <= PDK_SIZE_128)
    289  1.16.2.2  thorpej 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym128_pool);
    290  1.16.2.2  thorpej 	else
    291  1.16.2.2  thorpej 		pdk = NULL;	/* key too long */
    292  1.16.2.2  thorpej 
    293  1.16.2.2  thorpej 	if (pdk == NULL)
    294  1.16.2.2  thorpej 		return (NULL);
    295  1.16.2.2  thorpej 
    296  1.16.2.2  thorpej 	_prop_object_init(&pdk->pdk_obj, &_prop_object_type_dict_keysym);
    297  1.16.2.2  thorpej 
    298  1.16.2.2  thorpej 	strcpy(pdk->pdk_key, key);
    299  1.16.2.2  thorpej 	pdk->pdk_size = size;
    300  1.16.2.2  thorpej 
    301  1.16.2.2  thorpej 	/*
    302  1.16.2.2  thorpej 	 * We dropped the mutex when we allocated the new object, so
    303  1.16.2.2  thorpej 	 * we have to check again if it is in the tree.
    304  1.16.2.2  thorpej 	 */
    305  1.16.2.2  thorpej 	_PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
    306  1.16.2.2  thorpej 	n = _prop_rb_tree_find(&_prop_dict_keysym_tree, key);
    307  1.16.2.2  thorpej 	if (n != NULL) {
    308  1.16.2.2  thorpej 		opdk = RBNODE_TO_PDK(n);
    309  1.16.2.2  thorpej 		prop_object_retain(opdk);
    310  1.16.2.2  thorpej 		_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    311  1.16.2.2  thorpej 		_prop_dict_keysym_put(pdk);
    312  1.16.2.2  thorpej 		return (opdk);
    313  1.16.2.2  thorpej 	}
    314  1.16.2.2  thorpej 	_prop_rb_tree_insert_node(&_prop_dict_keysym_tree, &pdk->pdk_link);
    315  1.16.2.2  thorpej 	_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    316  1.16.2.2  thorpej 	return (pdk);
    317  1.16.2.2  thorpej }
    318  1.16.2.2  thorpej 
    319  1.16.2.2  thorpej static void
    320  1.16.2.2  thorpej _prop_dictionary_free(void *v)
    321  1.16.2.2  thorpej {
    322  1.16.2.2  thorpej 	prop_dictionary_t pd = v;
    323  1.16.2.2  thorpej 	prop_dictionary_keysym_t pdk;
    324  1.16.2.2  thorpej 	prop_object_t po;
    325  1.16.2.2  thorpej 	unsigned int idx;
    326  1.16.2.2  thorpej 
    327  1.16.2.2  thorpej 	_PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
    328  1.16.2.2  thorpej 	_PROP_ASSERT((pd->pd_capacity == 0 && pd->pd_array == NULL) ||
    329  1.16.2.2  thorpej 		     (pd->pd_capacity != 0 && pd->pd_array != NULL));
    330  1.16.2.2  thorpej 
    331  1.16.2.2  thorpej 	for (idx = 0; idx < pd->pd_count; idx++) {
    332  1.16.2.2  thorpej 		pdk = pd->pd_array[idx].pde_key;
    333  1.16.2.2  thorpej 		_PROP_ASSERT(pdk != NULL);
    334  1.16.2.2  thorpej 		prop_object_release(pdk);
    335  1.16.2.2  thorpej 		po = pd->pd_array[idx].pde_objref;
    336  1.16.2.2  thorpej 		_PROP_ASSERT(po != NULL);
    337  1.16.2.2  thorpej 		prop_object_release(po);
    338  1.16.2.2  thorpej 	}
    339  1.16.2.2  thorpej 
    340  1.16.2.2  thorpej 	if (pd->pd_array != NULL)
    341  1.16.2.2  thorpej 		_PROP_FREE(pd->pd_array, M_PROP_DICT);
    342  1.16.2.2  thorpej 
    343  1.16.2.2  thorpej 	_PROP_RWLOCK_DESTROY(pd->pd_rwlock);
    344  1.16.2.2  thorpej 
    345  1.16.2.2  thorpej 	_PROP_POOL_PUT(_prop_dictionary_pool, pd);
    346  1.16.2.2  thorpej }
    347  1.16.2.2  thorpej 
    348  1.16.2.2  thorpej static boolean_t
    349  1.16.2.2  thorpej _prop_dictionary_externalize(struct _prop_object_externalize_context *ctx,
    350  1.16.2.2  thorpej 			     void *v)
    351  1.16.2.2  thorpej {
    352  1.16.2.2  thorpej 	prop_dictionary_t pd = v;
    353  1.16.2.2  thorpej 	prop_dictionary_keysym_t pdk;
    354  1.16.2.2  thorpej 	struct _prop_object *po;
    355  1.16.2.2  thorpej 	prop_object_iterator_t pi;
    356  1.16.2.2  thorpej 	unsigned int i;
    357  1.16.2.2  thorpej 	boolean_t rv = FALSE;
    358  1.16.2.2  thorpej 
    359  1.16.2.2  thorpej 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
    360  1.16.2.2  thorpej 
    361  1.16.2.2  thorpej 	if (pd->pd_count == 0) {
    362  1.16.2.2  thorpej 		_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
    363  1.16.2.2  thorpej 		return (_prop_object_externalize_empty_tag(ctx, "dict"));
    364  1.16.2.2  thorpej 	}
    365  1.16.2.2  thorpej 
    366  1.16.2.2  thorpej 	if (_prop_object_externalize_start_tag(ctx, "dict") == FALSE ||
    367  1.16.2.2  thorpej 	    _prop_object_externalize_append_char(ctx, '\n') == FALSE)
    368  1.16.2.2  thorpej 		goto out;
    369  1.16.2.2  thorpej 
    370  1.16.2.2  thorpej 	pi = prop_dictionary_iterator(pd);
    371  1.16.2.2  thorpej 	if (pi == NULL)
    372  1.16.2.2  thorpej 		goto out;
    373  1.16.2.2  thorpej 
    374  1.16.2.2  thorpej 	ctx->poec_depth++;
    375  1.16.2.2  thorpej 	_PROP_ASSERT(ctx->poec_depth != 0);
    376  1.16.2.2  thorpej 
    377  1.16.2.2  thorpej 	while ((pdk = prop_object_iterator_next(pi)) != NULL) {
    378  1.16.2.2  thorpej 		po = prop_dictionary_get_keysym(pd, pdk);
    379  1.16.2.2  thorpej 		if (po == NULL ||
    380  1.16.2.2  thorpej 		    _prop_object_externalize_start_tag(ctx, "key") == FALSE ||
    381  1.16.2.2  thorpej 		    _prop_object_externalize_append_encoded_cstring(ctx,
    382  1.16.2.2  thorpej 						   pdk->pdk_key) == FALSE ||
    383  1.16.2.2  thorpej 		    _prop_object_externalize_end_tag(ctx, "key") == FALSE ||
    384  1.16.2.2  thorpej 		    (*po->po_type->pot_extern)(ctx, po) == FALSE) {
    385  1.16.2.2  thorpej 			prop_object_iterator_release(pi);
    386  1.16.2.2  thorpej 			goto out;
    387  1.16.2.2  thorpej 		}
    388  1.16.2.2  thorpej 	}
    389  1.16.2.2  thorpej 
    390  1.16.2.2  thorpej 	prop_object_iterator_release(pi);
    391  1.16.2.2  thorpej 
    392  1.16.2.2  thorpej 	ctx->poec_depth--;
    393  1.16.2.2  thorpej 	for (i = 0; i < ctx->poec_depth; i++) {
    394  1.16.2.2  thorpej 		if (_prop_object_externalize_append_char(ctx, '\t') == FALSE)
    395  1.16.2.2  thorpej 			goto out;
    396  1.16.2.2  thorpej 	}
    397  1.16.2.2  thorpej 	if (_prop_object_externalize_end_tag(ctx, "dict") == FALSE)
    398  1.16.2.2  thorpej 		goto out;
    399  1.16.2.2  thorpej 
    400  1.16.2.2  thorpej 	rv = TRUE;
    401  1.16.2.2  thorpej 
    402  1.16.2.2  thorpej  out:
    403  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
    404  1.16.2.2  thorpej 	return (rv);
    405  1.16.2.2  thorpej }
    406  1.16.2.2  thorpej 
    407  1.16.2.2  thorpej static boolean_t
    408  1.16.2.2  thorpej _prop_dictionary_equals(void *v1, void *v2)
    409  1.16.2.2  thorpej {
    410  1.16.2.2  thorpej 	prop_dictionary_t dict1 = v1;
    411  1.16.2.2  thorpej 	prop_dictionary_t dict2 = v2;
    412  1.16.2.2  thorpej 	const struct _prop_dict_entry *pde1, *pde2;
    413  1.16.2.2  thorpej 	unsigned int idx;
    414  1.16.2.2  thorpej 	boolean_t rv = FALSE;
    415  1.16.2.2  thorpej 
    416  1.16.2.2  thorpej 	if (! (prop_object_is_dictionary(dict1) &&
    417  1.16.2.2  thorpej 	       prop_object_is_dictionary(dict2)))
    418  1.16.2.2  thorpej 		return (FALSE);
    419  1.16.2.2  thorpej 
    420  1.16.2.2  thorpej 	if (dict1 == dict2)
    421  1.16.2.2  thorpej 		return (TRUE);
    422  1.16.2.2  thorpej 
    423  1.16.2.2  thorpej 	if ((uintptr_t)dict1 < (uintptr_t)dict2) {
    424  1.16.2.2  thorpej 		_PROP_RWLOCK_RDLOCK(dict1->pd_rwlock);
    425  1.16.2.2  thorpej 		_PROP_RWLOCK_RDLOCK(dict2->pd_rwlock);
    426  1.16.2.2  thorpej 	} else {
    427  1.16.2.2  thorpej 		_PROP_RWLOCK_RDLOCK(dict2->pd_rwlock);
    428  1.16.2.2  thorpej 		_PROP_RWLOCK_RDLOCK(dict1->pd_rwlock);
    429  1.16.2.2  thorpej 	}
    430  1.16.2.2  thorpej 
    431  1.16.2.2  thorpej 	if (dict1->pd_count != dict2->pd_count)
    432  1.16.2.2  thorpej 		goto out;
    433  1.16.2.2  thorpej 
    434  1.16.2.2  thorpej 	for (idx = 0; idx < dict1->pd_count; idx++) {
    435  1.16.2.2  thorpej 		pde1 = &dict1->pd_array[idx];
    436  1.16.2.2  thorpej 		pde2 = &dict2->pd_array[idx];
    437  1.16.2.2  thorpej 
    438  1.16.2.2  thorpej 		if (prop_dictionary_keysym_equals(pde1->pde_key,
    439  1.16.2.2  thorpej 						  pde2->pde_key) == FALSE)
    440  1.16.2.2  thorpej 			goto out;
    441  1.16.2.2  thorpej 		if (prop_object_equals(pde1->pde_objref,
    442  1.16.2.2  thorpej 				       pde2->pde_objref) == FALSE)
    443  1.16.2.2  thorpej 			goto out;
    444  1.16.2.2  thorpej 	}
    445  1.16.2.2  thorpej 
    446  1.16.2.2  thorpej 	rv = TRUE;
    447  1.16.2.2  thorpej 
    448  1.16.2.2  thorpej  out:
    449  1.16.2.2  thorpej  	_PROP_RWLOCK_UNLOCK(dict1->pd_rwlock);
    450  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(dict2->pd_rwlock);
    451  1.16.2.2  thorpej 	return (rv);
    452  1.16.2.2  thorpej }
    453  1.16.2.2  thorpej 
    454  1.16.2.2  thorpej static prop_dictionary_t
    455  1.16.2.2  thorpej _prop_dictionary_alloc(unsigned int capacity)
    456  1.16.2.2  thorpej {
    457  1.16.2.2  thorpej 	prop_dictionary_t pd;
    458  1.16.2.2  thorpej 	struct _prop_dict_entry *array;
    459  1.16.2.2  thorpej 
    460  1.16.2.2  thorpej 	if (capacity != 0) {
    461  1.16.2.2  thorpej 		array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
    462  1.16.2.2  thorpej 		if (array == NULL)
    463  1.16.2.2  thorpej 			return (NULL);
    464  1.16.2.2  thorpej 	} else
    465  1.16.2.2  thorpej 		array = NULL;
    466  1.16.2.2  thorpej 
    467  1.16.2.2  thorpej 	pd = _PROP_POOL_GET(_prop_dictionary_pool);
    468  1.16.2.2  thorpej 	if (pd != NULL) {
    469  1.16.2.2  thorpej 		_prop_object_init(&pd->pd_obj, &_prop_object_type_dictionary);
    470  1.16.2.2  thorpej 
    471  1.16.2.2  thorpej 		_PROP_RWLOCK_INIT(pd->pd_rwlock);
    472  1.16.2.2  thorpej 		pd->pd_array = array;
    473  1.16.2.2  thorpej 		pd->pd_capacity = capacity;
    474  1.16.2.2  thorpej 		pd->pd_count = 0;
    475  1.16.2.2  thorpej 		pd->pd_flags = 0;
    476  1.16.2.2  thorpej 
    477  1.16.2.2  thorpej 		pd->pd_version = 0;
    478  1.16.2.2  thorpej 	} else if (array != NULL)
    479  1.16.2.2  thorpej 		_PROP_FREE(array, M_PROP_DICT);
    480  1.16.2.2  thorpej 
    481  1.16.2.2  thorpej 	return (pd);
    482  1.16.2.2  thorpej }
    483  1.16.2.2  thorpej 
    484  1.16.2.2  thorpej static boolean_t
    485  1.16.2.2  thorpej _prop_dictionary_expand(prop_dictionary_t pd, unsigned int capacity)
    486  1.16.2.2  thorpej {
    487  1.16.2.2  thorpej 	struct _prop_dict_entry *array, *oarray;
    488  1.16.2.2  thorpej 
    489  1.16.2.2  thorpej 	/*
    490  1.16.2.2  thorpej 	 * Dictionary must be WRITE-LOCKED.
    491  1.16.2.2  thorpej 	 */
    492  1.16.2.2  thorpej 
    493  1.16.2.2  thorpej 	oarray = pd->pd_array;
    494  1.16.2.2  thorpej 
    495  1.16.2.2  thorpej 	array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
    496  1.16.2.2  thorpej 	if (array == NULL)
    497  1.16.2.2  thorpej 		return (FALSE);
    498  1.16.2.2  thorpej 	if (oarray != NULL)
    499  1.16.2.2  thorpej 		memcpy(array, oarray, pd->pd_capacity * sizeof(*array));
    500  1.16.2.2  thorpej 	pd->pd_array = array;
    501  1.16.2.2  thorpej 	pd->pd_capacity = capacity;
    502  1.16.2.2  thorpej 
    503  1.16.2.2  thorpej 	if (oarray != NULL)
    504  1.16.2.2  thorpej 		_PROP_FREE(oarray, M_PROP_DICT);
    505  1.16.2.2  thorpej 
    506  1.16.2.2  thorpej 	return (TRUE);
    507  1.16.2.2  thorpej }
    508  1.16.2.2  thorpej 
    509  1.16.2.2  thorpej static prop_object_t
    510  1.16.2.2  thorpej _prop_dictionary_iterator_next_object(void *v)
    511  1.16.2.2  thorpej {
    512  1.16.2.2  thorpej 	struct _prop_dictionary_iterator *pdi = v;
    513  1.16.2.2  thorpej 	prop_dictionary_t pd = pdi->pdi_base.pi_obj;
    514  1.16.2.2  thorpej 	prop_dictionary_keysym_t pdk = NULL;
    515  1.16.2.2  thorpej 
    516  1.16.2.2  thorpej 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    517  1.16.2.2  thorpej 
    518  1.16.2.2  thorpej 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
    519  1.16.2.2  thorpej 
    520  1.16.2.2  thorpej 	if (pd->pd_version != pdi->pdi_base.pi_version)
    521  1.16.2.2  thorpej 		goto out;	/* dictionary changed during iteration */
    522  1.16.2.2  thorpej 
    523  1.16.2.2  thorpej 	_PROP_ASSERT(pdi->pdi_index <= pd->pd_count);
    524  1.16.2.2  thorpej 
    525  1.16.2.2  thorpej 	if (pdi->pdi_index == pd->pd_count)
    526  1.16.2.2  thorpej 		goto out;	/* we've iterated all objects */
    527  1.16.2.2  thorpej 
    528  1.16.2.2  thorpej 	pdk = pd->pd_array[pdi->pdi_index].pde_key;
    529  1.16.2.2  thorpej 	pdi->pdi_index++;
    530  1.16.2.2  thorpej 
    531  1.16.2.2  thorpej  out:
    532  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
    533  1.16.2.2  thorpej 	return (pdk);
    534  1.16.2.2  thorpej }
    535  1.16.2.2  thorpej 
    536  1.16.2.2  thorpej static void
    537  1.16.2.2  thorpej _prop_dictionary_iterator_reset(void *v)
    538  1.16.2.2  thorpej {
    539  1.16.2.2  thorpej 	struct _prop_dictionary_iterator *pdi = v;
    540  1.16.2.2  thorpej 	prop_dictionary_t pd = pdi->pdi_base.pi_obj;
    541  1.16.2.2  thorpej 
    542  1.16.2.2  thorpej 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    543  1.16.2.2  thorpej 
    544  1.16.2.2  thorpej 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
    545  1.16.2.2  thorpej 
    546  1.16.2.2  thorpej 	pdi->pdi_index = 0;
    547  1.16.2.2  thorpej 	pdi->pdi_base.pi_version = pd->pd_version;
    548  1.16.2.2  thorpej 
    549  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
    550  1.16.2.2  thorpej }
    551  1.16.2.2  thorpej 
    552  1.16.2.2  thorpej /*
    553  1.16.2.2  thorpej  * prop_dictionary_create --
    554  1.16.2.2  thorpej  *	Create a dictionary.
    555  1.16.2.2  thorpej  */
    556  1.16.2.2  thorpej prop_dictionary_t
    557  1.16.2.2  thorpej prop_dictionary_create(void)
    558  1.16.2.2  thorpej {
    559  1.16.2.2  thorpej 
    560  1.16.2.2  thorpej 	return (_prop_dictionary_alloc(0));
    561  1.16.2.2  thorpej }
    562  1.16.2.2  thorpej 
    563  1.16.2.2  thorpej /*
    564  1.16.2.2  thorpej  * prop_dictionary_create_with_capacity --
    565  1.16.2.2  thorpej  *	Create a dictionary with the capacity to store N objects.
    566  1.16.2.2  thorpej  */
    567  1.16.2.2  thorpej prop_dictionary_t
    568  1.16.2.2  thorpej prop_dictionary_create_with_capacity(unsigned int capacity)
    569  1.16.2.2  thorpej {
    570  1.16.2.2  thorpej 
    571  1.16.2.2  thorpej 	return (_prop_dictionary_alloc(capacity));
    572  1.16.2.2  thorpej }
    573  1.16.2.2  thorpej 
    574  1.16.2.2  thorpej /*
    575  1.16.2.2  thorpej  * prop_dictionary_copy --
    576  1.16.2.2  thorpej  *	Copy a dictionary.  The new dictionary has an initial capacity equal
    577  1.16.2.2  thorpej  *	to the number of objects stored int the original dictionary.  The new
    578  1.16.2.2  thorpej  *	dictionary contains refrences to the original dictionary's objects,
    579  1.16.2.2  thorpej  *	not copies of those objects (i.e. a shallow copy).
    580  1.16.2.2  thorpej  */
    581  1.16.2.2  thorpej prop_dictionary_t
    582  1.16.2.2  thorpej prop_dictionary_copy(prop_dictionary_t opd)
    583  1.16.2.2  thorpej {
    584  1.16.2.2  thorpej 	prop_dictionary_t pd;
    585  1.16.2.2  thorpej 	prop_dictionary_keysym_t pdk;
    586  1.16.2.2  thorpej 	prop_object_t po;
    587  1.16.2.2  thorpej 	unsigned int idx;
    588  1.16.2.2  thorpej 
    589  1.16.2.2  thorpej 	if (! prop_object_is_dictionary(opd))
    590  1.16.2.2  thorpej 		return (NULL);
    591  1.16.2.2  thorpej 
    592  1.16.2.2  thorpej 	_PROP_RWLOCK_RDLOCK(opd->pd_rwlock);
    593  1.16.2.2  thorpej 
    594  1.16.2.2  thorpej 	pd = _prop_dictionary_alloc(opd->pd_count);
    595  1.16.2.2  thorpej 	if (pd != NULL) {
    596  1.16.2.2  thorpej 		for (idx = 0; idx < opd->pd_count; idx++) {
    597  1.16.2.2  thorpej 			pdk = opd->pd_array[idx].pde_key;
    598  1.16.2.2  thorpej 			po = opd->pd_array[idx].pde_objref;
    599  1.16.2.2  thorpej 
    600  1.16.2.2  thorpej 			prop_object_retain(pdk);
    601  1.16.2.2  thorpej 			prop_object_retain(po);
    602  1.16.2.2  thorpej 
    603  1.16.2.2  thorpej 			pd->pd_array[idx].pde_key = pdk;
    604  1.16.2.2  thorpej 			pd->pd_array[idx].pde_objref = po;
    605  1.16.2.2  thorpej 		}
    606  1.16.2.2  thorpej 		pd->pd_count = opd->pd_count;
    607  1.16.2.2  thorpej 		pd->pd_flags = opd->pd_flags;
    608  1.16.2.2  thorpej 	}
    609  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(opd->pd_rwlock);
    610  1.16.2.2  thorpej 	return (pd);
    611  1.16.2.2  thorpej }
    612  1.16.2.2  thorpej 
    613  1.16.2.2  thorpej /*
    614  1.16.2.2  thorpej  * prop_dictionary_copy_mutable --
    615  1.16.2.2  thorpej  *	Like prop_dictionary_copy(), but the resulting dictionary is
    616  1.16.2.2  thorpej  *	mutable.
    617  1.16.2.2  thorpej  */
    618  1.16.2.2  thorpej prop_dictionary_t
    619  1.16.2.2  thorpej prop_dictionary_copy_mutable(prop_dictionary_t opd)
    620  1.16.2.2  thorpej {
    621  1.16.2.2  thorpej 	prop_dictionary_t pd;
    622  1.16.2.2  thorpej 
    623  1.16.2.2  thorpej 	if (! prop_object_is_dictionary(opd))
    624  1.16.2.2  thorpej 		return (NULL);
    625  1.16.2.2  thorpej 
    626  1.16.2.2  thorpej 	pd = prop_dictionary_copy(opd);
    627  1.16.2.2  thorpej 	if (pd != NULL)
    628  1.16.2.2  thorpej 		pd->pd_flags &= ~PD_F_IMMUTABLE;
    629  1.16.2.2  thorpej 
    630  1.16.2.2  thorpej 	return (pd);
    631  1.16.2.2  thorpej }
    632  1.16.2.2  thorpej 
    633  1.16.2.2  thorpej /*
    634  1.16.2.2  thorpej  * prop_dictionary_count --
    635  1.16.2.2  thorpej  *	Return the number of objects stored in the dictionary.
    636  1.16.2.2  thorpej  */
    637  1.16.2.2  thorpej unsigned int
    638  1.16.2.2  thorpej prop_dictionary_count(prop_dictionary_t pd)
    639  1.16.2.2  thorpej {
    640  1.16.2.2  thorpej 	unsigned int rv;
    641  1.16.2.2  thorpej 
    642  1.16.2.2  thorpej 	if (! prop_object_is_dictionary(pd))
    643  1.16.2.2  thorpej 		return (0);
    644  1.16.2.2  thorpej 
    645  1.16.2.2  thorpej 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
    646  1.16.2.2  thorpej 	rv = pd->pd_count;
    647  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
    648  1.16.2.2  thorpej 
    649  1.16.2.2  thorpej 	return (rv);
    650  1.16.2.2  thorpej }
    651  1.16.2.2  thorpej 
    652  1.16.2.2  thorpej /*
    653  1.16.2.2  thorpej  * prop_dictionary_ensure_capacity --
    654  1.16.2.2  thorpej  *	Ensure that the dictionary has the capacity to store the specified
    655  1.16.2.2  thorpej  *	total number of objects (including the objects already stored in
    656  1.16.2.2  thorpej  *	the dictionary).
    657  1.16.2.2  thorpej  */
    658  1.16.2.2  thorpej boolean_t
    659  1.16.2.2  thorpej prop_dictionary_ensure_capacity(prop_dictionary_t pd, unsigned int capacity)
    660  1.16.2.2  thorpej {
    661  1.16.2.2  thorpej 	boolean_t rv;
    662  1.16.2.2  thorpej 
    663  1.16.2.2  thorpej 	if (! prop_object_is_dictionary(pd))
    664  1.16.2.2  thorpej 		return (FALSE);
    665  1.16.2.2  thorpej 
    666  1.16.2.2  thorpej 	_PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
    667  1.16.2.2  thorpej 	if (capacity > pd->pd_capacity)
    668  1.16.2.2  thorpej 		rv = _prop_dictionary_expand(pd, capacity);
    669  1.16.2.2  thorpej 	else
    670  1.16.2.2  thorpej 		rv = TRUE;
    671  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
    672  1.16.2.2  thorpej 	return (rv);
    673  1.16.2.2  thorpej }
    674  1.16.2.2  thorpej 
    675  1.16.2.2  thorpej /*
    676  1.16.2.2  thorpej  * prop_dictionary_iterator --
    677  1.16.2.2  thorpej  *	Return an iterator for the dictionary.  The dictionary is retained by
    678  1.16.2.2  thorpej  *	the iterator.
    679  1.16.2.2  thorpej  */
    680  1.16.2.2  thorpej prop_object_iterator_t
    681  1.16.2.2  thorpej prop_dictionary_iterator(prop_dictionary_t pd)
    682  1.16.2.2  thorpej {
    683  1.16.2.2  thorpej 	struct _prop_dictionary_iterator *pdi;
    684  1.16.2.2  thorpej 
    685  1.16.2.2  thorpej 	if (! prop_object_is_dictionary(pd))
    686  1.16.2.2  thorpej 		return (NULL);
    687  1.16.2.2  thorpej 
    688  1.16.2.2  thorpej 	pdi = _PROP_CALLOC(sizeof(*pdi), M_TEMP);
    689  1.16.2.2  thorpej 	if (pdi == NULL)
    690  1.16.2.2  thorpej 		return (NULL);
    691  1.16.2.2  thorpej 	pdi->pdi_base.pi_next_object = _prop_dictionary_iterator_next_object;
    692  1.16.2.2  thorpej 	pdi->pdi_base.pi_reset = _prop_dictionary_iterator_reset;
    693  1.16.2.2  thorpej 	prop_object_retain(pd);
    694  1.16.2.2  thorpej 	pdi->pdi_base.pi_obj = pd;
    695  1.16.2.2  thorpej 	_prop_dictionary_iterator_reset(pdi);
    696  1.16.2.2  thorpej 
    697  1.16.2.2  thorpej 	return (&pdi->pdi_base);
    698  1.16.2.2  thorpej }
    699  1.16.2.2  thorpej 
    700  1.16.2.2  thorpej /*
    701  1.16.2.2  thorpej  * prop_dictionary_all_keys --
    702  1.16.2.2  thorpej  *	Return an array containing a snapshot of all of the keys
    703  1.16.2.2  thorpej  *	in the dictionary.
    704  1.16.2.2  thorpej  */
    705  1.16.2.2  thorpej prop_array_t
    706  1.16.2.2  thorpej prop_dictionary_all_keys(prop_dictionary_t pd)
    707  1.16.2.2  thorpej {
    708  1.16.2.2  thorpej 	prop_array_t array;
    709  1.16.2.2  thorpej 	unsigned int idx;
    710  1.16.2.2  thorpej 	boolean_t rv = TRUE;
    711  1.16.2.2  thorpej 
    712  1.16.2.2  thorpej 	if (! prop_object_is_dictionary(pd))
    713  1.16.2.2  thorpej 		return (NULL);
    714  1.16.2.2  thorpej 
    715  1.16.2.2  thorpej 	/* There is no pressing need to lock the dictionary for this. */
    716  1.16.2.2  thorpej 	array = prop_array_create_with_capacity(pd->pd_count);
    717  1.16.2.2  thorpej 
    718  1.16.2.2  thorpej 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
    719  1.16.2.2  thorpej 
    720  1.16.2.2  thorpej 	for (idx = 0; idx < pd->pd_count; idx++) {
    721  1.16.2.2  thorpej 		rv = prop_array_add(array, pd->pd_array[idx].pde_key);
    722  1.16.2.2  thorpej 		if (rv == FALSE)
    723  1.16.2.2  thorpej 			break;
    724  1.16.2.2  thorpej 	}
    725  1.16.2.2  thorpej 
    726  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
    727  1.16.2.2  thorpej 
    728  1.16.2.2  thorpej 	if (rv == FALSE) {
    729  1.16.2.2  thorpej 		prop_object_release(array);
    730  1.16.2.2  thorpej 		array = NULL;
    731  1.16.2.2  thorpej 	}
    732  1.16.2.2  thorpej 	return (array);
    733  1.16.2.2  thorpej }
    734  1.16.2.2  thorpej 
    735  1.16.2.2  thorpej static struct _prop_dict_entry *
    736  1.16.2.2  thorpej _prop_dict_lookup(prop_dictionary_t pd, const char *key,
    737  1.16.2.2  thorpej 		  unsigned int *idxp)
    738  1.16.2.2  thorpej {
    739  1.16.2.2  thorpej 	struct _prop_dict_entry *pde;
    740  1.16.2.2  thorpej 	unsigned int base, idx, distance;
    741  1.16.2.2  thorpej 	int res;
    742  1.16.2.2  thorpej 
    743  1.16.2.2  thorpej 	/*
    744  1.16.2.2  thorpej 	 * Dictionary must be READ-LOCKED or WRITE-LOCKED.
    745  1.16.2.2  thorpej 	 */
    746  1.16.2.2  thorpej 
    747  1.16.2.2  thorpej 	for (idx = 0, base = 0, distance = pd->pd_count; distance != 0;
    748  1.16.2.2  thorpej 	     distance >>= 1) {
    749  1.16.2.2  thorpej 		idx = base + (distance >> 1);
    750  1.16.2.2  thorpej 		pde = &pd->pd_array[idx];
    751  1.16.2.2  thorpej 		_PROP_ASSERT(pde->pde_key != NULL);
    752  1.16.2.2  thorpej 		res = strcmp(key, pde->pde_key->pdk_key);
    753  1.16.2.2  thorpej 		if (res == 0) {
    754  1.16.2.2  thorpej 			if (idxp != NULL)
    755  1.16.2.2  thorpej 				*idxp = idx;
    756  1.16.2.2  thorpej 			return (pde);
    757  1.16.2.2  thorpej 		}
    758  1.16.2.2  thorpej 		if (res > 0) {	/* key > pdk_key: move right */
    759  1.16.2.2  thorpej 			base = idx + 1;
    760  1.16.2.2  thorpej 			distance--;
    761  1.16.2.2  thorpej 		}		/* else move left */
    762  1.16.2.2  thorpej 	}
    763  1.16.2.2  thorpej 
    764  1.16.2.2  thorpej 	/* idx points to the slot we looked at last. */
    765  1.16.2.2  thorpej 	if (idxp != NULL)
    766  1.16.2.2  thorpej 		*idxp = idx;
    767  1.16.2.2  thorpej 	return (NULL);
    768  1.16.2.2  thorpej }
    769  1.16.2.2  thorpej 
    770  1.16.2.2  thorpej /*
    771  1.16.2.2  thorpej  * prop_dictionary_get --
    772  1.16.2.2  thorpej  *	Return the object stored with specified key.
    773  1.16.2.2  thorpej  */
    774  1.16.2.2  thorpej prop_object_t
    775  1.16.2.2  thorpej prop_dictionary_get(prop_dictionary_t pd, const char *key)
    776  1.16.2.2  thorpej {
    777  1.16.2.2  thorpej 	const struct _prop_dict_entry *pde;
    778  1.16.2.2  thorpej 	prop_object_t po = NULL;
    779  1.16.2.2  thorpej 
    780  1.16.2.2  thorpej 	if (! prop_object_is_dictionary(pd))
    781  1.16.2.2  thorpej 		return (NULL);
    782  1.16.2.2  thorpej 
    783  1.16.2.2  thorpej 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
    784  1.16.2.2  thorpej 	pde = _prop_dict_lookup(pd, key, NULL);
    785  1.16.2.2  thorpej 	if (pde != NULL) {
    786  1.16.2.2  thorpej 		_PROP_ASSERT(pde->pde_objref != NULL);
    787  1.16.2.2  thorpej 		po = pde->pde_objref;
    788  1.16.2.2  thorpej 	}
    789  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
    790  1.16.2.2  thorpej 	return (po);
    791  1.16.2.2  thorpej }
    792  1.16.2.2  thorpej 
    793  1.16.2.2  thorpej /*
    794  1.16.2.2  thorpej  * prop_dictionary_get_keysym --
    795  1.16.2.2  thorpej  *	Return the object stored at the location encoded by the keysym.
    796  1.16.2.2  thorpej  */
    797  1.16.2.2  thorpej prop_object_t
    798  1.16.2.2  thorpej prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk)
    799  1.16.2.2  thorpej {
    800  1.16.2.2  thorpej 
    801  1.16.2.2  thorpej 	if (! (prop_object_is_dictionary(pd) &&
    802  1.16.2.2  thorpej 	       prop_object_is_dictionary_keysym(pdk)))
    803  1.16.2.2  thorpej 		return (NULL);
    804  1.16.2.2  thorpej 
    805  1.16.2.2  thorpej 	return (prop_dictionary_get(pd, pdk->pdk_key));
    806  1.16.2.2  thorpej }
    807  1.16.2.2  thorpej 
    808  1.16.2.2  thorpej /*
    809  1.16.2.2  thorpej  * prop_dictionary_set --
    810  1.16.2.2  thorpej  *	Store a reference to an object at with the specified key.
    811  1.16.2.2  thorpej  *	If the key already exisit, the original object is released.
    812  1.16.2.2  thorpej  */
    813  1.16.2.2  thorpej boolean_t
    814  1.16.2.2  thorpej prop_dictionary_set(prop_dictionary_t pd, const char *key, prop_object_t po)
    815  1.16.2.2  thorpej {
    816  1.16.2.2  thorpej 	struct _prop_dict_entry *pde;
    817  1.16.2.2  thorpej 	prop_dictionary_keysym_t pdk;
    818  1.16.2.2  thorpej 	unsigned int idx;
    819  1.16.2.2  thorpej 	boolean_t rv = FALSE;
    820  1.16.2.2  thorpej 
    821  1.16.2.2  thorpej 	if (! prop_object_is_dictionary(pd))
    822  1.16.2.2  thorpej 		return (FALSE);
    823  1.16.2.2  thorpej 
    824  1.16.2.2  thorpej 	_PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
    825  1.16.2.2  thorpej 
    826  1.16.2.2  thorpej 	if (prop_dictionary_is_immutable(pd))
    827  1.16.2.2  thorpej 		return (FALSE);
    828  1.16.2.2  thorpej 
    829  1.16.2.2  thorpej 	_PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
    830  1.16.2.2  thorpej 
    831  1.16.2.2  thorpej 	pde = _prop_dict_lookup(pd, key, &idx);
    832  1.16.2.2  thorpej 	if (pde != NULL) {
    833  1.16.2.2  thorpej 		prop_object_t opo = pde->pde_objref;
    834  1.16.2.2  thorpej 		prop_object_retain(po);
    835  1.16.2.2  thorpej 		pde->pde_objref = po;
    836  1.16.2.2  thorpej 		prop_object_release(opo);
    837  1.16.2.2  thorpej 		rv = TRUE;
    838  1.16.2.2  thorpej 		goto out;
    839  1.16.2.2  thorpej 	}
    840  1.16.2.2  thorpej 
    841  1.16.2.2  thorpej 	pdk = _prop_dict_keysym_alloc(key);
    842  1.16.2.2  thorpej 	if (pdk == NULL)
    843  1.16.2.2  thorpej 		goto out;
    844  1.16.2.2  thorpej 
    845  1.16.2.2  thorpej 	if (pd->pd_count == pd->pd_capacity &&
    846  1.16.2.2  thorpej 	    _prop_dictionary_expand(pd,
    847  1.16.2.2  thorpej 	    			    pd->pd_capacity + EXPAND_STEP) == FALSE) {
    848  1.16.2.2  thorpej 		prop_object_release(pdk);
    849  1.16.2.2  thorpej 	    	goto out;
    850  1.16.2.2  thorpej 	}
    851  1.16.2.2  thorpej 
    852  1.16.2.2  thorpej 	/* At this point, the store will succeed. */
    853  1.16.2.2  thorpej 	prop_object_retain(po);
    854  1.16.2.2  thorpej 
    855  1.16.2.2  thorpej 	if (pd->pd_count == 0) {
    856  1.16.2.2  thorpej 		pd->pd_array[0].pde_key = pdk;
    857  1.16.2.2  thorpej 		pd->pd_array[0].pde_objref = po;
    858  1.16.2.2  thorpej 		pd->pd_count++;
    859  1.16.2.2  thorpej 		pd->pd_version++;
    860  1.16.2.2  thorpej 		rv = TRUE;
    861  1.16.2.2  thorpej 		goto out;
    862  1.16.2.2  thorpej 	}
    863  1.16.2.2  thorpej 
    864  1.16.2.2  thorpej 	pde = &pd->pd_array[idx];
    865  1.16.2.2  thorpej 	_PROP_ASSERT(pde->pde_key != NULL);
    866  1.16.2.2  thorpej 
    867  1.16.2.2  thorpej 	if (strcmp(key, pde->pde_key->pdk_key) < 0) {
    868  1.16.2.2  thorpej 		/*
    869  1.16.2.2  thorpej 		 * key < pdk_key: insert to the left.  This is the same as
    870  1.16.2.2  thorpej 		 * inserting to the right, except we decrement the current
    871  1.16.2.2  thorpej 		 * index first.
    872  1.16.2.2  thorpej 		 *
    873  1.16.2.2  thorpej 		 * Because we're unsigned, we have to special case 0
    874  1.16.2.2  thorpej 		 * (grumble).
    875  1.16.2.2  thorpej 		 */
    876  1.16.2.2  thorpej 		if (idx == 0) {
    877  1.16.2.2  thorpej 			memmove(&pd->pd_array[1], &pd->pd_array[0],
    878  1.16.2.2  thorpej 				pd->pd_count * sizeof(*pde));
    879  1.16.2.2  thorpej 			pd->pd_array[0].pde_key = pdk;
    880  1.16.2.2  thorpej 			pd->pd_array[0].pde_objref = po;
    881  1.16.2.2  thorpej 			pd->pd_count++;
    882  1.16.2.2  thorpej 			pd->pd_version++;
    883  1.16.2.2  thorpej 			rv = TRUE;
    884  1.16.2.2  thorpej 			goto out;
    885  1.16.2.2  thorpej 		}
    886  1.16.2.2  thorpej 		idx--;
    887  1.16.2.2  thorpej 	}
    888  1.16.2.2  thorpej 
    889  1.16.2.2  thorpej 	memmove(&pd->pd_array[idx + 2], &pd->pd_array[idx + 1],
    890  1.16.2.2  thorpej 		(pd->pd_count - (idx + 1)) * sizeof(*pde));
    891  1.16.2.2  thorpej 	pd->pd_array[idx + 1].pde_key = pdk;
    892  1.16.2.2  thorpej 	pd->pd_array[idx + 1].pde_objref = po;
    893  1.16.2.2  thorpej 	pd->pd_count++;
    894  1.16.2.2  thorpej 
    895  1.16.2.2  thorpej 	pd->pd_version++;
    896  1.16.2.2  thorpej 
    897  1.16.2.2  thorpej 	rv = TRUE;
    898  1.16.2.2  thorpej 
    899  1.16.2.2  thorpej  out:
    900  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
    901  1.16.2.2  thorpej 	return (rv);
    902  1.16.2.2  thorpej }
    903  1.16.2.2  thorpej 
    904  1.16.2.2  thorpej /*
    905  1.16.2.2  thorpej  * prop_dictionary_set_keysym --
    906  1.16.2.2  thorpej  *	Replace the object in the dictionary at the location encoded by
    907  1.16.2.2  thorpej  *	the keysym.
    908  1.16.2.2  thorpej  */
    909  1.16.2.2  thorpej boolean_t
    910  1.16.2.2  thorpej prop_dictionary_set_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
    911  1.16.2.2  thorpej 			   prop_object_t po)
    912  1.16.2.2  thorpej {
    913  1.16.2.2  thorpej 
    914  1.16.2.2  thorpej 	if (! (prop_object_is_dictionary(pd) &&
    915  1.16.2.2  thorpej 	       prop_object_is_dictionary_keysym(pdk)))
    916  1.16.2.2  thorpej 		return (FALSE);
    917  1.16.2.2  thorpej 
    918  1.16.2.2  thorpej 	return (prop_dictionary_set(pd, pdk->pdk_key, po));
    919  1.16.2.2  thorpej }
    920  1.16.2.2  thorpej 
    921  1.16.2.2  thorpej static void
    922  1.16.2.2  thorpej _prop_dictionary_remove(prop_dictionary_t pd, struct _prop_dict_entry *pde,
    923  1.16.2.2  thorpej     unsigned int idx)
    924  1.16.2.2  thorpej {
    925  1.16.2.2  thorpej 	prop_dictionary_keysym_t pdk = pde->pde_key;
    926  1.16.2.2  thorpej 	prop_object_t po = pde->pde_objref;
    927  1.16.2.2  thorpej 
    928  1.16.2.2  thorpej 	/*
    929  1.16.2.2  thorpej 	 * Dictionary must be WRITE-LOCKED.
    930  1.16.2.2  thorpej 	 */
    931  1.16.2.2  thorpej 
    932  1.16.2.2  thorpej 	_PROP_ASSERT(pd->pd_count != 0);
    933  1.16.2.2  thorpej 	_PROP_ASSERT(idx < pd->pd_count);
    934  1.16.2.2  thorpej 	_PROP_ASSERT(pde == &pd->pd_array[idx]);
    935  1.16.2.2  thorpej 
    936  1.16.2.2  thorpej 	idx++;
    937  1.16.2.2  thorpej 	memmove(&pd->pd_array[idx - 1], &pd->pd_array[idx],
    938  1.16.2.2  thorpej 		(pd->pd_count - idx) * sizeof(*pde));
    939  1.16.2.2  thorpej 	pd->pd_count--;
    940  1.16.2.2  thorpej 	pd->pd_version++;
    941  1.16.2.2  thorpej 
    942  1.16.2.2  thorpej 	prop_object_release(pdk);
    943  1.16.2.2  thorpej 	prop_object_release(po);
    944  1.16.2.2  thorpej }
    945  1.16.2.2  thorpej 
    946  1.16.2.2  thorpej /*
    947  1.16.2.2  thorpej  * prop_dictionary_remove --
    948  1.16.2.2  thorpej  *	Remove the reference to an object with the specified key from
    949  1.16.2.2  thorpej  *	the dictionary.
    950  1.16.2.2  thorpej  */
    951  1.16.2.2  thorpej void
    952  1.16.2.2  thorpej prop_dictionary_remove(prop_dictionary_t pd, const char *key)
    953  1.16.2.2  thorpej {
    954  1.16.2.2  thorpej 	struct _prop_dict_entry *pde;
    955  1.16.2.2  thorpej 	unsigned int idx;
    956  1.16.2.2  thorpej 
    957  1.16.2.2  thorpej 	if (! prop_object_is_dictionary(pd))
    958  1.16.2.2  thorpej 		return;
    959  1.16.2.2  thorpej 
    960  1.16.2.2  thorpej 	_PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
    961  1.16.2.2  thorpej 
    962  1.16.2.2  thorpej 	/* XXX Should this be a _PROP_ASSERT()? */
    963  1.16.2.2  thorpej 	if (prop_dictionary_is_immutable(pd))
    964  1.16.2.2  thorpej 		goto out;
    965  1.16.2.2  thorpej 
    966  1.16.2.2  thorpej 	pde = _prop_dict_lookup(pd, key, &idx);
    967  1.16.2.2  thorpej 	/* XXX Should this be a _PROP_ASSERT()? */
    968  1.16.2.2  thorpej 	if (pde == NULL)
    969  1.16.2.2  thorpej 		goto out;
    970  1.16.2.2  thorpej 
    971  1.16.2.2  thorpej 	_prop_dictionary_remove(pd, pde, idx);
    972  1.16.2.2  thorpej  out:
    973  1.16.2.2  thorpej 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
    974  1.16.2.2  thorpej }
    975  1.16.2.2  thorpej 
    976  1.16.2.2  thorpej /*
    977  1.16.2.2  thorpej  * prop_dictionary_remove_keysym --
    978  1.16.2.2  thorpej  *	Remove a reference to an object stored in the dictionary at the
    979  1.16.2.2  thorpej  *	location encoded by the keysym.
    980  1.16.2.2  thorpej  */
    981  1.16.2.2  thorpej void
    982  1.16.2.2  thorpej prop_dictionary_remove_keysym(prop_dictionary_t pd,
    983  1.16.2.2  thorpej 			      prop_dictionary_keysym_t pdk)
    984  1.16.2.2  thorpej {
    985  1.16.2.2  thorpej 
    986  1.16.2.2  thorpej 	if (! (prop_object_is_dictionary(pd) &&
    987  1.16.2.2  thorpej 	       prop_object_is_dictionary_keysym(pdk)))
    988  1.16.2.2  thorpej 		return;
    989  1.16.2.2  thorpej 
    990  1.16.2.2  thorpej 	prop_dictionary_remove(pd, pdk->pdk_key);
    991  1.16.2.2  thorpej }
    992  1.16.2.2  thorpej 
    993  1.16.2.2  thorpej /*
    994  1.16.2.2  thorpej  * prop_dictionary_equals --
    995  1.16.2.2  thorpej  *	Return TRUE if the two dictionaries are equivalent.  Note we do a
    996  1.16.2.2  thorpej  *	by-value comparison of the objects in the dictionary.
    997  1.16.2.2  thorpej  */
    998  1.16.2.2  thorpej boolean_t
    999  1.16.2.2  thorpej prop_dictionary_equals(prop_dictionary_t dict1, prop_dictionary_t dict2)
   1000  1.16.2.2  thorpej {
   1001  1.16.2.2  thorpej 
   1002  1.16.2.2  thorpej 	return (_prop_dictionary_equals(dict1, dict2));
   1003  1.16.2.2  thorpej }
   1004  1.16.2.2  thorpej 
   1005  1.16.2.2  thorpej /*
   1006  1.16.2.2  thorpej  * prop_dictionary_keysym_cstring_nocopy --
   1007  1.16.2.2  thorpej  *	Return an immutable reference to the keysym's value.
   1008  1.16.2.2  thorpej  */
   1009  1.16.2.2  thorpej const char *
   1010  1.16.2.2  thorpej prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pdk)
   1011  1.16.2.2  thorpej {
   1012  1.16.2.2  thorpej 
   1013  1.16.2.2  thorpej 	if (! prop_object_is_dictionary_keysym(pdk))
   1014  1.16.2.2  thorpej 		return (NULL);
   1015  1.16.2.2  thorpej 
   1016  1.16.2.2  thorpej 	return (pdk->pdk_key);
   1017  1.16.2.2  thorpej }
   1018  1.16.2.2  thorpej 
   1019  1.16.2.2  thorpej /*
   1020  1.16.2.2  thorpej  * prop_dictionary_keysym_equals --
   1021  1.16.2.2  thorpej  *	Return TRUE if the two dictionary key symbols are equivalent.
   1022  1.16.2.2  thorpej  *	Note: We do not compare the object references.
   1023  1.16.2.2  thorpej  */
   1024  1.16.2.2  thorpej boolean_t
   1025  1.16.2.2  thorpej prop_dictionary_keysym_equals(prop_dictionary_keysym_t pdk1,
   1026  1.16.2.2  thorpej 			      prop_dictionary_keysym_t pdk2)
   1027  1.16.2.2  thorpej {
   1028  1.16.2.2  thorpej 
   1029  1.16.2.2  thorpej 	return (_prop_dict_keysym_equals(pdk1, pdk2));
   1030  1.16.2.2  thorpej }
   1031  1.16.2.2  thorpej 
   1032  1.16.2.2  thorpej /*
   1033  1.16.2.2  thorpej  * prop_dictionary_externalize --
   1034  1.16.2.2  thorpej  *	Externalize a dictionary, returning a NUL-terminated buffer
   1035  1.16.2.2  thorpej  *	containing the XML-style representation.  The buffer is allocated
   1036  1.16.2.2  thorpej  *	with the M_TEMP memory type.
   1037  1.16.2.2  thorpej  */
   1038  1.16.2.2  thorpej char *
   1039  1.16.2.2  thorpej prop_dictionary_externalize(prop_dictionary_t pd)
   1040  1.16.2.2  thorpej {
   1041  1.16.2.2  thorpej 	struct _prop_object_externalize_context *ctx;
   1042  1.16.2.2  thorpej 	char *cp;
   1043  1.16.2.2  thorpej 
   1044  1.16.2.2  thorpej 	ctx = _prop_object_externalize_context_alloc();
   1045  1.16.2.2  thorpej 	if (ctx == NULL)
   1046  1.16.2.2  thorpej 		return (NULL);
   1047  1.16.2.2  thorpej 
   1048  1.16.2.2  thorpej 	if (_prop_object_externalize_header(ctx) == FALSE ||
   1049  1.16.2.2  thorpej 	    (*pd->pd_obj.po_type->pot_extern)(ctx, pd) == FALSE ||
   1050  1.16.2.2  thorpej 	    _prop_object_externalize_footer(ctx) == FALSE) {
   1051  1.16.2.2  thorpej 		/* We are responsible for releasing the buffer. */
   1052  1.16.2.2  thorpej 		_PROP_FREE(ctx->poec_buf, M_TEMP);
   1053  1.16.2.2  thorpej 		_prop_object_externalize_context_free(ctx);
   1054  1.16.2.2  thorpej 		return (NULL);
   1055  1.16.2.2  thorpej 	}
   1056  1.16.2.2  thorpej 
   1057  1.16.2.2  thorpej 	cp = ctx->poec_buf;
   1058  1.16.2.2  thorpej 	_prop_object_externalize_context_free(ctx);
   1059  1.16.2.2  thorpej 
   1060  1.16.2.2  thorpej 	return (cp);
   1061  1.16.2.2  thorpej }
   1062  1.16.2.2  thorpej 
   1063  1.16.2.2  thorpej /*
   1064  1.16.2.2  thorpej  * _prop_dictionary_internalize --
   1065  1.16.2.2  thorpej  *	Parse a <dict>...</dict> and return the object created from the
   1066  1.16.2.2  thorpej  *	external representation.
   1067  1.16.2.2  thorpej  */
   1068  1.16.2.2  thorpej prop_object_t
   1069  1.16.2.2  thorpej _prop_dictionary_internalize(struct _prop_object_internalize_context *ctx)
   1070  1.16.2.2  thorpej {
   1071  1.16.2.2  thorpej 	prop_dictionary_t dict;
   1072  1.16.2.2  thorpej 	prop_object_t val;
   1073  1.16.2.2  thorpej 	size_t keylen;
   1074  1.16.2.2  thorpej 	char *tmpkey;
   1075  1.16.2.2  thorpej 
   1076  1.16.2.2  thorpej 	/* We don't currently understand any attributes. */
   1077  1.16.2.2  thorpej 	if (ctx->poic_tagattr != NULL)
   1078  1.16.2.2  thorpej 		return (NULL);
   1079  1.16.2.2  thorpej 
   1080  1.16.2.2  thorpej 	dict = prop_dictionary_create();
   1081  1.16.2.2  thorpej 	if (dict == NULL)
   1082  1.16.2.2  thorpej 		return (NULL);
   1083  1.16.2.2  thorpej 
   1084  1.16.2.2  thorpej 	if (ctx->poic_is_empty_element)
   1085  1.16.2.2  thorpej 		return (dict);
   1086  1.16.2.2  thorpej 
   1087  1.16.2.2  thorpej 	tmpkey = _PROP_MALLOC(PDK_MAXKEY + 1, M_TEMP);
   1088  1.16.2.2  thorpej 	if (tmpkey == NULL)
   1089  1.16.2.2  thorpej 		goto bad;
   1090  1.16.2.2  thorpej 
   1091  1.16.2.2  thorpej 	for (;;) {
   1092  1.16.2.2  thorpej 		/* Fetch the next tag. */
   1093  1.16.2.2  thorpej 		if (_prop_object_internalize_find_tag(ctx, NULL,
   1094  1.16.2.2  thorpej 					_PROP_TAG_TYPE_EITHER) == FALSE)
   1095  1.16.2.2  thorpej 			goto bad;
   1096  1.16.2.2  thorpej 
   1097  1.16.2.2  thorpej 		/* Check to see if this is the end of the dictionary. */
   1098  1.16.2.2  thorpej 		if (_PROP_TAG_MATCH(ctx, "dict") &&
   1099  1.16.2.2  thorpej 		    ctx->poic_tag_type == _PROP_TAG_TYPE_END)
   1100  1.16.2.2  thorpej 			break;
   1101  1.16.2.2  thorpej 
   1102  1.16.2.2  thorpej 		/* Ok, it must be a non-empty key start tag. */
   1103  1.16.2.2  thorpej 		if (!_PROP_TAG_MATCH(ctx, "key") ||
   1104  1.16.2.2  thorpej 		    ctx->poic_tag_type != _PROP_TAG_TYPE_START ||
   1105  1.16.2.2  thorpej 		    ctx->poic_is_empty_element)
   1106  1.16.2.2  thorpej 		    	goto bad;
   1107  1.16.2.2  thorpej 
   1108  1.16.2.2  thorpej 		if (_prop_object_internalize_decode_string(ctx,
   1109  1.16.2.2  thorpej 						tmpkey, PDK_MAXKEY, &keylen,
   1110  1.16.2.2  thorpej 						&ctx->poic_cp) == FALSE)
   1111  1.16.2.2  thorpej 			goto bad;
   1112  1.16.2.2  thorpej 
   1113  1.16.2.2  thorpej 		_PROP_ASSERT(keylen <= PDK_MAXKEY);
   1114  1.16.2.2  thorpej 		tmpkey[keylen] = '\0';
   1115  1.16.2.2  thorpej 
   1116  1.16.2.2  thorpej 		if (_prop_object_internalize_find_tag(ctx, "key",
   1117  1.16.2.2  thorpej 					_PROP_TAG_TYPE_END) == FALSE)
   1118  1.16.2.2  thorpej 			goto bad;
   1119  1.16.2.2  thorpej 
   1120  1.16.2.2  thorpej 		/* ..and now the beginning of the value. */
   1121  1.16.2.2  thorpej 		if (_prop_object_internalize_find_tag(ctx, NULL,
   1122  1.16.2.2  thorpej 					_PROP_TAG_TYPE_START) == FALSE)
   1123  1.16.2.2  thorpej 			goto bad;
   1124  1.16.2.2  thorpej 
   1125  1.16.2.2  thorpej 		val = _prop_object_internalize_by_tag(ctx);
   1126  1.16.2.2  thorpej 		if (val == NULL)
   1127  1.16.2.2  thorpej 			goto bad;
   1128  1.16.2.2  thorpej 
   1129  1.16.2.2  thorpej 		if (prop_dictionary_set(dict, tmpkey, val) == FALSE) {
   1130  1.16.2.2  thorpej 			prop_object_release(val);
   1131  1.16.2.2  thorpej 			goto bad;
   1132  1.16.2.2  thorpej 		}
   1133  1.16.2.2  thorpej 		prop_object_release(val);
   1134  1.16.2.2  thorpej 	}
   1135  1.16.2.2  thorpej 
   1136  1.16.2.2  thorpej 	_PROP_FREE(tmpkey, M_TEMP);
   1137  1.16.2.2  thorpej 	return (dict);
   1138  1.16.2.2  thorpej 
   1139  1.16.2.2  thorpej  bad:
   1140  1.16.2.2  thorpej 	if (tmpkey != NULL)
   1141  1.16.2.2  thorpej 		_PROP_FREE(tmpkey, M_TEMP);
   1142  1.16.2.2  thorpej 	prop_object_release(dict);
   1143  1.16.2.2  thorpej 	return (NULL);
   1144  1.16.2.2  thorpej }
   1145  1.16.2.2  thorpej 
   1146  1.16.2.2  thorpej /*
   1147  1.16.2.2  thorpej  * prop_dictionary_internalize --
   1148  1.16.2.2  thorpej  *	Create a dictionary by parsing the NUL-terminated XML-style
   1149  1.16.2.2  thorpej  *	representation.
   1150  1.16.2.2  thorpej  */
   1151  1.16.2.2  thorpej prop_dictionary_t
   1152  1.16.2.2  thorpej prop_dictionary_internalize(const char *xml)
   1153  1.16.2.2  thorpej {
   1154  1.16.2.2  thorpej 	prop_dictionary_t dict = NULL;
   1155  1.16.2.2  thorpej 	struct _prop_object_internalize_context *ctx;
   1156  1.16.2.2  thorpej 
   1157  1.16.2.2  thorpej 	ctx = _prop_object_internalize_context_alloc(xml);
   1158  1.16.2.2  thorpej 	if (ctx == NULL)
   1159  1.16.2.2  thorpej 		return (NULL);
   1160  1.16.2.2  thorpej 
   1161  1.16.2.2  thorpej 	/* We start with a <plist> tag. */
   1162  1.16.2.2  thorpej 	if (_prop_object_internalize_find_tag(ctx, "plist",
   1163  1.16.2.2  thorpej 					      _PROP_TAG_TYPE_START) == FALSE)
   1164  1.16.2.2  thorpej 		goto out;
   1165  1.16.2.2  thorpej 
   1166  1.16.2.2  thorpej 	/* Plist elements cannot be empty. */
   1167  1.16.2.2  thorpej 	if (ctx->poic_is_empty_element)
   1168  1.16.2.2  thorpej 		goto out;
   1169  1.16.2.2  thorpej 
   1170  1.16.2.2  thorpej 	/*
   1171  1.16.2.2  thorpej 	 * We don't understand any plist attributes, but Apple XML
   1172  1.16.2.2  thorpej 	 * property lists often have a "version" attribute.  If we
   1173  1.16.2.2  thorpej 	 * see that one, we simply ignore it.
   1174  1.16.2.2  thorpej 	 */
   1175  1.16.2.2  thorpej 	if (ctx->poic_tagattr != NULL &&
   1176  1.16.2.2  thorpej 	    !_PROP_TAGATTR_MATCH(ctx, "version"))
   1177  1.16.2.2  thorpej 		goto out;
   1178  1.16.2.2  thorpej 
   1179  1.16.2.2  thorpej 	/* Next we expect to see <dict>. */
   1180  1.16.2.2  thorpej 	if (_prop_object_internalize_find_tag(ctx, "dict",
   1181  1.16.2.2  thorpej 					      _PROP_TAG_TYPE_START) == FALSE)
   1182  1.16.2.2  thorpej 		goto out;
   1183  1.16.2.2  thorpej 
   1184  1.16.2.2  thorpej 	dict = _prop_dictionary_internalize(ctx);
   1185  1.16.2.2  thorpej 	if (dict == NULL)
   1186  1.16.2.2  thorpej 		goto out;
   1187  1.16.2.2  thorpej 
   1188  1.16.2.2  thorpej 	/* We've advanced past </dict>.  Now we want </plist>. */
   1189  1.16.2.2  thorpej 	if (_prop_object_internalize_find_tag(ctx, "plist",
   1190  1.16.2.2  thorpej 					      _PROP_TAG_TYPE_END) == FALSE) {
   1191  1.16.2.2  thorpej 		prop_object_release(dict);
   1192  1.16.2.2  thorpej 		dict = NULL;
   1193  1.16.2.2  thorpej 	}
   1194  1.16.2.2  thorpej 
   1195  1.16.2.2  thorpej  out:
   1196  1.16.2.2  thorpej  	_prop_object_internalize_context_free(ctx);
   1197  1.16.2.2  thorpej 	return (dict);
   1198  1.16.2.2  thorpej }
   1199  1.16.2.2  thorpej 
   1200  1.16.2.2  thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
   1201  1.16.2.2  thorpej /*
   1202  1.16.2.2  thorpej  * prop_dictionary_externalize_to_file --
   1203  1.16.2.2  thorpej  *	Externalize a dictionary to the specified file.
   1204  1.16.2.2  thorpej  */
   1205  1.16.2.2  thorpej boolean_t
   1206  1.16.2.2  thorpej prop_dictionary_externalize_to_file(prop_dictionary_t dict, const char *fname)
   1207  1.16.2.2  thorpej {
   1208  1.16.2.2  thorpej 	char *xml;
   1209  1.16.2.2  thorpej 	boolean_t rv;
   1210  1.16.2.2  thorpej 	int save_errno = 0;	/* XXXGCC -Wuninitialized [mips, ...] */
   1211  1.16.2.2  thorpej 
   1212  1.16.2.2  thorpej 	xml = prop_dictionary_externalize(dict);
   1213  1.16.2.2  thorpej 	if (xml == NULL)
   1214  1.16.2.2  thorpej 		return (FALSE);
   1215  1.16.2.2  thorpej 	rv = _prop_object_externalize_write_file(fname, xml, strlen(xml));
   1216  1.16.2.2  thorpej 	if (rv == FALSE)
   1217  1.16.2.2  thorpej 		save_errno = errno;
   1218  1.16.2.2  thorpej 	_PROP_FREE(xml, M_TEMP);
   1219  1.16.2.2  thorpej 	if (rv == FALSE)
   1220  1.16.2.2  thorpej 		errno = save_errno;
   1221  1.16.2.2  thorpej 
   1222  1.16.2.2  thorpej 	return (rv);
   1223  1.16.2.2  thorpej }
   1224  1.16.2.2  thorpej 
   1225  1.16.2.2  thorpej /*
   1226  1.16.2.2  thorpej  * prop_dictionary_internalize_from_file --
   1227  1.16.2.2  thorpej  *	Internalize a dictionary from a file.
   1228  1.16.2.2  thorpej  */
   1229  1.16.2.2  thorpej prop_dictionary_t
   1230  1.16.2.2  thorpej prop_dictionary_internalize_from_file(const char *fname)
   1231  1.16.2.2  thorpej {
   1232  1.16.2.2  thorpej 	struct _prop_object_internalize_mapped_file *mf;
   1233  1.16.2.2  thorpej 	prop_dictionary_t dict;
   1234  1.16.2.2  thorpej 
   1235  1.16.2.2  thorpej 	mf = _prop_object_internalize_map_file(fname);
   1236  1.16.2.2  thorpej 	if (mf == NULL)
   1237  1.16.2.2  thorpej 		return (NULL);
   1238  1.16.2.2  thorpej 	dict = prop_dictionary_internalize(mf->poimf_xml);
   1239  1.16.2.2  thorpej 	_prop_object_internalize_unmap_file(mf);
   1240  1.16.2.2  thorpej 
   1241  1.16.2.2  thorpej 	return (dict);
   1242  1.16.2.2  thorpej }
   1243  1.16.2.2  thorpej #endif /* !_KERNEL && !_STANDALONE */
   1244