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;
60 cl_ptr_vector_init(IN cl_ptr_vector_t * const p_vector,
61 IN const size_t min_size, 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;
105 cl_ptr_vector_at(IN const cl_ptr_vector_t * const p_vector,
106 IN const size_t index, OUT void **const p_element)
109 CL_ASSERT(p_vector->state == CL_INITIALIZED);
112 if (index >= p_vector->size)
113 return (CL_INVALID_PARAMETER);
115 *p_element = cl_ptr_vector_get(p_vector, index);
120 cl_ptr_vector_set(IN cl_ptr_vector_t * const p_vector,
121 IN const size_t index, IN const void *const element)
126 CL_ASSERT(p_vector->state == CL_INITIALIZED);
128 /* Determine if the vector has room for this element. */
129 if (index >= p_vector->size) {
130 /* Resize to accomodate the given index. */
131 status = cl_ptr_vector_set_size(p_vector, index + 1);
133 /* Check for failure on or before the given index. */
134 if ((status != CL_SUCCESS) && (p_vector->size < index))
138 /* At this point, the array is guaranteed to be big enough */
139 p_vector->p_ptr_array[index] = element;
144 void *cl_ptr_vector_remove(IN cl_ptr_vector_t * const p_vector,
145 IN const size_t index)
151 CL_ASSERT(p_vector->state == CL_INITIALIZED);
152 CL_ASSERT(p_vector->size > index);
154 /* Store a copy of the element to return. */
155 element = p_vector->p_ptr_array[index];
156 /* Shift all items above the removed item down. */
157 if (index < --p_vector->size) {
158 for (src = index; src < p_vector->size; src++)
159 p_vector->p_ptr_array[src] =
160 p_vector->p_ptr_array[src + 1];
162 /* Clear the entry for the element just outside of the new upper bound. */
163 p_vector->p_ptr_array[p_vector->size] = NULL;
165 return ((void *)element);
169 cl_ptr_vector_set_capacity(IN cl_ptr_vector_t * const p_vector,
170 IN const size_t new_capacity)
172 void *p_new_ptr_array;
175 CL_ASSERT(p_vector->state == CL_INITIALIZED);
177 /* Do we have to do anything here? */
178 if (new_capacity <= p_vector->capacity) {
183 /* Allocate our pointer array. */
184 p_new_ptr_array = malloc(new_capacity * sizeof(void *));
185 if (!p_new_ptr_array)
186 return (CL_INSUFFICIENT_MEMORY);
188 memset(p_new_ptr_array, 0, new_capacity * sizeof(void *));
190 if (p_vector->p_ptr_array) {
191 /* Copy the old pointer array into the new. */
192 memcpy(p_new_ptr_array, p_vector->p_ptr_array,
193 p_vector->capacity * sizeof(void *));
195 /* Free the old pointer array. */
196 free((void *)p_vector->p_ptr_array);
199 /* Set the new array. */
200 p_vector->p_ptr_array = p_new_ptr_array;
202 /* Update the vector with the new capactity. */
203 p_vector->capacity = new_capacity;
209 cl_ptr_vector_set_size(IN cl_ptr_vector_t * const p_vector,
210 IN const size_t size)
216 CL_ASSERT(p_vector->state == CL_INITIALIZED);
218 /* Check to see if the requested size is the same as the existing size. */
219 if (size == p_vector->size)
222 /* Determine if the vector has room for this element. */
223 if (size >= p_vector->capacity) {
224 if (!p_vector->grow_size)
225 return (CL_INSUFFICIENT_MEMORY);
227 /* Calculate the new capacity, taking into account the grow size. */
229 if (size % p_vector->grow_size) {
230 /* Round up to nearest grow_size boundary. */
231 new_capacity += p_vector->grow_size -
232 (size % p_vector->grow_size);
235 status = cl_ptr_vector_set_capacity(p_vector, new_capacity);
236 if (status != CL_SUCCESS)
240 p_vector->size = size;
245 cl_ptr_vector_set_min_size(IN cl_ptr_vector_t * const p_vector,
246 IN const size_t min_size)
249 CL_ASSERT(p_vector->state == CL_INITIALIZED);
251 if (min_size > p_vector->size) {
252 /* We have to resize the array */
253 return (cl_ptr_vector_set_size(p_vector, min_size));
256 /* We didn't have to do anything */
261 cl_ptr_vector_apply_func(IN const cl_ptr_vector_t * const p_vector,
262 IN cl_pfn_ptr_vec_apply_t pfn_callback,
263 IN const void *const context)
268 CL_ASSERT(p_vector->state == CL_INITIALIZED);
269 CL_ASSERT(pfn_callback);
271 for (i = 0; i < p_vector->size; i++)
272 pfn_callback(i, (void *)p_vector->p_ptr_array[i],
277 cl_ptr_vector_find_from_start(IN const cl_ptr_vector_t * const p_vector,
278 IN cl_pfn_ptr_vec_find_t pfn_callback,
279 IN const void *const context)
284 CL_ASSERT(p_vector->state == CL_INITIALIZED);
285 CL_ASSERT(pfn_callback);
287 for (i = 0; i < p_vector->size; i++) {
288 /* Invoke the callback */
289 if (pfn_callback(i, (void *)p_vector->p_ptr_array[i],
290 (void *)context) == CL_SUCCESS) {
298 cl_ptr_vector_find_from_end(IN const cl_ptr_vector_t * const p_vector,
299 IN cl_pfn_ptr_vec_find_t pfn_callback,
300 IN const void *const context)
305 CL_ASSERT(p_vector->state == CL_INITIALIZED);
306 CL_ASSERT(pfn_callback);
311 /* Invoke the callback for the current element. */
312 if (pfn_callback(i, (void *)p_vector->p_ptr_array[--i],
313 (void *)context) == CL_SUCCESS) {
318 return (p_vector->size);