xref: /freebsd/contrib/ofed/opensm/complib/cl_ptr_vector.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky  * Copyright (c) 2004-2006 Voltaire, Inc. All rights reserved.
3*d6b92ffaSHans Petter Selasky  * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4*d6b92ffaSHans Petter Selasky  * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5*d6b92ffaSHans Petter Selasky  *
6*d6b92ffaSHans Petter Selasky  * This software is available to you under a choice of one of two
7*d6b92ffaSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
8*d6b92ffaSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
9*d6b92ffaSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
10*d6b92ffaSHans Petter Selasky  * OpenIB.org BSD license below:
11*d6b92ffaSHans Petter Selasky  *
12*d6b92ffaSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
13*d6b92ffaSHans Petter Selasky  *     without modification, are permitted provided that the following
14*d6b92ffaSHans Petter Selasky  *     conditions are met:
15*d6b92ffaSHans Petter Selasky  *
16*d6b92ffaSHans Petter Selasky  *      - Redistributions of source code must retain the above
17*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
18*d6b92ffaSHans Petter Selasky  *        disclaimer.
19*d6b92ffaSHans Petter Selasky  *
20*d6b92ffaSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
21*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
22*d6b92ffaSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
23*d6b92ffaSHans Petter Selasky  *        provided with the distribution.
24*d6b92ffaSHans Petter Selasky  *
25*d6b92ffaSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26*d6b92ffaSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27*d6b92ffaSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28*d6b92ffaSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29*d6b92ffaSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30*d6b92ffaSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31*d6b92ffaSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32*d6b92ffaSHans Petter Selasky  * SOFTWARE.
33*d6b92ffaSHans Petter Selasky  *
34*d6b92ffaSHans Petter Selasky  */
35*d6b92ffaSHans Petter Selasky 
36*d6b92ffaSHans Petter Selasky /*
37*d6b92ffaSHans Petter Selasky  * Abstract:
38*d6b92ffaSHans Petter Selasky  *	This file contains ivector and isvector implementations.
39*d6b92ffaSHans Petter Selasky  *
40*d6b92ffaSHans Petter Selasky  */
41*d6b92ffaSHans Petter Selasky 
42*d6b92ffaSHans Petter Selasky #if HAVE_CONFIG_H
43*d6b92ffaSHans Petter Selasky #  include <config.h>
44*d6b92ffaSHans Petter Selasky #endif				/* HAVE_CONFIG_H */
45*d6b92ffaSHans Petter Selasky 
46*d6b92ffaSHans Petter Selasky #include <stdlib.h>
47*d6b92ffaSHans Petter Selasky #include <string.h>
48*d6b92ffaSHans Petter Selasky #include <complib/cl_ptr_vector.h>
49*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_construct(IN cl_ptr_vector_t * const p_vector)50*d6b92ffaSHans Petter Selasky void cl_ptr_vector_construct(IN cl_ptr_vector_t * const p_vector)
51*d6b92ffaSHans Petter Selasky {
52*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
53*d6b92ffaSHans Petter Selasky 
54*d6b92ffaSHans Petter Selasky 	memset(p_vector, 0, sizeof(cl_ptr_vector_t));
55*d6b92ffaSHans Petter Selasky 
56*d6b92ffaSHans Petter Selasky 	p_vector->state = CL_UNINITIALIZED;
57*d6b92ffaSHans Petter Selasky }
58*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_init(IN cl_ptr_vector_t * const p_vector,IN const size_t min_size,IN const size_t grow_size)59*d6b92ffaSHans Petter Selasky cl_status_t cl_ptr_vector_init(IN cl_ptr_vector_t * const p_vector,
60*d6b92ffaSHans Petter Selasky 			       IN const size_t min_size,
61*d6b92ffaSHans Petter Selasky 			       IN const size_t grow_size)
62*d6b92ffaSHans Petter Selasky {
63*d6b92ffaSHans Petter Selasky 	cl_status_t status = CL_SUCCESS;
64*d6b92ffaSHans Petter Selasky 
65*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
66*d6b92ffaSHans Petter Selasky 
67*d6b92ffaSHans Petter Selasky 	cl_ptr_vector_construct(p_vector);
68*d6b92ffaSHans Petter Selasky 
69*d6b92ffaSHans Petter Selasky 	p_vector->grow_size = grow_size;
70*d6b92ffaSHans Petter Selasky 
71*d6b92ffaSHans Petter Selasky 	/*
72*d6b92ffaSHans Petter Selasky 	 * Set the state to initialized so that the call to set_size
73*d6b92ffaSHans Petter Selasky 	 * doesn't assert.
74*d6b92ffaSHans Petter Selasky 	 */
75*d6b92ffaSHans Petter Selasky 	p_vector->state = CL_INITIALIZED;
76*d6b92ffaSHans Petter Selasky 
77*d6b92ffaSHans Petter Selasky 	/* get the storage needed by the user */
78*d6b92ffaSHans Petter Selasky 	if (min_size) {
79*d6b92ffaSHans Petter Selasky 		status = cl_ptr_vector_set_size(p_vector, min_size);
80*d6b92ffaSHans Petter Selasky 		if (status != CL_SUCCESS)
81*d6b92ffaSHans Petter Selasky 			cl_ptr_vector_destroy(p_vector);
82*d6b92ffaSHans Petter Selasky 	}
83*d6b92ffaSHans Petter Selasky 
84*d6b92ffaSHans Petter Selasky 	return (status);
85*d6b92ffaSHans Petter Selasky }
86*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_destroy(IN cl_ptr_vector_t * const p_vector)87*d6b92ffaSHans Petter Selasky void cl_ptr_vector_destroy(IN cl_ptr_vector_t * const p_vector)
88*d6b92ffaSHans Petter Selasky {
89*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
90*d6b92ffaSHans Petter Selasky 	CL_ASSERT(cl_is_state_valid(p_vector->state));
91*d6b92ffaSHans Petter Selasky 
92*d6b92ffaSHans Petter Selasky 	/* Call the user's destructor for each element in the array. */
93*d6b92ffaSHans Petter Selasky 	if (p_vector->state == CL_INITIALIZED) {
94*d6b92ffaSHans Petter Selasky 		/* Destroy the page vector. */
95*d6b92ffaSHans Petter Selasky 		if (p_vector->p_ptr_array) {
96*d6b92ffaSHans Petter Selasky 			free((void *)p_vector->p_ptr_array);
97*d6b92ffaSHans Petter Selasky 			p_vector->p_ptr_array = NULL;
98*d6b92ffaSHans Petter Selasky 		}
99*d6b92ffaSHans Petter Selasky 	}
100*d6b92ffaSHans Petter Selasky 
101*d6b92ffaSHans Petter Selasky 	p_vector->state = CL_UNINITIALIZED;
102*d6b92ffaSHans Petter Selasky }
103*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_at(IN const cl_ptr_vector_t * const p_vector,IN const size_t index,OUT void ** const p_element)104*d6b92ffaSHans Petter Selasky cl_status_t cl_ptr_vector_at(IN const cl_ptr_vector_t * const p_vector,
105*d6b92ffaSHans Petter Selasky 			     IN const size_t index, OUT void **const p_element)
106*d6b92ffaSHans Petter Selasky {
107*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
108*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector->state == CL_INITIALIZED);
109*d6b92ffaSHans Petter Selasky 
110*d6b92ffaSHans Petter Selasky 	/* Range check */
111*d6b92ffaSHans Petter Selasky 	if (index >= p_vector->size)
112*d6b92ffaSHans Petter Selasky 		return (CL_INVALID_PARAMETER);
113*d6b92ffaSHans Petter Selasky 
114*d6b92ffaSHans Petter Selasky 	*p_element = cl_ptr_vector_get(p_vector, index);
115*d6b92ffaSHans Petter Selasky 	return (CL_SUCCESS);
116*d6b92ffaSHans Petter Selasky }
117*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_set(IN cl_ptr_vector_t * const p_vector,IN const size_t index,IN const void * const element)118*d6b92ffaSHans Petter Selasky cl_status_t cl_ptr_vector_set(IN cl_ptr_vector_t * const p_vector,
119*d6b92ffaSHans Petter Selasky 			      IN const size_t index,
120*d6b92ffaSHans Petter Selasky 			      IN const void *const element)
121*d6b92ffaSHans Petter Selasky {
122*d6b92ffaSHans Petter Selasky 	cl_status_t status;
123*d6b92ffaSHans Petter Selasky 
124*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
125*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector->state == CL_INITIALIZED);
126*d6b92ffaSHans Petter Selasky 
127*d6b92ffaSHans Petter Selasky 	/* Determine if the vector has room for this element. */
128*d6b92ffaSHans Petter Selasky 	if (index >= p_vector->size) {
129*d6b92ffaSHans Petter Selasky 		/* Resize to accomodate the given index. */
130*d6b92ffaSHans Petter Selasky 		status = cl_ptr_vector_set_size(p_vector, index + 1);
131*d6b92ffaSHans Petter Selasky 
132*d6b92ffaSHans Petter Selasky 		/* Check for failure on or before the given index. */
133*d6b92ffaSHans Petter Selasky 		if ((status != CL_SUCCESS) && (p_vector->size < index))
134*d6b92ffaSHans Petter Selasky 			return (status);
135*d6b92ffaSHans Petter Selasky 	}
136*d6b92ffaSHans Petter Selasky 
137*d6b92ffaSHans Petter Selasky 	/* At this point, the array is guaranteed to be big enough */
138*d6b92ffaSHans Petter Selasky 	p_vector->p_ptr_array[index] = element;
139*d6b92ffaSHans Petter Selasky 
140*d6b92ffaSHans Petter Selasky 	return (CL_SUCCESS);
141*d6b92ffaSHans Petter Selasky }
142*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_remove(IN cl_ptr_vector_t * const p_vector,IN const size_t index)143*d6b92ffaSHans Petter Selasky void *cl_ptr_vector_remove(IN cl_ptr_vector_t * const p_vector,
144*d6b92ffaSHans Petter Selasky 			   IN const size_t index)
145*d6b92ffaSHans Petter Selasky {
146*d6b92ffaSHans Petter Selasky 	size_t src;
147*d6b92ffaSHans Petter Selasky 	const void *element;
148*d6b92ffaSHans Petter Selasky 
149*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
150*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector->state == CL_INITIALIZED);
151*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector->size > index);
152*d6b92ffaSHans Petter Selasky 
153*d6b92ffaSHans Petter Selasky 	/* Store a copy of the element to return. */
154*d6b92ffaSHans Petter Selasky 	element = p_vector->p_ptr_array[index];
155*d6b92ffaSHans Petter Selasky 	/* Shift all items above the removed item down. */
156*d6b92ffaSHans Petter Selasky 	if (index < --p_vector->size) {
157*d6b92ffaSHans Petter Selasky 		for (src = index; src < p_vector->size; src++)
158*d6b92ffaSHans Petter Selasky 			p_vector->p_ptr_array[src] =
159*d6b92ffaSHans Petter Selasky 			    p_vector->p_ptr_array[src + 1];
160*d6b92ffaSHans Petter Selasky 	}
161*d6b92ffaSHans Petter Selasky 	/* Clear the entry for the element just outside of the new upper bound. */
162*d6b92ffaSHans Petter Selasky 	p_vector->p_ptr_array[p_vector->size] = NULL;
163*d6b92ffaSHans Petter Selasky 
164*d6b92ffaSHans Petter Selasky 	return ((void *)element);
165*d6b92ffaSHans Petter Selasky }
166*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_set_capacity(IN cl_ptr_vector_t * const p_vector,IN const size_t new_capacity)167*d6b92ffaSHans Petter Selasky cl_status_t cl_ptr_vector_set_capacity(IN cl_ptr_vector_t * const p_vector,
168*d6b92ffaSHans Petter Selasky 				       IN const size_t new_capacity)
169*d6b92ffaSHans Petter Selasky {
170*d6b92ffaSHans Petter Selasky 	void *p_new_ptr_array;
171*d6b92ffaSHans Petter Selasky 
172*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
173*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector->state == CL_INITIALIZED);
174*d6b92ffaSHans Petter Selasky 
175*d6b92ffaSHans Petter Selasky 	/* Do we have to do anything here? */
176*d6b92ffaSHans Petter Selasky 	if (new_capacity <= p_vector->capacity) {
177*d6b92ffaSHans Petter Selasky 		/* Nope */
178*d6b92ffaSHans Petter Selasky 		return (CL_SUCCESS);
179*d6b92ffaSHans Petter Selasky 	}
180*d6b92ffaSHans Petter Selasky 
181*d6b92ffaSHans Petter Selasky 	/* Allocate our pointer array. */
182*d6b92ffaSHans Petter Selasky 	p_new_ptr_array = malloc(new_capacity * sizeof(void *));
183*d6b92ffaSHans Petter Selasky 	if (!p_new_ptr_array)
184*d6b92ffaSHans Petter Selasky 		return (CL_INSUFFICIENT_MEMORY);
185*d6b92ffaSHans Petter Selasky 	else
186*d6b92ffaSHans Petter Selasky 		memset(p_new_ptr_array, 0, new_capacity * sizeof(void *));
187*d6b92ffaSHans Petter Selasky 
188*d6b92ffaSHans Petter Selasky 	if (p_vector->p_ptr_array) {
189*d6b92ffaSHans Petter Selasky 		/* Copy the old pointer array into the new. */
190*d6b92ffaSHans Petter Selasky 		memcpy(p_new_ptr_array, p_vector->p_ptr_array,
191*d6b92ffaSHans Petter Selasky 		       p_vector->capacity * sizeof(void *));
192*d6b92ffaSHans Petter Selasky 
193*d6b92ffaSHans Petter Selasky 		/* Free the old pointer array. */
194*d6b92ffaSHans Petter Selasky 		free((void *)p_vector->p_ptr_array);
195*d6b92ffaSHans Petter Selasky 	}
196*d6b92ffaSHans Petter Selasky 
197*d6b92ffaSHans Petter Selasky 	/* Set the new array. */
198*d6b92ffaSHans Petter Selasky 	p_vector->p_ptr_array = p_new_ptr_array;
199*d6b92ffaSHans Petter Selasky 
200*d6b92ffaSHans Petter Selasky 	/* Update the vector with the new capactity. */
201*d6b92ffaSHans Petter Selasky 	p_vector->capacity = new_capacity;
202*d6b92ffaSHans Petter Selasky 
203*d6b92ffaSHans Petter Selasky 	return (CL_SUCCESS);
204*d6b92ffaSHans Petter Selasky }
205*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_set_size(IN cl_ptr_vector_t * const p_vector,IN const size_t size)206*d6b92ffaSHans Petter Selasky cl_status_t cl_ptr_vector_set_size(IN cl_ptr_vector_t * const p_vector,
207*d6b92ffaSHans Petter Selasky 				   IN const size_t size)
208*d6b92ffaSHans Petter Selasky {
209*d6b92ffaSHans Petter Selasky 	cl_status_t status;
210*d6b92ffaSHans Petter Selasky 	size_t new_capacity;
211*d6b92ffaSHans Petter Selasky 
212*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
213*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector->state == CL_INITIALIZED);
214*d6b92ffaSHans Petter Selasky 
215*d6b92ffaSHans Petter Selasky 	/* Check to see if the requested size is the same as the existing size. */
216*d6b92ffaSHans Petter Selasky 	if (size == p_vector->size)
217*d6b92ffaSHans Petter Selasky 		return (CL_SUCCESS);
218*d6b92ffaSHans Petter Selasky 
219*d6b92ffaSHans Petter Selasky 	/* Determine if the vector has room for this element. */
220*d6b92ffaSHans Petter Selasky 	if (size >= p_vector->capacity) {
221*d6b92ffaSHans Petter Selasky 		if (!p_vector->grow_size)
222*d6b92ffaSHans Petter Selasky 			return (CL_INSUFFICIENT_MEMORY);
223*d6b92ffaSHans Petter Selasky 
224*d6b92ffaSHans Petter Selasky 		/* Calculate the new capacity, taking into account the grow size. */
225*d6b92ffaSHans Petter Selasky 		new_capacity = size;
226*d6b92ffaSHans Petter Selasky 		if (size % p_vector->grow_size) {
227*d6b92ffaSHans Petter Selasky 			/* Round up to nearest grow_size boundary. */
228*d6b92ffaSHans Petter Selasky 			new_capacity += p_vector->grow_size -
229*d6b92ffaSHans Petter Selasky 			    (size % p_vector->grow_size);
230*d6b92ffaSHans Petter Selasky 		}
231*d6b92ffaSHans Petter Selasky 
232*d6b92ffaSHans Petter Selasky 		status = cl_ptr_vector_set_capacity(p_vector, new_capacity);
233*d6b92ffaSHans Petter Selasky 		if (status != CL_SUCCESS)
234*d6b92ffaSHans Petter Selasky 			return (status);
235*d6b92ffaSHans Petter Selasky 	}
236*d6b92ffaSHans Petter Selasky 
237*d6b92ffaSHans Petter Selasky 	p_vector->size = size;
238*d6b92ffaSHans Petter Selasky 	return (CL_SUCCESS);
239*d6b92ffaSHans Petter Selasky }
240*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_set_min_size(IN cl_ptr_vector_t * const p_vector,IN const size_t min_size)241*d6b92ffaSHans Petter Selasky cl_status_t cl_ptr_vector_set_min_size(IN cl_ptr_vector_t * const p_vector,
242*d6b92ffaSHans Petter Selasky 				       IN const size_t min_size)
243*d6b92ffaSHans Petter Selasky {
244*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
245*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector->state == CL_INITIALIZED);
246*d6b92ffaSHans Petter Selasky 
247*d6b92ffaSHans Petter Selasky 	if (min_size > p_vector->size) {
248*d6b92ffaSHans Petter Selasky 		/* We have to resize the array */
249*d6b92ffaSHans Petter Selasky 		return (cl_ptr_vector_set_size(p_vector, min_size));
250*d6b92ffaSHans Petter Selasky 	}
251*d6b92ffaSHans Petter Selasky 
252*d6b92ffaSHans Petter Selasky 	/* We didn't have to do anything */
253*d6b92ffaSHans Petter Selasky 	return (CL_SUCCESS);
254*d6b92ffaSHans Petter Selasky }
255*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_apply_func(IN const cl_ptr_vector_t * const p_vector,IN cl_pfn_ptr_vec_apply_t pfn_callback,IN const void * const context)256*d6b92ffaSHans Petter Selasky void cl_ptr_vector_apply_func(IN const cl_ptr_vector_t * const p_vector,
257*d6b92ffaSHans Petter Selasky 			      IN cl_pfn_ptr_vec_apply_t pfn_callback,
258*d6b92ffaSHans Petter Selasky 			      IN const void *const context)
259*d6b92ffaSHans Petter Selasky {
260*d6b92ffaSHans Petter Selasky 	size_t i;
261*d6b92ffaSHans Petter Selasky 
262*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
263*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector->state == CL_INITIALIZED);
264*d6b92ffaSHans Petter Selasky 	CL_ASSERT(pfn_callback);
265*d6b92ffaSHans Petter Selasky 
266*d6b92ffaSHans Petter Selasky 	for (i = 0; i < p_vector->size; i++)
267*d6b92ffaSHans Petter Selasky 		pfn_callback(i, (void *)p_vector->p_ptr_array[i],
268*d6b92ffaSHans Petter Selasky 			     (void *)context);
269*d6b92ffaSHans Petter Selasky }
270*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_find_from_start(IN const cl_ptr_vector_t * const p_vector,IN cl_pfn_ptr_vec_find_t pfn_callback,IN const void * const context)271*d6b92ffaSHans Petter Selasky size_t cl_ptr_vector_find_from_start(IN const cl_ptr_vector_t * const p_vector,
272*d6b92ffaSHans Petter Selasky 				     IN cl_pfn_ptr_vec_find_t pfn_callback,
273*d6b92ffaSHans Petter Selasky 				     IN const void *const context)
274*d6b92ffaSHans Petter Selasky {
275*d6b92ffaSHans Petter Selasky 	size_t i;
276*d6b92ffaSHans Petter Selasky 
277*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
278*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector->state == CL_INITIALIZED);
279*d6b92ffaSHans Petter Selasky 	CL_ASSERT(pfn_callback);
280*d6b92ffaSHans Petter Selasky 
281*d6b92ffaSHans Petter Selasky 	for (i = 0; i < p_vector->size; i++) {
282*d6b92ffaSHans Petter Selasky 		/* Invoke the callback */
283*d6b92ffaSHans Petter Selasky 		if (pfn_callback(i, (void *)p_vector->p_ptr_array[i],
284*d6b92ffaSHans Petter Selasky 				 (void *)context) == CL_SUCCESS) {
285*d6b92ffaSHans Petter Selasky 			break;
286*d6b92ffaSHans Petter Selasky 		}
287*d6b92ffaSHans Petter Selasky 	}
288*d6b92ffaSHans Petter Selasky 	return (i);
289*d6b92ffaSHans Petter Selasky }
290*d6b92ffaSHans Petter Selasky 
cl_ptr_vector_find_from_end(IN const cl_ptr_vector_t * const p_vector,IN cl_pfn_ptr_vec_find_t pfn_callback,IN const void * const context)291*d6b92ffaSHans Petter Selasky size_t cl_ptr_vector_find_from_end(IN const cl_ptr_vector_t * const p_vector,
292*d6b92ffaSHans Petter Selasky 				   IN cl_pfn_ptr_vec_find_t pfn_callback,
293*d6b92ffaSHans Petter Selasky 				   IN const void *const context)
294*d6b92ffaSHans Petter Selasky {
295*d6b92ffaSHans Petter Selasky 	size_t i;
296*d6b92ffaSHans Petter Selasky 
297*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector);
298*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_vector->state == CL_INITIALIZED);
299*d6b92ffaSHans Petter Selasky 	CL_ASSERT(pfn_callback);
300*d6b92ffaSHans Petter Selasky 
301*d6b92ffaSHans Petter Selasky 	i = p_vector->size;
302*d6b92ffaSHans Petter Selasky 
303*d6b92ffaSHans Petter Selasky 	while (i) {
304*d6b92ffaSHans Petter Selasky 		/* Invoke the callback for the current element. */
305*d6b92ffaSHans Petter Selasky 		i--;
306*d6b92ffaSHans Petter Selasky 		if (pfn_callback(i, (void *)p_vector->p_ptr_array[i],
307*d6b92ffaSHans Petter Selasky 				 (void *)context) == CL_SUCCESS) {
308*d6b92ffaSHans Petter Selasky 			return (i);
309*d6b92ffaSHans Petter Selasky 		}
310*d6b92ffaSHans Petter Selasky 	}
311*d6b92ffaSHans Petter Selasky 
312*d6b92ffaSHans Petter Selasky 	return (p_vector->size);
313*d6b92ffaSHans Petter Selasky }
314