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 #ifndef _SCI_SIMPLE_LIST_HEADER_
55f11c7f63SJim Harris #define _SCI_SIMPLE_LIST_HEADER_
56f11c7f63SJim Harris
57f11c7f63SJim Harris /**
58f11c7f63SJim Harris * @file
59f11c7f63SJim Harris *
60f11c7f63SJim Harris * @brief This header file contains simple linked list manipulation macros.
61f11c7f63SJim Harris * These macros differ from the SCI_FAST_LIST in that deletion of
62f11c7f63SJim Harris * an element from the list is O(n).
63f11c7f63SJim Harris * The reason for using this implementation over the SCI_FAST_LIST
64f11c7f63SJim Harris * is
65f11c7f63SJim Harris * 1) space savings as there is only a single link element instead
66f11c7f63SJim Harris * of the 2 link elements used in the SCI_FAST_LIST and
67f11c7f63SJim Harris * 2) it is possible to detach the entire list from its anchor
68f11c7f63SJim Harris * element for processing.
69f11c7f63SJim Harris *
70f11c7f63SJim Harris * @note Do not use the SCI_SIMPLE_LIST if you need to remove elements from
71f11c7f63SJim Harris * random locations within the list use instead the SCI_FAST_LIST.
72f11c7f63SJim Harris */
73f11c7f63SJim Harris
74f11c7f63SJim Harris
75f11c7f63SJim Harris //******************************************************************************
76f11c7f63SJim Harris //*
77f11c7f63SJim Harris //* P U B L I C M E T H O D S
78f11c7f63SJim Harris //*
79f11c7f63SJim Harris //******************************************************************************
80f11c7f63SJim Harris
81f11c7f63SJim Harris /**
82f11c7f63SJim Harris * Initialize the singely linked list anchor. The other macros require the
83f11c7f63SJim Harris * list anchor to be properly initialized.
84f11c7f63SJim Harris */
85f11c7f63SJim Harris #define sci_simple_list_init(anchor) \
86f11c7f63SJim Harris { \
87f11c7f63SJim Harris (anchor)->list_head = NULL; \
88f11c7f63SJim Harris (anchor)->list_tail = NULL; \
89f11c7f63SJim Harris (anchor)->list_count = 0; \
90f11c7f63SJim Harris }
91f11c7f63SJim Harris
92f11c7f63SJim Harris /**
93f11c7f63SJim Harris * Initialze the singely linked list element. The other macros require the
94f11c7f63SJim Harris * list element to be properly initialized.
95f11c7f63SJim Harris */
96f11c7f63SJim Harris #define sci_simple_list_element_init(list_object, element) \
97f11c7f63SJim Harris { \
98f11c7f63SJim Harris (element)->next = NULL; \
99f11c7f63SJim Harris (element)->object = (list_object); \
100f11c7f63SJim Harris }
101f11c7f63SJim Harris
102f11c7f63SJim Harris /**
103f11c7f63SJim Harris * See if there are any list elements on this list.
104f11c7f63SJim Harris */
105f11c7f63SJim Harris #define sci_simple_list_is_empty(anchor) ((anchor)->list_head == NULL)
106f11c7f63SJim Harris
107f11c7f63SJim Harris /**
108f11c7f63SJim Harris * Return a pointer to the list element at the head of the list. The list
109f11c7f63SJim Harris * element is not removed from the list.
110f11c7f63SJim Harris */
111f11c7f63SJim Harris #define sci_simple_list_get_head(anchor) ((anchor)->list_head)
112f11c7f63SJim Harris
113f11c7f63SJim Harris /**
114f11c7f63SJim Harris * Retuen a pointer to the lsit element at the tail of the list. The list
115f11c7f63SJim Harris * element is not removed from the list.
116f11c7f63SJim Harris */
117f11c7f63SJim Harris #define sci_simple_list_get_tail(anchor) ((anchor)->list_tail)
118f11c7f63SJim Harris
119f11c7f63SJim Harris /**
120f11c7f63SJim Harris * Return the count of the number of elements in this list.
121f11c7f63SJim Harris */
122f11c7f63SJim Harris #define sci_simple_list_get_count(anchor) ((anchor)->list_count)
123f11c7f63SJim Harris
124f11c7f63SJim Harris /**
125f11c7f63SJim Harris * Return a pointer to the list element following this list element.
126f11c7f63SJim Harris * If this is the last element in the list then NULL is returned.
127f11c7f63SJim Harris */
128f11c7f63SJim Harris #define sci_simple_list_get_next(element) ((element)->next)
129f11c7f63SJim Harris
130f11c7f63SJim Harris /**
131f11c7f63SJim Harris * Return the object represented by the list element.
132f11c7f63SJim Harris */
133f11c7f63SJim Harris #define sci_simple_list_get_object(element) ((element)->object)
134f11c7f63SJim Harris
135f11c7f63SJim Harris
136f11c7f63SJim Harris //******************************************************************************
137f11c7f63SJim Harris //*
138f11c7f63SJim Harris //* T Y P E S
139f11c7f63SJim Harris //*
140f11c7f63SJim Harris //******************************************************************************
141f11c7f63SJim Harris
142f11c7f63SJim Harris /**
143f11c7f63SJim Harris * @struct
144f11c7f63SJim Harris *
145f11c7f63SJim Harris * @brief This structure defines the list owner for singely linked list.
146f11c7f63SJim Harris */
147f11c7f63SJim Harris typedef struct SCI_SIMPLE_LIST
148f11c7f63SJim Harris {
149f11c7f63SJim Harris struct SCI_SIMPLE_LIST_ELEMENT *list_head;
150f11c7f63SJim Harris struct SCI_SIMPLE_LIST_ELEMENT *list_tail;
151f11c7f63SJim Harris U32 list_count;
152f11c7f63SJim Harris } SCI_SIMPLE_LIST_T;
153f11c7f63SJim Harris
154f11c7f63SJim Harris /**
155f11c7f63SJim Harris * @struct SCI_SIMPLE_LIST_ELEMENT
156f11c7f63SJim Harris *
157f11c7f63SJim Harris * @brief This structure defines what a singely linked list element contains.
158f11c7f63SJim Harris */
159f11c7f63SJim Harris typedef struct SCI_SIMPLE_LIST_ELEMENT
160f11c7f63SJim Harris {
161f11c7f63SJim Harris struct SCI_SIMPLE_LIST_ELEMENT *next;
162f11c7f63SJim Harris void *object;
163f11c7f63SJim Harris } SCI_SIMPLE_LIST_ELEMENT_T;
164f11c7f63SJim Harris
165f11c7f63SJim Harris /**
166f11c7f63SJim Harris * This method will insert the list element to the head of the list contained
167f11c7f63SJim Harris * by the anchor.
168f11c7f63SJim Harris *
169f11c7f63SJim Harris * @note Pushing new elements onto a list is more efficient than inserting
170f11c7f63SJim Harris * them to the tail of the list though both are O(1) operations.
171f11c7f63SJim Harris */
172f11c7f63SJim Harris INLINE
sci_simple_list_insert_head(SCI_SIMPLE_LIST_T * anchor,SCI_SIMPLE_LIST_ELEMENT_T * element)173f11c7f63SJim Harris static void sci_simple_list_insert_head(
174f11c7f63SJim Harris SCI_SIMPLE_LIST_T * anchor,
175f11c7f63SJim Harris SCI_SIMPLE_LIST_ELEMENT_T *element
176f11c7f63SJim Harris )
177f11c7f63SJim Harris {
178f11c7f63SJim Harris if (anchor->list_tail == NULL)
179f11c7f63SJim Harris {
180f11c7f63SJim Harris anchor->list_tail = element;
181f11c7f63SJim Harris }
182f11c7f63SJim Harris
183f11c7f63SJim Harris element->next = anchor->list_head;
184f11c7f63SJim Harris anchor->list_head = element;
185f11c7f63SJim Harris anchor->list_count++;
186f11c7f63SJim Harris }
187f11c7f63SJim Harris
188f11c7f63SJim Harris /**
189f11c7f63SJim Harris * This methos will insert the list element to the tail of the list contained
190f11c7f63SJim Harris * by the anchor.
191f11c7f63SJim Harris *
192f11c7f63SJim Harris * @param[in, out] anchor this is the list into which the element is to be
193f11c7f63SJim Harris * inserted
194f11c7f63SJim Harris * @param[in] element this is the element which to insert into the list.
195f11c7f63SJim Harris *
196f11c7f63SJim Harris * @note Pushing new elements onto a list is more efficient than inserting
197f11c7f63SJim Harris * them to the tail of the list though both are O(1) operations.
198f11c7f63SJim Harris */
199f11c7f63SJim Harris INLINE
sci_simple_list_insert_tail(SCI_SIMPLE_LIST_T * anchor,SCI_SIMPLE_LIST_ELEMENT_T * element)200f11c7f63SJim Harris static void sci_simple_list_insert_tail(
201f11c7f63SJim Harris SCI_SIMPLE_LIST_T * anchor,
202f11c7f63SJim Harris SCI_SIMPLE_LIST_ELEMENT_T *element
203f11c7f63SJim Harris )
204f11c7f63SJim Harris {
205f11c7f63SJim Harris if (anchor->list_tail == NULL)
206f11c7f63SJim Harris {
207f11c7f63SJim Harris anchor->list_head = element;
208f11c7f63SJim Harris }
209f11c7f63SJim Harris else
210f11c7f63SJim Harris {
211f11c7f63SJim Harris anchor->list_tail->next = element;
212f11c7f63SJim Harris }
213f11c7f63SJim Harris
214f11c7f63SJim Harris anchor->list_tail = element;
215f11c7f63SJim Harris anchor->list_count++;
216f11c7f63SJim Harris }
217f11c7f63SJim Harris
218f11c7f63SJim Harris /**
219f11c7f63SJim Harris * This method will remove the list element from the anchor and return the
220f11c7f63SJim Harris * object pointed to by that list element.
221f11c7f63SJim Harris *
222f11c7f63SJim Harris * @param[in, out] anchor this is the list into which the element is to be
223f11c7f63SJim Harris * inserted
224f11c7f63SJim Harris *
225f11c7f63SJim Harris * @return the list element at the head of the list.
226f11c7f63SJim Harris */
227f11c7f63SJim Harris INLINE
sci_simple_list_remove_head(SCI_SIMPLE_LIST_T * anchor)228f11c7f63SJim Harris static void * sci_simple_list_remove_head(
229f11c7f63SJim Harris SCI_SIMPLE_LIST_T * anchor
230f11c7f63SJim Harris )
231f11c7f63SJim Harris {
232f11c7f63SJim Harris void * object = NULL;
233f11c7f63SJim Harris
234f11c7f63SJim Harris if (anchor->list_head != NULL)
235f11c7f63SJim Harris {
236f11c7f63SJim Harris object = anchor->list_head->object;
237f11c7f63SJim Harris
238f11c7f63SJim Harris anchor->list_head = anchor->list_head->next;
239f11c7f63SJim Harris
240f11c7f63SJim Harris if (anchor->list_head == NULL)
241f11c7f63SJim Harris {
242f11c7f63SJim Harris anchor->list_tail = NULL;
243f11c7f63SJim Harris }
244f11c7f63SJim Harris
245f11c7f63SJim Harris anchor->list_count--;
246f11c7f63SJim Harris }
247f11c7f63SJim Harris
248f11c7f63SJim Harris return object;
249f11c7f63SJim Harris }
250f11c7f63SJim Harris
251f11c7f63SJim Harris /**
252f11c7f63SJim Harris * Move all the list elements from source anchor to the dest anchor.
253f11c7f63SJim Harris * The source anchor will have all of its elements removed making it
254f11c7f63SJim Harris * an empty list and the dest anchor will contain all of the source
255f11c7f63SJim Harris * and dest list elements.
256f11c7f63SJim Harris *
257f11c7f63SJim Harris * @param[in, out] dest_anchor this is the list into which all elements from
258f11c7f63SJim Harris * the source list are to be moved.
259f11c7f63SJim Harris * @param[in, out] source_anchor this is the list which is to be moved to the
260f11c7f63SJim Harris * destination list. This list will be empty on return.
261f11c7f63SJim Harris *
262f11c7f63SJim Harris * @return the list element at the head of the list.
263f11c7f63SJim Harris * @note If the destination has list elements use the insert at head
264f11c7f63SJim Harris * or tail routines instead.
265f11c7f63SJim Harris */
266f11c7f63SJim Harris INLINE
sci_simple_list_move_list(SCI_SIMPLE_LIST_T * dest_anchor,SCI_SIMPLE_LIST_T * source_anchor)267f11c7f63SJim Harris static void sci_simple_list_move_list(
268f11c7f63SJim Harris SCI_SIMPLE_LIST_T * dest_anchor,
269f11c7f63SJim Harris SCI_SIMPLE_LIST_T * source_anchor
270f11c7f63SJim Harris )
271f11c7f63SJim Harris {
272f11c7f63SJim Harris *dest_anchor = *source_anchor;
273f11c7f63SJim Harris
274f11c7f63SJim Harris sci_simple_list_init(source_anchor);
275f11c7f63SJim Harris }
276f11c7f63SJim Harris
277f11c7f63SJim Harris /**
278f11c7f63SJim Harris * This method will insert the list elements from the source anchor to the
279f11c7f63SJim Harris * destination list before all previous elements on the destination list.
280f11c7f63SJim Harris *
281f11c7f63SJim Harris * @param[in, out] dest_anchor this is the list into which all elements from
282f11c7f63SJim Harris * the source list are to be moved. The destination list will
283f11c7f63SJim Harris * now contain both sets of list elements.
284f11c7f63SJim Harris * @param[in, out] source_anchor this is the list which is to be moved to the
285f11c7f63SJim Harris * destination list. This list will be empty on return.
286f11c7f63SJim Harris */
287f11c7f63SJim Harris INLINE
sci_simple_list_insert_list_at_head(SCI_SIMPLE_LIST_T * dest_anchor,SCI_SIMPLE_LIST_T * source_anchor)288f11c7f63SJim Harris static void sci_simple_list_insert_list_at_head(
289f11c7f63SJim Harris SCI_SIMPLE_LIST_T * dest_anchor,
290f11c7f63SJim Harris SCI_SIMPLE_LIST_T * source_anchor
291f11c7f63SJim Harris )
292f11c7f63SJim Harris {
293f11c7f63SJim Harris if (!sci_simple_list_is_empty(source_anchor))
294f11c7f63SJim Harris {
295f11c7f63SJim Harris if (sci_simple_list_is_empty(dest_anchor))
296f11c7f63SJim Harris {
297f11c7f63SJim Harris // Destination is empty just copy the source on over
298f11c7f63SJim Harris *dest_anchor = *source_anchor;
299f11c7f63SJim Harris }
300f11c7f63SJim Harris else
301f11c7f63SJim Harris {
302f11c7f63SJim Harris source_anchor->list_tail->next = dest_anchor->list_head;
303f11c7f63SJim Harris dest_anchor->list_head = source_anchor->list_head;
304f11c7f63SJim Harris dest_anchor->list_count += source_anchor->list_count;
305f11c7f63SJim Harris }
306f11c7f63SJim Harris
307f11c7f63SJim Harris // Wipe the source list to make sure the list elements can not be accessed
308453130d9SPedro F. Giffuni // from two separate lists at the same time.
309f11c7f63SJim Harris sci_simple_list_init(source_anchor);
310f11c7f63SJim Harris }
311f11c7f63SJim Harris }
312f11c7f63SJim Harris
313f11c7f63SJim Harris /**
314f11c7f63SJim Harris * This method will insert the list elements from the source anchor to the
315f11c7f63SJim Harris * destination anchor after all list elements on the destination anchor.
316f11c7f63SJim Harris *
317f11c7f63SJim Harris * @param[in, out] dest_anchor this is the list into which all elements from
318f11c7f63SJim Harris * the source list are to be moved. The destination list will
319f11c7f63SJim Harris * contain both the source and destination list elements.
320f11c7f63SJim Harris * @param[in, out] source_anchor this is the list which is to be moved to the
321f11c7f63SJim Harris * destination list. This list will be empty on return.
322f11c7f63SJim Harris */
323f11c7f63SJim Harris INLINE
sci_simple_list_insert_list_at_tail(SCI_SIMPLE_LIST_T * dest_anchor,SCI_SIMPLE_LIST_T * source_anchor)324f11c7f63SJim Harris static void sci_simple_list_insert_list_at_tail(
325f11c7f63SJim Harris SCI_SIMPLE_LIST_T * dest_anchor,
326f11c7f63SJim Harris SCI_SIMPLE_LIST_T * source_anchor
327f11c7f63SJim Harris )
328f11c7f63SJim Harris {
329f11c7f63SJim Harris if (!sci_simple_list_is_empty(source_anchor))
330f11c7f63SJim Harris {
331f11c7f63SJim Harris if (sci_simple_list_is_empty(dest_anchor))
332f11c7f63SJim Harris {
333f11c7f63SJim Harris // Destination is empty just copy the source on over
334f11c7f63SJim Harris *dest_anchor = *source_anchor;
335f11c7f63SJim Harris }
336f11c7f63SJim Harris else
337f11c7f63SJim Harris {
338f11c7f63SJim Harris // If the source list is empty the desination list is the result.
339f11c7f63SJim Harris dest_anchor->list_tail->next = source_anchor->list_head;
340f11c7f63SJim Harris dest_anchor->list_tail = source_anchor->list_tail;
341f11c7f63SJim Harris dest_anchor->list_count += source_anchor->list_count;
342f11c7f63SJim Harris }
343f11c7f63SJim Harris
344f11c7f63SJim Harris // Wipe the source list to make sure the list elements can not be accessed
345453130d9SPedro F. Giffuni // from two separate lists at the same time.
346f11c7f63SJim Harris sci_simple_list_init(source_anchor);
347f11c7f63SJim Harris }
348f11c7f63SJim Harris }
349f11c7f63SJim Harris
350f11c7f63SJim Harris #endif // _SCI_SIMPLE_LIST_HEADER_
351