2 * Copyright (c) 2004-2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 * This file contains ivector and isvector implementations.
44 #endif /* HAVE_CONFIG_H */
48 #include <complib/cl_ptr_vector.h>
50 void cl_ptr_vector_construct(IN cl_ptr_vector_t * const p_vector)
54 memset(p_vector, 0, sizeof(cl_ptr_vector_t));
56 p_vector->state = CL_UNINITIALIZED;
59 cl_status_t cl_ptr_vector_init(IN cl_ptr_vector_t * const p_vector,
60 IN const size_t min_size,
61 IN const size_t grow_size)
63 cl_status_t status = CL_SUCCESS;
67 cl_ptr_vector_construct(p_vector);
69 p_vector->grow_size = grow_size;
72 * Set the state to initialized so that the call to set_size
75 p_vector->state = CL_INITIALIZED;
77 /* get the storage needed by the user */
79 status = cl_ptr_vector_set_size(p_vector, min_size);
80 if (status != CL_SUCCESS)
81 cl_ptr_vector_destroy(p_vector);
87 void cl_ptr_vector_destroy(IN cl_ptr_vector_t * const p_vector)
90 CL_ASSERT(cl_is_state_valid(p_vector->state));
92 /* Call the user's destructor for each element in the array. */
93 if (p_vector->state == CL_INITIALIZED) {
94 /* Destroy the page vector. */
95 if (p_vector->p_ptr_array) {
96 free((void *)p_vector->p_ptr_array);
97 p_vector->p_ptr_array = NULL;
101 p_vector->state = CL_UNINITIALIZED;
104 cl_status_t cl_ptr_vector_at(IN const cl_ptr_vector_t * const p_vector,
105 IN const size_t index, OUT void **const p_element)
108 CL_ASSERT(p_vector->state == CL_INITIALIZED);
111 if (index >= p_vector->size)
112 return (CL_INVALID_PARAMETER);
114 *p_element = cl_ptr_vector_get(p_vector, index);
118 cl_status_t cl_ptr_vector_set(IN cl_ptr_vector_t * const p_vector,
119 IN const size_t index,
120 IN const void *const element)
125 CL_ASSERT(p_vector->state == CL_INITIALIZED);
127 /* Determine if the vector has room for this element. */
128 if (index >= p_vector->size) {
129 /* Resize to accomodate the given index. */
130 status = cl_ptr_vector_set_size(p_vector, index + 1);
132 /* Check for failure on or before the given index. */
133 if ((status != CL_SUCCESS) && (p_vector->size < index))
137 /* At this point, the array is guaranteed to be big enough */
138 p_vector->p_ptr_array[index] = element;
143 void *cl_ptr_vector_remove(IN cl_ptr_vector_t * const p_vector,
144 IN const size_t index)
150 CL_ASSERT(p_vector->state == CL_INITIALIZED);
151 CL_ASSERT(p_vector->size > index);
153 /* Store a copy of the element to return. */
154 element = p_vector->p_ptr_array[index];
155 /* Shift all items above the removed item down. */
156 if (index < --p_vector->size) {
157 for (src = index; src < p_vector->size; src++)
158 p_vector->p_ptr_array[src] =
159 p_vector->p_ptr_array[src + 1];
161 /* Clear the entry for the element just outside of the new upper bound. */
162 p_vector->p_ptr_array[p_vector->size] = NULL;
164 return ((void *)element);
167 cl_status_t cl_ptr_vector_set_capacity(IN cl_ptr_vector_t * const p_vector,
168 IN const size_t new_capacity)
170 void *p_new_ptr_array;
173 CL_ASSERT(p_vector->state == CL_INITIALIZED);
175 /* Do we have to do anything here? */
176 if (new_capacity <= p_vector->capacity) {
181 /* Allocate our pointer array. */
182 p_new_ptr_array = malloc(new_capacity * sizeof(void *));
183 if (!p_new_ptr_array)
184 return (CL_INSUFFICIENT_MEMORY);
186 memset(p_new_ptr_array, 0, new_capacity * sizeof(void *));
188 if (p_vector->p_ptr_array) {
189 /* Copy the old pointer array into the new. */
190 memcpy(p_new_ptr_array, p_vector->p_ptr_array,
191 p_vector->capacity * sizeof(void *));
193 /* Free the old pointer array. */
194 free((void *)p_vector->p_ptr_array);
197 /* Set the new array. */
198 p_vector->p_ptr_array = p_new_ptr_array;
200 /* Update the vector with the new capactity. */
201 p_vector->capacity = new_capacity;
206 cl_status_t cl_ptr_vector_set_size(IN cl_ptr_vector_t * const p_vector,
207 IN const size_t size)
213 CL_ASSERT(p_vector->state == CL_INITIALIZED);
215 /* Check to see if the requested size is the same as the existing size. */
216 if (size == p_vector->size)
219 /* Determine if the vector has room for this element. */
220 if (size >= p_vector->capacity) {
221 if (!p_vector->grow_size)
222 return (CL_INSUFFICIENT_MEMORY);
224 /* Calculate the new capacity, taking into account the grow size. */
226 if (size % p_vector->grow_size) {
227 /* Round up to nearest grow_size boundary. */
228 new_capacity += p_vector->grow_size -
229 (size % p_vector->grow_size);
232 status = cl_ptr_vector_set_capacity(p_vector, new_capacity);
233 if (status != CL_SUCCESS)
237 p_vector->size = size;
241 cl_status_t cl_ptr_vector_set_min_size(IN cl_ptr_vector_t * const p_vector,
242 IN const size_t min_size)
245 CL_ASSERT(p_vector->state == CL_INITIALIZED);
247 if (min_size > p_vector->size) {
248 /* We have to resize the array */
249 return (cl_ptr_vector_set_size(p_vector, min_size));
252 /* We didn't have to do anything */
256 void cl_ptr_vector_apply_func(IN const cl_ptr_vector_t * const p_vector,
257 IN cl_pfn_ptr_vec_apply_t pfn_callback,
258 IN const void *const context)
263 CL_ASSERT(p_vector->state == CL_INITIALIZED);
264 CL_ASSERT(pfn_callback);
266 for (i = 0; i < p_vector->size; i++)
267 pfn_callback(i, (void *)p_vector->p_ptr_array[i],
271 size_t cl_ptr_vector_find_from_start(IN const cl_ptr_vector_t * const p_vector,
272 IN cl_pfn_ptr_vec_find_t pfn_callback,
273 IN const void *const context)
278 CL_ASSERT(p_vector->state == CL_INITIALIZED);
279 CL_ASSERT(pfn_callback);
281 for (i = 0; i < p_vector->size; i++) {
282 /* Invoke the callback */
283 if (pfn_callback(i, (void *)p_vector->p_ptr_array[i],
284 (void *)context) == CL_SUCCESS) {
291 size_t cl_ptr_vector_find_from_end(IN const cl_ptr_vector_t * const p_vector,
292 IN cl_pfn_ptr_vec_find_t pfn_callback,
293 IN const void *const context)
298 CL_ASSERT(p_vector->state == CL_INITIALIZED);
299 CL_ASSERT(pfn_callback);
304 /* Invoke the callback for the current element. */
306 if (pfn_callback(i, (void *)p_vector->p_ptr_array[i],
307 (void *)context) == CL_SUCCESS) {
312 return (p_vector->size);