prop_array.c revision 1.17 1 1.17 yamt /* $NetBSD: prop_array.c,v 1.17 2008/05/24 14:24:04 yamt Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.10 joerg * Copyright (c) 2006, 2007 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 *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <prop/prop_array.h>
33 1.1 thorpej #include "prop_object_impl.h"
34 1.1 thorpej
35 1.4 thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
36 1.4 thorpej #include <errno.h>
37 1.4 thorpej #endif
38 1.4 thorpej
39 1.1 thorpej struct _prop_array {
40 1.1 thorpej struct _prop_object pa_obj;
41 1.7 thorpej _PROP_RWLOCK_DECL(pa_rwlock)
42 1.1 thorpej prop_object_t * pa_array;
43 1.1 thorpej unsigned int pa_capacity;
44 1.1 thorpej unsigned int pa_count;
45 1.1 thorpej int pa_flags;
46 1.1 thorpej
47 1.1 thorpej uint32_t pa_version;
48 1.1 thorpej };
49 1.1 thorpej
50 1.1 thorpej #define PA_F_IMMUTABLE 0x01 /* array is immutable */
51 1.1 thorpej
52 1.1 thorpej _PROP_POOL_INIT(_prop_array_pool, sizeof(struct _prop_array), "proparay")
53 1.1 thorpej _PROP_MALLOC_DEFINE(M_PROP_ARRAY, "prop array",
54 1.1 thorpej "property array container object")
55 1.1 thorpej
56 1.10 joerg static int _prop_array_free(prop_stack_t, prop_object_t *);
57 1.10 joerg static void _prop_array_emergency_free(prop_object_t);
58 1.9 thorpej static bool _prop_array_externalize(
59 1.2 thorpej struct _prop_object_externalize_context *,
60 1.2 thorpej void *);
61 1.11 joerg static bool _prop_array_equals(prop_object_t, prop_object_t,
62 1.11 joerg void **, void **,
63 1.11 joerg prop_object_t *, prop_object_t *);
64 1.11 joerg static void _prop_array_equals_finish(prop_object_t, prop_object_t);
65 1.2 thorpej
66 1.2 thorpej static const struct _prop_object_type _prop_object_type_array = {
67 1.10 joerg .pot_type = PROP_TYPE_ARRAY,
68 1.10 joerg .pot_free = _prop_array_free,
69 1.10 joerg .pot_emergency_free = _prop_array_emergency_free,
70 1.10 joerg .pot_extern = _prop_array_externalize,
71 1.10 joerg .pot_equals = _prop_array_equals,
72 1.11 joerg .pot_equals_finish = _prop_array_equals_finish,
73 1.2 thorpej };
74 1.2 thorpej
75 1.2 thorpej #define prop_object_is_array(x) \
76 1.6 thorpej ((x) != NULL && (x)->pa_obj.po_type == &_prop_object_type_array)
77 1.1 thorpej
78 1.1 thorpej #define prop_array_is_immutable(x) (((x)->pa_flags & PA_F_IMMUTABLE) != 0)
79 1.1 thorpej
80 1.1 thorpej struct _prop_array_iterator {
81 1.1 thorpej struct _prop_object_iterator pai_base;
82 1.1 thorpej unsigned int pai_index;
83 1.1 thorpej };
84 1.1 thorpej
85 1.1 thorpej #define EXPAND_STEP 16
86 1.1 thorpej
87 1.10 joerg static int
88 1.10 joerg _prop_array_free(prop_stack_t stack, prop_object_t *obj)
89 1.1 thorpej {
90 1.10 joerg prop_array_t pa = *obj;
91 1.1 thorpej prop_object_t po;
92 1.1 thorpej
93 1.1 thorpej _PROP_ASSERT(pa->pa_count <= pa->pa_capacity);
94 1.1 thorpej _PROP_ASSERT((pa->pa_capacity == 0 && pa->pa_array == NULL) ||
95 1.1 thorpej (pa->pa_capacity != 0 && pa->pa_array != NULL));
96 1.1 thorpej
97 1.10 joerg /* The easy case is an empty array, just free and return. */
98 1.10 joerg if (pa->pa_count == 0) {
99 1.10 joerg if (pa->pa_array != NULL)
100 1.10 joerg _PROP_FREE(pa->pa_array, M_PROP_ARRAY);
101 1.10 joerg
102 1.10 joerg _PROP_RWLOCK_DESTROY(pa->pa_rwlock);
103 1.10 joerg
104 1.10 joerg _PROP_POOL_PUT(_prop_array_pool, pa);
105 1.10 joerg
106 1.10 joerg return (_PROP_OBJECT_FREE_DONE);
107 1.1 thorpej }
108 1.1 thorpej
109 1.10 joerg po = pa->pa_array[pa->pa_count - 1];
110 1.10 joerg _PROP_ASSERT(po != NULL);
111 1.1 thorpej
112 1.10 joerg if (stack == NULL) {
113 1.10 joerg /*
114 1.10 joerg * If we are in emergency release mode,
115 1.10 joerg * just let caller recurse down.
116 1.10 joerg */
117 1.10 joerg *obj = po;
118 1.10 joerg return (_PROP_OBJECT_FREE_FAILED);
119 1.10 joerg }
120 1.10 joerg
121 1.10 joerg /* Otherwise, try to push the current object on the stack. */
122 1.11 joerg if (!_prop_stack_push(stack, pa, NULL, NULL, NULL)) {
123 1.10 joerg /* Push failed, entering emergency release mode. */
124 1.10 joerg return (_PROP_OBJECT_FREE_FAILED);
125 1.10 joerg }
126 1.10 joerg /* Object pushed on stack, caller will release it. */
127 1.10 joerg --pa->pa_count;
128 1.10 joerg *obj = po;
129 1.10 joerg return (_PROP_OBJECT_FREE_RECURSE);
130 1.10 joerg }
131 1.7 thorpej
132 1.10 joerg static void
133 1.10 joerg _prop_array_emergency_free(prop_object_t obj)
134 1.10 joerg {
135 1.10 joerg prop_array_t pa = obj;
136 1.10 joerg
137 1.10 joerg _PROP_ASSERT(pa->pa_count != 0);
138 1.10 joerg --pa->pa_count;
139 1.1 thorpej }
140 1.1 thorpej
141 1.9 thorpej static bool
142 1.1 thorpej _prop_array_externalize(struct _prop_object_externalize_context *ctx,
143 1.1 thorpej void *v)
144 1.1 thorpej {
145 1.1 thorpej prop_array_t pa = v;
146 1.1 thorpej struct _prop_object *po;
147 1.1 thorpej prop_object_iterator_t pi;
148 1.1 thorpej unsigned int i;
149 1.9 thorpej bool rv = false;
150 1.7 thorpej
151 1.7 thorpej _PROP_RWLOCK_RDLOCK(pa->pa_rwlock);
152 1.1 thorpej
153 1.7 thorpej if (pa->pa_count == 0) {
154 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
155 1.1 thorpej return (_prop_object_externalize_empty_tag(ctx, "array"));
156 1.7 thorpej }
157 1.1 thorpej
158 1.1 thorpej /* XXXJRT Hint "count" for the internalize step? */
159 1.9 thorpej if (_prop_object_externalize_start_tag(ctx, "array") == false ||
160 1.9 thorpej _prop_object_externalize_append_char(ctx, '\n') == false)
161 1.7 thorpej goto out;
162 1.1 thorpej
163 1.1 thorpej pi = prop_array_iterator(pa);
164 1.1 thorpej if (pi == NULL)
165 1.7 thorpej goto out;
166 1.1 thorpej
167 1.1 thorpej ctx->poec_depth++;
168 1.1 thorpej _PROP_ASSERT(ctx->poec_depth != 0);
169 1.1 thorpej
170 1.1 thorpej while ((po = prop_object_iterator_next(pi)) != NULL) {
171 1.9 thorpej if ((*po->po_type->pot_extern)(ctx, po) == false) {
172 1.1 thorpej prop_object_iterator_release(pi);
173 1.7 thorpej goto out;
174 1.1 thorpej }
175 1.1 thorpej }
176 1.1 thorpej
177 1.1 thorpej prop_object_iterator_release(pi);
178 1.1 thorpej
179 1.1 thorpej ctx->poec_depth--;
180 1.1 thorpej for (i = 0; i < ctx->poec_depth; i++) {
181 1.9 thorpej if (_prop_object_externalize_append_char(ctx, '\t') == false)
182 1.7 thorpej goto out;
183 1.1 thorpej }
184 1.9 thorpej if (_prop_object_externalize_end_tag(ctx, "array") == false)
185 1.7 thorpej goto out;
186 1.7 thorpej
187 1.9 thorpej rv = true;
188 1.1 thorpej
189 1.7 thorpej out:
190 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
191 1.7 thorpej return (rv);
192 1.1 thorpej }
193 1.1 thorpej
194 1.11 joerg /* ARGSUSED */
195 1.9 thorpej static bool
196 1.11 joerg _prop_array_equals(prop_object_t v1, prop_object_t v2,
197 1.11 joerg void **stored_pointer1, void **stored_pointer2,
198 1.11 joerg prop_object_t *next_obj1, prop_object_t *next_obj2)
199 1.2 thorpej {
200 1.2 thorpej prop_array_t array1 = v1;
201 1.2 thorpej prop_array_t array2 = v2;
202 1.11 joerg uintptr_t idx;
203 1.11 joerg bool rv = _PROP_OBJECT_EQUALS_FALSE;
204 1.2 thorpej
205 1.11 joerg if (array1 == array2)
206 1.11 joerg return (_PROP_OBJECT_EQUALS_TRUE);
207 1.2 thorpej
208 1.11 joerg _PROP_ASSERT(*stored_pointer1 == *stored_pointer2);
209 1.11 joerg idx = (uintptr_t)*stored_pointer1;
210 1.7 thorpej
211 1.11 joerg /* For the first iteration, lock the objects. */
212 1.11 joerg if (idx == 0) {
213 1.11 joerg if ((uintptr_t)array1 < (uintptr_t)array2) {
214 1.11 joerg _PROP_RWLOCK_RDLOCK(array1->pa_rwlock);
215 1.11 joerg _PROP_RWLOCK_RDLOCK(array2->pa_rwlock);
216 1.11 joerg } else {
217 1.11 joerg _PROP_RWLOCK_RDLOCK(array2->pa_rwlock);
218 1.11 joerg _PROP_RWLOCK_RDLOCK(array1->pa_rwlock);
219 1.11 joerg }
220 1.7 thorpej }
221 1.7 thorpej
222 1.2 thorpej if (array1->pa_count != array2->pa_count)
223 1.7 thorpej goto out;
224 1.11 joerg if (idx == array1->pa_count) {
225 1.11 joerg rv = true;
226 1.11 joerg goto out;
227 1.11 joerg }
228 1.11 joerg _PROP_ASSERT(idx < array1->pa_count);
229 1.11 joerg
230 1.11 joerg *stored_pointer1 = (void *)(idx + 1);
231 1.11 joerg *stored_pointer2 = (void *)(idx + 1);
232 1.2 thorpej
233 1.11 joerg *next_obj1 = array1->pa_array[idx];
234 1.11 joerg *next_obj2 = array2->pa_array[idx];
235 1.2 thorpej
236 1.11 joerg return (_PROP_OBJECT_EQUALS_RECURSE);
237 1.7 thorpej
238 1.7 thorpej out:
239 1.7 thorpej _PROP_RWLOCK_UNLOCK(array1->pa_rwlock);
240 1.7 thorpej _PROP_RWLOCK_UNLOCK(array2->pa_rwlock);
241 1.7 thorpej return (rv);
242 1.2 thorpej }
243 1.2 thorpej
244 1.11 joerg static void
245 1.11 joerg _prop_array_equals_finish(prop_object_t v1, prop_object_t v2)
246 1.11 joerg {
247 1.11 joerg _PROP_RWLOCK_UNLOCK(((prop_array_t)v1)->pa_rwlock);
248 1.11 joerg _PROP_RWLOCK_UNLOCK(((prop_array_t)v2)->pa_rwlock);
249 1.11 joerg }
250 1.11 joerg
251 1.1 thorpej static prop_array_t
252 1.1 thorpej _prop_array_alloc(unsigned int capacity)
253 1.1 thorpej {
254 1.1 thorpej prop_array_t pa;
255 1.1 thorpej prop_object_t *array;
256 1.1 thorpej
257 1.1 thorpej if (capacity != 0) {
258 1.1 thorpej array = _PROP_CALLOC(capacity * sizeof(prop_object_t),
259 1.1 thorpej M_PROP_ARRAY);
260 1.1 thorpej if (array == NULL)
261 1.1 thorpej return (NULL);
262 1.1 thorpej } else
263 1.1 thorpej array = NULL;
264 1.1 thorpej
265 1.1 thorpej
266 1.1 thorpej pa = _PROP_POOL_GET(_prop_array_pool);
267 1.1 thorpej if (pa != NULL) {
268 1.2 thorpej _prop_object_init(&pa->pa_obj, &_prop_object_type_array);
269 1.2 thorpej pa->pa_obj.po_type = &_prop_object_type_array;
270 1.1 thorpej
271 1.7 thorpej _PROP_RWLOCK_INIT(pa->pa_rwlock);
272 1.1 thorpej pa->pa_array = array;
273 1.1 thorpej pa->pa_capacity = capacity;
274 1.1 thorpej pa->pa_count = 0;
275 1.1 thorpej pa->pa_flags = 0;
276 1.1 thorpej
277 1.1 thorpej pa->pa_version = 0;
278 1.1 thorpej } else if (array != NULL)
279 1.1 thorpej _PROP_FREE(array, M_PROP_ARRAY);
280 1.1 thorpej
281 1.1 thorpej return (pa);
282 1.1 thorpej }
283 1.1 thorpej
284 1.9 thorpej static bool
285 1.1 thorpej _prop_array_expand(prop_array_t pa, unsigned int capacity)
286 1.1 thorpej {
287 1.1 thorpej prop_object_t *array, *oarray;
288 1.1 thorpej
289 1.7 thorpej /*
290 1.7 thorpej * Array must be WRITE-LOCKED.
291 1.7 thorpej */
292 1.7 thorpej
293 1.1 thorpej oarray = pa->pa_array;
294 1.1 thorpej
295 1.3 thorpej array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_ARRAY);
296 1.1 thorpej if (array == NULL)
297 1.9 thorpej return (false);
298 1.1 thorpej if (oarray != NULL)
299 1.3 thorpej memcpy(array, oarray, pa->pa_capacity * sizeof(*array));
300 1.1 thorpej pa->pa_array = array;
301 1.1 thorpej pa->pa_capacity = capacity;
302 1.1 thorpej
303 1.1 thorpej if (oarray != NULL)
304 1.1 thorpej _PROP_FREE(oarray, M_PROP_ARRAY);
305 1.1 thorpej
306 1.9 thorpej return (true);
307 1.1 thorpej }
308 1.1 thorpej
309 1.1 thorpej static prop_object_t
310 1.1 thorpej _prop_array_iterator_next_object(void *v)
311 1.1 thorpej {
312 1.1 thorpej struct _prop_array_iterator *pai = v;
313 1.1 thorpej prop_array_t pa = pai->pai_base.pi_obj;
314 1.7 thorpej prop_object_t po = NULL;
315 1.1 thorpej
316 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
317 1.17 yamt
318 1.17 yamt _PROP_RWLOCK_RDLOCK(pa->pa_rwlock);
319 1.7 thorpej
320 1.1 thorpej if (pa->pa_version != pai->pai_base.pi_version)
321 1.7 thorpej goto out; /* array changed during iteration */
322 1.1 thorpej
323 1.1 thorpej _PROP_ASSERT(pai->pai_index <= pa->pa_count);
324 1.1 thorpej
325 1.1 thorpej if (pai->pai_index == pa->pa_count)
326 1.7 thorpej goto out; /* we've iterated all objects */
327 1.1 thorpej
328 1.1 thorpej po = pa->pa_array[pai->pai_index];
329 1.1 thorpej pai->pai_index++;
330 1.1 thorpej
331 1.7 thorpej out:
332 1.17 yamt _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
333 1.1 thorpej return (po);
334 1.1 thorpej }
335 1.1 thorpej
336 1.1 thorpej static void
337 1.1 thorpej _prop_array_iterator_reset(void *v)
338 1.1 thorpej {
339 1.1 thorpej struct _prop_array_iterator *pai = v;
340 1.1 thorpej prop_array_t pa = pai->pai_base.pi_obj;
341 1.1 thorpej
342 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
343 1.17 yamt
344 1.17 yamt _PROP_RWLOCK_RDLOCK(pa->pa_rwlock);
345 1.7 thorpej
346 1.1 thorpej pai->pai_index = 0;
347 1.1 thorpej pai->pai_base.pi_version = pa->pa_version;
348 1.14 xtraeme
349 1.17 yamt _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
350 1.1 thorpej }
351 1.1 thorpej
352 1.1 thorpej /*
353 1.1 thorpej * prop_array_create --
354 1.1 thorpej * Create an empty array.
355 1.1 thorpej */
356 1.1 thorpej prop_array_t
357 1.1 thorpej prop_array_create(void)
358 1.1 thorpej {
359 1.1 thorpej
360 1.1 thorpej return (_prop_array_alloc(0));
361 1.1 thorpej }
362 1.1 thorpej
363 1.1 thorpej /*
364 1.1 thorpej * prop_array_create_with_capacity --
365 1.1 thorpej * Create an array with the capacity to store N objects.
366 1.1 thorpej */
367 1.1 thorpej prop_array_t
368 1.1 thorpej prop_array_create_with_capacity(unsigned int capacity)
369 1.1 thorpej {
370 1.1 thorpej
371 1.1 thorpej return (_prop_array_alloc(capacity));
372 1.1 thorpej }
373 1.1 thorpej
374 1.1 thorpej /*
375 1.1 thorpej * prop_array_copy --
376 1.1 thorpej * Copy an array. The new array has an initial capacity equal to
377 1.1 thorpej * the number of objects stored in the original array. The new
378 1.1 thorpej * array contains references to the original array's objects, not
379 1.1 thorpej * copies of those objects (i.e. a shallow copy).
380 1.1 thorpej */
381 1.1 thorpej prop_array_t
382 1.1 thorpej prop_array_copy(prop_array_t opa)
383 1.1 thorpej {
384 1.1 thorpej prop_array_t pa;
385 1.1 thorpej prop_object_t po;
386 1.1 thorpej unsigned int idx;
387 1.1 thorpej
388 1.4 thorpej if (! prop_object_is_array(opa))
389 1.4 thorpej return (NULL);
390 1.1 thorpej
391 1.7 thorpej _PROP_RWLOCK_RDLOCK(opa->pa_rwlock);
392 1.7 thorpej
393 1.1 thorpej pa = _prop_array_alloc(opa->pa_count);
394 1.1 thorpej if (pa != NULL) {
395 1.1 thorpej for (idx = 0; idx < opa->pa_count; idx++) {
396 1.1 thorpej po = opa->pa_array[idx];
397 1.1 thorpej prop_object_retain(po);
398 1.1 thorpej pa->pa_array[idx] = po;
399 1.1 thorpej }
400 1.1 thorpej pa->pa_count = opa->pa_count;
401 1.1 thorpej pa->pa_flags = opa->pa_flags;
402 1.1 thorpej }
403 1.7 thorpej _PROP_RWLOCK_UNLOCK(opa->pa_rwlock);
404 1.1 thorpej return (pa);
405 1.1 thorpej }
406 1.1 thorpej
407 1.1 thorpej /*
408 1.1 thorpej * prop_array_copy_mutable --
409 1.1 thorpej * Like prop_array_copy(), but the resulting array is mutable.
410 1.1 thorpej */
411 1.1 thorpej prop_array_t
412 1.1 thorpej prop_array_copy_mutable(prop_array_t opa)
413 1.1 thorpej {
414 1.1 thorpej prop_array_t pa;
415 1.1 thorpej
416 1.1 thorpej pa = prop_array_copy(opa);
417 1.1 thorpej if (pa != NULL)
418 1.1 thorpej pa->pa_flags &= ~PA_F_IMMUTABLE;
419 1.1 thorpej
420 1.1 thorpej return (pa);
421 1.1 thorpej }
422 1.1 thorpej
423 1.1 thorpej /*
424 1.1 thorpej * prop_array_capacity --
425 1.1 thorpej * Return the capacity of the array.
426 1.1 thorpej */
427 1.1 thorpej unsigned int
428 1.1 thorpej prop_array_capacity(prop_array_t pa)
429 1.1 thorpej {
430 1.7 thorpej unsigned int rv;
431 1.1 thorpej
432 1.4 thorpej if (! prop_object_is_array(pa))
433 1.4 thorpej return (0);
434 1.4 thorpej
435 1.7 thorpej _PROP_RWLOCK_RDLOCK(pa->pa_rwlock);
436 1.7 thorpej rv = pa->pa_capacity;
437 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
438 1.7 thorpej
439 1.7 thorpej return (rv);
440 1.1 thorpej }
441 1.1 thorpej
442 1.1 thorpej /*
443 1.1 thorpej * prop_array_count --
444 1.1 thorpej * Return the number of objects stored in the array.
445 1.1 thorpej */
446 1.1 thorpej unsigned int
447 1.1 thorpej prop_array_count(prop_array_t pa)
448 1.1 thorpej {
449 1.7 thorpej unsigned int rv;
450 1.1 thorpej
451 1.4 thorpej if (! prop_object_is_array(pa))
452 1.4 thorpej return (0);
453 1.4 thorpej
454 1.7 thorpej _PROP_RWLOCK_RDLOCK(pa->pa_rwlock);
455 1.7 thorpej rv = pa->pa_count;
456 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
457 1.7 thorpej
458 1.7 thorpej return (rv);
459 1.1 thorpej }
460 1.1 thorpej
461 1.1 thorpej /*
462 1.1 thorpej * prop_array_ensure_capacity --
463 1.1 thorpej * Ensure that the array has the capacity to store the specified
464 1.1 thorpej * total number of objects (inluding the objects already stored
465 1.1 thorpej * in the array).
466 1.1 thorpej */
467 1.9 thorpej bool
468 1.1 thorpej prop_array_ensure_capacity(prop_array_t pa, unsigned int capacity)
469 1.1 thorpej {
470 1.9 thorpej bool rv;
471 1.1 thorpej
472 1.4 thorpej if (! prop_object_is_array(pa))
473 1.9 thorpej return (false);
474 1.4 thorpej
475 1.7 thorpej _PROP_RWLOCK_WRLOCK(pa->pa_rwlock);
476 1.1 thorpej if (capacity > pa->pa_capacity)
477 1.7 thorpej rv = _prop_array_expand(pa, capacity);
478 1.7 thorpej else
479 1.9 thorpej rv = true;
480 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
481 1.7 thorpej
482 1.7 thorpej return (rv);
483 1.1 thorpej }
484 1.1 thorpej
485 1.1 thorpej /*
486 1.1 thorpej * prop_array_iterator --
487 1.1 thorpej * Return an iterator for the array. The array is retained by
488 1.1 thorpej * the iterator.
489 1.1 thorpej */
490 1.1 thorpej prop_object_iterator_t
491 1.1 thorpej prop_array_iterator(prop_array_t pa)
492 1.1 thorpej {
493 1.1 thorpej struct _prop_array_iterator *pai;
494 1.1 thorpej
495 1.4 thorpej if (! prop_object_is_array(pa))
496 1.4 thorpej return (NULL);
497 1.1 thorpej
498 1.1 thorpej pai = _PROP_CALLOC(sizeof(*pai), M_TEMP);
499 1.1 thorpej if (pai == NULL)
500 1.1 thorpej return (NULL);
501 1.1 thorpej pai->pai_base.pi_next_object = _prop_array_iterator_next_object;
502 1.1 thorpej pai->pai_base.pi_reset = _prop_array_iterator_reset;
503 1.1 thorpej prop_object_retain(pa);
504 1.1 thorpej pai->pai_base.pi_obj = pa;
505 1.1 thorpej _prop_array_iterator_reset(pai);
506 1.1 thorpej
507 1.1 thorpej return (&pai->pai_base);
508 1.1 thorpej }
509 1.1 thorpej
510 1.1 thorpej /*
511 1.1 thorpej * prop_array_make_immutable --
512 1.1 thorpej * Make the array immutable.
513 1.1 thorpej */
514 1.1 thorpej void
515 1.1 thorpej prop_array_make_immutable(prop_array_t pa)
516 1.1 thorpej {
517 1.1 thorpej
518 1.7 thorpej _PROP_RWLOCK_WRLOCK(pa->pa_rwlock);
519 1.9 thorpej if (prop_array_is_immutable(pa) == false)
520 1.1 thorpej pa->pa_flags |= PA_F_IMMUTABLE;
521 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
522 1.1 thorpej }
523 1.1 thorpej
524 1.1 thorpej /*
525 1.1 thorpej * prop_array_mutable --
526 1.9 thorpej * Returns true if the array is mutable.
527 1.1 thorpej */
528 1.9 thorpej bool
529 1.1 thorpej prop_array_mutable(prop_array_t pa)
530 1.1 thorpej {
531 1.9 thorpej bool rv;
532 1.1 thorpej
533 1.7 thorpej _PROP_RWLOCK_RDLOCK(pa->pa_rwlock);
534 1.9 thorpej rv = prop_array_is_immutable(pa) == false;
535 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
536 1.7 thorpej
537 1.7 thorpej return (rv);
538 1.1 thorpej }
539 1.1 thorpej
540 1.1 thorpej /*
541 1.1 thorpej * prop_array_get --
542 1.1 thorpej * Return the object stored at the specified array index.
543 1.1 thorpej */
544 1.1 thorpej prop_object_t
545 1.1 thorpej prop_array_get(prop_array_t pa, unsigned int idx)
546 1.1 thorpej {
547 1.7 thorpej prop_object_t po = NULL;
548 1.1 thorpej
549 1.4 thorpej if (! prop_object_is_array(pa))
550 1.4 thorpej return (NULL);
551 1.4 thorpej
552 1.7 thorpej _PROP_RWLOCK_RDLOCK(pa->pa_rwlock);
553 1.1 thorpej if (idx >= pa->pa_count)
554 1.7 thorpej goto out;
555 1.1 thorpej po = pa->pa_array[idx];
556 1.1 thorpej _PROP_ASSERT(po != NULL);
557 1.7 thorpej out:
558 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
559 1.1 thorpej return (po);
560 1.1 thorpej }
561 1.1 thorpej
562 1.9 thorpej static bool
563 1.7 thorpej _prop_array_add(prop_array_t pa, prop_object_t po)
564 1.7 thorpej {
565 1.7 thorpej
566 1.7 thorpej /*
567 1.7 thorpej * Array must be WRITE-LOCKED.
568 1.7 thorpej */
569 1.7 thorpej
570 1.7 thorpej _PROP_ASSERT(pa->pa_count <= pa->pa_capacity);
571 1.7 thorpej
572 1.7 thorpej if (prop_array_is_immutable(pa) ||
573 1.7 thorpej (pa->pa_count == pa->pa_capacity &&
574 1.9 thorpej _prop_array_expand(pa, pa->pa_capacity + EXPAND_STEP) == false))
575 1.9 thorpej return (false);
576 1.7 thorpej
577 1.7 thorpej prop_object_retain(po);
578 1.7 thorpej pa->pa_array[pa->pa_count++] = po;
579 1.7 thorpej pa->pa_version++;
580 1.7 thorpej
581 1.9 thorpej return (true);
582 1.7 thorpej }
583 1.7 thorpej
584 1.1 thorpej /*
585 1.1 thorpej * prop_array_set --
586 1.1 thorpej * Store a reference to an object at the specified array index.
587 1.1 thorpej * This method is not allowed to create holes in the array; the
588 1.1 thorpej * caller must either be setting the object just beyond the existing
589 1.1 thorpej * count or replacing an already existing object reference.
590 1.1 thorpej */
591 1.9 thorpej bool
592 1.1 thorpej prop_array_set(prop_array_t pa, unsigned int idx, prop_object_t po)
593 1.1 thorpej {
594 1.1 thorpej prop_object_t opo;
595 1.9 thorpej bool rv = false;
596 1.1 thorpej
597 1.4 thorpej if (! prop_object_is_array(pa))
598 1.9 thorpej return (false);
599 1.1 thorpej
600 1.7 thorpej _PROP_RWLOCK_WRLOCK(pa->pa_rwlock);
601 1.7 thorpej
602 1.1 thorpej if (prop_array_is_immutable(pa))
603 1.7 thorpej goto out;
604 1.1 thorpej
605 1.7 thorpej if (idx == pa->pa_count) {
606 1.7 thorpej rv = _prop_array_add(pa, po);
607 1.7 thorpej goto out;
608 1.7 thorpej }
609 1.1 thorpej
610 1.1 thorpej _PROP_ASSERT(idx < pa->pa_count);
611 1.1 thorpej
612 1.1 thorpej opo = pa->pa_array[idx];
613 1.1 thorpej _PROP_ASSERT(opo != NULL);
614 1.1 thorpej
615 1.1 thorpej prop_object_retain(po);
616 1.1 thorpej pa->pa_array[idx] = po;
617 1.1 thorpej pa->pa_version++;
618 1.1 thorpej
619 1.1 thorpej prop_object_release(opo);
620 1.1 thorpej
621 1.9 thorpej rv = true;
622 1.7 thorpej
623 1.7 thorpej out:
624 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
625 1.7 thorpej return (rv);
626 1.1 thorpej }
627 1.1 thorpej
628 1.1 thorpej /*
629 1.1 thorpej * prop_array_add --
630 1.1 thorpej * Add a refrerence to an object to the specified array, appending
631 1.1 thorpej * to the end and growing the array's capacity, if necessary.
632 1.1 thorpej */
633 1.9 thorpej bool
634 1.1 thorpej prop_array_add(prop_array_t pa, prop_object_t po)
635 1.1 thorpej {
636 1.9 thorpej bool rv;
637 1.1 thorpej
638 1.4 thorpej if (! prop_object_is_array(pa))
639 1.9 thorpej return (false);
640 1.4 thorpej
641 1.7 thorpej _PROP_RWLOCK_WRLOCK(pa->pa_rwlock);
642 1.7 thorpej rv = _prop_array_add(pa, po);
643 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
644 1.1 thorpej
645 1.7 thorpej return (rv);
646 1.1 thorpej }
647 1.1 thorpej
648 1.1 thorpej /*
649 1.1 thorpej * prop_array_remove --
650 1.1 thorpej * Remove the reference to an object from an array at the specified
651 1.1 thorpej * index. The array will be compacted following the removal.
652 1.1 thorpej */
653 1.1 thorpej void
654 1.1 thorpej prop_array_remove(prop_array_t pa, unsigned int idx)
655 1.1 thorpej {
656 1.1 thorpej prop_object_t po;
657 1.1 thorpej
658 1.4 thorpej if (! prop_object_is_array(pa))
659 1.4 thorpej return;
660 1.4 thorpej
661 1.7 thorpej _PROP_RWLOCK_WRLOCK(pa->pa_rwlock);
662 1.7 thorpej
663 1.1 thorpej _PROP_ASSERT(idx < pa->pa_count);
664 1.1 thorpej
665 1.1 thorpej /* XXX Should this be a _PROP_ASSERT()? */
666 1.7 thorpej if (prop_array_is_immutable(pa)) {
667 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
668 1.1 thorpej return;
669 1.7 thorpej }
670 1.1 thorpej
671 1.1 thorpej po = pa->pa_array[idx];
672 1.1 thorpej _PROP_ASSERT(po != NULL);
673 1.1 thorpej
674 1.1 thorpej for (++idx; idx < pa->pa_count; idx++)
675 1.1 thorpej pa->pa_array[idx - 1] = pa->pa_array[idx];
676 1.1 thorpej pa->pa_count--;
677 1.1 thorpej pa->pa_version++;
678 1.7 thorpej
679 1.7 thorpej _PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
680 1.1 thorpej
681 1.1 thorpej prop_object_release(po);
682 1.1 thorpej }
683 1.1 thorpej
684 1.1 thorpej /*
685 1.2 thorpej * prop_array_equals --
686 1.9 thorpej * Return true if the two arrays are equivalent. Note we do a
687 1.2 thorpej * by-value comparison of the objects in the array.
688 1.2 thorpej */
689 1.9 thorpej bool
690 1.2 thorpej prop_array_equals(prop_array_t array1, prop_array_t array2)
691 1.2 thorpej {
692 1.11 joerg if (!prop_object_is_array(array1) || !prop_object_is_array(array2))
693 1.11 joerg return (false);
694 1.2 thorpej
695 1.11 joerg return (prop_object_equals(array1, array2));
696 1.2 thorpej }
697 1.2 thorpej
698 1.2 thorpej /*
699 1.4 thorpej * prop_array_externalize --
700 1.4 thorpej * Externalize an array, return a NUL-terminated buffer
701 1.4 thorpej * containing the XML-style representation. The buffer is allocated
702 1.4 thorpej * with the M_TEMP memory type.
703 1.4 thorpej */
704 1.4 thorpej char *
705 1.4 thorpej prop_array_externalize(prop_array_t pa)
706 1.4 thorpej {
707 1.4 thorpej struct _prop_object_externalize_context *ctx;
708 1.4 thorpej char *cp;
709 1.4 thorpej
710 1.4 thorpej ctx = _prop_object_externalize_context_alloc();
711 1.4 thorpej if (ctx == NULL)
712 1.4 thorpej return (NULL);
713 1.4 thorpej
714 1.9 thorpej if (_prop_object_externalize_header(ctx) == false ||
715 1.9 thorpej (*pa->pa_obj.po_type->pot_extern)(ctx, pa) == false ||
716 1.9 thorpej _prop_object_externalize_footer(ctx) == false) {
717 1.4 thorpej /* We are responsible for releasing the buffer. */
718 1.4 thorpej _PROP_FREE(ctx->poec_buf, M_TEMP);
719 1.4 thorpej _prop_object_externalize_context_free(ctx);
720 1.4 thorpej return (NULL);
721 1.4 thorpej }
722 1.4 thorpej
723 1.4 thorpej cp = ctx->poec_buf;
724 1.4 thorpej _prop_object_externalize_context_free(ctx);
725 1.4 thorpej
726 1.4 thorpej return (cp);
727 1.4 thorpej }
728 1.4 thorpej
729 1.4 thorpej /*
730 1.1 thorpej * _prop_array_internalize --
731 1.1 thorpej * Parse an <array>...</array> and return the object created from the
732 1.1 thorpej * external representation.
733 1.1 thorpej */
734 1.10 joerg static bool _prop_array_internalize_body(prop_stack_t, prop_object_t *,
735 1.10 joerg struct _prop_object_internalize_context *);
736 1.10 joerg
737 1.10 joerg bool
738 1.10 joerg _prop_array_internalize(prop_stack_t stack, prop_object_t *obj,
739 1.10 joerg struct _prop_object_internalize_context *ctx)
740 1.1 thorpej {
741 1.1 thorpej /* We don't currently understand any attributes. */
742 1.1 thorpej if (ctx->poic_tagattr != NULL)
743 1.10 joerg return (true);
744 1.1 thorpej
745 1.10 joerg *obj = prop_array_create();
746 1.10 joerg /*
747 1.10 joerg * We are done if the create failed or no child elements exist.
748 1.10 joerg */
749 1.10 joerg if (*obj == NULL || ctx->poic_is_empty_element)
750 1.10 joerg return (true);
751 1.10 joerg
752 1.10 joerg /*
753 1.10 joerg * Opening tag is found, now continue to the first element.
754 1.10 joerg */
755 1.10 joerg return (_prop_array_internalize_body(stack, obj, ctx));
756 1.10 joerg }
757 1.10 joerg
758 1.10 joerg static bool
759 1.10 joerg _prop_array_internalize_continue(prop_stack_t stack,
760 1.10 joerg prop_object_t *obj,
761 1.10 joerg struct _prop_object_internalize_context *ctx,
762 1.10 joerg void *data, prop_object_t child)
763 1.10 joerg {
764 1.10 joerg prop_array_t array;
765 1.10 joerg
766 1.10 joerg _PROP_ASSERT(data == NULL);
767 1.10 joerg
768 1.10 joerg if (child == NULL)
769 1.10 joerg goto bad; /* Element could not be parsed. */
770 1.10 joerg
771 1.10 joerg array = *obj;
772 1.10 joerg
773 1.10 joerg if (prop_array_add(array, child) == false) {
774 1.10 joerg prop_object_release(child);
775 1.10 joerg goto bad;
776 1.10 joerg }
777 1.10 joerg prop_object_release(child);
778 1.10 joerg
779 1.10 joerg /*
780 1.10 joerg * Current element is processed and added, look for next.
781 1.10 joerg */
782 1.10 joerg return (_prop_array_internalize_body(stack, obj, ctx));
783 1.10 joerg
784 1.10 joerg bad:
785 1.10 joerg prop_object_release(*obj);
786 1.10 joerg *obj = NULL;
787 1.10 joerg return (true);
788 1.10 joerg }
789 1.10 joerg
790 1.10 joerg static bool
791 1.10 joerg _prop_array_internalize_body(prop_stack_t stack, prop_object_t *obj,
792 1.10 joerg struct _prop_object_internalize_context *ctx)
793 1.10 joerg {
794 1.10 joerg prop_array_t array = *obj;
795 1.10 joerg
796 1.10 joerg _PROP_ASSERT(array != NULL);
797 1.10 joerg
798 1.10 joerg /* Fetch the next tag. */
799 1.10 joerg if (_prop_object_internalize_find_tag(ctx, NULL,
800 1.10 joerg _PROP_TAG_TYPE_EITHER) == false)
801 1.10 joerg goto bad;
802 1.10 joerg
803 1.10 joerg /* Check to see if this is the end of the array. */
804 1.10 joerg if (_PROP_TAG_MATCH(ctx, "array") &&
805 1.10 joerg ctx->poic_tag_type == _PROP_TAG_TYPE_END) {
806 1.10 joerg /* It is, so don't iterate any further. */
807 1.10 joerg return (true);
808 1.1 thorpej }
809 1.1 thorpej
810 1.11 joerg if (_prop_stack_push(stack, array,
811 1.11 joerg _prop_array_internalize_continue, NULL, NULL))
812 1.10 joerg return (false);
813 1.1 thorpej
814 1.1 thorpej bad:
815 1.1 thorpej prop_object_release(array);
816 1.10 joerg *obj = NULL;
817 1.10 joerg return (true);
818 1.1 thorpej }
819 1.4 thorpej
820 1.4 thorpej /*
821 1.4 thorpej * prop_array_internalize --
822 1.4 thorpej * Create an array by parsing the XML-style representation.
823 1.4 thorpej */
824 1.4 thorpej prop_array_t
825 1.4 thorpej prop_array_internalize(const char *xml)
826 1.4 thorpej {
827 1.8 joerg return _prop_generic_internalize(xml, "array");
828 1.4 thorpej }
829 1.4 thorpej
830 1.4 thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
831 1.4 thorpej /*
832 1.4 thorpej * prop_array_externalize_to_file --
833 1.4 thorpej * Externalize an array to the specified file.
834 1.4 thorpej */
835 1.9 thorpej bool
836 1.4 thorpej prop_array_externalize_to_file(prop_array_t array, const char *fname)
837 1.4 thorpej {
838 1.4 thorpej char *xml;
839 1.9 thorpej bool rv;
840 1.5 he int save_errno = 0; /* XXXGCC -Wuninitialized [mips, ...] */
841 1.4 thorpej
842 1.4 thorpej xml = prop_array_externalize(array);
843 1.4 thorpej if (xml == NULL)
844 1.9 thorpej return (false);
845 1.4 thorpej rv = _prop_object_externalize_write_file(fname, xml, strlen(xml));
846 1.9 thorpej if (rv == false)
847 1.4 thorpej save_errno = errno;
848 1.4 thorpej _PROP_FREE(xml, M_TEMP);
849 1.9 thorpej if (rv == false)
850 1.4 thorpej errno = save_errno;
851 1.4 thorpej
852 1.4 thorpej return (rv);
853 1.4 thorpej }
854 1.4 thorpej
855 1.4 thorpej /*
856 1.4 thorpej * prop_array_internalize_from_file --
857 1.4 thorpej * Internalize an array from a file.
858 1.4 thorpej */
859 1.4 thorpej prop_array_t
860 1.4 thorpej prop_array_internalize_from_file(const char *fname)
861 1.4 thorpej {
862 1.4 thorpej struct _prop_object_internalize_mapped_file *mf;
863 1.4 thorpej prop_array_t array;
864 1.4 thorpej
865 1.4 thorpej mf = _prop_object_internalize_map_file(fname);
866 1.4 thorpej if (mf == NULL)
867 1.4 thorpej return (NULL);
868 1.4 thorpej array = prop_array_internalize(mf->poimf_xml);
869 1.4 thorpej _prop_object_internalize_unmap_file(mf);
870 1.4 thorpej
871 1.4 thorpej return (array);
872 1.4 thorpej }
873 1.4 thorpej #endif /* _KERNEL && !_STANDALONE */
874