/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* prop_array_create_with_capacity --
* Create an array with the capacity to store N objects.
*/
prop_array_t
prop_array_create_with_capacity(unsigned int capacity)
{
return (_prop_array_alloc(capacity));
}
/*
* prop_array_copy --
* Copy an array. The new array has an initial capacity equal to
* the number of objects stored in the original array. The new
* array contains references to the original array's objects, not
* copies of those objects (i.e. a shallow copy).
*/
prop_array_t
prop_array_copy(prop_array_t opa)
{
prop_array_t pa;
prop_object_t po;
unsigned int idx;
if (! prop_object_is_array(opa))
return (NULL);
_PROP_RWLOCK_RDLOCK(opa->pa_rwlock);
pa = _prop_array_alloc(opa->pa_count);
if (pa != NULL) {
for (idx = 0; idx < opa->pa_count; idx++) {
po = opa->pa_array[idx];
prop_object_retain(po);
pa->pa_array[idx] = po;
}
pa->pa_count = opa->pa_count;
pa->pa_flags = opa->pa_flags;
}
_PROP_RWLOCK_UNLOCK(opa->pa_rwlock);
return (pa);
}
/*
* prop_array_copy_mutable --
* Like prop_array_copy(), but the resulting array is mutable.
*/
prop_array_t
prop_array_copy_mutable(prop_array_t opa)
{
prop_array_t pa;
pa = prop_array_copy(opa);
if (pa != NULL)
pa->pa_flags &= ~PA_F_IMMUTABLE;
return (pa);
}
/*
* prop_array_capacity --
* Return the capacity of the array.
*/
unsigned int
prop_array_capacity(prop_array_t pa)
{
unsigned int rv;
/*
* prop_array_ensure_capacity --
* Ensure that the array has the capacity to store the specified
* total number of objects (inluding the objects already stored
* in the array).
*/
boolean_t
prop_array_ensure_capacity(prop_array_t pa, unsigned int capacity)
{
boolean_t rv;
/*
* prop_array_iterator --
* Return an iterator for the array. The array is retained by
* the iterator.
*/
prop_object_iterator_t
prop_array_iterator(prop_array_t pa)
{
struct _prop_array_iterator *pai;
/*
* prop_array_set --
* Store a reference to an object at the specified array index.
* This method is not allowed to create holes in the array; the
* caller must either be setting the object just beyond the existing
* count or replacing an already existing object reference.
*/
boolean_t
prop_array_set(prop_array_t pa, unsigned int idx, prop_object_t po)
{
prop_object_t opo;
boolean_t rv = FALSE;
/*
* prop_array_add --
* Add a refrerence to an object to the specified array, appending
* to the end and growing the array's capacity, if necessary.
*/
boolean_t
prop_array_add(prop_array_t pa, prop_object_t po)
{
boolean_t rv;
/*
* prop_array_remove --
* Remove the reference to an object from an array at the specified
* index. The array will be compacted following the removal.
*/
void
prop_array_remove(prop_array_t pa, unsigned int idx)
{
prop_object_t po;
if (! prop_object_is_array(pa))
return;
_PROP_RWLOCK_WRLOCK(pa->pa_rwlock);
_PROP_ASSERT(idx < pa->pa_count);
/* XXX Should this be a _PROP_ASSERT()? */
if (prop_array_is_immutable(pa)) {
_PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
return;
}
/*
* prop_array_equals --
* Return TRUE if the two arrays are equivalent. Note we do a
* by-value comparison of the objects in the array.
*/
boolean_t
prop_array_equals(prop_array_t array1, prop_array_t array2)
{
return (_prop_array_equals(array1, array2));
}
#if !defined(_KERNEL) && !defined(_STANDALONE)
/*
* prop_array_externalize_to_file --
* Externalize an array to the specified file.
*/
boolean_t
prop_array_externalize_to_file(prop_array_t array, const char *fname)
{
char *xml;
boolean_t rv;
int save_errno = 0; /* XXXGCC -Wuninitialized [mips, ...] */
xml = prop_array_externalize(array);
if (xml == NULL)
return (FALSE);
rv = _prop_object_externalize_write_file(fname, xml, strlen(xml));
if (rv == FALSE)
save_errno = errno;
_PROP_FREE(xml, M_TEMP);
if (rv == FALSE)
errno = save_errno;
return (rv);
}
/*
* prop_array_internalize_from_file --
* Internalize an array from a file.
*/
prop_array_t
prop_array_internalize_from_file(const char *fname)
{
struct _prop_object_internalize_mapped_file *mf;
prop_array_t array;