EM-ODP  3.7.0
Event Machine on ODP
list.h
1 /*
2  * Copyright (c) 2012, Nokia Siemens Networks
3  * Copyright (c) 2013-2015, Nokia Solutions and Networks
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef MISC_LIST_H_
33 #define MISC_LIST_H_
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /**
40  * Double linked list node
41  */
42 typedef struct list_node_t {
43  struct list_node_t *next;
44  struct list_node_t *prev;
45 } list_node_t;
46 
47 /**
48  * Initialize the list
49  */
50 static inline void
51 list_init(list_node_t *const head)
52 {
53  head->next = head;
54  head->prev = head;
55 }
56 
57 /**
58  * Check whether the list is empty
59  */
60 static inline int
61 list_is_empty(const list_node_t *const head)
62 {
63  return (head->next == head);
64 }
65 
66 /**
67  * Double linked list add node (add last in list)
68  */
69 static inline void
70 list_add(list_node_t *const head, list_node_t *const node)
71 {
72  list_node_t *const next = head;
73  list_node_t *const prev = head->prev;
74 
75  node->next = next;
76  node->prev = prev;
77 
78  prev->next = node;
79  next->prev = node;
80 }
81 
82 /**
83  * Double linked list remove node
84  */
85 static inline void
86 list_rem(const list_node_t *const head, list_node_t *const node)
87 {
88  list_node_t *const next = node->next;
89  list_node_t *const prev = node->prev;
90 
91  (void)head; /* unused */
92 
93  prev->next = next;
94  next->prev = prev;
95 
96  /* just for safety */
97  node->next = node;
98  node->prev = node;
99 }
100 
101 /**
102  * Double linked list remove first node (FIFO-mode)
103  */
104 static inline list_node_t *
105 list_rem_first(const list_node_t *const head)
106 {
107  list_node_t *node = NULL;
108 
109  if (!list_is_empty(head)) {
110  /* first node in the list */
111  node = head->next;
112  list_rem(head, node);
113  }
114 
115  return node;
116 }
117 
118 /**
119  * Double linked list remove last node (LIFO-mode)
120  */
121 static inline list_node_t *
122 list_rem_last(const list_node_t *const head)
123 {
124  list_node_t *node = NULL;
125 
126  if (!list_is_empty(head)) {
127  /* last node in the list */
128  node = head->prev;
129  list_rem(head, node);
130  }
131 
132  return node;
133 }
134 
135 /**
136  * Macro for accessing each node in a queue list
137  *
138  * @param head Points to the head node of the list
139  * @param pos Internal pointer for tracking position, don't use in loop!
140  * @param cur Points to the current node, access this inside loop
141  */
142 #define list_for_each(head, pos, cur) \
143  for ((pos) = (head)->next; \
144  (cur) = (void *)(pos), (pos) = (pos)->next, \
145  (cur) != (head);)
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif
list_node_t
Definition: list.h:42