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