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