Home | History | Annotate | Line # | Download | only in libprop
prop_dictionary.c revision 1.11
      1  1.11  thorpej /*	$NetBSD: prop_dictionary.c,v 1.11 2006/08/22 21:21:23 thorpej Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*-
      4   1.1  thorpej  * Copyright (c) 2006 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.1  thorpej #include <prop/prop_dictionary.h>
     40   1.1  thorpej #include <prop/prop_string.h>
     41   1.1  thorpej #include "prop_object_impl.h"
     42   1.1  thorpej 
     43   1.8  thorpej #if defined(__NetBSD__)
     44   1.8  thorpej #include <sys/tree.h>
     45   1.8  thorpej #else
     46   1.8  thorpej #error Need to find a NetBSD sys/tree.h
     47   1.8  thorpej #endif
     48   1.8  thorpej 
     49   1.9  thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
     50   1.9  thorpej #include <errno.h>
     51   1.9  thorpej #endif
     52   1.9  thorpej 
     53   1.1  thorpej /*
     54   1.1  thorpej  * We implement these like arrays, but we keep them sorted by key.
     55   1.1  thorpej  * This allows us to binary-search as well as keep externalized output
     56   1.1  thorpej  * sane-looking for human eyes.
     57   1.1  thorpej  */
     58   1.1  thorpej 
     59   1.1  thorpej #define	EXPAND_STEP		16
     60   1.1  thorpej 
     61   1.1  thorpej /*
     62   1.1  thorpej  * prop_dictionary_keysym_t is allocated with space at the end to hold the
     63   1.1  thorpej  * key.  This must be a regular object so that we can maintain sane iterator
     64   1.1  thorpej  * semantics -- we don't want to require that the caller release the result
     65   1.1  thorpej  * of prop_object_iterator_next().
     66   1.1  thorpej  *
     67   1.1  thorpej  * We'd like to have some small'ish keysym objects for up-to-16 characters
     68   1.1  thorpej  * in a key, some for up-to-32 characters in a key, and then a final bucket
     69   1.1  thorpej  * for up-to-128 characters in a key (not including NUL).  Keys longer than
     70   1.1  thorpej  * 128 characters are not allowed.
     71   1.1  thorpej  */
     72   1.1  thorpej struct _prop_dictionary_keysym {
     73   1.4  thorpej 	struct _prop_object		pdk_obj;
     74   1.4  thorpej 	size_t				pdk_size;
     75   1.8  thorpej 	RB_ENTRY(_prop_dictionary_keysym) pdk_link;
     76   1.4  thorpej 	char 				pdk_key[1];
     77   1.1  thorpej 	/* actually variable length */
     78   1.1  thorpej };
     79   1.1  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.4  thorpej #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.6  thorpej 	struct _prop_dict_entry	*pd_array;
     99   1.1  thorpej 	unsigned int		pd_capacity;
    100   1.1  thorpej 	unsigned int		pd_count;
    101   1.1  thorpej 	int			pd_flags;
    102   1.1  thorpej 
    103   1.1  thorpej 	uint32_t		pd_version;
    104   1.1  thorpej };
    105   1.1  thorpej 
    106   1.1  thorpej #define	PD_F_IMMUTABLE		0x01	/* dictionary is immutable */
    107   1.1  thorpej 
    108   1.1  thorpej _PROP_POOL_INIT(_prop_dictionary_pool, sizeof(struct _prop_dictionary),
    109   1.1  thorpej 		"propdict")
    110   1.1  thorpej _PROP_MALLOC_DEFINE(M_PROP_DICT, "prop dictionary",
    111   1.1  thorpej 		    "property dictionary container object")
    112   1.1  thorpej 
    113   1.3  thorpej static void		_prop_dictionary_free(void *);
    114   1.3  thorpej static boolean_t	_prop_dictionary_externalize(
    115   1.3  thorpej 				struct _prop_object_externalize_context *,
    116   1.3  thorpej 				void *);
    117   1.3  thorpej static boolean_t	_prop_dictionary_equals(void *, void *);
    118   1.3  thorpej 
    119   1.3  thorpej static const struct _prop_object_type _prop_object_type_dictionary = {
    120   1.3  thorpej 	.pot_type	=	PROP_TYPE_DICTIONARY,
    121   1.3  thorpej 	.pot_free	=	_prop_dictionary_free,
    122   1.3  thorpej 	.pot_extern	=	_prop_dictionary_externalize,
    123   1.3  thorpej 	.pot_equals	=	_prop_dictionary_equals,
    124   1.3  thorpej };
    125   1.3  thorpej 
    126   1.5  thorpej static void		_prop_dict_keysym_free(void *);
    127   1.5  thorpej static boolean_t	_prop_dict_keysym_externalize(
    128   1.3  thorpej 				struct _prop_object_externalize_context *,
    129   1.3  thorpej 				void *);
    130   1.5  thorpej static boolean_t	_prop_dict_keysym_equals(void *, void *);
    131   1.3  thorpej 
    132   1.3  thorpej static const struct _prop_object_type _prop_object_type_dict_keysym = {
    133   1.3  thorpej 	.pot_type	=	PROP_TYPE_DICT_KEYSYM,
    134   1.5  thorpej 	.pot_free	=	_prop_dict_keysym_free,
    135   1.5  thorpej 	.pot_extern	=	_prop_dict_keysym_externalize,
    136   1.5  thorpej 	.pot_equals	=	_prop_dict_keysym_equals,
    137   1.3  thorpej };
    138   1.3  thorpej 
    139   1.1  thorpej #define	prop_object_is_dictionary(x)		\
    140  1.11  thorpej 	((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_dictionary)
    141   1.1  thorpej #define	prop_object_is_dictionary_keysym(x)	\
    142  1.11  thorpej 	((x) != NULL && (x)->pdk_obj.po_type == &_prop_object_type_dict_keysym)
    143   1.1  thorpej 
    144   1.1  thorpej #define	prop_dictionary_is_immutable(x)		\
    145   1.1  thorpej 				(((x)->pd_flags & PD_F_IMMUTABLE) != 0)
    146   1.1  thorpej 
    147   1.1  thorpej struct _prop_dictionary_iterator {
    148   1.1  thorpej 	struct _prop_object_iterator pdi_base;
    149   1.1  thorpej 	unsigned int		pdi_index;
    150   1.1  thorpej };
    151   1.1  thorpej 
    152   1.6  thorpej /*
    153   1.6  thorpej  * Dictionary key symbols are immutable, and we are likely to have many
    154   1.6  thorpej  * duplicated key symbols.  So, to save memory, we unique'ify key symbols
    155   1.6  thorpej  * so we only have to have one copy of each string.
    156   1.6  thorpej  */
    157   1.6  thorpej 
    158   1.8  thorpej static int
    159   1.8  thorpej _prop_dict_keysym_tree_cmp(prop_dictionary_keysym_t pdk1,
    160   1.8  thorpej 			   prop_dictionary_keysym_t pdk2)
    161   1.6  thorpej {
    162   1.6  thorpej 
    163   1.8  thorpej 	return (strcmp(pdk1->pdk_key, pdk2->pdk_key));
    164   1.6  thorpej }
    165   1.6  thorpej 
    166   1.8  thorpej static RB_HEAD(_prop_dict_keysym_tree, _prop_dictionary_keysym)
    167   1.8  thorpej     _prop_dict_keysym_tree = RB_INITIALIZER(&_prop_dict_keysym_tree);
    168   1.8  thorpej RB_PROTOTYPE(_prop_dict_keysym_tree, _prop_dictionary_keysym, pdk_link,
    169   1.8  thorpej 	     _prop_dict_keysym_tree_cmp)
    170   1.8  thorpej RB_GENERATE(_prop_dict_keysym_tree, _prop_dictionary_keysym, pdk_link,
    171   1.8  thorpej 	    _prop_dict_keysym_tree_cmp)
    172   1.6  thorpej 
    173   1.8  thorpej _PROP_MUTEX_DECL(_prop_dict_keysym_tree_mutex)
    174   1.6  thorpej 
    175   1.1  thorpej static void
    176   1.6  thorpej _prop_dict_keysym_put(prop_dictionary_keysym_t pdk)
    177   1.1  thorpej {
    178   1.1  thorpej 
    179   1.4  thorpej 	if (pdk->pdk_size <= PDK_SIZE_16)
    180   1.4  thorpej 		_PROP_POOL_PUT(_prop_dictionary_keysym16_pool, pdk);
    181   1.4  thorpej 	else if (pdk->pdk_size <= PDK_SIZE_32)
    182   1.4  thorpej 		_PROP_POOL_PUT(_prop_dictionary_keysym32_pool, pdk);
    183   1.1  thorpej 	else {
    184   1.4  thorpej 		_PROP_ASSERT(pdk->pdk_size <= PDK_SIZE_128);
    185   1.4  thorpej 		_PROP_POOL_PUT(_prop_dictionary_keysym128_pool, pdk);
    186   1.1  thorpej 	}
    187   1.6  thorpej }
    188   1.6  thorpej 
    189   1.6  thorpej static void
    190   1.6  thorpej _prop_dict_keysym_free(void *v)
    191   1.6  thorpej {
    192   1.6  thorpej 	prop_dictionary_keysym_t pdk = v;
    193   1.6  thorpej 
    194   1.8  thorpej 	_PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
    195   1.8  thorpej 	RB_REMOVE(_prop_dict_keysym_tree, &_prop_dict_keysym_tree, pdk);
    196   1.8  thorpej 	_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    197   1.6  thorpej 
    198   1.6  thorpej 	_prop_dict_keysym_put(pdk);
    199   1.1  thorpej }
    200   1.1  thorpej 
    201   1.1  thorpej static boolean_t
    202   1.5  thorpej _prop_dict_keysym_externalize(struct _prop_object_externalize_context *ctx,
    203   1.1  thorpej 			     void *v)
    204   1.1  thorpej {
    205   1.4  thorpej 	prop_dictionary_keysym_t pdk = v;
    206   1.1  thorpej 
    207   1.1  thorpej 	/* We externalize these as strings, and they're never empty. */
    208   1.1  thorpej 
    209   1.4  thorpej 	_PROP_ASSERT(pdk->pdk_key[0] != '\0');
    210   1.1  thorpej 
    211   1.1  thorpej 	if (_prop_object_externalize_start_tag(ctx, "string") == FALSE ||
    212   1.1  thorpej 	    _prop_object_externalize_append_encoded_cstring(ctx,
    213   1.4  thorpej 						pdk->pdk_key) == FALSE ||
    214   1.1  thorpej 	    _prop_object_externalize_end_tag(ctx, "string") == FALSE)
    215   1.1  thorpej 		return (FALSE);
    216   1.1  thorpej 
    217   1.1  thorpej 	return (TRUE);
    218   1.1  thorpej }
    219   1.1  thorpej 
    220   1.3  thorpej static boolean_t
    221   1.5  thorpej _prop_dict_keysym_equals(void *v1, void *v2)
    222   1.3  thorpej {
    223   1.4  thorpej 	prop_dictionary_keysym_t pdk1 = v1;
    224   1.4  thorpej 	prop_dictionary_keysym_t pdk2 = v2;
    225   1.3  thorpej 
    226   1.9  thorpej 	if (! (prop_object_is_dictionary_keysym(pdk1) &&
    227   1.9  thorpej 	       prop_object_is_dictionary_keysym(pdk2)))
    228   1.9  thorpej 		return (FALSE);
    229   1.8  thorpej 
    230   1.8  thorpej 	/*
    231   1.8  thorpej 	 * There is only ever one copy of a keysym at any given time,
    232   1.8  thorpej 	 * so we can reduce this to a simple pointer equality check.
    233   1.8  thorpej 	 */
    234   1.8  thorpej 	return (pdk1 == pdk2);
    235   1.3  thorpej }
    236   1.3  thorpej 
    237   1.1  thorpej static prop_dictionary_keysym_t
    238   1.6  thorpej _prop_dict_keysym_alloc(const char *key)
    239   1.1  thorpej {
    240   1.6  thorpej 	prop_dictionary_keysym_t opdk, pdk;
    241   1.1  thorpej 	size_t size;
    242   1.6  thorpej 
    243   1.6  thorpej 	/*
    244   1.8  thorpej 	 * Because of the way our RB trees work, we need to create the
    245   1.8  thorpej 	 * new keysym in order to check if it's already in the tree.
    246   1.8  thorpej 	 * Oh well.
    247   1.6  thorpej 	 */
    248   1.1  thorpej 
    249   1.4  thorpej 	size = sizeof(*pdk) + strlen(key) /* pdk_key[1] covers the NUL */;
    250   1.1  thorpej 
    251   1.4  thorpej 	if (size <= PDK_SIZE_16)
    252   1.4  thorpej 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym16_pool);
    253   1.4  thorpej 	else if (size <= PDK_SIZE_32)
    254   1.4  thorpej 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym32_pool);
    255   1.4  thorpej 	else if (size <= PDK_SIZE_128)
    256   1.4  thorpej 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym128_pool);
    257   1.1  thorpej 	else
    258   1.8  thorpej 		pdk = NULL;	/* key too long */
    259   1.8  thorpej 
    260   1.8  thorpej 	if (pdk == NULL)
    261   1.8  thorpej 		return (NULL);
    262   1.1  thorpej 
    263   1.8  thorpej 	_prop_object_init(&pdk->pdk_obj, &_prop_object_type_dict_keysym);
    264   1.1  thorpej 
    265   1.8  thorpej 	strcpy(pdk->pdk_key, key);
    266   1.8  thorpej 	pdk->pdk_size = size;
    267   1.1  thorpej 
    268   1.6  thorpej 	/*
    269   1.8  thorpej 	 * Now check to see if this already exists in the tree.  If it
    270   1.8  thorpej 	 * does, we return a reference to the existing one and free the
    271   1.8  thorpej 	 * new one we just created.
    272   1.6  thorpej 	 */
    273   1.8  thorpej 	_PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
    274   1.8  thorpej 	opdk = RB_INSERT(_prop_dict_keysym_tree, &_prop_dict_keysym_tree, pdk);
    275   1.6  thorpej 	if (opdk != NULL) {
    276   1.6  thorpej 		prop_object_retain(opdk);
    277   1.8  thorpej 		_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    278   1.6  thorpej 		_prop_dict_keysym_put(pdk);
    279   1.6  thorpej 		return (opdk);
    280   1.6  thorpej 	}
    281   1.8  thorpej 	_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    282   1.4  thorpej 	return (pdk);
    283   1.1  thorpej }
    284   1.1  thorpej 
    285   1.1  thorpej static void
    286   1.1  thorpej _prop_dictionary_free(void *v)
    287   1.1  thorpej {
    288   1.1  thorpej 	prop_dictionary_t pd = v;
    289   1.4  thorpej 	prop_dictionary_keysym_t pdk;
    290   1.6  thorpej 	prop_object_t po;
    291   1.1  thorpej 	unsigned int idx;
    292   1.1  thorpej 
    293   1.1  thorpej 	_PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
    294   1.1  thorpej 	_PROP_ASSERT((pd->pd_capacity == 0 && pd->pd_array == NULL) ||
    295   1.1  thorpej 		     (pd->pd_capacity != 0 && pd->pd_array != NULL));
    296   1.1  thorpej 
    297   1.1  thorpej 	for (idx = 0; idx < pd->pd_count; idx++) {
    298   1.6  thorpej 		pdk = pd->pd_array[idx].pde_key;
    299   1.4  thorpej 		_PROP_ASSERT(pdk != NULL);
    300   1.4  thorpej 		prop_object_release(pdk);
    301   1.6  thorpej 		po = pd->pd_array[idx].pde_objref;
    302   1.6  thorpej 		_PROP_ASSERT(po != NULL);
    303   1.6  thorpej 		prop_object_release(po);
    304   1.1  thorpej 	}
    305   1.1  thorpej 
    306   1.1  thorpej 	if (pd->pd_array != NULL)
    307   1.1  thorpej 		_PROP_FREE(pd->pd_array, M_PROP_DICT);
    308   1.1  thorpej 
    309   1.1  thorpej 	_PROP_POOL_PUT(_prop_dictionary_pool, pd);
    310   1.1  thorpej }
    311   1.1  thorpej 
    312   1.1  thorpej static boolean_t
    313   1.1  thorpej _prop_dictionary_externalize(struct _prop_object_externalize_context *ctx,
    314   1.1  thorpej 			     void *v)
    315   1.1  thorpej {
    316   1.1  thorpej 	prop_dictionary_t pd = v;
    317   1.4  thorpej 	prop_dictionary_keysym_t pdk;
    318   1.1  thorpej 	struct _prop_object *po;
    319   1.1  thorpej 	prop_object_iterator_t pi;
    320   1.1  thorpej 	unsigned int i;
    321   1.1  thorpej 
    322   1.1  thorpej 	if (pd->pd_count == 0)
    323   1.1  thorpej 		return (_prop_object_externalize_empty_tag(ctx, "dict"));
    324   1.1  thorpej 
    325   1.1  thorpej 	if (_prop_object_externalize_start_tag(ctx, "dict") == FALSE ||
    326   1.1  thorpej 	    _prop_object_externalize_append_char(ctx, '\n') == FALSE)
    327   1.1  thorpej 		return (FALSE);
    328   1.1  thorpej 
    329   1.1  thorpej 	pi = prop_dictionary_iterator(pd);
    330   1.1  thorpej 	if (pi == NULL)
    331   1.1  thorpej 		return (FALSE);
    332   1.1  thorpej 
    333   1.1  thorpej 	ctx->poec_depth++;
    334   1.1  thorpej 	_PROP_ASSERT(ctx->poec_depth != 0);
    335   1.1  thorpej 
    336   1.4  thorpej 	while ((pdk = prop_object_iterator_next(pi)) != NULL) {
    337   1.4  thorpej 		po = prop_dictionary_get_keysym(pd, pdk);
    338   1.1  thorpej 		if (po == NULL ||
    339   1.1  thorpej 		    _prop_object_externalize_start_tag(ctx, "key") == FALSE ||
    340   1.1  thorpej 		    _prop_object_externalize_append_encoded_cstring(ctx,
    341   1.4  thorpej 						   pdk->pdk_key) == FALSE ||
    342   1.1  thorpej 		    _prop_object_externalize_end_tag(ctx, "key") == FALSE ||
    343   1.3  thorpej 		    (*po->po_type->pot_extern)(ctx, po) == FALSE) {
    344   1.1  thorpej 			prop_object_iterator_release(pi);
    345   1.1  thorpej 			return (FALSE);
    346   1.1  thorpej 		}
    347   1.1  thorpej 	}
    348   1.1  thorpej 
    349   1.1  thorpej 	prop_object_iterator_release(pi);
    350   1.1  thorpej 
    351   1.1  thorpej 	ctx->poec_depth--;
    352   1.1  thorpej 	for (i = 0; i < ctx->poec_depth; i++) {
    353   1.1  thorpej 		if (_prop_object_externalize_append_char(ctx, '\t') == FALSE)
    354   1.1  thorpej 			return (FALSE);
    355   1.1  thorpej 	}
    356   1.1  thorpej 	if (_prop_object_externalize_end_tag(ctx, "dict") == FALSE)
    357   1.1  thorpej 		return (FALSE);
    358   1.1  thorpej 
    359   1.1  thorpej 	return (TRUE);
    360   1.1  thorpej }
    361   1.1  thorpej 
    362   1.3  thorpej static boolean_t
    363   1.3  thorpej _prop_dictionary_equals(void *v1, void *v2)
    364   1.3  thorpej {
    365   1.3  thorpej 	prop_dictionary_t dict1 = v1;
    366   1.3  thorpej 	prop_dictionary_t dict2 = v2;
    367   1.6  thorpej 	const struct _prop_dict_entry *pde1, *pde2;
    368   1.3  thorpej 	unsigned int idx;
    369   1.3  thorpej 
    370   1.9  thorpej 	if (! (prop_object_is_dictionary(dict1) &&
    371   1.9  thorpej 	       prop_object_is_dictionary(dict2)))
    372   1.9  thorpej 		return (FALSE);
    373   1.9  thorpej 
    374   1.3  thorpej 	if (dict1 == dict2)
    375   1.3  thorpej 		return (TRUE);
    376   1.3  thorpej 	if (dict1->pd_count != dict2->pd_count)
    377   1.3  thorpej 		return (FALSE);
    378   1.3  thorpej 
    379   1.3  thorpej 	for (idx = 0; idx < dict1->pd_count; idx++) {
    380   1.6  thorpej 		pde1 = &dict1->pd_array[idx];
    381   1.6  thorpej 		pde2 = &dict2->pd_array[idx];
    382   1.3  thorpej 
    383   1.6  thorpej 		if (prop_dictionary_keysym_equals(pde1->pde_key,
    384   1.6  thorpej 						  pde2->pde_key) == FALSE)
    385   1.3  thorpej 			return (FALSE);
    386   1.6  thorpej 		if (prop_object_equals(pde1->pde_objref,
    387   1.6  thorpej 				       pde2->pde_objref) == FALSE)
    388   1.3  thorpej 			return (FALSE);
    389   1.3  thorpej 	}
    390   1.3  thorpej 
    391   1.3  thorpej 	return (TRUE);
    392   1.3  thorpej }
    393   1.3  thorpej 
    394   1.1  thorpej static prop_dictionary_t
    395   1.1  thorpej _prop_dictionary_alloc(unsigned int capacity)
    396   1.1  thorpej {
    397   1.1  thorpej 	prop_dictionary_t pd;
    398   1.6  thorpej 	struct _prop_dict_entry *array;
    399   1.1  thorpej 
    400   1.1  thorpej 	if (capacity != 0) {
    401   1.6  thorpej 		array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
    402   1.1  thorpej 		if (array == NULL)
    403   1.1  thorpej 			return (NULL);
    404   1.1  thorpej 	} else
    405   1.1  thorpej 		array = NULL;
    406   1.1  thorpej 
    407   1.1  thorpej 	pd = _PROP_POOL_GET(_prop_dictionary_pool);
    408   1.1  thorpej 	if (pd != NULL) {
    409   1.3  thorpej 		_prop_object_init(&pd->pd_obj, &_prop_object_type_dictionary);
    410   1.1  thorpej 
    411   1.1  thorpej 		pd->pd_array = array;
    412   1.1  thorpej 		pd->pd_capacity = capacity;
    413   1.1  thorpej 		pd->pd_count = 0;
    414   1.2   simonb 		pd->pd_flags = 0;
    415   1.1  thorpej 
    416   1.1  thorpej 		pd->pd_version = 0;
    417   1.1  thorpej 	} else if (array != NULL)
    418   1.1  thorpej 		_PROP_FREE(array, M_PROP_DICT);
    419   1.1  thorpej 
    420   1.1  thorpej 	return (pd);
    421   1.1  thorpej }
    422   1.1  thorpej 
    423   1.1  thorpej static boolean_t
    424   1.1  thorpej _prop_dictionary_expand(prop_dictionary_t pd, unsigned int capacity)
    425   1.1  thorpej {
    426   1.6  thorpej 	struct _prop_dict_entry *array, *oarray;
    427   1.1  thorpej 
    428   1.1  thorpej 	oarray = pd->pd_array;
    429   1.1  thorpej 
    430   1.6  thorpej 	array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
    431   1.1  thorpej 	if (array == NULL)
    432   1.1  thorpej 		return (FALSE);
    433   1.1  thorpej 	if (oarray != NULL)
    434   1.6  thorpej 		memcpy(array, oarray, pd->pd_capacity * sizeof(*array));
    435   1.1  thorpej 	pd->pd_array = array;
    436   1.1  thorpej 	pd->pd_capacity = capacity;
    437   1.1  thorpej 
    438   1.1  thorpej 	if (oarray != NULL)
    439   1.1  thorpej 		_PROP_FREE(oarray, M_PROP_DICT);
    440   1.1  thorpej 
    441   1.1  thorpej 	return (TRUE);
    442   1.1  thorpej }
    443   1.1  thorpej 
    444   1.1  thorpej static prop_object_t
    445   1.1  thorpej _prop_dictionary_iterator_next_object(void *v)
    446   1.1  thorpej {
    447   1.1  thorpej 	struct _prop_dictionary_iterator *pdi = v;
    448   1.1  thorpej 	prop_dictionary_t pd = pdi->pdi_base.pi_obj;
    449   1.4  thorpej 	prop_dictionary_keysym_t pdk;
    450   1.1  thorpej 
    451   1.1  thorpej 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    452   1.1  thorpej 
    453   1.1  thorpej 	if (pd->pd_version != pdi->pdi_base.pi_version)
    454   1.1  thorpej 		return (NULL);	/* dictionary changed during iteration */
    455   1.1  thorpej 
    456   1.1  thorpej 	_PROP_ASSERT(pdi->pdi_index <= pd->pd_count);
    457   1.1  thorpej 
    458   1.1  thorpej 	if (pdi->pdi_index == pd->pd_count)
    459   1.1  thorpej 		return (NULL);	/* we've iterated all objects */
    460   1.1  thorpej 
    461   1.6  thorpej 	pdk = pd->pd_array[pdi->pdi_index].pde_key;
    462   1.1  thorpej 	pdi->pdi_index++;
    463   1.1  thorpej 
    464   1.4  thorpej 	return (pdk);
    465   1.1  thorpej }
    466   1.1  thorpej 
    467   1.1  thorpej static void
    468   1.1  thorpej _prop_dictionary_iterator_reset(void *v)
    469   1.1  thorpej {
    470   1.1  thorpej 	struct _prop_dictionary_iterator *pdi = v;
    471   1.1  thorpej 	prop_dictionary_t pd = pdi->pdi_base.pi_obj;
    472   1.1  thorpej 
    473   1.1  thorpej 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    474   1.1  thorpej 
    475   1.1  thorpej 	pdi->pdi_index = 0;
    476   1.1  thorpej 	pdi->pdi_base.pi_version = pd->pd_version;
    477   1.1  thorpej }
    478   1.1  thorpej 
    479   1.1  thorpej /*
    480   1.1  thorpej  * prop_dictionary_create --
    481   1.1  thorpej  *	Create a dictionary.
    482   1.1  thorpej  */
    483   1.1  thorpej prop_dictionary_t
    484   1.1  thorpej prop_dictionary_create(void)
    485   1.1  thorpej {
    486   1.1  thorpej 
    487   1.1  thorpej 	return (_prop_dictionary_alloc(0));
    488   1.1  thorpej }
    489   1.1  thorpej 
    490   1.1  thorpej /*
    491   1.1  thorpej  * prop_dictionary_create_with_capacity --
    492   1.1  thorpej  *	Create a dictionary with the capacity to store N objects.
    493   1.1  thorpej  */
    494   1.1  thorpej prop_dictionary_t
    495   1.1  thorpej prop_dictionary_create_with_capacity(unsigned int capacity)
    496   1.1  thorpej {
    497   1.1  thorpej 
    498   1.1  thorpej 	return (_prop_dictionary_alloc(capacity));
    499   1.1  thorpej }
    500   1.1  thorpej 
    501   1.1  thorpej /*
    502   1.1  thorpej  * prop_dictionary_copy --
    503   1.6  thorpej  *	Copy a dictionary.  The new dictionary has an initial capacity equal
    504   1.6  thorpej  *	to the number of objects stored int the original dictionary.  The new
    505   1.6  thorpej  *	dictionary contains refrences to the original dictionary's objects,
    506   1.6  thorpej  *	not copies of those objects (i.e. a shallow copy).
    507   1.1  thorpej  */
    508   1.1  thorpej prop_dictionary_t
    509   1.1  thorpej prop_dictionary_copy(prop_dictionary_t opd)
    510   1.1  thorpej {
    511   1.1  thorpej 	prop_dictionary_t pd;
    512   1.4  thorpej 	prop_dictionary_keysym_t pdk;
    513   1.6  thorpej 	prop_object_t po;
    514   1.1  thorpej 	unsigned int idx;
    515   1.1  thorpej 
    516   1.9  thorpej 	if (! prop_object_is_dictionary(opd))
    517   1.9  thorpej 		return (NULL);
    518   1.1  thorpej 
    519   1.1  thorpej 	pd = _prop_dictionary_alloc(opd->pd_count);
    520   1.1  thorpej 	if (pd != NULL) {
    521   1.1  thorpej 		for (idx = 0; idx < opd->pd_count; idx++) {
    522   1.6  thorpej 			pdk = opd->pd_array[idx].pde_key;
    523   1.6  thorpej 			po = opd->pd_array[idx].pde_objref;
    524   1.6  thorpej 
    525   1.4  thorpej 			prop_object_retain(pdk);
    526   1.6  thorpej 			prop_object_retain(po);
    527   1.6  thorpej 
    528   1.6  thorpej 			pd->pd_array[idx].pde_key = pdk;
    529   1.6  thorpej 			pd->pd_array[idx].pde_objref = po;
    530   1.1  thorpej 		}
    531   1.1  thorpej 		pd->pd_count = opd->pd_count;
    532   1.1  thorpej 		pd->pd_flags = opd->pd_flags;
    533   1.1  thorpej 	}
    534   1.1  thorpej 	return (pd);
    535   1.1  thorpej }
    536   1.1  thorpej 
    537   1.1  thorpej /*
    538   1.1  thorpej  * prop_dictionary_copy_mutable --
    539   1.1  thorpej  *	Like prop_dictionary_copy(), but the resulting dictionary is
    540   1.1  thorpej  *	mutable.
    541   1.1  thorpej  */
    542   1.1  thorpej prop_dictionary_t
    543   1.1  thorpej prop_dictionary_copy_mutable(prop_dictionary_t opd)
    544   1.1  thorpej {
    545   1.1  thorpej 	prop_dictionary_t pd;
    546   1.1  thorpej 
    547   1.9  thorpej 	if (! prop_object_is_dictionary(opd))
    548   1.9  thorpej 		return (NULL);
    549   1.9  thorpej 
    550   1.6  thorpej 	pd = prop_dictionary_copy(opd);
    551   1.6  thorpej 	if (pd != NULL)
    552   1.6  thorpej 		pd->pd_flags &= ~PD_F_IMMUTABLE;
    553   1.1  thorpej 
    554   1.1  thorpej 	return (pd);
    555   1.1  thorpej }
    556   1.1  thorpej 
    557   1.1  thorpej /*
    558   1.1  thorpej  * prop_dictionary_count --
    559   1.1  thorpej  *	Return the number of objects stored in the dictionary.
    560   1.1  thorpej  */
    561   1.1  thorpej unsigned int
    562   1.1  thorpej prop_dictionary_count(prop_dictionary_t pd)
    563   1.1  thorpej {
    564   1.1  thorpej 
    565   1.9  thorpej 	if (! prop_object_is_dictionary(pd))
    566   1.9  thorpej 		return (0);
    567   1.9  thorpej 
    568   1.1  thorpej 	return (pd->pd_count);
    569   1.1  thorpej }
    570   1.1  thorpej 
    571   1.1  thorpej /*
    572   1.6  thorpej  * prop_dictionary_ensure_capacity --
    573   1.6  thorpej  *	Ensure that the dictionary has the capacity to store the specified
    574   1.6  thorpej  *	total number of objects (including the objects already stored in
    575   1.6  thorpej  *	the dictionary).
    576   1.6  thorpej  */
    577   1.6  thorpej boolean_t
    578   1.6  thorpej prop_dictionary_ensure_capacity(prop_dictionary_t pd, unsigned int capacity)
    579   1.6  thorpej {
    580   1.6  thorpej 
    581   1.9  thorpej 	if (! prop_object_is_dictionary(pd))
    582   1.9  thorpej 		return (FALSE);
    583   1.9  thorpej 
    584   1.6  thorpej 	if (capacity > pd->pd_capacity)
    585   1.6  thorpej 		return (_prop_dictionary_expand(pd, capacity));
    586   1.6  thorpej 	return (TRUE);
    587   1.6  thorpej }
    588   1.6  thorpej 
    589   1.6  thorpej /*
    590   1.1  thorpej  * prop_dictionary_iterator --
    591   1.1  thorpej  *	Return an iterator for the dictionary.  The dictionary is retained by
    592   1.1  thorpej  *	the iterator.
    593   1.1  thorpej  */
    594   1.1  thorpej prop_object_iterator_t
    595   1.1  thorpej prop_dictionary_iterator(prop_dictionary_t pd)
    596   1.1  thorpej {
    597   1.1  thorpej 	struct _prop_dictionary_iterator *pdi;
    598   1.1  thorpej 
    599   1.9  thorpej 	if (! prop_object_is_dictionary(pd))
    600   1.9  thorpej 		return (NULL);
    601   1.1  thorpej 
    602   1.1  thorpej 	pdi = _PROP_CALLOC(sizeof(*pdi), M_TEMP);
    603   1.1  thorpej 	if (pdi == NULL)
    604   1.1  thorpej 		return (NULL);
    605   1.1  thorpej 	pdi->pdi_base.pi_next_object = _prop_dictionary_iterator_next_object;
    606   1.1  thorpej 	pdi->pdi_base.pi_reset = _prop_dictionary_iterator_reset;
    607   1.1  thorpej 	prop_object_retain(pd);
    608   1.1  thorpej 	pdi->pdi_base.pi_obj = pd;
    609   1.1  thorpej 	pdi->pdi_base.pi_version = pd->pd_version;
    610   1.1  thorpej 
    611   1.1  thorpej 	_prop_dictionary_iterator_reset(pdi);
    612   1.1  thorpej 
    613   1.1  thorpej 	return (&pdi->pdi_base);
    614   1.1  thorpej }
    615   1.1  thorpej 
    616   1.6  thorpej static struct _prop_dict_entry *
    617   1.1  thorpej _prop_dict_lookup(prop_dictionary_t pd, const char *key,
    618   1.1  thorpej 		  unsigned int *idxp)
    619   1.1  thorpej {
    620   1.6  thorpej 	struct _prop_dict_entry *pde;
    621   1.1  thorpej 	unsigned int base, idx, distance;
    622   1.1  thorpej 	int res;
    623   1.1  thorpej 
    624   1.1  thorpej 	for (idx = 0, base = 0, distance = pd->pd_count; distance != 0;
    625   1.1  thorpej 	     distance >>= 1) {
    626   1.1  thorpej 		idx = base + (distance >> 1);
    627   1.6  thorpej 		pde = &pd->pd_array[idx];
    628   1.6  thorpej 		_PROP_ASSERT(pde->pde_key != NULL);
    629   1.6  thorpej 		res = strcmp(key, pde->pde_key->pdk_key);
    630   1.1  thorpej 		if (res == 0) {
    631   1.1  thorpej 			if (idxp != NULL)
    632   1.1  thorpej 				*idxp = idx;
    633   1.6  thorpej 			return (pde);
    634   1.1  thorpej 		}
    635   1.6  thorpej 		if (res > 0) {	/* key > pdk_key: move right */
    636   1.1  thorpej 			base = idx + 1;
    637   1.1  thorpej 			distance--;
    638   1.1  thorpej 		}		/* else move left */
    639   1.1  thorpej 	}
    640   1.1  thorpej 
    641   1.1  thorpej 	/* idx points to the slot we looked at last. */
    642   1.1  thorpej 	if (idxp != NULL)
    643   1.1  thorpej 		*idxp = idx;
    644   1.1  thorpej 	return (NULL);
    645   1.1  thorpej }
    646   1.1  thorpej 
    647   1.1  thorpej /*
    648   1.1  thorpej  * prop_dictionary_get --
    649   1.1  thorpej  *	Return the object stored with specified key.
    650   1.1  thorpej  */
    651   1.1  thorpej prop_object_t
    652   1.1  thorpej prop_dictionary_get(prop_dictionary_t pd, const char *key)
    653   1.1  thorpej {
    654   1.6  thorpej 	const struct _prop_dict_entry *pde;
    655   1.1  thorpej 
    656   1.9  thorpej 	if (! prop_object_is_dictionary(pd))
    657   1.9  thorpej 		return (NULL);
    658   1.1  thorpej 
    659   1.6  thorpej 	pde = _prop_dict_lookup(pd, key, NULL);
    660   1.6  thorpej 	if (pde != NULL) {
    661   1.6  thorpej 		_PROP_ASSERT(pde->pde_objref != NULL);
    662   1.6  thorpej 		return (pde->pde_objref);
    663   1.1  thorpej 	}
    664   1.1  thorpej 	return (NULL);
    665   1.1  thorpej }
    666   1.1  thorpej 
    667   1.1  thorpej /*
    668   1.1  thorpej  * prop_dictionary_get_keysym --
    669   1.1  thorpej  *	Return the object stored at the location encoded by the keysym.
    670   1.1  thorpej  */
    671   1.1  thorpej prop_object_t
    672   1.4  thorpej prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk)
    673   1.1  thorpej {
    674   1.1  thorpej 
    675   1.9  thorpej 	if (! (prop_object_is_dictionary(pd) &&
    676   1.9  thorpej 	       prop_object_is_dictionary_keysym(pdk)))
    677   1.9  thorpej 		return (NULL);
    678   1.1  thorpej 
    679   1.6  thorpej 	return (prop_dictionary_get(pd, pdk->pdk_key));
    680   1.1  thorpej }
    681   1.1  thorpej 
    682   1.1  thorpej /*
    683   1.1  thorpej  * prop_dictionary_set --
    684   1.1  thorpej  *	Store a reference to an object at with the specified key.
    685   1.1  thorpej  *	If the key already exisit, the original object is released.
    686   1.1  thorpej  */
    687   1.1  thorpej boolean_t
    688   1.1  thorpej prop_dictionary_set(prop_dictionary_t pd, const char *key, prop_object_t po)
    689   1.1  thorpej {
    690   1.6  thorpej 	struct _prop_dict_entry *pde;
    691   1.6  thorpej 	prop_dictionary_keysym_t pdk;
    692   1.1  thorpej 	unsigned int idx;
    693   1.1  thorpej 
    694   1.9  thorpej 	if (! prop_object_is_dictionary(pd))
    695   1.9  thorpej 		return (FALSE);
    696   1.9  thorpej 
    697   1.1  thorpej 	_PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
    698   1.1  thorpej 
    699   1.1  thorpej 	if (prop_dictionary_is_immutable(pd))
    700   1.1  thorpej 		return (FALSE);
    701   1.1  thorpej 
    702   1.6  thorpej 	pde = _prop_dict_lookup(pd, key, &idx);
    703   1.6  thorpej 	if (pde != NULL) {
    704   1.6  thorpej 		prop_object_t opo = pde->pde_objref;
    705   1.1  thorpej 		prop_object_retain(po);
    706   1.6  thorpej 		pde->pde_objref = po;
    707   1.1  thorpej 		prop_object_release(opo);
    708   1.1  thorpej 		return (TRUE);
    709   1.1  thorpej 	}
    710   1.1  thorpej 
    711   1.6  thorpej 	pdk = _prop_dict_keysym_alloc(key);
    712   1.4  thorpej 	if (pdk == NULL)
    713   1.1  thorpej 		return (FALSE);
    714   1.1  thorpej 
    715   1.1  thorpej 	if (pd->pd_count == pd->pd_capacity &&
    716   1.6  thorpej 	    _prop_dictionary_expand(pd,
    717   1.6  thorpej 	    			    pd->pd_capacity + EXPAND_STEP) == FALSE) {
    718   1.6  thorpej 		prop_object_release(pdk);
    719   1.1  thorpej 	    	return (FALSE);
    720   1.1  thorpej 	}
    721   1.1  thorpej 
    722   1.6  thorpej 	/* At this point, the store will succeed. */
    723   1.6  thorpej 	prop_object_retain(po);
    724   1.6  thorpej 
    725   1.1  thorpej 	if (pd->pd_count == 0) {
    726   1.6  thorpej 		pd->pd_array[0].pde_key = pdk;
    727   1.6  thorpej 		pd->pd_array[0].pde_objref = po;
    728   1.1  thorpej 		pd->pd_count++;
    729   1.1  thorpej 		pd->pd_version++;
    730   1.1  thorpej 		return (TRUE);
    731   1.1  thorpej 	}
    732   1.1  thorpej 
    733   1.6  thorpej 	pde = &pd->pd_array[idx];
    734   1.6  thorpej 	_PROP_ASSERT(pde->pde_key != NULL);
    735   1.1  thorpej 
    736   1.6  thorpej 	if (strcmp(key, pde->pde_key->pdk_key) < 0) {
    737   1.1  thorpej 		/*
    738   1.6  thorpej 		 * key < pdk_key: insert to the left.  This is the same as
    739   1.6  thorpej 		 * inserting to the right, except we decrement the current
    740   1.6  thorpej 		 * index first.
    741   1.1  thorpej 		 *
    742   1.1  thorpej 		 * Because we're unsigned, we have to special case 0
    743   1.1  thorpej 		 * (grumble).
    744   1.1  thorpej 		 */
    745   1.1  thorpej 		if (idx == 0) {
    746   1.1  thorpej 			memmove(&pd->pd_array[1], &pd->pd_array[0],
    747   1.6  thorpej 				pd->pd_count * sizeof(*pde));
    748   1.6  thorpej 			pd->pd_array[0].pde_key = pdk;
    749   1.6  thorpej 			pd->pd_array[0].pde_objref = po;
    750   1.1  thorpej 			pd->pd_count++;
    751   1.1  thorpej 			pd->pd_version++;
    752   1.1  thorpej 			return (TRUE);
    753   1.1  thorpej 		}
    754   1.1  thorpej 		idx--;
    755   1.1  thorpej 	}
    756   1.1  thorpej 
    757   1.1  thorpej 	memmove(&pd->pd_array[idx + 2], &pd->pd_array[idx + 1],
    758   1.6  thorpej 		(pd->pd_count - (idx + 1)) * sizeof(*pde));
    759   1.6  thorpej 	pd->pd_array[idx + 1].pde_key = pdk;
    760   1.6  thorpej 	pd->pd_array[idx + 1].pde_objref = po;
    761   1.1  thorpej 	pd->pd_count++;
    762   1.1  thorpej 
    763   1.1  thorpej 	pd->pd_version++;
    764   1.1  thorpej 
    765   1.1  thorpej 	return (TRUE);
    766   1.1  thorpej }
    767   1.1  thorpej 
    768   1.1  thorpej /*
    769   1.1  thorpej  * prop_dictionary_set_keysym --
    770   1.1  thorpej  *	Replace the object in the dictionary at the location encoded by
    771   1.1  thorpej  *	the keysym.
    772   1.1  thorpej  */
    773   1.1  thorpej boolean_t
    774   1.4  thorpej prop_dictionary_set_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
    775   1.1  thorpej 			   prop_object_t po)
    776   1.1  thorpej {
    777   1.1  thorpej 
    778   1.9  thorpej 	if (! (prop_object_is_dictionary(pd) &&
    779   1.9  thorpej 	       prop_object_is_dictionary_keysym(pdk)))
    780   1.9  thorpej 		return (FALSE);
    781   1.1  thorpej 
    782   1.1  thorpej 	if (prop_dictionary_is_immutable(pd))
    783   1.1  thorpej 		return (FALSE);
    784   1.1  thorpej 
    785   1.6  thorpej 	return (prop_dictionary_set(pd, pdk->pdk_key, po));
    786   1.1  thorpej }
    787   1.1  thorpej 
    788   1.1  thorpej static void
    789   1.6  thorpej _prop_dictionary_remove(prop_dictionary_t pd, struct _prop_dict_entry *pde,
    790   1.1  thorpej     unsigned int idx)
    791   1.1  thorpej {
    792   1.6  thorpej 	prop_dictionary_keysym_t pdk = pde->pde_key;
    793   1.6  thorpej 	prop_object_t po = pde->pde_objref;
    794   1.1  thorpej 
    795   1.1  thorpej 	_PROP_ASSERT(pd->pd_count != 0);
    796   1.1  thorpej 	_PROP_ASSERT(idx < pd->pd_count);
    797   1.6  thorpej 	_PROP_ASSERT(pde == &pd->pd_array[idx]);
    798   1.1  thorpej 
    799   1.1  thorpej 	idx++;
    800   1.1  thorpej 	memmove(&pd->pd_array[idx - 1], &pd->pd_array[idx],
    801   1.6  thorpej 		(pd->pd_count - idx) * sizeof(*pde));
    802   1.1  thorpej 	pd->pd_count--;
    803   1.1  thorpej 	pd->pd_version++;
    804   1.1  thorpej 
    805   1.4  thorpej 	prop_object_release(pdk);
    806   1.6  thorpej 	prop_object_release(po);
    807   1.1  thorpej }
    808   1.1  thorpej 
    809   1.1  thorpej /*
    810   1.1  thorpej  * prop_dictionary_remove --
    811   1.1  thorpej  *	Remove the reference to an object with the specified key from
    812   1.1  thorpej  *	the dictionary.
    813   1.1  thorpej  */
    814   1.1  thorpej void
    815   1.1  thorpej prop_dictionary_remove(prop_dictionary_t pd, const char *key)
    816   1.1  thorpej {
    817   1.6  thorpej 	struct _prop_dict_entry *pde;
    818   1.1  thorpej 	unsigned int idx;
    819   1.1  thorpej 
    820   1.9  thorpej 	if (! prop_object_is_dictionary(pd))
    821   1.9  thorpej 		return;
    822   1.1  thorpej 
    823   1.1  thorpej 	/* XXX Should this be a _PROP_ASSERT()? */
    824   1.1  thorpej 	if (prop_dictionary_is_immutable(pd))
    825   1.1  thorpej 		return;
    826   1.1  thorpej 
    827   1.6  thorpej 	pde = _prop_dict_lookup(pd, key, &idx);
    828   1.1  thorpej 	/* XXX Should this be a _PROP_ASSERT()? */
    829   1.6  thorpej 	if (pde == NULL)
    830   1.1  thorpej 		return;
    831   1.1  thorpej 
    832   1.6  thorpej 	_prop_dictionary_remove(pd, pde, idx);
    833   1.1  thorpej }
    834   1.1  thorpej 
    835   1.1  thorpej /*
    836   1.1  thorpej  * prop_dictionary_remove_keysym --
    837   1.1  thorpej  *	Remove a reference to an object stored in the dictionary at the
    838   1.1  thorpej  *	location encoded by the keysym.
    839   1.1  thorpej  */
    840   1.1  thorpej void
    841   1.1  thorpej prop_dictionary_remove_keysym(prop_dictionary_t pd,
    842   1.4  thorpej 			      prop_dictionary_keysym_t pdk)
    843   1.1  thorpej {
    844   1.1  thorpej 
    845   1.9  thorpej 	if (! (prop_object_is_dictionary(pd) &&
    846   1.9  thorpej 	       prop_object_is_dictionary_keysym(pdk)))
    847   1.9  thorpej 		return;
    848   1.1  thorpej 
    849   1.1  thorpej 	/* XXX Should this be a _PROP_ASSERT()? */
    850   1.1  thorpej 	if (prop_dictionary_is_immutable(pd))
    851   1.1  thorpej 		return;
    852   1.1  thorpej 
    853   1.6  thorpej 	prop_dictionary_remove(pd, pdk->pdk_key);
    854   1.1  thorpej }
    855   1.1  thorpej 
    856   1.1  thorpej /*
    857   1.3  thorpej  * prop_dictionary_equals --
    858   1.3  thorpej  *	Return TRUE if the two dictionaries are equivalent.  Note we do a
    859   1.3  thorpej  *	by-value comparison of the objects in the dictionary.
    860   1.3  thorpej  */
    861   1.3  thorpej boolean_t
    862   1.3  thorpej prop_dictionary_equals(prop_dictionary_t dict1, prop_dictionary_t dict2)
    863   1.3  thorpej {
    864   1.3  thorpej 
    865   1.3  thorpej 	return (_prop_dictionary_equals(dict1, dict2));
    866   1.3  thorpej }
    867   1.3  thorpej 
    868   1.3  thorpej /*
    869   1.1  thorpej  * prop_dictionary_keysym_cstring_nocopy --
    870   1.1  thorpej  *	Return an immutable reference to the keysym's value.
    871   1.1  thorpej  */
    872   1.1  thorpej const char *
    873   1.4  thorpej prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pdk)
    874   1.1  thorpej {
    875   1.1  thorpej 
    876   1.9  thorpej 	if (! prop_object_is_dictionary_keysym(pdk))
    877   1.9  thorpej 		return (NULL);
    878   1.9  thorpej 
    879   1.4  thorpej 	return (pdk->pdk_key);
    880   1.1  thorpej }
    881   1.1  thorpej 
    882   1.1  thorpej /*
    883   1.3  thorpej  * prop_dictionary_keysym_equals --
    884   1.3  thorpej  *	Return TRUE if the two dictionary key symbols are equivalent.
    885   1.3  thorpej  *	Note: We do not compare the object references.
    886   1.3  thorpej  */
    887   1.3  thorpej boolean_t
    888   1.4  thorpej prop_dictionary_keysym_equals(prop_dictionary_keysym_t pdk1,
    889   1.4  thorpej 			      prop_dictionary_keysym_t pdk2)
    890   1.3  thorpej {
    891   1.3  thorpej 
    892   1.5  thorpej 	return (_prop_dict_keysym_equals(pdk1, pdk2));
    893   1.3  thorpej }
    894   1.3  thorpej 
    895   1.3  thorpej /*
    896   1.1  thorpej  * prop_dictionary_externalize --
    897   1.1  thorpej  *	Externalize a dictionary, returning a NUL-terminated buffer
    898   1.1  thorpej  *	containing the XML-style representation.  The buffer is allocated
    899   1.1  thorpej  *	with the M_TEMP memory type.
    900   1.1  thorpej  */
    901   1.1  thorpej char *
    902   1.1  thorpej prop_dictionary_externalize(prop_dictionary_t pd)
    903   1.1  thorpej {
    904   1.1  thorpej 	struct _prop_object_externalize_context *ctx;
    905   1.1  thorpej 	char *cp;
    906   1.1  thorpej 
    907   1.1  thorpej 	ctx = _prop_object_externalize_context_alloc();
    908   1.1  thorpej 	if (ctx == NULL)
    909   1.1  thorpej 		return (NULL);
    910   1.1  thorpej 
    911   1.9  thorpej 	if (_prop_object_externalize_header(ctx) == FALSE ||
    912   1.3  thorpej 	    (*pd->pd_obj.po_type->pot_extern)(ctx, pd) == FALSE ||
    913   1.9  thorpej 	    _prop_object_externalize_footer(ctx) == FALSE) {
    914   1.1  thorpej 		/* We are responsible for releasing the buffer. */
    915   1.1  thorpej 		_PROP_FREE(ctx->poec_buf, M_TEMP);
    916   1.1  thorpej 		_prop_object_externalize_context_free(ctx);
    917   1.1  thorpej 		return (NULL);
    918   1.1  thorpej 	}
    919   1.1  thorpej 
    920   1.1  thorpej 	cp = ctx->poec_buf;
    921   1.1  thorpej 	_prop_object_externalize_context_free(ctx);
    922   1.1  thorpej 
    923   1.1  thorpej 	return (cp);
    924   1.1  thorpej }
    925   1.1  thorpej 
    926   1.1  thorpej /*
    927   1.1  thorpej  * _prop_dictionary_internalize --
    928   1.1  thorpej  *	Parse a <dict>...</dict> and return the object created from the
    929   1.1  thorpej  *	external representation.
    930   1.1  thorpej  */
    931   1.1  thorpej prop_object_t
    932   1.1  thorpej _prop_dictionary_internalize(struct _prop_object_internalize_context *ctx)
    933   1.1  thorpej {
    934   1.1  thorpej 	prop_dictionary_t dict;
    935   1.1  thorpej 	prop_object_t val;
    936   1.1  thorpej 	size_t keylen;
    937   1.1  thorpej 	char *tmpkey;
    938   1.1  thorpej 
    939   1.1  thorpej 	/* We don't currently understand any attributes. */
    940   1.1  thorpej 	if (ctx->poic_tagattr != NULL)
    941   1.1  thorpej 		return (NULL);
    942   1.1  thorpej 
    943   1.1  thorpej 	dict = prop_dictionary_create();
    944   1.1  thorpej 	if (dict == NULL)
    945   1.1  thorpej 		return (NULL);
    946   1.1  thorpej 
    947   1.1  thorpej 	if (ctx->poic_is_empty_element)
    948   1.1  thorpej 		return (dict);
    949   1.1  thorpej 
    950   1.4  thorpej 	tmpkey = _PROP_MALLOC(PDK_MAXKEY + 1, M_TEMP);
    951   1.1  thorpej 	if (tmpkey == NULL)
    952   1.1  thorpej 		goto bad;
    953   1.1  thorpej 
    954   1.1  thorpej 	for (;;) {
    955   1.1  thorpej 		/* Fetch the next tag. */
    956   1.1  thorpej 		if (_prop_object_internalize_find_tag(ctx, NULL,
    957   1.1  thorpej 					_PROP_TAG_TYPE_EITHER) == FALSE)
    958   1.1  thorpej 			goto bad;
    959   1.1  thorpej 
    960   1.1  thorpej 		/* Check to see if this is the end of the dictionary. */
    961   1.1  thorpej 		if (_PROP_TAG_MATCH(ctx, "dict") &&
    962   1.1  thorpej 		    ctx->poic_tag_type == _PROP_TAG_TYPE_END)
    963   1.1  thorpej 			break;
    964   1.1  thorpej 
    965   1.1  thorpej 		/* Ok, it must be a non-empty key start tag. */
    966   1.1  thorpej 		if (!_PROP_TAG_MATCH(ctx, "key") ||
    967   1.1  thorpej 		    ctx->poic_tag_type != _PROP_TAG_TYPE_START ||
    968   1.1  thorpej 		    ctx->poic_is_empty_element)
    969   1.1  thorpej 		    	goto bad;
    970   1.1  thorpej 
    971   1.1  thorpej 		if (_prop_object_internalize_decode_string(ctx,
    972   1.4  thorpej 						tmpkey, PDK_MAXKEY, &keylen,
    973   1.1  thorpej 						&ctx->poic_cp) == FALSE)
    974   1.1  thorpej 			goto bad;
    975   1.1  thorpej 
    976   1.4  thorpej 		_PROP_ASSERT(keylen <= PDK_MAXKEY);
    977   1.1  thorpej 		tmpkey[keylen] = '\0';
    978   1.1  thorpej 
    979   1.1  thorpej 		if (_prop_object_internalize_find_tag(ctx, "key",
    980   1.1  thorpej 					_PROP_TAG_TYPE_END) == FALSE)
    981   1.1  thorpej 			goto bad;
    982   1.1  thorpej 
    983   1.1  thorpej 		/* ..and now the beginning of the value. */
    984   1.1  thorpej 		if (_prop_object_internalize_find_tag(ctx, NULL,
    985   1.1  thorpej 					_PROP_TAG_TYPE_START) == FALSE)
    986   1.1  thorpej 			goto bad;
    987   1.1  thorpej 
    988   1.1  thorpej 		val = _prop_object_internalize_by_tag(ctx);
    989   1.1  thorpej 		if (val == NULL)
    990   1.1  thorpej 			goto bad;
    991   1.1  thorpej 
    992   1.1  thorpej 		if (prop_dictionary_set(dict, tmpkey, val) == FALSE) {
    993   1.1  thorpej 			prop_object_release(val);
    994   1.1  thorpej 			goto bad;
    995   1.1  thorpej 		}
    996   1.1  thorpej 		prop_object_release(val);
    997   1.1  thorpej 	}
    998   1.1  thorpej 
    999   1.1  thorpej 	_PROP_FREE(tmpkey, M_TEMP);
   1000   1.1  thorpej 	return (dict);
   1001   1.1  thorpej 
   1002   1.1  thorpej  bad:
   1003   1.1  thorpej 	if (tmpkey != NULL)
   1004   1.1  thorpej 		_PROP_FREE(tmpkey, M_TEMP);
   1005   1.1  thorpej 	prop_object_release(dict);
   1006   1.1  thorpej 	return (NULL);
   1007   1.1  thorpej }
   1008   1.1  thorpej 
   1009   1.1  thorpej /*
   1010   1.1  thorpej  * prop_dictionary_internalize --
   1011   1.1  thorpej  *	Create a dictionary by parsing the NUL-terminated XML-style
   1012   1.1  thorpej  *	representation.
   1013   1.1  thorpej  */
   1014   1.1  thorpej prop_dictionary_t
   1015   1.1  thorpej prop_dictionary_internalize(const char *xml)
   1016   1.1  thorpej {
   1017   1.1  thorpej 	prop_dictionary_t dict = NULL;
   1018   1.1  thorpej 	struct _prop_object_internalize_context *ctx;
   1019   1.1  thorpej 
   1020   1.1  thorpej 	ctx = _prop_object_internalize_context_alloc(xml);
   1021   1.1  thorpej 	if (ctx == NULL)
   1022   1.1  thorpej 		return (NULL);
   1023   1.1  thorpej 
   1024   1.1  thorpej 	/* We start with a <plist> tag. */
   1025   1.1  thorpej 	if (_prop_object_internalize_find_tag(ctx, "plist",
   1026   1.1  thorpej 					      _PROP_TAG_TYPE_START) == FALSE)
   1027   1.1  thorpej 		goto out;
   1028   1.1  thorpej 
   1029   1.1  thorpej 	/* Plist elements cannot be empty. */
   1030   1.1  thorpej 	if (ctx->poic_is_empty_element)
   1031   1.1  thorpej 		goto out;
   1032   1.1  thorpej 
   1033   1.1  thorpej 	/*
   1034   1.1  thorpej 	 * We don't understand any plist attributes, but Apple XML
   1035   1.9  thorpej 	 * property lists often have a "version" attribute.  If we
   1036   1.1  thorpej 	 * see that one, we simply ignore it.
   1037   1.1  thorpej 	 */
   1038   1.1  thorpej 	if (ctx->poic_tagattr != NULL &&
   1039   1.1  thorpej 	    !_PROP_TAGATTR_MATCH(ctx, "version"))
   1040   1.1  thorpej 		goto out;
   1041   1.1  thorpej 
   1042   1.1  thorpej 	/* Next we expect to see <dict>. */
   1043   1.1  thorpej 	if (_prop_object_internalize_find_tag(ctx, "dict",
   1044   1.1  thorpej 					      _PROP_TAG_TYPE_START) == FALSE)
   1045   1.1  thorpej 		goto out;
   1046   1.1  thorpej 
   1047   1.1  thorpej 	dict = _prop_dictionary_internalize(ctx);
   1048   1.1  thorpej 	if (dict == NULL)
   1049   1.1  thorpej 		goto out;
   1050   1.1  thorpej 
   1051   1.1  thorpej 	/* We've advanced past </dict>.  Now we want </plist>. */
   1052   1.1  thorpej 	if (_prop_object_internalize_find_tag(ctx, "plist",
   1053   1.1  thorpej 					      _PROP_TAG_TYPE_END) == FALSE) {
   1054   1.1  thorpej 		prop_object_release(dict);
   1055   1.1  thorpej 		dict = NULL;
   1056   1.1  thorpej 	}
   1057   1.1  thorpej 
   1058   1.1  thorpej  out:
   1059   1.1  thorpej  	_prop_object_internalize_context_free(ctx);
   1060   1.1  thorpej 	return (dict);
   1061   1.1  thorpej }
   1062   1.9  thorpej 
   1063   1.9  thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
   1064   1.9  thorpej /*
   1065   1.9  thorpej  * prop_dictionary_externalize_to_file --
   1066   1.9  thorpej  *	Externalize a dictionary to the specified file.
   1067   1.9  thorpej  */
   1068   1.9  thorpej boolean_t
   1069   1.9  thorpej prop_dictionary_externalize_to_file(prop_dictionary_t dict, const char *fname)
   1070   1.9  thorpej {
   1071   1.9  thorpej 	char *xml;
   1072   1.9  thorpej 	boolean_t rv;
   1073  1.10       he 	int save_errno = 0;	/* XXXGCC -Wuninitialized [mips, ...] */
   1074   1.9  thorpej 
   1075   1.9  thorpej 	xml = prop_dictionary_externalize(dict);
   1076   1.9  thorpej 	if (xml == NULL)
   1077   1.9  thorpej 		return (FALSE);
   1078   1.9  thorpej 	rv = _prop_object_externalize_write_file(fname, xml, strlen(xml));
   1079   1.9  thorpej 	if (rv == FALSE)
   1080   1.9  thorpej 		save_errno = errno;
   1081   1.9  thorpej 	_PROP_FREE(xml, M_TEMP);
   1082   1.9  thorpej 	if (rv == FALSE)
   1083   1.9  thorpej 		errno = save_errno;
   1084   1.9  thorpej 
   1085   1.9  thorpej 	return (rv);
   1086   1.9  thorpej }
   1087   1.9  thorpej 
   1088   1.9  thorpej /*
   1089   1.9  thorpej  * prop_dictionary_internalize_from_file --
   1090   1.9  thorpej  *	Internalize a dictionary from a file.
   1091   1.9  thorpej  */
   1092   1.9  thorpej prop_dictionary_t
   1093   1.9  thorpej prop_dictionary_internalize_from_file(const char *fname)
   1094   1.9  thorpej {
   1095   1.9  thorpej 	struct _prop_object_internalize_mapped_file *mf;
   1096   1.9  thorpej 	prop_dictionary_t dict;
   1097   1.9  thorpej 
   1098   1.9  thorpej 	mf = _prop_object_internalize_map_file(fname);
   1099   1.9  thorpej 	if (mf == NULL)
   1100   1.9  thorpej 		return (NULL);
   1101   1.9  thorpej 	dict = prop_dictionary_internalize(mf->poimf_xml);
   1102   1.9  thorpej 	_prop_object_internalize_unmap_file(mf);
   1103   1.9  thorpej 
   1104   1.9  thorpej 	return (dict);
   1105   1.9  thorpej }
   1106   1.9  thorpej #endif /* !_KERNEL && !_STANDALONE */
   1107