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