1/*
2Copyright (c) 2012, Broadcom Europe Ltd
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef MMAL_LIST_H
29#define MMAL_LIST_H
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35/** \defgroup MmalList Generic Linked List
36 * This provides a thread-safe implementation of a linked list which can be used
37 * with any data type. */
38/* @{ */
39
40/** Single element in the list */
41typedef struct MMAL_LIST_ELEMENT_T
42{
43 struct MMAL_LIST_ELEMENT_T *next;
44 struct MMAL_LIST_ELEMENT_T *prev;
45} MMAL_LIST_ELEMENT_T;
46
47/** Linked list type.
48 * Clients shouldn't modify this directly. Use the provided API functions to
49 * add new elements. The public members are only for debug purposes.
50 * */
51typedef struct MMAL_LIST_T
52{
53 unsigned int length; /**< Number of elements in the list (read-only) */
54 MMAL_LIST_ELEMENT_T *first; /**< First element in the list (read-only) */
55 MMAL_LIST_ELEMENT_T *last; /**< Last element in the list (read-only) */
56} MMAL_LIST_T;
57
58/** Create a new linked list.
59 *
60 * @return Pointer to new queue (NULL on failure).
61 */
62MMAL_LIST_T* mmal_list_create(void);
63
64/** Destroy a linked list.
65 *
66 * @param list List to destroy
67 */
68void mmal_list_destroy(MMAL_LIST_T *list);
69
70/** Remove the last element in the list.
71 *
72 * @param list List to remove from
73 *
74 * @return Pointer to the last element (or NULL if empty)
75 */
76MMAL_LIST_ELEMENT_T* mmal_list_pop_back(MMAL_LIST_T *list);
77
78/** Remove the first element in the list.
79 *
80 * @param list List to remove from
81 *
82 * @return Pointer to the first element (or NULL if empty)
83 */
84MMAL_LIST_ELEMENT_T* mmal_list_pop_front(MMAL_LIST_T *list);
85
86/** Add an element to the front of the list.
87 *
88 * @param list List to add to
89 * @param element The element to add
90 */
91void mmal_list_push_front(MMAL_LIST_T *list, MMAL_LIST_ELEMENT_T *element);
92
93/** Add an element to the back of the list.
94 *
95 * @param list List to add to
96 * @param element The element to add
97 */
98void mmal_list_push_back(MMAL_LIST_T *list, MMAL_LIST_ELEMENT_T *element);
99
100/** List comparison function.
101 * This is supplied by a client when inserting an element in
102 * the middle of the list. The list will always insert a smaller
103 * element in front of a larger element.
104 *
105 * @return TRUE: lhs < rhs
106 * FALSE: lhs >= rhs
107 */
108typedef int (*MMAL_LIST_COMPARE_T)(MMAL_LIST_ELEMENT_T *lhs, MMAL_LIST_ELEMENT_T *rhs);
109
110/** Insert an element into the list.
111 * The location where the element is inserted is determined using
112 * the supplied comparison function. Smaller elements are inserted
113 * in front of larger elements.
114 *
115 * @param list List to add to
116 * @param element The element to insert
117 * @param compare Comparison function supplied by the client
118 */
119void mmal_list_insert(MMAL_LIST_T *list, MMAL_LIST_ELEMENT_T *element, MMAL_LIST_COMPARE_T compare);
120
121/* @} */
122
123#ifdef __cplusplus
124}
125#endif
126
127#endif /* MMAL_LIST_H */
128