2 * Copyright (c) 2004-2009 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005,2009 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 * Implementation of quick list, and list.
44 #endif /* HAVE_CONFIG_H */
46 #include <complib/cl_qlist.h>
47 #include <complib/cl_list.h>
49 #define FREE_ITEM_GROW_SIZE 10
51 /******************************************************************************
52 IMPLEMENTATION OF QUICK LIST
53 ******************************************************************************/
54 void cl_qlist_insert_array_head(IN cl_qlist_t * const p_list,
55 IN cl_list_item_t * const p_array,
56 IN uint32_t item_count,
57 IN const uint32_t item_size)
59 cl_list_item_t *p_item;
62 CL_ASSERT(p_list->state == CL_INITIALIZED);
64 CL_ASSERT(item_size >= sizeof(cl_list_item_t));
65 CL_ASSERT(item_count);
68 * To add items from the array to the list in the same order as
69 * the elements appear in the array, we add them starting with
70 * the last one first. Locate the last item.
72 p_item = (cl_list_item_t *) ((uint8_t *) p_array +
73 (item_size * (item_count - 1)));
75 /* Continue to add all items to the list. */
76 while (item_count--) {
77 cl_qlist_insert_head(p_list, p_item);
79 /* Get the next object to add to the list. */
80 p_item = (cl_list_item_t *) ((uint8_t *) p_item - item_size);
84 void cl_qlist_insert_array_tail(IN cl_qlist_t * const p_list,
85 IN cl_list_item_t * const p_array,
86 IN uint32_t item_count,
87 IN const uint32_t item_size)
89 cl_list_item_t *p_item;
92 CL_ASSERT(p_list->state == CL_INITIALIZED);
94 CL_ASSERT(item_size >= sizeof(cl_list_item_t));
95 CL_ASSERT(item_count);
97 /* Set the first item to add to the list. */
100 /* Continue to add all items to the list. */
101 while (item_count--) {
102 cl_qlist_insert_tail(p_list, p_item);
104 /* Get the next object to add to the list. */
105 p_item = (cl_list_item_t *) ((uint8_t *) p_item + item_size);
109 void cl_qlist_insert_list_head(IN cl_qlist_t * const p_dest_list,
110 IN cl_qlist_t * const p_src_list)
112 #if defined( _DEBUG_ )
113 cl_list_item_t *p_item;
116 CL_ASSERT(p_dest_list);
117 CL_ASSERT(p_src_list);
118 CL_ASSERT(p_dest_list->state == CL_INITIALIZED);
119 CL_ASSERT(p_src_list->state == CL_INITIALIZED);
122 * Is the src list empty?
123 * We must have this check here for code below to work.
125 if (cl_is_qlist_empty(p_src_list))
128 #if defined( _DEBUG_ )
129 /* Check that all items in the source list belong there. */
130 p_item = cl_qlist_head(p_src_list);
131 while (p_item != cl_qlist_end(p_src_list)) {
132 /* All list items in the source list must point to it. */
133 CL_ASSERT(p_item->p_list == p_src_list);
134 /* Point them all to the destination list. */
135 p_item->p_list = p_dest_list;
136 p_item = cl_qlist_next(p_item);
140 /* Chain the destination list to the tail of the source list. */
141 cl_qlist_tail(p_src_list)->p_next = cl_qlist_head(p_dest_list);
142 cl_qlist_head(p_dest_list)->p_prev = cl_qlist_tail(p_src_list);
145 * Update the head of the destination list to the head of
148 p_dest_list->end.p_next = cl_qlist_head(p_src_list);
149 cl_qlist_head(p_src_list)->p_prev = &p_dest_list->end;
152 * Update the count of the destination to reflect the source items having
155 p_dest_list->count += p_src_list->count;
157 /* Update source list to reflect being empty. */
158 __cl_qlist_reset(p_src_list);
161 void cl_qlist_insert_list_tail(IN cl_qlist_t * const p_dest_list,
162 IN cl_qlist_t * const p_src_list)
164 #if defined( _DEBUG_ )
165 cl_list_item_t *p_item;
168 CL_ASSERT(p_dest_list);
169 CL_ASSERT(p_src_list);
170 CL_ASSERT(p_dest_list->state == CL_INITIALIZED);
171 CL_ASSERT(p_src_list->state == CL_INITIALIZED);
174 * Is the src list empty?
175 * We must have this check here for code below to work.
177 if (cl_is_qlist_empty(p_src_list))
180 #if defined( _DEBUG_ )
181 /* Check that all items in the source list belong there. */
182 p_item = cl_qlist_head(p_src_list);
183 while (p_item != cl_qlist_end(p_src_list)) {
184 /* All list items in the source list must point to it. */
185 CL_ASSERT(p_item->p_list == p_src_list);
186 /* Point them all to the destination list. */
187 p_item->p_list = p_dest_list;
188 p_item = cl_qlist_next(p_item);
192 /* Chain the source list to the tail of the destination list. */
193 cl_qlist_tail(p_dest_list)->p_next = cl_qlist_head(p_src_list);
194 cl_qlist_head(p_src_list)->p_prev = cl_qlist_tail(p_dest_list);
197 * Update the tail of the destination list to the tail of
200 p_dest_list->end.p_prev = cl_qlist_tail(p_src_list);
201 cl_qlist_tail(p_src_list)->p_next = &p_dest_list->end;
204 * Update the count of the destination to reflect the source items having
207 p_dest_list->count += p_src_list->count;
209 /* Update source list to reflect being empty. */
210 __cl_qlist_reset(p_src_list);
213 boolean_t cl_is_item_in_qlist(IN const cl_qlist_t * const p_list,
214 IN const cl_list_item_t * const p_list_item)
216 const cl_list_item_t *p_temp;
219 CL_ASSERT(p_list_item);
220 CL_ASSERT(p_list->state == CL_INITIALIZED);
222 /* Traverse looking for a match */
223 p_temp = cl_qlist_head(p_list);
224 while (p_temp != cl_qlist_end(p_list)) {
225 if (p_temp == p_list_item) {
226 CL_ASSERT(p_list_item->p_list == p_list);
230 p_temp = cl_qlist_next(p_temp);
236 cl_list_item_t *cl_qlist_find_next(IN const cl_qlist_t * const p_list,
237 IN const cl_list_item_t * const p_list_item,
238 IN cl_pfn_qlist_find_t pfn_func,
239 IN const void *const context)
241 cl_list_item_t *p_found_item;
244 CL_ASSERT(p_list->state == CL_INITIALIZED);
245 CL_ASSERT(p_list_item);
246 CL_ASSERT(p_list_item->p_list == p_list);
249 p_found_item = cl_qlist_next(p_list_item);
251 /* The user provided a compare function */
252 while (p_found_item != cl_qlist_end(p_list)) {
253 CL_ASSERT(p_found_item->p_list == p_list);
255 if (pfn_func(p_found_item, (void *)context) == CL_SUCCESS)
258 p_found_item = cl_qlist_next(p_found_item);
262 return (p_found_item);
265 cl_list_item_t *cl_qlist_find_prev(IN const cl_qlist_t * const p_list,
266 IN const cl_list_item_t * const p_list_item,
267 IN cl_pfn_qlist_find_t pfn_func,
268 IN const void *const context)
270 cl_list_item_t *p_found_item;
273 CL_ASSERT(p_list->state == CL_INITIALIZED);
274 CL_ASSERT(p_list_item);
275 CL_ASSERT(p_list_item->p_list == p_list);
278 p_found_item = cl_qlist_prev(p_list_item);
280 /* The user provided a compare function */
281 while (p_found_item != cl_qlist_end(p_list)) {
282 CL_ASSERT(p_found_item->p_list == p_list);
284 if (pfn_func(p_found_item, (void *)context) == CL_SUCCESS)
287 p_found_item = cl_qlist_prev(p_found_item);
291 return (p_found_item);
294 void cl_qlist_apply_func(IN const cl_qlist_t * const p_list,
295 IN cl_pfn_qlist_apply_t pfn_func,
296 IN const void *const context)
298 cl_list_item_t *p_list_item;
300 /* Note that context can have any arbitrary value. */
302 CL_ASSERT(p_list->state == CL_INITIALIZED);
305 p_list_item = cl_qlist_head(p_list);
306 while (p_list_item != cl_qlist_end(p_list)) {
307 pfn_func(p_list_item, (void *)context);
308 p_list_item = cl_qlist_next(p_list_item);
312 void cl_qlist_move_items(IN cl_qlist_t * const p_src_list,
313 IN cl_qlist_t * const p_dest_list,
314 IN cl_pfn_qlist_find_t pfn_func,
315 IN const void *const context)
317 cl_list_item_t *p_current_item, *p_next;
319 CL_ASSERT(p_src_list);
320 CL_ASSERT(p_dest_list);
321 CL_ASSERT(p_src_list->state == CL_INITIALIZED);
322 CL_ASSERT(p_dest_list->state == CL_INITIALIZED);
325 p_current_item = cl_qlist_head(p_src_list);
327 while (p_current_item != cl_qlist_end(p_src_list)) {
328 /* Before we do anything, get a pointer to the next item. */
329 p_next = cl_qlist_next(p_current_item);
331 if (pfn_func(p_current_item, (void *)context) == CL_SUCCESS) {
332 /* Move the item from one list to the other. */
333 cl_qlist_remove_item(p_src_list, p_current_item);
334 cl_qlist_insert_tail(p_dest_list, p_current_item);
336 p_current_item = p_next;
340 /******************************************************************************
341 IMPLEMENTATION OF LIST
342 ******************************************************************************/
343 void cl_list_construct(IN cl_list_t * const p_list)
347 cl_qpool_construct(&p_list->list_item_pool);
350 cl_status_t cl_list_init(IN cl_list_t * const p_list, IN const size_t min_items)
355 cl_qlist_init(&p_list->list);
358 * We will grow by min_items/8 items at a time, with a minimum of
359 * FREE_ITEM_GROW_SIZE.
361 grow_size = (uint32_t) min_items >> 3;
362 if (grow_size < FREE_ITEM_GROW_SIZE)
363 grow_size = FREE_ITEM_GROW_SIZE;
365 /* Initialize the pool of list items. */
366 return (cl_qpool_init(&p_list->list_item_pool, min_items, 0, grow_size,
367 sizeof(cl_pool_obj_t), NULL, NULL, NULL));
370 void cl_list_destroy(IN cl_list_t * const p_list)
374 cl_qpool_destroy(&p_list->list_item_pool);
377 static cl_status_t cl_list_find_cb(IN const cl_list_item_t * const p_list_item,
378 IN void *const context)
380 CL_ASSERT(p_list_item);
382 if (cl_list_obj(p_list_item) == context)
385 return (CL_NOT_FOUND);
388 cl_status_t cl_list_remove_object(IN cl_list_t * const p_list,
389 IN const void *const p_object)
391 cl_list_item_t *p_list_item;
394 CL_ASSERT(cl_is_qpool_inited(&p_list->list_item_pool));
396 /* find the item in question */
398 cl_qlist_find_from_head(&p_list->list, cl_list_find_cb, p_object);
399 if (p_list_item != cl_qlist_end(&p_list->list)) {
400 /* remove this item */
401 cl_qlist_remove_item(&p_list->list, p_list_item);
402 cl_qpool_put(&p_list->list_item_pool,
403 (cl_pool_item_t *) p_list_item);
406 return (CL_NOT_FOUND);
409 boolean_t cl_is_object_in_list(IN const cl_list_t * const p_list,
410 IN const void *const p_object)
413 CL_ASSERT(cl_is_qpool_inited(&p_list->list_item_pool));
415 return (cl_qlist_find_from_head
416 (&p_list->list, cl_list_find_cb, p_object)
417 != cl_qlist_end(&p_list->list));
420 cl_status_t cl_list_insert_array_head(IN cl_list_t * const p_list,
421 IN const void *const p_array,
422 IN uint32_t item_count,
423 IN const uint32_t item_size)
427 uint32_t items_remain = item_count;
430 CL_ASSERT(cl_is_qpool_inited(&p_list->list_item_pool));
432 CL_ASSERT(item_size);
433 CL_ASSERT(item_count);
436 * To add items from the array to the list in the same order as
437 * the elements appear in the array, we add them starting with
438 * the last one first. Locate the last item.
440 p_object = ((uint8_t *) p_array + (item_size * (item_count - 1)));
442 /* Continue to add all items to the list. */
443 while (items_remain--) {
444 status = cl_list_insert_head(p_list, p_object);
445 if (status != CL_SUCCESS) {
446 /* Remove all items that have been inserted. */
447 while (items_remain++ < (item_count - 1))
448 cl_list_remove_head(p_list);
452 /* Get the next object to add to the list. */
453 p_object = ((uint8_t *) p_object - item_size);
459 cl_status_t cl_list_insert_array_tail(IN cl_list_t * const p_list,
460 IN const void *const p_array,
461 IN uint32_t item_count,
462 IN const uint32_t item_size)
466 uint32_t items_remain = item_count;
469 CL_ASSERT(cl_is_qpool_inited(&p_list->list_item_pool));
471 CL_ASSERT(item_size);
472 CL_ASSERT(item_count);
474 /* Set the first item to add to the list. */
475 p_object = (void *)p_array;
477 /* Continue to add all items to the list. */
478 while (items_remain--) {
479 status = cl_list_insert_tail(p_list, p_object);
480 if (status != CL_SUCCESS) {
481 /* Remove all items that have been inserted. */
482 while (items_remain++ < (item_count - 1))
483 cl_list_remove_tail(p_list);
487 /* Get the next object to add to the list. */
488 p_object = ((uint8_t *) p_object + item_size);
494 cl_list_iterator_t cl_list_find_from_head(IN const cl_list_t * const p_list,
495 IN cl_pfn_list_find_t pfn_func,
496 IN const void *const context)
499 cl_list_iterator_t itor;
501 /* Note that context can have any arbitrary value. */
503 CL_ASSERT(cl_is_qpool_inited(&p_list->list_item_pool));
506 itor = cl_list_head(p_list);
508 while (itor != cl_list_end(p_list)) {
509 status = pfn_func(cl_list_obj(itor), (void *)context);
510 if (status == CL_SUCCESS)
513 itor = cl_list_next(itor);
520 cl_list_iterator_t cl_list_find_from_tail(IN const cl_list_t * const p_list,
521 IN cl_pfn_list_find_t pfn_func,
522 IN const void *const context)
525 cl_list_iterator_t itor;
527 /* Note that context can have any arbitrary value. */
529 CL_ASSERT(cl_is_qpool_inited(&p_list->list_item_pool));
532 itor = cl_list_tail(p_list);
534 while (itor != cl_list_end(p_list)) {
535 status = pfn_func(cl_list_obj(itor), (void *)context);
536 if (status == CL_SUCCESS)
539 itor = cl_list_prev(itor);
546 void cl_list_apply_func(IN const cl_list_t * const p_list,
547 IN cl_pfn_list_apply_t pfn_func,
548 IN const void *const context)
550 cl_list_iterator_t itor;
552 /* Note that context can have any arbitrary value. */
554 CL_ASSERT(cl_is_qpool_inited(&p_list->list_item_pool));
557 itor = cl_list_head(p_list);
559 while (itor != cl_list_end(p_list)) {
560 pfn_func(cl_list_obj(itor), (void *)context);
562 itor = cl_list_next(itor);