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