1 /*
2  * Copyright 2017, NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __SRTM_LIST_H__
10 #define __SRTM_LIST_H__
11 
12 #include <assert.h>
13 
14 /*!
15  * @addtogroup srtm
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 /**
23  * @brief Get SRTM list object structure pointer.
24  */
25 #define SRTM_LIST_OBJ(type, field, list) (type)((uint32_t)(list) - (uint32_t)(&((type)0)->field))
26 
27 /**
28  * @brief SRTM list fields
29  */
30 typedef struct _srtm_list
31 {
32     struct _srtm_list *prev; /*!< previous list node */
33     struct _srtm_list *next; /*!< next list node */
34 } srtm_list_t;
35 
36 /*******************************************************************************
37  * API
38  ******************************************************************************/
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /*!
44  * @brief Initialize SRTM list head.
45  *
46  * @param list SRTM list head pointer.
47  */
SRTM_List_Init(srtm_list_t * list)48 static inline void SRTM_List_Init(srtm_list_t *list)
49 {
50     assert(list);
51 
52     list->prev = list;
53     list->next = list;
54 }
55 
56 /*!
57  * @brief Check whether SRTM list is empty.
58  *
59  * @param list SRTM list head pointer.
60  * @return TRUE when list is empty, FALSE otherwise.
61  */
SRTM_List_IsEmpty(srtm_list_t * list)62 static inline bool SRTM_List_IsEmpty(srtm_list_t *list)
63 {
64     assert(list);
65 
66     return list->next == list;
67 }
68 
69 /*!
70  * @brief Add list node at list head.
71  *
72  * @param list SRTM list head pointer.
73  * @param node SRTM list node pointer to add.
74  */
SRTM_List_AddHead(srtm_list_t * list,srtm_list_t * node)75 static inline void SRTM_List_AddHead(srtm_list_t *list, srtm_list_t *node)
76 {
77     assert(list);
78     assert(node);
79 
80     node->next       = list->next;
81     node->prev       = list;
82     list->next->prev = node;
83     list->next       = node;
84 }
85 
86 /*!
87  * @brief Add list node at list tail.
88  *
89  * @param list SRTM list head pointer.
90  * @param node SRTM list node pointer to add.
91  */
SRTM_List_AddTail(srtm_list_t * list,srtm_list_t * node)92 static inline void SRTM_List_AddTail(srtm_list_t *list, srtm_list_t *node)
93 {
94     assert(list);
95     assert(node);
96 
97     node->prev       = list->prev;
98     node->next       = list;
99     list->prev->next = node;
100     list->prev       = node;
101 }
102 
103 /*!
104  * @brief Insert list node before another.
105  *
106  * @param anchor SRTM list anchor node pointer.
107  * @param node SRTM list node pointer to insert.
108  */
SRTM_List_InsertBefore(srtm_list_t * anchor,srtm_list_t * node)109 static inline void SRTM_List_InsertBefore(srtm_list_t *anchor, srtm_list_t *node)
110 {
111     SRTM_List_AddTail(anchor, node);
112 }
113 
114 /*!
115  * @brief Insert list node after another.
116  *
117  * @param anchor SRTM list anchor node pointer.
118  * @param node SRTM list node pointer to insert.
119  */
SRTM_List_InsertAfter(srtm_list_t * anchor,srtm_list_t * node)120 static inline void SRTM_List_InsertAfter(srtm_list_t *anchor, srtm_list_t *node)
121 {
122     SRTM_List_AddHead(anchor, node);
123 }
124 
125 /*!
126  * @brief Remove list node from list.
127  *
128  * @param node SRTM list node pointer to remove.
129  */
SRTM_List_Remove(srtm_list_t * node)130 static inline void SRTM_List_Remove(srtm_list_t *node)
131 {
132     assert(node);
133 
134     node->prev->next = node->next;
135     node->next->prev = node->prev;
136     /* clear node */
137     SRTM_List_Init(node);
138 }
139 
140 #ifdef __cplusplus
141 }
142 #endif
143 
144 /*! @} */
145 
146 #endif /* __SRTM_LIST_H__ */
147