xref: /freebsd/sys/dev/isci/scil/sci_abstract_list.c (revision d889926162b4a8575bd554416b5f1a987ce68a83)
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       alElement_p = alElement_p->next_p;
234f11c7f63SJim Harris    }
235f11c7f63SJim Harris }
236f11c7f63SJim Harris #endif // defined(SCI_LOGGING)
237f11c7f63SJim Harris 
238f11c7f63SJim Harris 
239f11c7f63SJim Harris /**
240f11c7f63SJim Harris  * This method will simply search the supplied list for the desired object.
241f11c7f63SJim Harris  * It will return a pointer to the object, if it is found.  Otherwise
242f11c7f63SJim Harris  * it will return NULL.
243f11c7f63SJim Harris  */
sci_abstract_list_find(SCI_ABSTRACT_LIST_T * list_p,void * obj_p)244f11c7f63SJim Harris void * sci_abstract_list_find(
245f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p,
246f11c7f63SJim Harris    void * obj_p
247f11c7f63SJim Harris )
248f11c7f63SJim Harris {
249f11c7f63SJim Harris    return
250f11c7f63SJim Harris       sci_abstract_list_get_object(private_find(&(list_p)->elements, (obj_p)));
251f11c7f63SJim Harris }
252f11c7f63SJim Harris 
253f11c7f63SJim Harris 
254f11c7f63SJim Harris /**
255f11c7f63SJim Harris  * This method will simply remove the element at the back (tail) of the list.
256f11c7f63SJim Harris  * It will return a pointer to the object that was removed or NULL if not
257f11c7f63SJim Harris  * found.
258f11c7f63SJim Harris  */
sci_abstract_list_popback(SCI_ABSTRACT_LIST_T * list_p)259f11c7f63SJim Harris void * sci_abstract_list_popback(
260f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
261f11c7f63SJim Harris )
262f11c7f63SJim Harris {
263f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * elem_list = &(list_p)->elements;
264f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T      * alElement_p = elem_list->back_p;
265f11c7f63SJim Harris    void * obj_p = NULL;
266f11c7f63SJim Harris 
267f11c7f63SJim Harris    if (alElement_p != NULL)
268f11c7f63SJim Harris    {
269f11c7f63SJim Harris       obj_p = alElement_p->object_p;
270f11c7f63SJim Harris       if (elem_list->back_p == elem_list->front_p)
271f11c7f63SJim Harris       {
272f11c7f63SJim Harris          elem_list->back_p = elem_list->front_p = NULL;
273f11c7f63SJim Harris       }
274f11c7f63SJim Harris       else
275f11c7f63SJim Harris       {
276f11c7f63SJim Harris          elem_list->back_p = elem_list->back_p->previous_p;
277f11c7f63SJim Harris          elem_list->back_p->next_p = NULL;
278f11c7f63SJim Harris       }
279f11c7f63SJim Harris 
280f11c7f63SJim Harris       elem_list->size--;
281f11c7f63SJim Harris       private_pool_free((list_p)->free_pool, alElement_p);
282f11c7f63SJim Harris    }
283f11c7f63SJim Harris 
284f11c7f63SJim Harris    return obj_p;
285f11c7f63SJim Harris }
286f11c7f63SJim Harris 
287f11c7f63SJim Harris /**
288f11c7f63SJim Harris  * This method simply removes the list element at the head of the list
289f11c7f63SJim Harris  * and returns the pointer to the object that was removed.
290f11c7f63SJim Harris  */
sci_abstract_list_popfront(SCI_ABSTRACT_LIST_T * list_p)291f11c7f63SJim Harris void * sci_abstract_list_popfront(
292f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
293f11c7f63SJim Harris )
294f11c7f63SJim Harris {
295f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p =
296f11c7f63SJim Harris                               private_pop_front(&(list_p)->elements);
297f11c7f63SJim Harris    void * obj_p = NULL;
298f11c7f63SJim Harris 
299f11c7f63SJim Harris    if (alElement_p != NULL)
300f11c7f63SJim Harris    {
301f11c7f63SJim Harris       obj_p = alElement_p->object_p;
302f11c7f63SJim Harris       private_pool_free((list_p)->free_pool, alElement_p);
303f11c7f63SJim Harris    }
304f11c7f63SJim Harris 
305f11c7f63SJim Harris    return obj_p;
306f11c7f63SJim Harris }
307f11c7f63SJim Harris 
308f11c7f63SJim Harris 
309f11c7f63SJim Harris 
310f11c7f63SJim Harris /**
311f11c7f63SJim Harris  * This method will erase (remove) all instances of the supplied object from
312f11c7f63SJim Harris  * anywhere in the list.
313f11c7f63SJim Harris  */
sci_abstract_list_erase(SCI_ABSTRACT_LIST_T * list_p,void * obj_p)314f11c7f63SJim Harris void sci_abstract_list_erase(
315f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p,
316f11c7f63SJim Harris    void * obj_p
317f11c7f63SJim Harris )
318f11c7f63SJim Harris {
319f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * elem_list = &(list_p)->elements;
320f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T      * alElement_p;
321f11c7f63SJim Harris 
322f11c7f63SJim Harris    while ((alElement_p = private_find(elem_list, (obj_p))) != NULL)
323f11c7f63SJim Harris    {
324f11c7f63SJim Harris       if (alElement_p == elem_list->front_p)
325f11c7f63SJim Harris       {
326f11c7f63SJim Harris          sci_abstract_list_popfront(list_p);
327f11c7f63SJim Harris       }
328f11c7f63SJim Harris       else if (alElement_p == elem_list->back_p)
329f11c7f63SJim Harris       {
330f11c7f63SJim Harris          sci_abstract_list_popback(list_p);
331f11c7f63SJim Harris       }
332f11c7f63SJim Harris       else
333f11c7f63SJim Harris       {
334f11c7f63SJim Harris          alElement_p->previous_p->next_p = alElement_p->next_p;
335f11c7f63SJim Harris          alElement_p->next_p->previous_p = alElement_p->previous_p;
336f11c7f63SJim Harris          elem_list->size--;
337f11c7f63SJim Harris          private_pool_free((list_p)->free_pool, alElement_p);
338f11c7f63SJim Harris       }
339f11c7f63SJim Harris    }
340f11c7f63SJim Harris    return;
341f11c7f63SJim Harris }
342f11c7f63SJim Harris 
343f11c7f63SJim Harris /**
344f11c7f63SJim Harris  * This method simply adds a LIST_ELEMENT for the supplied object to the back
345f11c7f63SJim Harris  * (tail) of the supplied list.
346f11c7f63SJim Harris  */
sci_abstract_list_pushback(SCI_ABSTRACT_LIST_T * list_p,void * obj_p)347f11c7f63SJim Harris void sci_abstract_list_pushback(
348f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p,
349f11c7f63SJim Harris    void * obj_p
350f11c7f63SJim Harris )
351f11c7f63SJim Harris {
352f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * elem_list = &(list_p)->elements;
353f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T      * alElement_p
354f11c7f63SJim Harris       = private_pool_allocate((list_p)->free_pool);
355f11c7f63SJim Harris //   assert(alElement_p != NULL);
356f11c7f63SJim Harris 
357f11c7f63SJim Harris    alElement_p->object_p = (obj_p);
358f11c7f63SJim Harris 
359f11c7f63SJim Harris    if (elem_list->front_p == NULL)
360f11c7f63SJim Harris    {
361f11c7f63SJim Harris       elem_list->front_p = elem_list->back_p = alElement_p;
362f11c7f63SJim Harris    }
363f11c7f63SJim Harris    else
364f11c7f63SJim Harris    {
365f11c7f63SJim Harris       elem_list->back_p->next_p = alElement_p;
366f11c7f63SJim Harris       alElement_p->previous_p   = elem_list->back_p;
367f11c7f63SJim Harris       elem_list->back_p         = alElement_p;
368f11c7f63SJim Harris    }
369f11c7f63SJim Harris 
370f11c7f63SJim Harris    elem_list->size++;
371f11c7f63SJim Harris }
372f11c7f63SJim Harris 
373f11c7f63SJim Harris 
374f11c7f63SJim Harris 
375f11c7f63SJim Harris /**
376f11c7f63SJim Harris  * This method simply adds a LIST_ELEMENT for the supplied object to the front
377f11c7f63SJim Harris  * (head) of the supplied list.
378f11c7f63SJim Harris  */
sci_abstract_list_pushfront(SCI_ABSTRACT_LIST_T * list_p,void * obj_p)379f11c7f63SJim Harris void sci_abstract_list_pushfront(
380f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p,
381f11c7f63SJim Harris    void * obj_p
382f11c7f63SJim Harris )
383f11c7f63SJim Harris {
384f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p =
385f11c7f63SJim Harris          private_pool_allocate((list_p)->free_pool);
386f11c7f63SJim Harris    alElement_p->object_p = (obj_p);
387f11c7f63SJim Harris    private_push_front(&(list_p)->elements, alElement_p);
388f11c7f63SJim Harris }
389f11c7f63SJim Harris 
390f11c7f63SJim Harris 
391f11c7f63SJim Harris /**
392f11c7f63SJim Harris  * This method will add the objToAdd_p object to the list before the obj_p.
393f11c7f63SJim Harris  *
394f11c7f63SJim Harris  */
sci_abstract_list_insert(SCI_ABSTRACT_LIST_T * list_p,void * obj_p,void * objToAdd_p)395f11c7f63SJim Harris void sci_abstract_list_insert(
396f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p,
397f11c7f63SJim Harris    void * obj_p,
398f11c7f63SJim Harris    void * objToAdd_p
399f11c7f63SJim Harris )
400f11c7f63SJim Harris {
401f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * elem_list = &(list_p)->elements;
402f11c7f63SJim Harris 
403f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * obj_element = private_find(elem_list, obj_p);
404f11c7f63SJim Harris 
405f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * objToAdd_element =
406f11c7f63SJim Harris       private_pool_allocate((list_p)->free_pool);
407f11c7f63SJim Harris 
408f11c7f63SJim Harris    objToAdd_element->object_p = objToAdd_p;
409f11c7f63SJim Harris 
410f11c7f63SJim Harris    ASSERT(obj_element != NULL);
411f11c7f63SJim Harris    ASSERT(objToAdd_element != NULL);
412f11c7f63SJim Harris 
413f11c7f63SJim Harris    if (obj_element == elem_list->front_p)
414f11c7f63SJim Harris    {
415f11c7f63SJim Harris       objToAdd_element->object_p = (objToAdd_p);
416f11c7f63SJim Harris       private_push_front(&(list_p)->elements, objToAdd_element);
417f11c7f63SJim Harris    }
418f11c7f63SJim Harris    else
419f11c7f63SJim Harris    {
420f11c7f63SJim Harris       obj_element->previous_p->next_p = objToAdd_element;
421f11c7f63SJim Harris       objToAdd_element->previous_p = obj_element->previous_p;
422f11c7f63SJim Harris 
423f11c7f63SJim Harris       obj_element->previous_p = objToAdd_element;
424f11c7f63SJim Harris       objToAdd_element->next_p = obj_element;
425f11c7f63SJim Harris 
426f11c7f63SJim Harris       elem_list->size++;
427f11c7f63SJim Harris    }
428f11c7f63SJim Harris }
429f11c7f63SJim Harris 
430f11c7f63SJim Harris /**
431f11c7f63SJim Harris  * This method simply frees all the items from the list.
432f11c7f63SJim Harris  */
sci_abstract_list_clear(SCI_ABSTRACT_LIST_T * list_p)433f11c7f63SJim Harris void sci_abstract_list_clear(
434f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * list_p
435f11c7f63SJim Harris )
436f11c7f63SJim Harris {
437f11c7f63SJim Harris    while ((list_p)->elements.size > 0)
438f11c7f63SJim Harris       sci_abstract_list_popfront((list_p));
439f11c7f63SJim Harris }
440f11c7f63SJim Harris 
441f11c7f63SJim Harris /**
442f11c7f63SJim Harris  * This method simply returns the object being pointed to by the list element
443f11c7f63SJim Harris  * (The item being listed).
444f11c7f63SJim Harris  */
sci_abstract_list_get_object(SCI_ABSTRACT_ELEMENT_T * alElement_p)445f11c7f63SJim Harris void * sci_abstract_list_get_object(
446f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p
447f11c7f63SJim Harris )
448f11c7f63SJim Harris {
449f11c7f63SJim Harris    void * obj_p = NULL;
450f11c7f63SJim Harris    if ((alElement_p) != NULL)
451f11c7f63SJim Harris       obj_p = (alElement_p)->object_p;
452f11c7f63SJim Harris 
453f11c7f63SJim Harris    return obj_p;
454f11c7f63SJim Harris }
455f11c7f63SJim Harris 
456f11c7f63SJim Harris 
457f11c7f63SJim Harris /**
458f11c7f63SJim Harris  * This method is simply a wrapper to provide the number of elements in
459f11c7f63SJim Harris  * the free list.
460f11c7f63SJim Harris  */
sci_abstract_list_freeList_size(SCI_ABSTRACT_LIST_T * freeList)461f11c7f63SJim Harris U32 sci_abstract_list_freeList_size(
462f11c7f63SJim Harris    SCI_ABSTRACT_LIST_T * freeList
463f11c7f63SJim Harris )
464f11c7f63SJim Harris {
465f11c7f63SJim Harris    return (sci_abstract_list_size(freeList));
466f11c7f63SJim Harris }
467f11c7f63SJim Harris 
468f11c7f63SJim Harris //******************************************************************************
469f11c7f63SJim Harris //*
470f11c7f63SJim Harris //*     P R I V A T E   M E T H O D S
471f11c7f63SJim Harris //*
472f11c7f63SJim Harris //******************************************************************************
473f11c7f63SJim Harris 
474f11c7f63SJim Harris /**
475f11c7f63SJim Harris  * This method simply performs the common portion of pushing a list element
476f11c7f63SJim Harris  * onto a list.
477f11c7f63SJim Harris  *
478f11c7f63SJim Harris  * WARNING: This is a private helper method that should not be called directly
479f11c7f63SJim Harris  *          by any users.
480f11c7f63SJim Harris  */
private_push_front(SCI_ABSTRACT_ELEMENT_LIST_T * privateList_p,SCI_ABSTRACT_ELEMENT_T * alElement_p)481f11c7f63SJim Harris void private_push_front(
482f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * privateList_p,
483f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p
484f11c7f63SJim Harris )
485f11c7f63SJim Harris {
486f11c7f63SJim Harris    if ((privateList_p)->front_p == NULL)
487f11c7f63SJim Harris    {
488f11c7f63SJim Harris       (privateList_p)->front_p = (privateList_p)->back_p = (alElement_p);
489f11c7f63SJim Harris       (alElement_p)->next_p = (alElement_p)->previous_p = NULL;
490f11c7f63SJim Harris    }
491f11c7f63SJim Harris    else
492f11c7f63SJim Harris    {
493f11c7f63SJim Harris       (alElement_p)->next_p                = (privateList_p)->front_p;
494f11c7f63SJim Harris       (alElement_p)->previous_p            = NULL;
495f11c7f63SJim Harris       (privateList_p)->front_p->previous_p = (alElement_p);
496f11c7f63SJim Harris       (privateList_p)->front_p             = (alElement_p);
497f11c7f63SJim Harris    }
498f11c7f63SJim Harris 
499f11c7f63SJim Harris    (privateList_p)->size++;
500f11c7f63SJim Harris }
501f11c7f63SJim Harris 
502f11c7f63SJim Harris /**
503f11c7f63SJim Harris  * This method simply performs the common portion of popping a list element
504f11c7f63SJim Harris  * from a list.
505f11c7f63SJim Harris  *
506f11c7f63SJim Harris  * WARNING: This is a private helper method that should not be called directly
507f11c7f63SJim Harris  *          by any users.
508f11c7f63SJim Harris  */
private_pop_front(SCI_ABSTRACT_ELEMENT_LIST_T * privateList_p)509f11c7f63SJim Harris SCI_ABSTRACT_ELEMENT_T * private_pop_front(
510f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * privateList_p
511f11c7f63SJim Harris )
512f11c7f63SJim Harris {
513f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p = (privateList_p)->front_p;
514f11c7f63SJim Harris 
515f11c7f63SJim Harris    if (alElement_p != NULL)
516f11c7f63SJim Harris    {
517f11c7f63SJim Harris       if ((privateList_p)->front_p == (privateList_p)->back_p)
518f11c7f63SJim Harris       {
519f11c7f63SJim Harris          (privateList_p)->front_p = (privateList_p)->back_p = NULL;
520f11c7f63SJim Harris       }
521f11c7f63SJim Harris       else
522f11c7f63SJim Harris       {
523f11c7f63SJim Harris          (privateList_p)->front_p = (privateList_p)->front_p->next_p;
524f11c7f63SJim Harris          (privateList_p)->front_p->previous_p = NULL;
525f11c7f63SJim Harris       }
526f11c7f63SJim Harris 
527f11c7f63SJim Harris       (privateList_p)->size--;
528f11c7f63SJim Harris    }
529f11c7f63SJim Harris 
530f11c7f63SJim Harris    return alElement_p;
531f11c7f63SJim Harris }
532f11c7f63SJim Harris 
533f11c7f63SJim Harris /**
534f11c7f63SJim Harris  * This method will simply search the supplied list for the desired object.
535f11c7f63SJim Harris  * It will return a pointer to the abstract_list_element if found, otherwise
536f11c7f63SJim Harris  * it will return NULL.
537f11c7f63SJim Harris  */
private_find(SCI_ABSTRACT_ELEMENT_LIST_T * list_p,void * obj_p)538f11c7f63SJim Harris SCI_ABSTRACT_ELEMENT_T * private_find(
539f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_LIST_T * list_p,
540f11c7f63SJim Harris    void * obj_p
541f11c7f63SJim Harris )
542f11c7f63SJim Harris {
543f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p = (list_p)->front_p;
544f11c7f63SJim Harris 
545f11c7f63SJim Harris    while (alElement_p != NULL)
546f11c7f63SJim Harris    {
547f11c7f63SJim Harris       /* Check to see if we found the object for which we are searching. */
548f11c7f63SJim Harris       if (alElement_p->object_p == (void*) (obj_p))
549f11c7f63SJim Harris       {
550f11c7f63SJim Harris          break;
551f11c7f63SJim Harris       }
552f11c7f63SJim Harris 
553f11c7f63SJim Harris       alElement_p = alElement_p->next_p;
554f11c7f63SJim Harris    }
555f11c7f63SJim Harris 
556f11c7f63SJim Harris    return alElement_p;
557f11c7f63SJim Harris }
558f11c7f63SJim Harris 
559f11c7f63SJim Harris /**
560f11c7f63SJim Harris  * This private method will free the supplied list element back to the pool
561f11c7f63SJim Harris  * of free list elements.
562f11c7f63SJim Harris  */
private_pool_free(SCI_ABSTRACT_ELEMENT_POOL_T * free_pool,SCI_ABSTRACT_ELEMENT_T * alElement_p)563f11c7f63SJim Harris void private_pool_free(
564f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_POOL_T * free_pool,
565f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p
566f11c7f63SJim Harris )
567f11c7f63SJim Harris {
568f11c7f63SJim Harris    /* Push the list element back to the head to get better locality of */
569f11c7f63SJim Harris    /* reference with the cache.                                        */
570f11c7f63SJim Harris    private_push_front(&(free_pool)->free_list, (alElement_p));
571f11c7f63SJim Harris }
572f11c7f63SJim Harris 
573f11c7f63SJim Harris /**
574f11c7f63SJim Harris  * This private method will allocate a list element from the pool of free
575f11c7f63SJim Harris  * list elements.
576f11c7f63SJim Harris  */
private_pool_allocate(SCI_ABSTRACT_ELEMENT_POOL_T * free_pool)577f11c7f63SJim Harris SCI_ABSTRACT_ELEMENT_T * private_pool_allocate(
578f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_POOL_T * free_pool
579f11c7f63SJim Harris )
580f11c7f63SJim Harris {
581f11c7f63SJim Harris    SCI_ABSTRACT_ELEMENT_T * alElement_p;
582f11c7f63SJim Harris 
583f11c7f63SJim Harris    alElement_p = private_pop_front(&(free_pool)->free_list);
584f11c7f63SJim Harris 
585f11c7f63SJim Harris    alElement_p->next_p     = NULL;
586f11c7f63SJim Harris    alElement_p->previous_p = NULL;
587f11c7f63SJim Harris    alElement_p->object_p   = NULL;
588f11c7f63SJim Harris 
589f11c7f63SJim Harris    return alElement_p;
590f11c7f63SJim Harris }
591f11c7f63SJim Harris 
592f11c7f63SJim Harris #endif // USE_ABSTRACT_LIST_FUNCTIONS
593