EM-ODP 4.4.0
Event Machine on ODP
Loading...
Searching...
No Matches
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#include <stddef.h> /* NULL */
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
42 * Double linked list node
43 */
44typedef struct list_node_t {
45 struct list_node_t *next;
46 struct list_node_t *prev;
48
49/**
50 * Initialize the list
51 */
52static inline void
53list_init(list_node_t *const head)
54{
55 head->next = head;
56 head->prev = head;
57}
58
59/**
60 * Check whether the list is empty
61 */
62static inline int
63list_is_empty(const list_node_t *const head)
64{
65 return (head->next == head);
66}
67
68/**
69 * Double linked list add node (add last in list)
70 */
71static inline void
72list_add(list_node_t *const head, list_node_t *const node)
73{
74 list_node_t *const next = head;
75 list_node_t *const prev = head->prev;
76
77 node->next = next;
78 node->prev = prev;
79
80 prev->next = node;
81 next->prev = node;
82}
83
84/**
85 * Double linked list remove node
86 */
87static inline void
88list_rem(const list_node_t *const head, list_node_t *const node)
89{
90 list_node_t *const next = node->next;
91 list_node_t *const prev = node->prev;
92
93 (void)head; /* unused */
94
95 prev->next = next;
96 next->prev = prev;
97
98 /* just for safety */
99 node->next = node;
100 node->prev = node;
101}
102
103/**
104 * Double linked list remove first node (FIFO-mode)
105 */
106static inline list_node_t *
107list_rem_first(const list_node_t *const head)
108{
109 list_node_t *node = NULL;
110
111 if (!list_is_empty(head)) {
112 /* first node in the list */
113 node = head->next;
114 list_rem(head, node);
115 }
116
117 return node;
118}
119
120/**
121 * Double linked list remove last node (LIFO-mode)
122 */
123static inline list_node_t *
124list_rem_last(const list_node_t *const head)
125{
126 list_node_t *node = NULL;
127
128 if (!list_is_empty(head)) {
129 /* last node in the list */
130 node = head->prev;
131 list_rem(head, node);
132 }
133
134 return node;
135}
136
137/**
138 * Macro for accessing each node in a queue list
139 *
140 * @param head Points to the head node of the list
141 * @param cur Points to the current node, access this inside loop
142 */
143#define list_for_each(head, cur) \
144 for (list_node_t *_pos_ = (head)->next; \
145 (cur) = (void *)(_pos_), _pos_ = (_pos_)->next, \
146 (cur) != (head);)
147
148#ifdef __cplusplus
149}
150#endif
151
152#endif