3 Subroutines that support the omapi extensible array type. */
6 * Copyright (c) 2001 Internet Software Consortium.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
38 * To learn more about the Internet Software Consortium, see
39 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
40 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
41 * ``http://www.nominum.com''.
44 #include <omapip/omapip_p.h>
46 /* Allocate a new extensible array. */
48 isc_result_t omapi_array_allocate (omapi_array_t **array,
49 omapi_array_ref_t ref,
50 omapi_array_deref_t deref,
51 const char *file, int line)
57 return ISC_R_INVALIDARG;
58 aptr = dmalloc (sizeof (omapi_array_t),file, line);
60 return ISC_R_NOMEMORY;
63 aptr -> deref = deref;
67 isc_result_t omapi_array_free (omapi_array_t **array,
68 const char *file, int line)
74 if (!array || !*array)
75 return ISC_R_INVALIDARG;
77 for (i = 0; i < aptr -> count; i++)
78 if (aptr -> data [i] && aptr -> deref)
79 (*aptr -> deref) (&aptr -> data [i], file, line);
80 dfree (aptr -> data, MDL);
82 *array = (omapi_array_t *)0;
86 /* Extend the size of the array by one entry (we may allocate more than that)
87 and store the specified value in the new array element. */
89 isc_result_t omapi_array_extend (omapi_array_t *array, char *ptr,
90 int *index, const char *file, int line)
93 int new = array -> count;
94 status = omapi_array_set (array, ptr, new, file, line);
95 if (index && status == ISC_R_SUCCESS)
100 /* Set a value in the specified array, extending it if necessary. */
102 isc_result_t omapi_array_set (omapi_array_t *array, void *ptr, int index,
103 const char *file, int line)
110 return ISC_R_INVALIDARG;
112 return ISC_R_INVALIDARG;
114 return ISC_R_INVALIDARG;
116 /* If the proposed index is larger than the current available
117 space in the array, make more space in the array. */
118 if (array -> max <= index) {
119 delta = index - array -> max + 10;
120 newbuf = dmalloc ((array -> max + delta) * sizeof (char *),
123 return ISC_R_NOMEMORY;
124 /* Zero the new elements. */
125 memset (&newbuf [array -> max], 0, (sizeof (char *)) * delta);
126 array -> max += delta;
127 /* Copy the old array data into the new buffer. */
130 array -> data, array -> count * sizeof (char *));
131 dfree (array -> data, file, line);
133 array -> data = newbuf;
135 /* If there's already data there, and this is an array
136 of references, dereference what's there. */
137 if (array -> data [index]) {
138 status = ((*array -> deref) (&array -> data [index],
141 if (status != ISC_R_SUCCESS)
146 /* Store the pointer using the referencer function. We have
147 either just memset this to zero or dereferenced what was
148 there previously, so there is no need to do anything if the
149 pointer we have been asked to store is null. */
151 status = (*array -> ref) (&array -> data [index], ptr,
153 if (status != ISC_R_SUCCESS)
156 if (index >= array -> count)
157 array -> count = index + 1;
158 return ISC_R_SUCCESS;
161 isc_result_t omapi_array_lookup (char **ptr, omapi_array_t *array, int index,
162 const char *file, int line)
164 if (!array || !ptr || *ptr || index < 0 || index >= array -> count)
165 return ISC_R_INVALIDARG;
166 if (array -> data [index])
167 return (*array -> ref) (ptr,
168 array -> data [index], file, line);
169 return ISC_R_NOTFOUND;
172 OMAPI_ARRAY_TYPE_DECL(omapi_object, omapi_object_t);