Imported Portable proplib version 0.1 without autotools stuff generated.
[portableproplib.git] / src / prop_dictionary.c
blob7815086e929cf6131e405c1cbf4870e59f51dfbc
1 /* $NetBSD: prop_dictionary.c,v 1.32 2008/08/03 04:00:12 thorpej Exp $ */
3 /*-
4 * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "proplib.h"
33 #include "prop_object_impl.h"
34 #include "prop_rb_impl.h"
36 #include <errno.h>
39 * We implement these like arrays, but we keep them sorted by key.
40 * This allows us to binary-search as well as keep externalized output
41 * sane-looking for human eyes.
44 #define EXPAND_STEP 16
47 * prop_dictionary_keysym_t is allocated with space at the end to hold the
48 * key. This must be a regular object so that we can maintain sane iterator
49 * semantics -- we don't want to require that the caller release the result
50 * of prop_object_iterator_next().
52 * We'd like to have some small'ish keysym objects for up-to-16 characters
53 * in a key, some for up-to-32 characters in a key, and then a final bucket
54 * for up-to-128 characters in a key (not including NUL). Keys longer than
55 * 128 characters are not allowed.
57 struct _prop_dictionary_keysym {
58 struct _prop_object pdk_obj;
59 size_t pdk_size;
60 struct rb_node pdk_link;
61 char pdk_key[1];
62 /* actually variable length */
65 #define RBNODE_TO_PDK(n) \
66 ((struct _prop_dictionary_keysym *) \
67 ((uintptr_t)n - offsetof(struct _prop_dictionary_keysym, pdk_link)))
69 /* pdk_key[1] takes care of the NUL */
70 #define PDK_SIZE_16 (sizeof(struct _prop_dictionary_keysym) + 16)
71 #define PDK_SIZE_32 (sizeof(struct _prop_dictionary_keysym) + 32)
72 #define PDK_SIZE_128 (sizeof(struct _prop_dictionary_keysym) + 128)
74 #define PDK_MAXKEY 128
76 _PROP_POOL_INIT(_prop_dictionary_keysym16_pool, PDK_SIZE_16, "pdict16")
77 _PROP_POOL_INIT(_prop_dictionary_keysym32_pool, PDK_SIZE_32, "pdict32")
78 _PROP_POOL_INIT(_prop_dictionary_keysym128_pool, PDK_SIZE_128, "pdict128")
80 struct _prop_dict_entry {
81 prop_dictionary_keysym_t pde_key;
82 prop_object_t pde_objref;
85 struct _prop_dictionary {
86 struct _prop_object pd_obj;
87 _PROP_RWLOCK_DECL(pd_rwlock)
88 struct _prop_dict_entry *pd_array;
89 unsigned int pd_capacity;
90 unsigned int pd_count;
91 int pd_flags;
93 uint32_t pd_version;
96 #define PD_F_IMMUTABLE 0x01 /* dictionary is immutable */
98 _PROP_POOL_INIT(_prop_dictionary_pool, sizeof(struct _prop_dictionary),
99 "propdict")
100 _PROP_MALLOC_DEFINE(M_PROP_DICT, "prop dictionary",
101 "property dictionary container object")
103 static _prop_object_free_rv_t
104 _prop_dictionary_free(prop_stack_t, prop_object_t *);
105 static void _prop_dictionary_emergency_free(prop_object_t);
106 static bool _prop_dictionary_externalize(
107 struct _prop_object_externalize_context *,
108 void *);
109 static _prop_object_equals_rv_t
110 _prop_dictionary_equals(prop_object_t, prop_object_t,
111 void **, void **,
112 prop_object_t *, prop_object_t *);
113 static void _prop_dictionary_equals_finish(prop_object_t, prop_object_t);
114 static prop_object_iterator_t
115 _prop_dictionary_iterator_locked(prop_dictionary_t);
116 static prop_object_t
117 _prop_dictionary_iterator_next_object_locked(void *);
118 static prop_object_t
119 _prop_dictionary_get_keysym(prop_dictionary_t,
120 prop_dictionary_keysym_t, bool);
121 static prop_object_t
122 _prop_dictionary_get(prop_dictionary_t, const char *, bool);
124 static const struct _prop_object_type _prop_object_type_dictionary = {
125 .pot_type = PROP_TYPE_DICTIONARY,
126 .pot_free = _prop_dictionary_free,
127 .pot_emergency_free = _prop_dictionary_emergency_free,
128 .pot_extern = _prop_dictionary_externalize,
129 .pot_equals = _prop_dictionary_equals,
130 .pot_equals_finish = _prop_dictionary_equals_finish,
133 static _prop_object_free_rv_t
134 _prop_dict_keysym_free(prop_stack_t, prop_object_t *);
135 static bool _prop_dict_keysym_externalize(
136 struct _prop_object_externalize_context *,
137 void *);
138 static _prop_object_equals_rv_t
139 _prop_dict_keysym_equals(prop_object_t, prop_object_t,
140 void **, void **,
141 prop_object_t *, prop_object_t *);
143 static const struct _prop_object_type _prop_object_type_dict_keysym = {
144 .pot_type = PROP_TYPE_DICT_KEYSYM,
145 .pot_free = _prop_dict_keysym_free,
146 .pot_extern = _prop_dict_keysym_externalize,
147 .pot_equals = _prop_dict_keysym_equals,
150 #define prop_object_is_dictionary(x) \
151 ((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_dictionary)
152 #define prop_object_is_dictionary_keysym(x) \
153 ((x) != NULL && (x)->pdk_obj.po_type == &_prop_object_type_dict_keysym)
155 #define prop_dictionary_is_immutable(x) \
156 (((x)->pd_flags & PD_F_IMMUTABLE) != 0)
158 struct _prop_dictionary_iterator {
159 struct _prop_object_iterator pdi_base;
160 unsigned int pdi_index;
164 * Dictionary key symbols are immutable, and we are likely to have many
165 * duplicated key symbols. So, to save memory, we unique'ify key symbols
166 * so we only have to have one copy of each string.
169 static int
170 _prop_dict_keysym_rb_compare_nodes(const struct rb_node *n1,
171 const struct rb_node *n2)
173 const prop_dictionary_keysym_t pdk1 = RBNODE_TO_PDK(n1);
174 const prop_dictionary_keysym_t pdk2 = RBNODE_TO_PDK(n2);
176 return (strcmp(pdk1->pdk_key, pdk2->pdk_key));
179 static int
180 _prop_dict_keysym_rb_compare_key(const struct rb_node *n,
181 const void *v)
183 const prop_dictionary_keysym_t pdk = RBNODE_TO_PDK(n);
184 const char *cp = v;
186 return (strcmp(pdk->pdk_key, cp));
189 static const struct rb_tree_ops _prop_dict_keysym_rb_tree_ops = {
190 .rbto_compare_nodes = _prop_dict_keysym_rb_compare_nodes,
191 .rbto_compare_key = _prop_dict_keysym_rb_compare_key,
194 static struct rb_tree _prop_dict_keysym_tree;
195 static bool _prop_dict_keysym_tree_initialized;
197 _PROP_MUTEX_DECL_STATIC(_prop_dict_keysym_tree_mutex)
199 static void
200 _prop_dict_keysym_put(prop_dictionary_keysym_t pdk)
203 if (pdk->pdk_size <= PDK_SIZE_16)
204 _PROP_POOL_PUT(_prop_dictionary_keysym16_pool, pdk);
205 else if (pdk->pdk_size <= PDK_SIZE_32)
206 _PROP_POOL_PUT(_prop_dictionary_keysym32_pool, pdk);
207 else {
208 _PROP_ASSERT(pdk->pdk_size <= PDK_SIZE_128);
209 _PROP_POOL_PUT(_prop_dictionary_keysym128_pool, pdk);
213 /* ARGSUSED */
214 static _prop_object_free_rv_t
215 _prop_dict_keysym_free(prop_stack_t stack, prop_object_t *obj)
217 prop_dictionary_keysym_t pdk = *obj;
219 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
220 _prop_rb_tree_remove_node(&_prop_dict_keysym_tree, &pdk->pdk_link);
221 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
223 _prop_dict_keysym_put(pdk);
225 return _PROP_OBJECT_FREE_DONE;
228 static bool
229 _prop_dict_keysym_externalize(struct _prop_object_externalize_context *ctx,
230 void *v)
232 prop_dictionary_keysym_t pdk = v;
234 /* We externalize these as strings, and they're never empty. */
236 _PROP_ASSERT(pdk->pdk_key[0] != '\0');
238 if (_prop_object_externalize_start_tag(ctx, "string") == false ||
239 _prop_object_externalize_append_encoded_cstring(ctx,
240 pdk->pdk_key) == false ||
241 _prop_object_externalize_end_tag(ctx, "string") == false)
242 return (false);
244 return (true);
247 /* ARGSUSED */
248 static _prop_object_equals_rv_t
249 _prop_dict_keysym_equals(prop_object_t v1, prop_object_t v2,
250 void **stored_pointer1, void **stored_pointer2,
251 prop_object_t *next_obj1, prop_object_t *next_obj2)
253 prop_dictionary_keysym_t pdk1 = v1;
254 prop_dictionary_keysym_t pdk2 = v2;
257 * There is only ever one copy of a keysym at any given time,
258 * so we can reduce this to a simple pointer equality check.
260 if (pdk1 == pdk2)
261 return _PROP_OBJECT_EQUALS_TRUE;
262 else
263 return _PROP_OBJECT_EQUALS_FALSE;
266 static prop_dictionary_keysym_t
267 _prop_dict_keysym_alloc(const char *key)
269 prop_dictionary_keysym_t opdk, pdk;
270 const struct rb_node *n;
271 size_t size;
272 bool rv;
275 * Check to see if this already exists in the tree. If it does,
276 * we just retain it and return it.
278 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
279 if (! _prop_dict_keysym_tree_initialized) {
280 _prop_rb_tree_init(&_prop_dict_keysym_tree,
281 &_prop_dict_keysym_rb_tree_ops);
282 _prop_dict_keysym_tree_initialized = true;
283 } else {
284 n = _prop_rb_tree_find(&_prop_dict_keysym_tree, key);
285 if (n != NULL) {
286 opdk = RBNODE_TO_PDK(n);
287 prop_object_retain(opdk);
288 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
289 return (opdk);
292 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
295 * Not in the tree. Create it now.
298 size = sizeof(*pdk) + strlen(key) /* pdk_key[1] covers the NUL */;
300 if (size <= PDK_SIZE_16)
301 pdk = _PROP_POOL_GET(_prop_dictionary_keysym16_pool);
302 else if (size <= PDK_SIZE_32)
303 pdk = _PROP_POOL_GET(_prop_dictionary_keysym32_pool);
304 else if (size <= PDK_SIZE_128)
305 pdk = _PROP_POOL_GET(_prop_dictionary_keysym128_pool);
306 else
307 pdk = NULL; /* key too long */
309 if (pdk == NULL)
310 return (NULL);
312 _prop_object_init(&pdk->pdk_obj, &_prop_object_type_dict_keysym);
314 strcpy(pdk->pdk_key, key);
315 pdk->pdk_size = size;
318 * We dropped the mutex when we allocated the new object, so
319 * we have to check again if it is in the tree.
321 _PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
322 n = _prop_rb_tree_find(&_prop_dict_keysym_tree, key);
323 if (n != NULL) {
324 opdk = RBNODE_TO_PDK(n);
325 prop_object_retain(opdk);
326 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
327 _prop_dict_keysym_put(pdk);
328 return (opdk);
330 rv = _prop_rb_tree_insert_node(&_prop_dict_keysym_tree, &pdk->pdk_link);
331 _PROP_ASSERT(rv == true);
332 _PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
333 return (pdk);
336 static _prop_object_free_rv_t
337 _prop_dictionary_free(prop_stack_t stack, prop_object_t *obj)
339 prop_dictionary_t pd = *obj;
340 prop_dictionary_keysym_t pdk;
341 prop_object_t po;
343 _PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
344 _PROP_ASSERT((pd->pd_capacity == 0 && pd->pd_array == NULL) ||
345 (pd->pd_capacity != 0 && pd->pd_array != NULL));
347 /* The empty dictorinary is easy, handle that first. */
348 if (pd->pd_count == 0) {
349 if (pd->pd_array != NULL)
350 _PROP_FREE(pd->pd_array, M_PROP_DICT);
352 _PROP_RWLOCK_DESTROY(pd->pd_rwlock);
354 _PROP_POOL_PUT(_prop_dictionary_pool, pd);
356 return (_PROP_OBJECT_FREE_DONE);
359 po = pd->pd_array[pd->pd_count - 1].pde_objref;
360 _PROP_ASSERT(po != NULL);
362 if (stack == NULL) {
364 * If we are in emergency release mode,
365 * just let caller recurse down.
367 *obj = po;
368 return (_PROP_OBJECT_FREE_FAILED);
371 /* Otherwise, try to push the current object on the stack. */
372 if (!_prop_stack_push(stack, pd, NULL, NULL, NULL)) {
373 /* Push failed, entering emergency release mode. */
374 return (_PROP_OBJECT_FREE_FAILED);
376 /* Object pushed on stack, caller will release it. */
377 --pd->pd_count;
378 pdk = pd->pd_array[pd->pd_count].pde_key;
379 _PROP_ASSERT(pdk != NULL);
380 prop_object_release(pdk);
381 *obj = po;
382 return (_PROP_OBJECT_FREE_RECURSE);
385 static void
386 _prop_dictionary_emergency_free(prop_object_t obj)
388 prop_dictionary_t pd = obj;
389 prop_dictionary_keysym_t pdk;
391 _PROP_ASSERT(pd->pd_count != 0);
392 --pd->pd_count;
394 pdk = pd->pd_array[pd->pd_count].pde_key;
395 _PROP_ASSERT(pdk != NULL);
396 prop_object_release(pdk);
399 static bool
400 _prop_dictionary_externalize(struct _prop_object_externalize_context *ctx,
401 void *v)
403 prop_dictionary_t pd = v;
404 prop_dictionary_keysym_t pdk;
405 struct _prop_object *po;
406 prop_object_iterator_t pi;
407 unsigned int i;
408 bool rv = false;
410 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
412 if (pd->pd_count == 0) {
413 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
414 return (_prop_object_externalize_empty_tag(ctx, "dict"));
417 if (_prop_object_externalize_start_tag(ctx, "dict") == false ||
418 _prop_object_externalize_append_char(ctx, '\n') == false)
419 goto out;
421 pi = _prop_dictionary_iterator_locked(pd);
422 if (pi == NULL)
423 goto out;
425 ctx->poec_depth++;
426 _PROP_ASSERT(ctx->poec_depth != 0);
428 while ((pdk = _prop_dictionary_iterator_next_object_locked(pi))
429 != NULL) {
430 po = _prop_dictionary_get_keysym(pd, pdk, true);
431 if (po == NULL ||
432 _prop_object_externalize_start_tag(ctx, "key") == false ||
433 _prop_object_externalize_append_encoded_cstring(ctx,
434 pdk->pdk_key) == false ||
435 _prop_object_externalize_end_tag(ctx, "key") == false ||
436 (*po->po_type->pot_extern)(ctx, po) == false) {
437 prop_object_iterator_release(pi);
438 goto out;
442 prop_object_iterator_release(pi);
444 ctx->poec_depth--;
445 for (i = 0; i < ctx->poec_depth; i++) {
446 if (_prop_object_externalize_append_char(ctx, '\t') == false)
447 goto out;
449 if (_prop_object_externalize_end_tag(ctx, "dict") == false)
450 goto out;
452 rv = true;
454 out:
455 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
456 return (rv);
459 /* ARGSUSED */
460 static _prop_object_equals_rv_t
461 _prop_dictionary_equals(prop_object_t v1, prop_object_t v2,
462 void **stored_pointer1, void **stored_pointer2,
463 prop_object_t *next_obj1, prop_object_t *next_obj2)
465 prop_dictionary_t dict1 = v1;
466 prop_dictionary_t dict2 = v2;
467 uintptr_t idx;
468 _prop_object_equals_rv_t rv = _PROP_OBJECT_EQUALS_FALSE;
470 if (dict1 == dict2)
471 return (_PROP_OBJECT_EQUALS_TRUE);
473 _PROP_ASSERT(*stored_pointer1 == *stored_pointer2);
475 idx = (uintptr_t)*stored_pointer1;
477 if (idx == 0) {
478 if ((uintptr_t)dict1 < (uintptr_t)dict2) {
479 _PROP_RWLOCK_RDLOCK(dict1->pd_rwlock);
480 _PROP_RWLOCK_RDLOCK(dict2->pd_rwlock);
481 } else {
482 _PROP_RWLOCK_RDLOCK(dict2->pd_rwlock);
483 _PROP_RWLOCK_RDLOCK(dict1->pd_rwlock);
487 if (dict1->pd_count != dict2->pd_count)
488 goto out;
490 if (idx == dict1->pd_count) {
491 rv = _PROP_OBJECT_EQUALS_TRUE;
492 goto out;
495 _PROP_ASSERT(idx < dict1->pd_count);
497 *stored_pointer1 = (void *)(idx + 1);
498 *stored_pointer2 = (void *)(idx + 1);
500 *next_obj1 = &dict1->pd_array[idx].pde_objref;
501 *next_obj2 = &dict2->pd_array[idx].pde_objref;
503 if (!prop_dictionary_keysym_equals(dict1->pd_array[idx].pde_key,
504 dict2->pd_array[idx].pde_key))
505 goto out;
507 return (_PROP_OBJECT_EQUALS_RECURSE);
509 out:
510 _PROP_RWLOCK_UNLOCK(dict1->pd_rwlock);
511 _PROP_RWLOCK_UNLOCK(dict2->pd_rwlock);
512 return (rv);
515 static void
516 _prop_dictionary_equals_finish(prop_object_t v1, prop_object_t v2)
518 _PROP_RWLOCK_UNLOCK(((prop_dictionary_t)v1)->pd_rwlock);
519 _PROP_RWLOCK_UNLOCK(((prop_dictionary_t)v2)->pd_rwlock);
522 static prop_dictionary_t
523 _prop_dictionary_alloc(unsigned int capacity)
525 prop_dictionary_t pd;
526 struct _prop_dict_entry *array;
528 if (capacity != 0) {
529 array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
530 if (array == NULL)
531 return (NULL);
532 } else
533 array = NULL;
535 pd = _PROP_POOL_GET(_prop_dictionary_pool);
536 if (pd != NULL) {
537 _prop_object_init(&pd->pd_obj, &_prop_object_type_dictionary);
539 _PROP_RWLOCK_INIT(pd->pd_rwlock);
540 pd->pd_array = array;
541 pd->pd_capacity = capacity;
542 pd->pd_count = 0;
543 pd->pd_flags = 0;
545 pd->pd_version = 0;
546 } else if (array != NULL)
547 _PROP_FREE(array, M_PROP_DICT);
549 return (pd);
552 static bool
553 _prop_dictionary_expand(prop_dictionary_t pd, unsigned int capacity)
555 struct _prop_dict_entry *array, *oarray;
558 * Dictionary must be WRITE-LOCKED.
561 oarray = pd->pd_array;
563 array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
564 if (array == NULL)
565 return (false);
566 if (oarray != NULL)
567 memcpy(array, oarray, pd->pd_capacity * sizeof(*array));
568 pd->pd_array = array;
569 pd->pd_capacity = capacity;
571 if (oarray != NULL)
572 _PROP_FREE(oarray, M_PROP_DICT);
574 return (true);
577 static prop_object_t
578 _prop_dictionary_iterator_next_object_locked(void *v)
580 struct _prop_dictionary_iterator *pdi = v;
581 prop_dictionary_t pd = pdi->pdi_base.pi_obj;
582 prop_dictionary_keysym_t pdk = NULL;
584 _PROP_ASSERT(prop_object_is_dictionary(pd));
586 if (pd->pd_version != pdi->pdi_base.pi_version)
587 goto out; /* dictionary changed during iteration */
589 _PROP_ASSERT(pdi->pdi_index <= pd->pd_count);
591 if (pdi->pdi_index == pd->pd_count)
592 goto out; /* we've iterated all objects */
594 pdk = pd->pd_array[pdi->pdi_index].pde_key;
595 pdi->pdi_index++;
597 out:
598 return (pdk);
601 static prop_object_t
602 _prop_dictionary_iterator_next_object(void *v)
604 struct _prop_dictionary_iterator *pdi = v;
605 prop_dictionary_t pd = pdi->pdi_base.pi_obj;
606 prop_dictionary_keysym_t pdk;
608 _PROP_ASSERT(prop_object_is_dictionary(pd));
610 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
611 pdk = _prop_dictionary_iterator_next_object_locked(pdi);
612 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
613 return (pdk);
616 static void
617 _prop_dictionary_iterator_reset_locked(void *v)
619 struct _prop_dictionary_iterator *pdi = v;
620 prop_dictionary_t pd = pdi->pdi_base.pi_obj;
622 _PROP_ASSERT(prop_object_is_dictionary(pd));
624 pdi->pdi_index = 0;
625 pdi->pdi_base.pi_version = pd->pd_version;
628 static void
629 _prop_dictionary_iterator_reset(void *v)
631 struct _prop_dictionary_iterator *pdi = v;
632 prop_dictionary_t pd = pdi->pdi_base.pi_obj;
634 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
635 _prop_dictionary_iterator_reset_locked(pdi);
636 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
640 * prop_dictionary_create --
641 * Create a dictionary.
643 prop_dictionary_t
644 prop_dictionary_create(void)
647 return (_prop_dictionary_alloc(0));
651 * prop_dictionary_create_with_capacity --
652 * Create a dictionary with the capacity to store N objects.
654 prop_dictionary_t
655 prop_dictionary_create_with_capacity(unsigned int capacity)
658 return (_prop_dictionary_alloc(capacity));
662 * prop_dictionary_copy --
663 * Copy a dictionary. The new dictionary has an initial capacity equal
664 * to the number of objects stored int the original dictionary. The new
665 * dictionary contains refrences to the original dictionary's objects,
666 * not copies of those objects (i.e. a shallow copy).
668 prop_dictionary_t
669 prop_dictionary_copy(prop_dictionary_t opd)
671 prop_dictionary_t pd;
672 prop_dictionary_keysym_t pdk;
673 prop_object_t po;
674 unsigned int idx;
676 if (! prop_object_is_dictionary(opd))
677 return (NULL);
679 _PROP_RWLOCK_RDLOCK(opd->pd_rwlock);
681 pd = _prop_dictionary_alloc(opd->pd_count);
682 if (pd != NULL) {
683 for (idx = 0; idx < opd->pd_count; idx++) {
684 pdk = opd->pd_array[idx].pde_key;
685 po = opd->pd_array[idx].pde_objref;
687 prop_object_retain(pdk);
688 prop_object_retain(po);
690 pd->pd_array[idx].pde_key = pdk;
691 pd->pd_array[idx].pde_objref = po;
693 pd->pd_count = opd->pd_count;
694 pd->pd_flags = opd->pd_flags;
696 _PROP_RWLOCK_UNLOCK(opd->pd_rwlock);
697 return (pd);
701 * prop_dictionary_copy_mutable --
702 * Like prop_dictionary_copy(), but the resulting dictionary is
703 * mutable.
705 prop_dictionary_t
706 prop_dictionary_copy_mutable(prop_dictionary_t opd)
708 prop_dictionary_t pd;
710 if (! prop_object_is_dictionary(opd))
711 return (NULL);
713 pd = prop_dictionary_copy(opd);
714 if (pd != NULL)
715 pd->pd_flags &= ~PD_F_IMMUTABLE;
717 return (pd);
721 * prop_dictionary_make_immutable --
722 * Set the immutable flag on that dictionary.
724 void
725 prop_dictionary_make_immutable(prop_dictionary_t pd)
728 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
729 if (prop_dictionary_is_immutable(pd) == false)
730 pd->pd_flags |= PD_F_IMMUTABLE;
731 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
735 * prop_dictionary_count --
736 * Return the number of objects stored in the dictionary.
738 unsigned int
739 prop_dictionary_count(prop_dictionary_t pd)
741 unsigned int rv;
743 if (! prop_object_is_dictionary(pd))
744 return (0);
746 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
747 rv = pd->pd_count;
748 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
750 return (rv);
754 * prop_dictionary_ensure_capacity --
755 * Ensure that the dictionary has the capacity to store the specified
756 * total number of objects (including the objects already stored in
757 * the dictionary).
759 bool
760 prop_dictionary_ensure_capacity(prop_dictionary_t pd, unsigned int capacity)
762 bool rv;
764 if (! prop_object_is_dictionary(pd))
765 return (false);
767 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
768 if (capacity > pd->pd_capacity)
769 rv = _prop_dictionary_expand(pd, capacity);
770 else
771 rv = true;
772 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
773 return (rv);
776 static prop_object_iterator_t
777 _prop_dictionary_iterator_locked(prop_dictionary_t pd)
779 struct _prop_dictionary_iterator *pdi;
781 if (! prop_object_is_dictionary(pd))
782 return (NULL);
784 pdi = _PROP_CALLOC(sizeof(*pdi), M_TEMP);
785 if (pdi == NULL)
786 return (NULL);
787 pdi->pdi_base.pi_next_object = _prop_dictionary_iterator_next_object;
788 pdi->pdi_base.pi_reset = _prop_dictionary_iterator_reset;
789 prop_object_retain(pd);
790 pdi->pdi_base.pi_obj = pd;
791 _prop_dictionary_iterator_reset_locked(pdi);
793 return (&pdi->pdi_base);
797 * prop_dictionary_iterator --
798 * Return an iterator for the dictionary. The dictionary is retained by
799 * the iterator.
801 prop_object_iterator_t
802 prop_dictionary_iterator(prop_dictionary_t pd)
804 prop_object_iterator_t pi;
806 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
807 pi = _prop_dictionary_iterator_locked(pd);
808 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
809 return (pi);
813 * prop_dictionary_all_keys --
814 * Return an array containing a snapshot of all of the keys
815 * in the dictionary.
817 prop_array_t
818 prop_dictionary_all_keys(prop_dictionary_t pd)
820 prop_array_t array;
821 unsigned int idx;
822 bool rv = true;
824 if (! prop_object_is_dictionary(pd))
825 return (NULL);
827 /* There is no pressing need to lock the dictionary for this. */
828 array = prop_array_create_with_capacity(pd->pd_count);
830 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
832 for (idx = 0; idx < pd->pd_count; idx++) {
833 rv = prop_array_add(array, pd->pd_array[idx].pde_key);
834 if (rv == false)
835 break;
838 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
840 if (rv == false) {
841 prop_object_release(array);
842 array = NULL;
844 return (array);
847 static struct _prop_dict_entry *
848 _prop_dict_lookup(prop_dictionary_t pd, const char *key,
849 unsigned int *idxp)
851 struct _prop_dict_entry *pde;
852 unsigned int base, idx, distance;
853 int res;
856 * Dictionary must be READ-LOCKED or WRITE-LOCKED.
859 for (idx = 0, base = 0, distance = pd->pd_count; distance != 0;
860 distance >>= 1) {
861 idx = base + (distance >> 1);
862 pde = &pd->pd_array[idx];
863 _PROP_ASSERT(pde->pde_key != NULL);
864 res = strcmp(key, pde->pde_key->pdk_key);
865 if (res == 0) {
866 if (idxp != NULL)
867 *idxp = idx;
868 return (pde);
870 if (res > 0) { /* key > pdk_key: move right */
871 base = idx + 1;
872 distance--;
873 } /* else move left */
876 /* idx points to the slot we looked at last. */
877 if (idxp != NULL)
878 *idxp = idx;
879 return (NULL);
882 static prop_object_t
883 _prop_dictionary_get(prop_dictionary_t pd, const char *key, bool locked)
885 const struct _prop_dict_entry *pde;
886 prop_object_t po = NULL;
888 if (! prop_object_is_dictionary(pd))
889 return (NULL);
891 if (!locked)
892 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
893 pde = _prop_dict_lookup(pd, key, NULL);
894 if (pde != NULL) {
895 _PROP_ASSERT(pde->pde_objref != NULL);
896 po = pde->pde_objref;
898 if (!locked)
899 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
900 return (po);
903 * prop_dictionary_get --
904 * Return the object stored with specified key.
906 prop_object_t
907 prop_dictionary_get(prop_dictionary_t pd, const char *key)
909 prop_object_t po;
911 _PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
912 po = _prop_dictionary_get(pd, key, true);
913 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
914 return (po);
917 static prop_object_t
918 _prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
919 bool locked)
922 if (! (prop_object_is_dictionary(pd) &&
923 prop_object_is_dictionary_keysym(pdk)))
924 return (NULL);
926 return (_prop_dictionary_get(pd, pdk->pdk_key, locked));
930 * prop_dictionary_get_keysym --
931 * Return the object stored at the location encoded by the keysym.
933 prop_object_t
934 prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk)
937 return (_prop_dictionary_get_keysym(pd, pdk, false));
941 * prop_dictionary_set --
942 * Store a reference to an object at with the specified key.
943 * If the key already exisit, the original object is released.
945 bool
946 prop_dictionary_set(prop_dictionary_t pd, const char *key, prop_object_t po)
948 struct _prop_dict_entry *pde;
949 prop_dictionary_keysym_t pdk;
950 unsigned int idx;
951 bool rv = false;
953 if (! prop_object_is_dictionary(pd))
954 return (false);
956 _PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
958 if (prop_dictionary_is_immutable(pd))
959 return (false);
961 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
963 pde = _prop_dict_lookup(pd, key, &idx);
964 if (pde != NULL) {
965 prop_object_t opo = pde->pde_objref;
966 prop_object_retain(po);
967 pde->pde_objref = po;
968 prop_object_release(opo);
969 rv = true;
970 goto out;
973 pdk = _prop_dict_keysym_alloc(key);
974 if (pdk == NULL)
975 goto out;
977 if (pd->pd_count == pd->pd_capacity &&
978 _prop_dictionary_expand(pd,
979 pd->pd_capacity + EXPAND_STEP) == false) {
980 prop_object_release(pdk);
981 goto out;
984 /* At this point, the store will succeed. */
985 prop_object_retain(po);
987 if (pd->pd_count == 0) {
988 pd->pd_array[0].pde_key = pdk;
989 pd->pd_array[0].pde_objref = po;
990 pd->pd_count++;
991 pd->pd_version++;
992 rv = true;
993 goto out;
996 pde = &pd->pd_array[idx];
997 _PROP_ASSERT(pde->pde_key != NULL);
999 if (strcmp(key, pde->pde_key->pdk_key) < 0) {
1001 * key < pdk_key: insert to the left. This is the same as
1002 * inserting to the right, except we decrement the current
1003 * index first.
1005 * Because we're unsigned, we have to special case 0
1006 * (grumble).
1008 if (idx == 0) {
1009 memmove(&pd->pd_array[1], &pd->pd_array[0],
1010 pd->pd_count * sizeof(*pde));
1011 pd->pd_array[0].pde_key = pdk;
1012 pd->pd_array[0].pde_objref = po;
1013 pd->pd_count++;
1014 pd->pd_version++;
1015 rv = true;
1016 goto out;
1018 idx--;
1021 memmove(&pd->pd_array[idx + 2], &pd->pd_array[idx + 1],
1022 (pd->pd_count - (idx + 1)) * sizeof(*pde));
1023 pd->pd_array[idx + 1].pde_key = pdk;
1024 pd->pd_array[idx + 1].pde_objref = po;
1025 pd->pd_count++;
1027 pd->pd_version++;
1029 rv = true;
1031 out:
1032 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
1033 return (rv);
1037 * prop_dictionary_set_keysym --
1038 * Replace the object in the dictionary at the location encoded by
1039 * the keysym.
1041 bool
1042 prop_dictionary_set_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
1043 prop_object_t po)
1046 if (! (prop_object_is_dictionary(pd) &&
1047 prop_object_is_dictionary_keysym(pdk)))
1048 return (false);
1050 return (prop_dictionary_set(pd, pdk->pdk_key, po));
1053 static void
1054 _prop_dictionary_remove(prop_dictionary_t pd, struct _prop_dict_entry *pde,
1055 unsigned int idx)
1057 prop_dictionary_keysym_t pdk = pde->pde_key;
1058 prop_object_t po = pde->pde_objref;
1061 * Dictionary must be WRITE-LOCKED.
1064 _PROP_ASSERT(pd->pd_count != 0);
1065 _PROP_ASSERT(idx < pd->pd_count);
1066 _PROP_ASSERT(pde == &pd->pd_array[idx]);
1068 idx++;
1069 memmove(&pd->pd_array[idx - 1], &pd->pd_array[idx],
1070 (pd->pd_count - idx) * sizeof(*pde));
1071 pd->pd_count--;
1072 pd->pd_version++;
1074 prop_object_release(pdk);
1075 prop_object_release(po);
1079 * prop_dictionary_remove --
1080 * Remove the reference to an object with the specified key from
1081 * the dictionary.
1083 void
1084 prop_dictionary_remove(prop_dictionary_t pd, const char *key)
1086 struct _prop_dict_entry *pde;
1087 unsigned int idx;
1089 if (! prop_object_is_dictionary(pd))
1090 return;
1092 _PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
1094 /* XXX Should this be a _PROP_ASSERT()? */
1095 if (prop_dictionary_is_immutable(pd))
1096 goto out;
1098 pde = _prop_dict_lookup(pd, key, &idx);
1099 /* XXX Should this be a _PROP_ASSERT()? */
1100 if (pde == NULL)
1101 goto out;
1103 _prop_dictionary_remove(pd, pde, idx);
1104 out:
1105 _PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
1109 * prop_dictionary_remove_keysym --
1110 * Remove a reference to an object stored in the dictionary at the
1111 * location encoded by the keysym.
1113 void
1114 prop_dictionary_remove_keysym(prop_dictionary_t pd,
1115 prop_dictionary_keysym_t pdk)
1118 if (! (prop_object_is_dictionary(pd) &&
1119 prop_object_is_dictionary_keysym(pdk)))
1120 return;
1122 prop_dictionary_remove(pd, pdk->pdk_key);
1126 * prop_dictionary_equals --
1127 * Return true if the two dictionaries are equivalent. Note we do a
1128 * by-value comparison of the objects in the dictionary.
1130 bool
1131 prop_dictionary_equals(prop_dictionary_t dict1, prop_dictionary_t dict2)
1133 if (!prop_object_is_dictionary(dict1) ||
1134 !prop_object_is_dictionary(dict2))
1135 return (false);
1137 return (prop_object_equals(dict1, dict2));
1141 * prop_dictionary_keysym_cstring_nocopy --
1142 * Return an immutable reference to the keysym's value.
1144 const char *
1145 prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pdk)
1148 if (! prop_object_is_dictionary_keysym(pdk))
1149 return (NULL);
1151 return (pdk->pdk_key);
1155 * prop_dictionary_keysym_equals --
1156 * Return true if the two dictionary key symbols are equivalent.
1157 * Note: We do not compare the object references.
1159 bool
1160 prop_dictionary_keysym_equals(prop_dictionary_keysym_t pdk1,
1161 prop_dictionary_keysym_t pdk2)
1163 if (!prop_object_is_dictionary_keysym(pdk1) ||
1164 !prop_object_is_dictionary_keysym(pdk2))
1165 return (false);
1167 return (prop_object_equals(pdk1, pdk2));
1171 * prop_dictionary_externalize --
1172 * Externalize a dictionary, returning a NUL-terminated buffer
1173 * containing the XML-style representation. The buffer is allocated
1174 * with the M_TEMP memory type.
1176 char *
1177 prop_dictionary_externalize(prop_dictionary_t pd)
1179 struct _prop_object_externalize_context *ctx;
1180 char *cp;
1182 ctx = _prop_object_externalize_context_alloc();
1183 if (ctx == NULL)
1184 return (NULL);
1186 if (_prop_object_externalize_header(ctx) == false ||
1187 (*pd->pd_obj.po_type->pot_extern)(ctx, pd) == false ||
1188 _prop_object_externalize_footer(ctx) == false) {
1189 /* We are responsible for releasing the buffer. */
1190 _PROP_FREE(ctx->poec_buf, M_TEMP);
1191 _prop_object_externalize_context_free(ctx);
1192 return (NULL);
1195 cp = ctx->poec_buf;
1196 _prop_object_externalize_context_free(ctx);
1198 return (cp);
1202 * _prop_dictionary_internalize --
1203 * Parse a <dict>...</dict> and return the object created from the
1204 * external representation.
1206 * Internal state in via rec_data is the storage area for the last processed
1207 * key.
1208 * _prop_dictionary_internalize_body is the upper half of the parse loop.
1209 * It is responsible for parsing the key directly and storing it in the area
1210 * referenced by rec_data.
1211 * _prop_dictionary_internalize_cont is the lower half and called with the value
1212 * associated with the key.
1214 static bool _prop_dictionary_internalize_body(prop_stack_t,
1215 prop_object_t *, struct _prop_object_internalize_context *, char *);
1217 bool
1218 _prop_dictionary_internalize(prop_stack_t stack, prop_object_t *obj,
1219 struct _prop_object_internalize_context *ctx)
1221 prop_dictionary_t dict;
1222 char *tmpkey;
1224 /* We don't currently understand any attributes. */
1225 if (ctx->poic_tagattr != NULL)
1226 return (true);
1228 dict = prop_dictionary_create();
1229 if (dict == NULL)
1230 return (true);
1232 if (ctx->poic_is_empty_element) {
1233 *obj = dict;
1234 return (true);
1237 tmpkey = _PROP_MALLOC(PDK_MAXKEY + 1, M_TEMP);
1238 if (tmpkey == NULL) {
1239 prop_object_release(dict);
1240 return (true);
1243 *obj = dict;
1245 * Opening tag is found, storage for key allocated and
1246 * now continue to the first element.
1248 return _prop_dictionary_internalize_body(stack, obj, ctx, tmpkey);
1251 static bool
1252 _prop_dictionary_internalize_continue(prop_stack_t stack, prop_object_t *obj,
1253 struct _prop_object_internalize_context *ctx, void *data, prop_object_t child)
1255 prop_dictionary_t dict = *obj;
1256 char *tmpkey = data;
1258 _PROP_ASSERT(tmpkey != NULL);
1260 if (child == NULL ||
1261 prop_dictionary_set(dict, tmpkey, child) == false) {
1262 _PROP_FREE(tmpkey, M_TEMP);
1263 if (child != NULL)
1264 prop_object_release(child);
1265 prop_object_release(dict);
1266 *obj = NULL;
1267 return (true);
1270 prop_object_release(child);
1273 * key, value was added, now continue looking for the next key
1274 * or the closing tag.
1276 return _prop_dictionary_internalize_body(stack, obj, ctx, tmpkey);
1279 static bool
1280 _prop_dictionary_internalize_body(prop_stack_t stack, prop_object_t *obj,
1281 struct _prop_object_internalize_context *ctx, char *tmpkey)
1283 prop_dictionary_t dict = *obj;
1284 size_t keylen;
1286 /* Fetch the next tag. */
1287 if (_prop_object_internalize_find_tag(ctx, NULL, _PROP_TAG_TYPE_EITHER) == false)
1288 goto bad;
1290 /* Check to see if this is the end of the dictionary. */
1291 if (_PROP_TAG_MATCH(ctx, "dict") &&
1292 ctx->poic_tag_type == _PROP_TAG_TYPE_END) {
1293 _PROP_FREE(tmpkey, M_TEMP);
1294 return (true);
1297 /* Ok, it must be a non-empty key start tag. */
1298 if (!_PROP_TAG_MATCH(ctx, "key") ||
1299 ctx->poic_tag_type != _PROP_TAG_TYPE_START ||
1300 ctx->poic_is_empty_element)
1301 goto bad;
1303 if (_prop_object_internalize_decode_string(ctx,
1304 tmpkey, PDK_MAXKEY, &keylen,
1305 &ctx->poic_cp) == false)
1306 goto bad;
1308 _PROP_ASSERT(keylen <= PDK_MAXKEY);
1309 tmpkey[keylen] = '\0';
1311 if (_prop_object_internalize_find_tag(ctx, "key",
1312 _PROP_TAG_TYPE_END) == false)
1313 goto bad;
1315 /* ..and now the beginning of the value. */
1316 if (_prop_object_internalize_find_tag(ctx, NULL,
1317 _PROP_TAG_TYPE_START) == false)
1318 goto bad;
1321 * Key is found, now wait for value to be parsed.
1323 if (_prop_stack_push(stack, *obj,
1324 _prop_dictionary_internalize_continue,
1325 tmpkey, NULL))
1326 return (false);
1328 bad:
1329 _PROP_FREE(tmpkey, M_TEMP);
1330 prop_object_release(dict);
1331 *obj = NULL;
1332 return (true);
1336 * prop_dictionary_internalize --
1337 * Create a dictionary by parsing the NUL-terminated XML-style
1338 * representation.
1340 prop_dictionary_t
1341 prop_dictionary_internalize(const char *xml)
1343 return _prop_generic_internalize(xml, "dict");
1347 * prop_dictionary_externalize_to_file --
1348 * Externalize a dictionary to the specified file.
1350 bool
1351 prop_dictionary_externalize_to_file(prop_dictionary_t dict, const char *fname)
1353 char *xml;
1354 bool rv;
1355 int save_errno = 0; /* XXXGCC -Wuninitialized [mips, ...] */
1357 xml = prop_dictionary_externalize(dict);
1358 if (xml == NULL)
1359 return (false);
1360 rv = _prop_object_externalize_write_file(fname, xml, strlen(xml));
1361 if (rv == false)
1362 save_errno = errno;
1363 _PROP_FREE(xml, M_TEMP);
1364 if (rv == false)
1365 errno = save_errno;
1367 return (rv);
1371 * prop_dictionary_internalize_from_file --
1372 * Internalize a dictionary from a file.
1374 prop_dictionary_t
1375 prop_dictionary_internalize_from_file(const char *fname)
1377 struct _prop_object_internalize_mapped_file *mf;
1378 prop_dictionary_t dict;
1380 mf = _prop_object_internalize_map_file(fname);
1381 if (mf == NULL)
1382 return (NULL);
1383 dict = prop_dictionary_internalize(mf->poimf_xml);
1384 _prop_object_internalize_unmap_file(mf);
1386 return (dict);