xref: /freebsd-src/sys/dev/isci/scil/sci_abstract_list.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1f11c7f63SJim Harris /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3*718cf2ccSPedro F. Giffuni  *
4f11c7f63SJim Harris  * This file is provided under a dual BSD/GPLv2 license.  When using or
5f11c7f63SJim Harris  * redistributing this file, you may do so under either license.
6f11c7f63SJim Harris  *
7f11c7f63SJim Harris  * GPL LICENSE SUMMARY
8f11c7f63SJim Harris  *
9f11c7f63SJim Harris  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10f11c7f63SJim Harris  *
11f11c7f63SJim Harris  * This program is free software; you can redistribute it and/or modify
12f11c7f63SJim Harris  * it under the terms of version 2 of the GNU General Public License as
13f11c7f63SJim Harris  * published by the Free Software Foundation.
14f11c7f63SJim Harris  *
15f11c7f63SJim Harris  * This program is distributed in the hope that it will be useful, but
16f11c7f63SJim Harris  * WITHOUT ANY WARRANTY; without even the implied warranty of
17f11c7f63SJim Harris  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18f11c7f63SJim Harris  * General Public License for more details.
19f11c7f63SJim Harris  *
20f11c7f63SJim Harris  * You should have received a copy of the GNU General Public License
21f11c7f63SJim Harris  * along with this program; if not, write to the Free Software
22f11c7f63SJim Harris  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23f11c7f63SJim Harris  * The full GNU General Public License is included in this distribution
24f11c7f63SJim Harris  * in the file called LICENSE.GPL.
25f11c7f63SJim Harris  *
26f11c7f63SJim Harris  * BSD LICENSE
27f11c7f63SJim Harris  *
28f11c7f63SJim Harris  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29f11c7f63SJim Harris  * All rights reserved.
30f11c7f63SJim Harris  *
31f11c7f63SJim Harris  * Redistribution and use in source and binary forms, with or without
32f11c7f63SJim Harris  * modification, are permitted provided that the following conditions
33f11c7f63SJim Harris  * are met:
34f11c7f63SJim Harris  *
35f11c7f63SJim Harris  *   * Redistributions of source code must retain the above copyright
36f11c7f63SJim Harris  *     notice, this list of conditions and the following disclaimer.
37f11c7f63SJim Harris  *   * Redistributions in binary form must reproduce the above copyright
38f11c7f63SJim Harris  *     notice, this list of conditions and the following disclaimer in
39f11c7f63SJim Harris  *     the documentation and/or other materials provided with the
40f11c7f63SJim Harris  *     distribution.
41f11c7f63SJim Harris  *
42f11c7f63SJim Harris  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43f11c7f63SJim Harris  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44f11c7f63SJim Harris  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45f11c7f63SJim Harris  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46f11c7f63SJim Harris  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47f11c7f63SJim Harris  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48f11c7f63SJim Harris  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49f11c7f63SJim Harris  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50f11c7f63SJim Harris  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51f11c7f63SJim Harris  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52f11c7f63SJim Harris  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53f11c7f63SJim Harris  */
54f11c7f63SJim Harris 
55f11c7f63SJim Harris #include <sys/cdefs.h>
56f11c7f63SJim Harris /**
57f11c7f63SJim Harris  * @file
58f11c7f63SJim Harris  *
59f11c7f63SJim Harris  * @brief This file contains the implementation of an abstract list class.
60f11c7f63SJim Harris  *        This class will allow for the same item to occur multiple times in
61f11c7f63SJim Harris  *        the list.  It will provide an interface that is similar to the
62f11c7f63SJim Harris  *        C++ standard template list interface.
63f11c7f63SJim Harris  */
64f11c7f63SJim Harris 
65f11c7f63SJim Harris //******************************************************************************
66f11c7f63SJim Harris //*
67f11c7f63SJim Harris //*     I N C L U D E S
68f11c7f63SJim Harris //*
69f11c7f63SJim Harris //******************************************************************************
70f11c7f63SJim Harris 
71f11c7f63SJim Harris #include <dev/isci/scil/sci_abstract_list.h>
72f11c7f63SJim Harris 
73f11c7f63SJim Harris 
74f11c7f63SJim Harris //******************************************************************************
75f11c7f63SJim Harris //*
76f11c7f63SJim Harris //*     P R I V A T E   M E M B E R S
77f11c7f63SJim Harris //*
78f11c7f63SJim Harris //******************************************************************************
79f11c7f63SJim Harris 
80f11c7f63SJim Harris //******************************************************************************
81f11c7f63SJim Harris //*
82f11c7f63SJim Harris //*     P R O T E C T E D   M E T H O D S
83f11c7f63SJim Harris //*
84f11c7f63SJim Harris //******************************************************************************
85f11c7f63SJim Harris 
86f11c7f63SJim Harris /**
87f11c7f63SJim Harris  * @brief Initialize the abstract list
88f11c7f63SJim Harris  *
89f11c7f63SJim Harris  * @pre The supplied free pool should be constructed prior to utilization
90f11c7f63SJim Harris  *      of this abstract list.  It isn't mandatory for the free pool to be
91f11c7f63SJim Harris  *      constructed before invoking this method, but suggested.
92f11c7f63SJim Harris  *
93f11c7f63SJim Harris  * @param[in] list This parameter specifies the abstract list to be
94f11c7f63SJim Harris  *            constructed.
95f11c7f63SJim Harris  * @param[in] free_pool This parameter specifies the free pool to be
96f11c7f63SJim Harris  *            utilized as the repository of free elements for list usage.
97f11c7f63SJim Harris  *
98f11c7f63SJim Harris  * @return none
99f11c7f63SJim Harris  */
sci_abstract_list_construct(SCI_ABSTRACT_LIST_T * list,SCI_ABSTRACT_ELEMENT_POOL_T * free_pool)100f11c7f63SJim Harris void sci_abstract_list_construct(
101f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T         * list,
102f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_POOL_T * free_pool
103f11c7f63SJim Harris )
104f11c7f63SJim Harris {
105f11c7f63SJim Harris    memset(list, 0, sizeof(SCI_ABSTRACT_LIST_T));
106f11c7f63SJim Harris    list->free_pool = free_pool;
107f11c7f63SJim Harris }
108f11c7f63SJim Harris 
109f11c7f63SJim Harris /**
110f11c7f63SJim Harris  * Initialize the abstract list with its free pool
111f11c7f63SJim Harris  *
112f11c7f63SJim Harris  * @param[in] pool
113f11c7f63SJim Harris  *    the free pool from which the elements will be extracted
114f11c7f63SJim Harris  * @param[in] list_elements
115f11c7f63SJim Harris  *    the array of list elements to be added to the free list
116f11c7f63SJim Harris  * @param[in] element_count
117f11c7f63SJim Harris  *    the count of the elements to be added to the free list these should be
118f11c7f63SJim Harris  *    the same as the array size of list elements
119f11c7f63SJim Harris  *
120f11c7f63SJim Harris  * @return none
121f11c7f63SJim Harris  */
sci_abstract_element_pool_construct(SCI_ABSTRACT_ELEMENT_POOL_T * pool,SCI_ABSTRACT_ELEMENT_T * list_elements,int element_count)122f11c7f63SJim Harris void sci_abstract_element_pool_construct(
123f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_POOL_T * pool,
124f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T      * list_elements,
125f11c7f63SJim Harris    int                           element_count
126f11c7f63SJim Harris )
127f11c7f63SJim Harris {
128f11c7f63SJim Harris    int index;
129f11c7f63SJim Harris 
130f11c7f63SJim Harris    memset(pool, 0, sizeof(SCI_ABSTRACT_ELEMENT_POOL_T));
131f11c7f63SJim Harris    memset(list_elements, 0, sizeof(SCI_ABSTRACT_ELEMENT_T) * element_count);
132f11c7f63SJim Harris 
133f11c7f63SJim Harris    pool->elements     = list_elements;
134f11c7f63SJim Harris    pool->max_elements = element_count;
135f11c7f63SJim Harris 
136f11c7f63SJim Harris    // Loop through all of the elements in the array and push them onto the
137f11c7f63SJim Harris    // pool's free list.
138f11c7f63SJim Harris    for (index = element_count - 1; index >= 0; index--)
139f11c7f63SJim Harris    {
140f11c7f63SJim Harris       private_pool_free(pool, &(list_elements[index]));
141f11c7f63SJim Harris    }
142f11c7f63SJim Harris }
143f11c7f63SJim Harris 
144f11c7f63SJim Harris 
145f11c7f63SJim Harris #ifdef USE_ABSTRACT_LIST_FUNCTIONS
146f11c7f63SJim Harris 
147f11c7f63SJim Harris //******************************************************************************
148f11c7f63SJim Harris //*
149f11c7f63SJim Harris //*     P U B L I C   M E T H O D S
150f11c7f63SJim Harris //*
151f11c7f63SJim Harris //******************************************************************************
152f11c7f63SJim Harris 
153f11c7f63SJim Harris /**
154f11c7f63SJim Harris  * Simply return the front element pointer of the list.  This returns an element
155f11c7f63SJim Harris  * element as opposed to what the element is pointing to.
156f11c7f63SJim Harris  */
sci_abstract_list_get_front(SCI_ABSTRACT_LIST_T * list_p)157f11c7f63SJim Harris SCI_ABSTRACT_ELEMENT_T * sci_abstract_list_get_front(
158f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
159f11c7f63SJim Harris )
160f11c7f63SJim Harris {
161f11c7f63SJim Harris    return (list_p)->elements.front_p;
162f11c7f63SJim Harris }
163f11c7f63SJim Harris 
164f11c7f63SJim Harris /**
165f11c7f63SJim Harris  * This method simply returns the object pointed to by the head (front) of
166f11c7f63SJim Harris  * the list.
167f11c7f63SJim Harris  */
sci_abstract_list_front(SCI_ABSTRACT_LIST_T * list_p)168f11c7f63SJim Harris void * sci_abstract_list_front(
169f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
170f11c7f63SJim Harris )
171f11c7f63SJim Harris {
172f11c7f63SJim Harris    return
173f11c7f63SJim Harris       ( ( (list_p)->elements.front_p ) ? ((list_p)->elements.front_p->object_p) : NULL );
174f11c7f63SJim Harris }
175f11c7f63SJim Harris 
176f11c7f63SJim Harris /**
177f11c7f63SJim Harris  * This method simply returns the object pointed to by the tail (back) of
178f11c7f63SJim Harris  * the list.
179f11c7f63SJim Harris  */
sci_abstract_list_back(SCI_ABSTRACT_LIST_T * list_p)180f11c7f63SJim Harris void * sci_abstract_list_back(
181f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
182f11c7f63SJim Harris )
183f11c7f63SJim Harris {
184f11c7f63SJim Harris    return
185f11c7f63SJim Harris       ( ( (list_p)->elements.back_p ) ? ((list_p)->elements.back_p->object_p) : NULL );
186f11c7f63SJim Harris }
187f11c7f63SJim Harris 
188f11c7f63SJim Harris /**
189f11c7f63SJim Harris  * This method will return FALSE if the list is not empty.
190f11c7f63SJim Harris  */
sci_abstract_list_is_empty(SCI_ABSTRACT_LIST_T * list_p)191f11c7f63SJim Harris BOOL sci_abstract_list_is_empty(
192f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
193f11c7f63SJim Harris )
194f11c7f63SJim Harris {
195f11c7f63SJim Harris    return ( (list_p)->elements.front_p == NULL );
196f11c7f63SJim Harris }
197f11c7f63SJim Harris 
198f11c7f63SJim Harris 
199f11c7f63SJim Harris /**
200f11c7f63SJim Harris  * This method will return the number of elements queued in the list.
201f11c7f63SJim Harris  */
sci_abstract_list_size(SCI_ABSTRACT_LIST_T * list_p)202f11c7f63SJim Harris U32 sci_abstract_list_size(
203f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
204f11c7f63SJim Harris )
205f11c7f63SJim Harris {
206f11c7f63SJim Harris    return ( (list_p)->elements.size );
207f11c7f63SJim Harris }
208f11c7f63SJim Harris 
209f11c7f63SJim Harris 
210f11c7f63SJim Harris /**
211f11c7f63SJim Harris  * This method simply returns the next list element in the list.
212f11c7f63SJim Harris  */
sci_abstract_list_get_next(SCI_ABSTRACT_ELEMENT_T * alElement_p)213f11c7f63SJim Harris SCI_ABSTRACT_ELEMENT_T * sci_abstract_list_get_next(
214f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p
215f11c7f63SJim Harris )
216f11c7f63SJim Harris {
217f11c7f63SJim Harris    return ( (alElement_p)->next_p );
218f11c7f63SJim Harris }
219f11c7f63SJim Harris 
220f11c7f63SJim Harris 
221f11c7f63SJim Harris #if defined(SCI_LOGGING)
222f11c7f63SJim Harris /**
223f11c7f63SJim Harris  * This method simply prints the contents of the list.
224f11c7f63SJim Harris  */
sci_abstract_list_print(SCI_ABSTRACT_LIST_T * list_p)225f11c7f63SJim Harris void  sci_abstract_list_print(
226f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
227f11c7f63SJim Harris )
228f11c7f63SJim Harris {
229f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p = list_p->elements.front_p;
230f11c7f63SJim Harris 
231f11c7f63SJim Harris    while (alElement_p != NULL)
232f11c7f63SJim Harris    {
233f11c7f63SJim Harris #ifdef UNIT_TEST_DEBUG
234f11c7f63SJim Harris       /* Check to see if we found the object for which we are searching. */
235f11c7f63SJim Harris       printf("ITEM next_p 0x%x prev_p 0x%x obj_p 0x%x, 0x%x\n",
236f11c7f63SJim Harris              alElement_p->next_p,
237f11c7f63SJim Harris              alElement_p->previous_p,
238f11c7f63SJim Harris              (U32*) (alElement_p->object_p));
239f11c7f63SJim Harris #endif
240f11c7f63SJim Harris       alElement_p = alElement_p->next_p;
241f11c7f63SJim Harris    }
242f11c7f63SJim Harris }
243f11c7f63SJim Harris #endif // defined(SCI_LOGGING)
244f11c7f63SJim Harris 
245f11c7f63SJim Harris 
246f11c7f63SJim Harris /**
247f11c7f63SJim Harris  * This method will simply search the supplied list for the desired object.
248f11c7f63SJim Harris  * It will return a pointer to the object, if it is found.  Otherwise
249f11c7f63SJim Harris  * it will return NULL.
250f11c7f63SJim Harris  */
sci_abstract_list_find(SCI_ABSTRACT_LIST_T * list_p,void * obj_p)251f11c7f63SJim Harris void * sci_abstract_list_find(
252f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p,
253f11c7f63SJim Harris    void * obj_p
254f11c7f63SJim Harris )
255f11c7f63SJim Harris {
256f11c7f63SJim Harris    return
257f11c7f63SJim Harris       sci_abstract_list_get_object(private_find(&(list_p)->elements, (obj_p)));
258f11c7f63SJim Harris }
259f11c7f63SJim Harris 
260f11c7f63SJim Harris 
261f11c7f63SJim Harris /**
262f11c7f63SJim Harris  * This method will simply remove the element at the back (tail) of the list.
263f11c7f63SJim Harris  * It will return a pointer to the object that was removed or NULL if not
264f11c7f63SJim Harris  * found.
265f11c7f63SJim Harris  */
sci_abstract_list_popback(SCI_ABSTRACT_LIST_T * list_p)266f11c7f63SJim Harris void * sci_abstract_list_popback(
267f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
268f11c7f63SJim Harris )
269f11c7f63SJim Harris {
270f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * elem_list = &(list_p)->elements;
271f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T      * alElement_p = elem_list->back_p;
272f11c7f63SJim Harris    void * obj_p = NULL;
273f11c7f63SJim Harris 
274f11c7f63SJim Harris    if (alElement_p != NULL)
275f11c7f63SJim Harris    {
276f11c7f63SJim Harris       obj_p = alElement_p->object_p;
277f11c7f63SJim Harris       if (elem_list->back_p == elem_list->front_p)
278f11c7f63SJim Harris       {
279f11c7f63SJim Harris          elem_list->back_p = elem_list->front_p = NULL;
280f11c7f63SJim Harris       }
281f11c7f63SJim Harris       else
282f11c7f63SJim Harris       {
283f11c7f63SJim Harris          elem_list->back_p = elem_list->back_p->previous_p;
284f11c7f63SJim Harris          elem_list->back_p->next_p = NULL;
285f11c7f63SJim Harris       }
286f11c7f63SJim Harris 
287f11c7f63SJim Harris       elem_list->size--;
288f11c7f63SJim Harris       private_pool_free((list_p)->free_pool, alElement_p);
289f11c7f63SJim Harris    }
290f11c7f63SJim Harris 
291f11c7f63SJim Harris    return obj_p;
292f11c7f63SJim Harris }
293f11c7f63SJim Harris 
294f11c7f63SJim Harris /**
295f11c7f63SJim Harris  * This method simply removes the list element at the head of the list
296f11c7f63SJim Harris  * and returns the pointer to the object that was removed.
297f11c7f63SJim Harris  */
sci_abstract_list_popfront(SCI_ABSTRACT_LIST_T * list_p)298f11c7f63SJim Harris void * sci_abstract_list_popfront(
299f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
300f11c7f63SJim Harris )
301f11c7f63SJim Harris {
302f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p =
303f11c7f63SJim Harris                               private_pop_front(&(list_p)->elements);
304f11c7f63SJim Harris    void * obj_p = NULL;
305f11c7f63SJim Harris 
306f11c7f63SJim Harris    if (alElement_p != NULL)
307f11c7f63SJim Harris    {
308f11c7f63SJim Harris       obj_p = alElement_p->object_p;
309f11c7f63SJim Harris       private_pool_free((list_p)->free_pool, alElement_p);
310f11c7f63SJim Harris    }
311f11c7f63SJim Harris 
312f11c7f63SJim Harris    return obj_p;
313f11c7f63SJim Harris }
314f11c7f63SJim Harris 
315f11c7f63SJim Harris 
316f11c7f63SJim Harris 
317f11c7f63SJim Harris /**
318f11c7f63SJim Harris  * This method will erase (remove) all instances of the supplied object from
319f11c7f63SJim Harris  * anywhere in the list.
320f11c7f63SJim Harris  */
sci_abstract_list_erase(SCI_ABSTRACT_LIST_T * list_p,void * obj_p)321f11c7f63SJim Harris void sci_abstract_list_erase(
322f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p,
323f11c7f63SJim Harris    void * obj_p
324f11c7f63SJim Harris )
325f11c7f63SJim Harris {
326f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * elem_list = &(list_p)->elements;
327f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T      * alElement_p;
328f11c7f63SJim Harris 
329f11c7f63SJim Harris    while ((alElement_p = private_find(elem_list, (obj_p))) != NULL)
330f11c7f63SJim Harris    {
331f11c7f63SJim Harris       if (alElement_p == elem_list->front_p)
332f11c7f63SJim Harris       {
333f11c7f63SJim Harris          sci_abstract_list_popfront(list_p);
334f11c7f63SJim Harris       }
335f11c7f63SJim Harris       else if (alElement_p == elem_list->back_p)
336f11c7f63SJim Harris       {
337f11c7f63SJim Harris          sci_abstract_list_popback(list_p);
338f11c7f63SJim Harris       }
339f11c7f63SJim Harris       else
340f11c7f63SJim Harris       {
341f11c7f63SJim Harris          alElement_p->previous_p->next_p = alElement_p->next_p;
342f11c7f63SJim Harris          alElement_p->next_p->previous_p = alElement_p->previous_p;
343f11c7f63SJim Harris          elem_list->size--;
344f11c7f63SJim Harris          private_pool_free((list_p)->free_pool, alElement_p);
345f11c7f63SJim Harris       }
346f11c7f63SJim Harris    }
347f11c7f63SJim Harris    return;
348f11c7f63SJim Harris }
349f11c7f63SJim Harris 
350f11c7f63SJim Harris /**
351f11c7f63SJim Harris  * This method simply adds a LIST_ELEMENT for the supplied object to the back
352f11c7f63SJim Harris  * (tail) of the supplied list.
353f11c7f63SJim Harris  */
sci_abstract_list_pushback(SCI_ABSTRACT_LIST_T * list_p,void * obj_p)354f11c7f63SJim Harris void sci_abstract_list_pushback(
355f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p,
356f11c7f63SJim Harris    void * obj_p
357f11c7f63SJim Harris )
358f11c7f63SJim Harris {
359f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * elem_list = &(list_p)->elements;
360f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T      * alElement_p
361f11c7f63SJim Harris       = private_pool_allocate((list_p)->free_pool);
362f11c7f63SJim Harris //   assert(alElement_p != NULL);
363f11c7f63SJim Harris 
364f11c7f63SJim Harris    alElement_p->object_p = (obj_p);
365f11c7f63SJim Harris 
366f11c7f63SJim Harris    if (elem_list->front_p == NULL)
367f11c7f63SJim Harris    {
368f11c7f63SJim Harris       elem_list->front_p = elem_list->back_p = alElement_p;
369f11c7f63SJim Harris    }
370f11c7f63SJim Harris    else
371f11c7f63SJim Harris    {
372f11c7f63SJim Harris       elem_list->back_p->next_p = alElement_p;
373f11c7f63SJim Harris       alElement_p->previous_p   = elem_list->back_p;
374f11c7f63SJim Harris       elem_list->back_p         = alElement_p;
375f11c7f63SJim Harris    }
376f11c7f63SJim Harris 
377f11c7f63SJim Harris    elem_list->size++;
378f11c7f63SJim Harris }
379f11c7f63SJim Harris 
380f11c7f63SJim Harris 
381f11c7f63SJim Harris 
382f11c7f63SJim Harris /**
383f11c7f63SJim Harris  * This method simply adds a LIST_ELEMENT for the supplied object to the front
384f11c7f63SJim Harris  * (head) of the supplied list.
385f11c7f63SJim Harris  */
sci_abstract_list_pushfront(SCI_ABSTRACT_LIST_T * list_p,void * obj_p)386f11c7f63SJim Harris void sci_abstract_list_pushfront(
387f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p,
388f11c7f63SJim Harris    void * obj_p
389f11c7f63SJim Harris )
390f11c7f63SJim Harris {
391f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p =
392f11c7f63SJim Harris          private_pool_allocate((list_p)->free_pool);
393f11c7f63SJim Harris    alElement_p->object_p = (obj_p);
394f11c7f63SJim Harris    private_push_front(&(list_p)->elements, alElement_p);
395f11c7f63SJim Harris }
396f11c7f63SJim Harris 
397f11c7f63SJim Harris 
398f11c7f63SJim Harris /**
399f11c7f63SJim Harris  * This method will add the objToAdd_p object to the list before the obj_p.
400f11c7f63SJim Harris  *
401f11c7f63SJim Harris  */
sci_abstract_list_insert(SCI_ABSTRACT_LIST_T * list_p,void * obj_p,void * objToAdd_p)402f11c7f63SJim Harris void sci_abstract_list_insert(
403f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p,
404f11c7f63SJim Harris    void * obj_p,
405f11c7f63SJim Harris    void * objToAdd_p
406f11c7f63SJim Harris )
407f11c7f63SJim Harris {
408f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * elem_list = &(list_p)->elements;
409f11c7f63SJim Harris 
410f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * obj_element = private_find(elem_list, obj_p);
411f11c7f63SJim Harris 
412f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * objToAdd_element =
413f11c7f63SJim Harris       private_pool_allocate((list_p)->free_pool);
414f11c7f63SJim Harris 
415f11c7f63SJim Harris    objToAdd_element->object_p = objToAdd_p;
416f11c7f63SJim Harris 
417f11c7f63SJim Harris    ASSERT(obj_element != NULL);
418f11c7f63SJim Harris    ASSERT(objToAdd_element != NULL);
419f11c7f63SJim Harris 
420f11c7f63SJim Harris    if (obj_element == elem_list->front_p)
421f11c7f63SJim Harris    {
422f11c7f63SJim Harris       objToAdd_element->object_p = (objToAdd_p);
423f11c7f63SJim Harris       private_push_front(&(list_p)->elements, objToAdd_element);
424f11c7f63SJim Harris    }
425f11c7f63SJim Harris    else
426f11c7f63SJim Harris    {
427f11c7f63SJim Harris       obj_element->previous_p->next_p = objToAdd_element;
428f11c7f63SJim Harris       objToAdd_element->previous_p = obj_element->previous_p;
429f11c7f63SJim Harris 
430f11c7f63SJim Harris       obj_element->previous_p = objToAdd_element;
431f11c7f63SJim Harris       objToAdd_element->next_p = obj_element;
432f11c7f63SJim Harris 
433f11c7f63SJim Harris       elem_list->size++;
434f11c7f63SJim Harris    }
435f11c7f63SJim Harris }
436f11c7f63SJim Harris 
437f11c7f63SJim Harris /**
438f11c7f63SJim Harris  * This method simply frees all the items from the list.
439f11c7f63SJim Harris  */
sci_abstract_list_clear(SCI_ABSTRACT_LIST_T * list_p)440f11c7f63SJim Harris void sci_abstract_list_clear(
441f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
442f11c7f63SJim Harris )
443f11c7f63SJim Harris {
444f11c7f63SJim Harris    while ((list_p)->elements.size > 0)
445f11c7f63SJim Harris       sci_abstract_list_popfront((list_p));
446f11c7f63SJim Harris }
447f11c7f63SJim Harris 
448f11c7f63SJim Harris /**
449f11c7f63SJim Harris  * This method simply returns the object being pointed to by the list element
450f11c7f63SJim Harris  * (The item being listed).
451f11c7f63SJim Harris  */
sci_abstract_list_get_object(SCI_ABSTRACT_ELEMENT_T * alElement_p)452f11c7f63SJim Harris void * sci_abstract_list_get_object(
453f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p
454f11c7f63SJim Harris )
455f11c7f63SJim Harris {
456f11c7f63SJim Harris    void * obj_p = NULL;
457f11c7f63SJim Harris    if ((alElement_p) != NULL)
458f11c7f63SJim Harris       obj_p = (alElement_p)->object_p;
459f11c7f63SJim Harris 
460f11c7f63SJim Harris    return obj_p;
461f11c7f63SJim Harris }
462f11c7f63SJim Harris 
463f11c7f63SJim Harris 
464f11c7f63SJim Harris /**
465f11c7f63SJim Harris  * This method is simply a wrapper to provide the number of elements in
466f11c7f63SJim Harris  * the free list.
467f11c7f63SJim Harris  */
sci_abstract_list_freeList_size(SCI_ABSTRACT_LIST_T * freeList)468f11c7f63SJim Harris U32 sci_abstract_list_freeList_size(
469f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * freeList
470f11c7f63SJim Harris )
471f11c7f63SJim Harris {
472f11c7f63SJim Harris    return (sci_abstract_list_size(freeList));
473f11c7f63SJim Harris }
474f11c7f63SJim Harris 
475f11c7f63SJim Harris //******************************************************************************
476f11c7f63SJim Harris //*
477f11c7f63SJim Harris //*     P R I V A T E   M E T H O D S
478f11c7f63SJim Harris //*
479f11c7f63SJim Harris //******************************************************************************
480f11c7f63SJim Harris 
481f11c7f63SJim Harris /**
482f11c7f63SJim Harris  * This method simply performs the common portion of pushing a list element
483f11c7f63SJim Harris  * onto a list.
484f11c7f63SJim Harris  *
485f11c7f63SJim Harris  * WARNING: This is a private helper method that should not be called directly
486f11c7f63SJim Harris  *          by any users.
487f11c7f63SJim Harris  */
private_push_front(SCI_ABSTRACT_ELEMENT_LIST_T * privateList_p,SCI_ABSTRACT_ELEMENT_T * alElement_p)488f11c7f63SJim Harris void private_push_front(
489f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * privateList_p,
490f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p
491f11c7f63SJim Harris )
492f11c7f63SJim Harris {
493f11c7f63SJim Harris    if ((privateList_p)->front_p == NULL)
494f11c7f63SJim Harris    {
495f11c7f63SJim Harris       (privateList_p)->front_p = (privateList_p)->back_p = (alElement_p);
496f11c7f63SJim Harris       (alElement_p)->next_p = (alElement_p)->previous_p = NULL;
497f11c7f63SJim Harris    }
498f11c7f63SJim Harris    else
499f11c7f63SJim Harris    {
500f11c7f63SJim Harris       (alElement_p)->next_p                = (privateList_p)->front_p;
501f11c7f63SJim Harris       (alElement_p)->previous_p            = NULL;
502f11c7f63SJim Harris       (privateList_p)->front_p->previous_p = (alElement_p);
503f11c7f63SJim Harris       (privateList_p)->front_p             = (alElement_p);
504f11c7f63SJim Harris    }
505f11c7f63SJim Harris 
506f11c7f63SJim Harris    (privateList_p)->size++;
507f11c7f63SJim Harris }
508f11c7f63SJim Harris 
509f11c7f63SJim Harris /**
510f11c7f63SJim Harris  * This method simply performs the common portion of popping a list element
511f11c7f63SJim Harris  * from a list.
512f11c7f63SJim Harris  *
513f11c7f63SJim Harris  * WARNING: This is a private helper method that should not be called directly
514f11c7f63SJim Harris  *          by any users.
515f11c7f63SJim Harris  */
private_pop_front(SCI_ABSTRACT_ELEMENT_LIST_T * privateList_p)516f11c7f63SJim Harris SCI_ABSTRACT_ELEMENT_T * private_pop_front(
517f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * privateList_p
518f11c7f63SJim Harris )
519f11c7f63SJim Harris {
520f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p = (privateList_p)->front_p;
521f11c7f63SJim Harris 
522f11c7f63SJim Harris    if (alElement_p != NULL)
523f11c7f63SJim Harris    {
524f11c7f63SJim Harris       if ((privateList_p)->front_p == (privateList_p)->back_p)
525f11c7f63SJim Harris       {
526f11c7f63SJim Harris          (privateList_p)->front_p = (privateList_p)->back_p = NULL;
527f11c7f63SJim Harris       }
528f11c7f63SJim Harris       else
529f11c7f63SJim Harris       {
530f11c7f63SJim Harris          (privateList_p)->front_p = (privateList_p)->front_p->next_p;
531f11c7f63SJim Harris          (privateList_p)->front_p->previous_p = NULL;
532f11c7f63SJim Harris       }
533f11c7f63SJim Harris 
534f11c7f63SJim Harris       (privateList_p)->size--;
535f11c7f63SJim Harris    }
536f11c7f63SJim Harris 
537f11c7f63SJim Harris    return alElement_p;
538f11c7f63SJim Harris }
539f11c7f63SJim Harris 
540f11c7f63SJim Harris /**
541f11c7f63SJim Harris  * This method will simply search the supplied list for the desired object.
542f11c7f63SJim Harris  * It will return a pointer to the abstract_list_element if found, otherwise
543f11c7f63SJim Harris  * it will return NULL.
544f11c7f63SJim Harris  */
private_find(SCI_ABSTRACT_ELEMENT_LIST_T * list_p,void * obj_p)545f11c7f63SJim Harris SCI_ABSTRACT_ELEMENT_T * private_find(
546f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * list_p,
547f11c7f63SJim Harris    void * obj_p
548f11c7f63SJim Harris )
549f11c7f63SJim Harris {
550f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p = (list_p)->front_p;
551f11c7f63SJim Harris 
552f11c7f63SJim Harris    while (alElement_p != NULL)
553f11c7f63SJim Harris    {
554f11c7f63SJim Harris       /* Check to see if we found the object for which we are searching. */
555f11c7f63SJim Harris       if (alElement_p->object_p == (void*) (obj_p))
556f11c7f63SJim Harris       {
557f11c7f63SJim Harris          break;
558f11c7f63SJim Harris       }
559f11c7f63SJim Harris 
560f11c7f63SJim Harris       alElement_p = alElement_p->next_p;
561f11c7f63SJim Harris    }
562f11c7f63SJim Harris 
563f11c7f63SJim Harris    return alElement_p;
564f11c7f63SJim Harris }
565f11c7f63SJim Harris 
566f11c7f63SJim Harris /**
567f11c7f63SJim Harris  * This private method will free the supplied list element back to the pool
568f11c7f63SJim Harris  * of free list elements.
569f11c7f63SJim Harris  */
private_pool_free(SCI_ABSTRACT_ELEMENT_POOL_T * free_pool,SCI_ABSTRACT_ELEMENT_T * alElement_p)570f11c7f63SJim Harris void private_pool_free(
571f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_POOL_T * free_pool,
572f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p
573f11c7f63SJim Harris )
574f11c7f63SJim Harris {
575f11c7f63SJim Harris    /* Push the list element back to the head to get better locality of */
576f11c7f63SJim Harris    /* reference with the cache.                                        */
577f11c7f63SJim Harris    private_push_front(&(free_pool)->free_list, (alElement_p));
578f11c7f63SJim Harris }
579f11c7f63SJim Harris 
580f11c7f63SJim Harris /**
581f11c7f63SJim Harris  * This private method will allocate a list element from the pool of free
582f11c7f63SJim Harris  * list elements.
583f11c7f63SJim Harris  */
private_pool_allocate(SCI_ABSTRACT_ELEMENT_POOL_T * free_pool)584f11c7f63SJim Harris SCI_ABSTRACT_ELEMENT_T * private_pool_allocate(
585f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_POOL_T * free_pool
586f11c7f63SJim Harris )
587f11c7f63SJim Harris {
588f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p;
589f11c7f63SJim Harris 
590f11c7f63SJim Harris    alElement_p = private_pop_front(&(free_pool)->free_list);
591f11c7f63SJim Harris 
592f11c7f63SJim Harris    alElement_p->next_p     = NULL;
593f11c7f63SJim Harris    alElement_p->previous_p = NULL;
594f11c7f63SJim Harris    alElement_p->object_p   = NULL;
595f11c7f63SJim Harris 
596f11c7f63SJim Harris    return alElement_p;
597f11c7f63SJim Harris }
598f11c7f63SJim Harris 
599f11c7f63SJim Harris #endif // USE_ABSTRACT_LIST_FUNCTIONS
600