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