2 * Copyright (c) 2017 Broadcom. All rights reserved.
3 * The term "Broadcom" refers to Broadcom Limited and/or its subsidiaries.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
41 #if !defined(__OCS_LIST_H__)
42 #define __OCS_LIST_H__
44 #define OCS_LIST_DEBUG
46 #if defined(OCS_LIST_DEBUG)
48 #define ocs_list_magic_decl uint32_t magic;
49 #define OCS_LIST_LIST_MAGIC 0xcafe0000
50 #define OCS_LIST_LINK_MAGIC 0xcafe0001
51 #define ocs_list_set_list_magic list->magic = OCS_LIST_LIST_MAGIC
52 #define ocs_list_set_link_magic list->magic = OCS_LIST_LINK_MAGIC
54 #define ocs_list_assert(cond, ...) \
59 #define ocs_list_magic_decl
60 #define ocs_list_assert(cond, ...)
61 #define ocs_list_set_list_magic
62 #define ocs_list_set_link_magic
66 * @brief list/link structure
68 * used for both the list object, and the link object(s). offset
69 * is specified when the list is initialized; this implies that a list
70 * will always point to objects of the same type. offset is not used
71 * when ocs_list_t is used as a link (ocs_list_link_t).
75 typedef struct ocs_list_s ocs_list_t;
77 ocs_list_magic_decl /*<< used if debugging is enabled */
78 ocs_list_t *next; /*<< pointer to head of list (or next if link) */
79 ocs_list_t *prev; /*<< pointer to tail of list (or previous if link) */
80 uint32_t offset; /*<< offset in bytes to the link element of the objects in list */
82 typedef ocs_list_t ocs_list_link_t;
84 /* item2link - return pointer to link given pointer to an item */
85 #define item2link(list, item) ((ocs_list_t*) (((uint8_t*)(item)) + (list)->offset))
87 /* link2item - return pointer to item given pointer to a link */
88 #define link2item(list, link) ((void*) (((uint8_t*)(link)) - (list)->offset))
91 * @brief Initialize a list
93 * A list object is initialized. Helper define is used to call _ocs_list_init() with
94 * offsetof(type, link)
96 * @param list Pointer to list
97 * @param offset Offset in bytes in item to the link element
102 _ocs_list_init(ocs_list_t *list, uint32_t offset)
104 ocs_list_assert(list);
105 ocs_list_set_list_magic;
109 list->offset = offset;
111 #define ocs_list_init(head, type, link) _ocs_list_init(head, offsetof(type, link))
115 * @brief Test if a list is empty
117 * @param list Pointer to list head
119 * @return 1 if empty, 0 otherwise
121 static inline int32_t
122 ocs_list_empty(ocs_list_t *list)
124 ocs_list_assert(list, 1);
125 ocs_list_assert(list->magic == OCS_LIST_LIST_MAGIC, 1);
126 return list->next == list;
131 * @brief Test if a list is valid (ready for use)
133 * @param list Pointer to list head
135 * @return true if list is usable, false otherwise
138 ocs_list_valid(ocs_list_t *list)
140 return (list->magic == OCS_LIST_LIST_MAGIC);
144 * @brief Insert link between two other links
146 * Inserts a link in between two other links
148 * @param a Pointer to first link
149 * @param b Pointer to next link
150 * @param c Pointer to link to insert between a and b
155 _ocs_list_insert_link(ocs_list_t *a, ocs_list_t *b, ocs_list_t *c)
158 ocs_list_assert((a->magic == OCS_LIST_LIST_MAGIC) || (a->magic == OCS_LIST_LINK_MAGIC));
159 ocs_list_assert(a->next);
160 ocs_list_assert(a->prev);
162 ocs_list_assert((b->magic == OCS_LIST_LIST_MAGIC) || (b->magic == OCS_LIST_LINK_MAGIC));
163 ocs_list_assert(b->next);
164 ocs_list_assert(b->prev);
166 ocs_list_assert((c->magic == OCS_LIST_LIST_MAGIC) || (c->magic == OCS_LIST_LINK_MAGIC));
167 ocs_list_assert(!c->next);
168 ocs_list_assert(!c->prev);
170 ocs_list_assert(a->offset == b->offset);
171 ocs_list_assert(b->offset == c->offset);
179 #if defined(OCS_LIST_DEBUG)
181 * @brief Initialize a list link for debug purposes
183 * For debugging a linked list link element has a magic number that is initialized,
184 * and the offset value initialzied and used for subsequent assertions.
187 * @param list Pointer to list head
188 * @param link Pointer to link to be initialized
193 ocs_list_init_link(ocs_list_t *list, ocs_list_t *link)
195 ocs_list_assert(list);
196 ocs_list_assert(list->magic == OCS_LIST_LIST_MAGIC);
197 ocs_list_assert(link);
199 if (link->magic == 0) {
200 link->magic = OCS_LIST_LINK_MAGIC;
201 link->offset = list->offset;
207 #define ocs_list_init_link(...)
212 * @brief Add an item to the head of the list
214 * @param list Pointer to list head
215 * @param item Item to add
218 ocs_list_add_head(ocs_list_t *list, void *item)
222 ocs_list_assert(list);
223 ocs_list_assert(list->magic == OCS_LIST_LIST_MAGIC);
224 ocs_list_assert(item);
226 link = item2link(list, item);
227 ocs_list_init_link(list, link);
229 ocs_list_assert(link->magic == OCS_LIST_LINK_MAGIC);
230 ocs_list_assert(link->offset == list->offset);
231 ocs_list_assert(link->next == NULL);
232 ocs_list_assert(link->prev == NULL);
234 _ocs_list_insert_link(list, list->next, item2link(list, item));
239 * @brief Add an item to the tail of the list
241 * @param list Head of the list
242 * @param item Item to add
245 ocs_list_add_tail(ocs_list_t *list, void *item)
249 ocs_list_assert(list);
250 ocs_list_assert(list->magic == OCS_LIST_LIST_MAGIC);
251 ocs_list_assert(item);
253 link = item2link(list, item);
254 ocs_list_init_link(list, link);
256 ocs_list_assert(link->magic == OCS_LIST_LINK_MAGIC);
257 ocs_list_assert(link->offset == list->offset);
258 ocs_list_assert(link->next == NULL);
259 ocs_list_assert(link->prev == NULL);
261 _ocs_list_insert_link(list->prev, list, link);
266 * @brief Return the first item in the list
268 * @param list Head of the list
270 * @return pointer to the first item, NULL otherwise
273 ocs_list_get_head(ocs_list_t *list)
275 ocs_list_assert(list, NULL);
276 ocs_list_assert(list->magic == OCS_LIST_LIST_MAGIC, NULL);
277 return ocs_list_empty(list) ? NULL : link2item(list, list->next);
282 * @brief Return the first item in the list
284 * @param list head of the list
286 * @return pointer to the last item, NULL otherwise
289 ocs_list_get_tail(ocs_list_t *list)
291 ocs_list_assert(list, NULL);
292 ocs_list_assert(list->magic == OCS_LIST_LIST_MAGIC, NULL);
293 return ocs_list_empty(list) ? NULL : link2item(list, list->prev);
298 * @brief Return the last item in the list
300 * @param list Pointer to list head
302 * @return pointer to the last item, NULL otherwise
304 static inline void *ocs_list_tail(ocs_list_t *list)
306 ocs_list_assert(list, NULL);
307 ocs_list_assert(list->magic == OCS_LIST_LIST_MAGIC, NULL);
308 return ocs_list_empty(list) ? NULL : link2item(list, list->prev);
313 * @brief Get the next item on the list
315 * @param list head of the list
316 * @param item current item
318 * @return pointer to the next item, NULL otherwise
320 static inline void *ocs_list_next(ocs_list_t *list, void *item)
328 ocs_list_assert(list, NULL);
329 ocs_list_assert(list->magic == OCS_LIST_LIST_MAGIC, NULL);
330 ocs_list_assert(item, NULL);
332 link = item2link(list, item);
334 ocs_list_assert(link->magic == OCS_LIST_LINK_MAGIC, NULL);
335 ocs_list_assert(link->offset == list->offset, NULL);
336 ocs_list_assert(link->next, NULL);
337 ocs_list_assert(link->prev, NULL);
339 if ((link->next) == list) {
343 return link2item(list, link->next);
348 * @brief Remove and return an item from the head of the list
350 * @param list head of the list
352 * @return pointer to returned item, or NULL if list is empty
354 #define ocs_list_remove_head(list) ocs_list_remove(list, ocs_list_get_head(list))
358 * @brief Remove an item from the list
360 * @param list Head of the list
361 * @param item Item to remove
363 * @return pointer to item, or NULL if item is not found.
365 static inline void *ocs_list_remove(ocs_list_t *list, void *item)
374 ocs_list_assert(list, NULL);
375 ocs_list_assert(list->magic == OCS_LIST_LIST_MAGIC, NULL);
377 link = item2link(list, item);
379 ocs_list_assert(link->magic == OCS_LIST_LINK_MAGIC, NULL);
380 ocs_list_assert(link->offset == list->offset, NULL);
381 ocs_list_assert(link->next, NULL);
382 ocs_list_assert(link->prev, NULL);
390 link->next = link->prev = NULL;
396 * @brief Iterate a linked list
398 * Iterate a linked list.
400 * @param list Pointer to list
401 * @param item Pointer to iterated item
403 * note, item is NULL after full list is traversed.
408 #define ocs_list_foreach(list, item) \
409 for (item = ocs_list_get_head((list)); item; item = ocs_list_next((list), item) )
412 * @brief Iterate a linked list safely
414 * Iterate a linked list safely, meaning that the iterated item
415 * may be safely removed from the list.
417 * @param list Pointer to list
418 * @param item Pointer to iterated item
419 * @param nxt Pointer to saveed iterated item
421 * note, item is NULL after full list is traversed.
426 #define ocs_list_foreach_safe(list, item, nxt) \
427 for (item = ocs_list_get_head(list), nxt = item ? ocs_list_next(list, item) : NULL; item; \
428 item = nxt, nxt = ocs_list_next(list, item))
431 * @brief Test if object is on a list
433 * Returns True if object is on a list
435 * @param link Pointer to list link
437 * @return returns True if object is on a list
439 static inline int32_t
440 ocs_list_on_list(ocs_list_link_t *link)
442 return (link->next != NULL);
445 #endif /* __OCS_LIST_H__ */