prop_array.c revision 1.1 1 1.1 thorpej /* $NetBSD: prop_array.c,v 1.1 2006/04/27 20:11:27 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.1 thorpej struct _prop_array {
43 1.1 thorpej struct _prop_object pa_obj;
44 1.1 thorpej prop_object_t * pa_array;
45 1.1 thorpej unsigned int pa_capacity;
46 1.1 thorpej unsigned int pa_count;
47 1.1 thorpej int pa_flags;
48 1.1 thorpej
49 1.1 thorpej uint32_t pa_version;
50 1.1 thorpej };
51 1.1 thorpej
52 1.1 thorpej #define PA_F_IMMUTABLE 0x01 /* array is immutable */
53 1.1 thorpej
54 1.1 thorpej _PROP_POOL_INIT(_prop_array_pool, sizeof(struct _prop_array), "proparay")
55 1.1 thorpej _PROP_MALLOC_DEFINE(M_PROP_ARRAY, "prop array",
56 1.1 thorpej "property array container object")
57 1.1 thorpej
58 1.1 thorpej #define prop_object_is_array(x) ((x)->pa_obj.po_type == PROP_TYPE_ARRAY)
59 1.1 thorpej
60 1.1 thorpej #define prop_array_is_immutable(x) (((x)->pa_flags & PA_F_IMMUTABLE) != 0)
61 1.1 thorpej
62 1.1 thorpej struct _prop_array_iterator {
63 1.1 thorpej struct _prop_object_iterator pai_base;
64 1.1 thorpej unsigned int pai_index;
65 1.1 thorpej };
66 1.1 thorpej
67 1.1 thorpej #define EXPAND_STEP 16
68 1.1 thorpej
69 1.1 thorpej static void
70 1.1 thorpej _prop_array_free(void *v)
71 1.1 thorpej {
72 1.1 thorpej prop_array_t pa = v;
73 1.1 thorpej prop_object_t po;
74 1.1 thorpej unsigned int idx;
75 1.1 thorpej
76 1.1 thorpej _PROP_ASSERT(pa->pa_count <= pa->pa_capacity);
77 1.1 thorpej _PROP_ASSERT((pa->pa_capacity == 0 && pa->pa_array == NULL) ||
78 1.1 thorpej (pa->pa_capacity != 0 && pa->pa_array != NULL));
79 1.1 thorpej
80 1.1 thorpej for (idx = 0; idx < pa->pa_count; idx++) {
81 1.1 thorpej po = pa->pa_array[idx];
82 1.1 thorpej _PROP_ASSERT(po != NULL);
83 1.1 thorpej prop_object_release(po);
84 1.1 thorpej }
85 1.1 thorpej
86 1.1 thorpej if (pa->pa_array != NULL)
87 1.1 thorpej _PROP_FREE(pa->pa_array, M_PROP_ARRAY);
88 1.1 thorpej
89 1.1 thorpej _PROP_POOL_PUT(_prop_array_pool, pa);
90 1.1 thorpej }
91 1.1 thorpej
92 1.1 thorpej static boolean_t
93 1.1 thorpej _prop_array_externalize(struct _prop_object_externalize_context *ctx,
94 1.1 thorpej void *v)
95 1.1 thorpej {
96 1.1 thorpej prop_array_t pa = v;
97 1.1 thorpej struct _prop_object *po;
98 1.1 thorpej prop_object_iterator_t pi;
99 1.1 thorpej unsigned int i;
100 1.1 thorpej
101 1.1 thorpej if (pa->pa_count == 0)
102 1.1 thorpej return (_prop_object_externalize_empty_tag(ctx, "array"));
103 1.1 thorpej
104 1.1 thorpej /* XXXJRT Hint "count" for the internalize step? */
105 1.1 thorpej if (_prop_object_externalize_start_tag(ctx, "array") == FALSE ||
106 1.1 thorpej _prop_object_externalize_append_char(ctx, '\n') == FALSE)
107 1.1 thorpej return (FALSE);
108 1.1 thorpej
109 1.1 thorpej pi = prop_array_iterator(pa);
110 1.1 thorpej if (pi == NULL)
111 1.1 thorpej return (FALSE);
112 1.1 thorpej
113 1.1 thorpej ctx->poec_depth++;
114 1.1 thorpej _PROP_ASSERT(ctx->poec_depth != 0);
115 1.1 thorpej
116 1.1 thorpej while ((po = prop_object_iterator_next(pi)) != NULL) {
117 1.1 thorpej if ((*po->po_extern)(ctx, po) == FALSE) {
118 1.1 thorpej prop_object_iterator_release(pi);
119 1.1 thorpej return (FALSE);
120 1.1 thorpej }
121 1.1 thorpej }
122 1.1 thorpej
123 1.1 thorpej prop_object_iterator_release(pi);
124 1.1 thorpej
125 1.1 thorpej ctx->poec_depth--;
126 1.1 thorpej for (i = 0; i < ctx->poec_depth; i++) {
127 1.1 thorpej if (_prop_object_externalize_append_char(ctx, '\t') == FALSE)
128 1.1 thorpej return (FALSE);
129 1.1 thorpej }
130 1.1 thorpej if (_prop_object_externalize_end_tag(ctx, "array") == FALSE)
131 1.1 thorpej return (FALSE);
132 1.1 thorpej
133 1.1 thorpej return (TRUE);
134 1.1 thorpej }
135 1.1 thorpej
136 1.1 thorpej static prop_array_t
137 1.1 thorpej _prop_array_alloc(unsigned int capacity)
138 1.1 thorpej {
139 1.1 thorpej prop_array_t pa;
140 1.1 thorpej prop_object_t *array;
141 1.1 thorpej
142 1.1 thorpej if (capacity != 0) {
143 1.1 thorpej array = _PROP_CALLOC(capacity * sizeof(prop_object_t),
144 1.1 thorpej M_PROP_ARRAY);
145 1.1 thorpej if (array == NULL)
146 1.1 thorpej return (NULL);
147 1.1 thorpej } else
148 1.1 thorpej array = NULL;
149 1.1 thorpej
150 1.1 thorpej
151 1.1 thorpej pa = _PROP_POOL_GET(_prop_array_pool);
152 1.1 thorpej if (pa != NULL) {
153 1.1 thorpej _prop_object_init(&pa->pa_obj);
154 1.1 thorpej pa->pa_obj.po_type = PROP_TYPE_ARRAY;
155 1.1 thorpej pa->pa_obj.po_free = _prop_array_free;
156 1.1 thorpej pa->pa_obj.po_extern = _prop_array_externalize;
157 1.1 thorpej
158 1.1 thorpej pa->pa_array = array;
159 1.1 thorpej pa->pa_capacity = capacity;
160 1.1 thorpej pa->pa_count = 0;
161 1.1 thorpej pa->pa_flags = 0;
162 1.1 thorpej
163 1.1 thorpej pa->pa_version = 0;
164 1.1 thorpej } else if (array != NULL)
165 1.1 thorpej _PROP_FREE(array, M_PROP_ARRAY);
166 1.1 thorpej
167 1.1 thorpej return (pa);
168 1.1 thorpej }
169 1.1 thorpej
170 1.1 thorpej static boolean_t
171 1.1 thorpej _prop_array_expand(prop_array_t pa, unsigned int capacity)
172 1.1 thorpej {
173 1.1 thorpej prop_object_t *array, *oarray;
174 1.1 thorpej
175 1.1 thorpej oarray = pa->pa_array;
176 1.1 thorpej
177 1.1 thorpej array = _PROP_CALLOC(capacity * sizeof(prop_object_t), M_PROP_ARRAY);
178 1.1 thorpej if (array == NULL)
179 1.1 thorpej return (FALSE);
180 1.1 thorpej if (oarray != NULL)
181 1.1 thorpej memcpy(array, oarray, pa->pa_capacity * sizeof(prop_object_t));
182 1.1 thorpej pa->pa_array = array;
183 1.1 thorpej pa->pa_capacity = capacity;
184 1.1 thorpej
185 1.1 thorpej if (oarray != NULL)
186 1.1 thorpej _PROP_FREE(oarray, M_PROP_ARRAY);
187 1.1 thorpej
188 1.1 thorpej return (TRUE);
189 1.1 thorpej }
190 1.1 thorpej
191 1.1 thorpej static prop_object_t
192 1.1 thorpej _prop_array_iterator_next_object(void *v)
193 1.1 thorpej {
194 1.1 thorpej struct _prop_array_iterator *pai = v;
195 1.1 thorpej prop_array_t pa = pai->pai_base.pi_obj;
196 1.1 thorpej prop_object_t po;
197 1.1 thorpej
198 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
199 1.1 thorpej
200 1.1 thorpej if (pa->pa_version != pai->pai_base.pi_version)
201 1.1 thorpej return (NULL); /* array changed during iteration */
202 1.1 thorpej
203 1.1 thorpej _PROP_ASSERT(pai->pai_index <= pa->pa_count);
204 1.1 thorpej
205 1.1 thorpej if (pai->pai_index == pa->pa_count)
206 1.1 thorpej return (NULL); /* we've iterated all objects */
207 1.1 thorpej
208 1.1 thorpej po = pa->pa_array[pai->pai_index];
209 1.1 thorpej pai->pai_index++;
210 1.1 thorpej
211 1.1 thorpej return (po);
212 1.1 thorpej }
213 1.1 thorpej
214 1.1 thorpej static void
215 1.1 thorpej _prop_array_iterator_reset(void *v)
216 1.1 thorpej {
217 1.1 thorpej struct _prop_array_iterator *pai = v;
218 1.1 thorpej prop_array_t pa = pai->pai_base.pi_obj;
219 1.1 thorpej
220 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
221 1.1 thorpej
222 1.1 thorpej pai->pai_index = 0;
223 1.1 thorpej pai->pai_base.pi_version = pa->pa_version;
224 1.1 thorpej }
225 1.1 thorpej
226 1.1 thorpej /*
227 1.1 thorpej * prop_array_create --
228 1.1 thorpej * Create an empty array.
229 1.1 thorpej */
230 1.1 thorpej prop_array_t
231 1.1 thorpej prop_array_create(void)
232 1.1 thorpej {
233 1.1 thorpej
234 1.1 thorpej return (_prop_array_alloc(0));
235 1.1 thorpej }
236 1.1 thorpej
237 1.1 thorpej /*
238 1.1 thorpej * prop_array_create_with_capacity --
239 1.1 thorpej * Create an array with the capacity to store N objects.
240 1.1 thorpej */
241 1.1 thorpej prop_array_t
242 1.1 thorpej prop_array_create_with_capacity(unsigned int capacity)
243 1.1 thorpej {
244 1.1 thorpej
245 1.1 thorpej return (_prop_array_alloc(capacity));
246 1.1 thorpej }
247 1.1 thorpej
248 1.1 thorpej /*
249 1.1 thorpej * prop_array_copy --
250 1.1 thorpej * Copy an array. The new array has an initial capacity equal to
251 1.1 thorpej * the number of objects stored in the original array. The new
252 1.1 thorpej * array contains references to the original array's objects, not
253 1.1 thorpej * copies of those objects (i.e. a shallow copy).
254 1.1 thorpej */
255 1.1 thorpej prop_array_t
256 1.1 thorpej prop_array_copy(prop_array_t opa)
257 1.1 thorpej {
258 1.1 thorpej prop_array_t pa;
259 1.1 thorpej prop_object_t po;
260 1.1 thorpej unsigned int idx;
261 1.1 thorpej
262 1.1 thorpej _PROP_ASSERT(prop_object_is_array(opa));
263 1.1 thorpej
264 1.1 thorpej pa = _prop_array_alloc(opa->pa_count);
265 1.1 thorpej if (pa != NULL) {
266 1.1 thorpej for (idx = 0; idx < opa->pa_count; idx++) {
267 1.1 thorpej po = opa->pa_array[idx];
268 1.1 thorpej prop_object_retain(po);
269 1.1 thorpej pa->pa_array[idx] = po;
270 1.1 thorpej }
271 1.1 thorpej pa->pa_count = opa->pa_count;
272 1.1 thorpej pa->pa_flags = opa->pa_flags;
273 1.1 thorpej }
274 1.1 thorpej return (pa);
275 1.1 thorpej }
276 1.1 thorpej
277 1.1 thorpej /*
278 1.1 thorpej * prop_array_copy_mutable --
279 1.1 thorpej * Like prop_array_copy(), but the resulting array is mutable.
280 1.1 thorpej */
281 1.1 thorpej prop_array_t
282 1.1 thorpej prop_array_copy_mutable(prop_array_t opa)
283 1.1 thorpej {
284 1.1 thorpej prop_array_t pa;
285 1.1 thorpej
286 1.1 thorpej pa = prop_array_copy(opa);
287 1.1 thorpej if (pa != NULL)
288 1.1 thorpej pa->pa_flags &= ~PA_F_IMMUTABLE;
289 1.1 thorpej
290 1.1 thorpej return (pa);
291 1.1 thorpej }
292 1.1 thorpej
293 1.1 thorpej /*
294 1.1 thorpej * prop_array_capacity --
295 1.1 thorpej * Return the capacity of the array.
296 1.1 thorpej */
297 1.1 thorpej unsigned int
298 1.1 thorpej prop_array_capacity(prop_array_t pa)
299 1.1 thorpej {
300 1.1 thorpej
301 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
302 1.1 thorpej return (pa->pa_capacity);
303 1.1 thorpej }
304 1.1 thorpej
305 1.1 thorpej /*
306 1.1 thorpej * prop_array_count --
307 1.1 thorpej * Return the number of objects stored in the array.
308 1.1 thorpej */
309 1.1 thorpej unsigned int
310 1.1 thorpej prop_array_count(prop_array_t pa)
311 1.1 thorpej {
312 1.1 thorpej
313 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
314 1.1 thorpej return (pa->pa_count);
315 1.1 thorpej }
316 1.1 thorpej
317 1.1 thorpej /*
318 1.1 thorpej * prop_array_ensure_capacity --
319 1.1 thorpej * Ensure that the array has the capacity to store the specified
320 1.1 thorpej * total number of objects (inluding the objects already stored
321 1.1 thorpej * in the array).
322 1.1 thorpej */
323 1.1 thorpej boolean_t
324 1.1 thorpej prop_array_ensure_capacity(prop_array_t pa, unsigned int capacity)
325 1.1 thorpej {
326 1.1 thorpej
327 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
328 1.1 thorpej if (capacity > pa->pa_capacity)
329 1.1 thorpej return (_prop_array_expand(pa, capacity - pa->pa_capacity));
330 1.1 thorpej return (TRUE);
331 1.1 thorpej }
332 1.1 thorpej
333 1.1 thorpej /*
334 1.1 thorpej * prop_array_iterator --
335 1.1 thorpej * Return an iterator for the array. The array is retained by
336 1.1 thorpej * the iterator.
337 1.1 thorpej */
338 1.1 thorpej prop_object_iterator_t
339 1.1 thorpej prop_array_iterator(prop_array_t pa)
340 1.1 thorpej {
341 1.1 thorpej struct _prop_array_iterator *pai;
342 1.1 thorpej
343 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
344 1.1 thorpej
345 1.1 thorpej pai = _PROP_CALLOC(sizeof(*pai), M_TEMP);
346 1.1 thorpej if (pai == NULL)
347 1.1 thorpej return (NULL);
348 1.1 thorpej pai->pai_base.pi_next_object = _prop_array_iterator_next_object;
349 1.1 thorpej pai->pai_base.pi_reset = _prop_array_iterator_reset;
350 1.1 thorpej prop_object_retain(pa);
351 1.1 thorpej pai->pai_base.pi_obj = pa;
352 1.1 thorpej pai->pai_base.pi_version = pa->pa_version;
353 1.1 thorpej
354 1.1 thorpej _prop_array_iterator_reset(pai);
355 1.1 thorpej
356 1.1 thorpej return (&pai->pai_base);
357 1.1 thorpej }
358 1.1 thorpej
359 1.1 thorpej /*
360 1.1 thorpej * prop_array_make_immutable --
361 1.1 thorpej * Make the array immutable.
362 1.1 thorpej */
363 1.1 thorpej void
364 1.1 thorpej prop_array_make_immutable(prop_array_t pa)
365 1.1 thorpej {
366 1.1 thorpej
367 1.1 thorpej if (prop_array_is_immutable(pa) == FALSE)
368 1.1 thorpej pa->pa_flags |= PA_F_IMMUTABLE;
369 1.1 thorpej }
370 1.1 thorpej
371 1.1 thorpej /*
372 1.1 thorpej * prop_array_mutable --
373 1.1 thorpej * Returns TRUE if the array is mutable.
374 1.1 thorpej */
375 1.1 thorpej boolean_t
376 1.1 thorpej prop_array_mutable(prop_array_t pa)
377 1.1 thorpej {
378 1.1 thorpej
379 1.1 thorpej return (prop_array_is_immutable(pa) == FALSE);
380 1.1 thorpej }
381 1.1 thorpej
382 1.1 thorpej /*
383 1.1 thorpej * prop_array_get --
384 1.1 thorpej * Return the object stored at the specified array index.
385 1.1 thorpej */
386 1.1 thorpej prop_object_t
387 1.1 thorpej prop_array_get(prop_array_t pa, unsigned int idx)
388 1.1 thorpej {
389 1.1 thorpej prop_object_t po;
390 1.1 thorpej
391 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
392 1.1 thorpej if (idx >= pa->pa_count)
393 1.1 thorpej return (NULL);
394 1.1 thorpej po = pa->pa_array[idx];
395 1.1 thorpej _PROP_ASSERT(po != NULL);
396 1.1 thorpej return (po);
397 1.1 thorpej }
398 1.1 thorpej
399 1.1 thorpej /*
400 1.1 thorpej * prop_array_set --
401 1.1 thorpej * Store a reference to an object at the specified array index.
402 1.1 thorpej * This method is not allowed to create holes in the array; the
403 1.1 thorpej * caller must either be setting the object just beyond the existing
404 1.1 thorpej * count or replacing an already existing object reference.
405 1.1 thorpej */
406 1.1 thorpej boolean_t
407 1.1 thorpej prop_array_set(prop_array_t pa, unsigned int idx, prop_object_t po)
408 1.1 thorpej {
409 1.1 thorpej prop_object_t opo;
410 1.1 thorpej
411 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
412 1.1 thorpej
413 1.1 thorpej if (prop_array_is_immutable(pa))
414 1.1 thorpej return (FALSE);
415 1.1 thorpej
416 1.1 thorpej if (idx == pa->pa_count)
417 1.1 thorpej return (prop_array_add(pa, po));
418 1.1 thorpej
419 1.1 thorpej _PROP_ASSERT(idx < pa->pa_count);
420 1.1 thorpej
421 1.1 thorpej opo = pa->pa_array[idx];
422 1.1 thorpej _PROP_ASSERT(opo != NULL);
423 1.1 thorpej
424 1.1 thorpej prop_object_retain(po);
425 1.1 thorpej pa->pa_array[idx] = po;
426 1.1 thorpej pa->pa_version++;
427 1.1 thorpej
428 1.1 thorpej prop_object_release(opo);
429 1.1 thorpej
430 1.1 thorpej return (TRUE);
431 1.1 thorpej }
432 1.1 thorpej
433 1.1 thorpej /*
434 1.1 thorpej * prop_array_add --
435 1.1 thorpej * Add a refrerence to an object to the specified array, appending
436 1.1 thorpej * to the end and growing the array's capacity, if necessary.
437 1.1 thorpej */
438 1.1 thorpej boolean_t
439 1.1 thorpej prop_array_add(prop_array_t pa, prop_object_t po)
440 1.1 thorpej {
441 1.1 thorpej
442 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
443 1.1 thorpej _PROP_ASSERT(pa->pa_count <= pa->pa_capacity);
444 1.1 thorpej
445 1.1 thorpej if (prop_array_is_immutable(pa) ||
446 1.1 thorpej (pa->pa_count == pa->pa_capacity &&
447 1.1 thorpej _prop_array_expand(pa, pa->pa_capacity + EXPAND_STEP) == FALSE))
448 1.1 thorpej return (FALSE);
449 1.1 thorpej
450 1.1 thorpej prop_object_retain(po);
451 1.1 thorpej pa->pa_array[pa->pa_count++] = po;
452 1.1 thorpej pa->pa_version++;
453 1.1 thorpej
454 1.1 thorpej return (TRUE);
455 1.1 thorpej }
456 1.1 thorpej
457 1.1 thorpej /*
458 1.1 thorpej * prop_array_remove --
459 1.1 thorpej * Remove the reference to an object from an array at the specified
460 1.1 thorpej * index. The array will be compacted following the removal.
461 1.1 thorpej */
462 1.1 thorpej void
463 1.1 thorpej prop_array_remove(prop_array_t pa, unsigned int idx)
464 1.1 thorpej {
465 1.1 thorpej prop_object_t po;
466 1.1 thorpej
467 1.1 thorpej _PROP_ASSERT(prop_object_is_array(pa));
468 1.1 thorpej _PROP_ASSERT(idx < pa->pa_count);
469 1.1 thorpej
470 1.1 thorpej /* XXX Should this be a _PROP_ASSERT()? */
471 1.1 thorpej if (prop_array_is_immutable(pa))
472 1.1 thorpej return;
473 1.1 thorpej
474 1.1 thorpej po = pa->pa_array[idx];
475 1.1 thorpej _PROP_ASSERT(po != NULL);
476 1.1 thorpej
477 1.1 thorpej for (++idx; idx < pa->pa_count; idx++)
478 1.1 thorpej pa->pa_array[idx - 1] = pa->pa_array[idx];
479 1.1 thorpej pa->pa_count--;
480 1.1 thorpej pa->pa_version++;
481 1.1 thorpej
482 1.1 thorpej prop_object_release(po);
483 1.1 thorpej }
484 1.1 thorpej
485 1.1 thorpej /*
486 1.1 thorpej * _prop_array_internalize --
487 1.1 thorpej * Parse an <array>...</array> and return the object created from the
488 1.1 thorpej * external representation.
489 1.1 thorpej */
490 1.1 thorpej prop_object_t
491 1.1 thorpej _prop_array_internalize(struct _prop_object_internalize_context *ctx)
492 1.1 thorpej {
493 1.1 thorpej prop_array_t array;
494 1.1 thorpej prop_object_t obj;
495 1.1 thorpej
496 1.1 thorpej /* We don't currently understand any attributes. */
497 1.1 thorpej if (ctx->poic_tagattr != NULL)
498 1.1 thorpej return (NULL);
499 1.1 thorpej
500 1.1 thorpej array = prop_array_create();
501 1.1 thorpej if (array == NULL)
502 1.1 thorpej return (NULL);
503 1.1 thorpej
504 1.1 thorpej if (ctx->poic_is_empty_element)
505 1.1 thorpej return (array);
506 1.1 thorpej
507 1.1 thorpej for (;;) {
508 1.1 thorpej /* Fetch the next tag. */
509 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, NULL,
510 1.1 thorpej _PROP_TAG_TYPE_EITHER) == FALSE)
511 1.1 thorpej goto bad;
512 1.1 thorpej
513 1.1 thorpej /* Check to see if this is the end of the array. */
514 1.1 thorpej if (_PROP_TAG_MATCH(ctx, "array") &&
515 1.1 thorpej ctx->poic_tag_type == _PROP_TAG_TYPE_END)
516 1.1 thorpej break;
517 1.1 thorpej
518 1.1 thorpej /* Fetch the object. */
519 1.1 thorpej obj = _prop_object_internalize_by_tag(ctx);
520 1.1 thorpej if (obj == NULL)
521 1.1 thorpej goto bad;
522 1.1 thorpej
523 1.1 thorpej if (prop_array_add(array, obj) == FALSE) {
524 1.1 thorpej prop_object_release(obj);
525 1.1 thorpej goto bad;
526 1.1 thorpej }
527 1.1 thorpej prop_object_release(obj);
528 1.1 thorpej }
529 1.1 thorpej
530 1.1 thorpej return (array);
531 1.1 thorpej
532 1.1 thorpej bad:
533 1.1 thorpej prop_object_release(array);
534 1.1 thorpej return (NULL);
535 1.1 thorpej }
536