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