EVE 1.0
dlist.h
Go to the documentation of this file.
1 #ifndef EVE_DLIST_H_INCLUDED
2 #define EVE_DLIST_H_INCLUDED
3 
4 /**********************************************************************/
5 /*
6  * Copyright (c) 2013-2015, Jetro AS
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
23  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
25  * OF 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) ARISING
28  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
29  * OF SUCH DAMAGE.
30  *
31  * This file is part of the EVE platform.
32  */
33 
34  /**
35  * \file
36  * \brief The code implements Dummy Headed Doubly Linked Circularlist (DHDLC) primitive
37  *
38  * \author DT, Jetro AS
39  */ /******************************************************************/
40 
41 /**
42  * \defgroup dlist Doubly linked circularlist
43  * \ingroup util
44  * @{
45  *
46  * Dummy Headed Doubly Linked Circularlist (DHDLC) primitive.
47  */
48 
49 /**********************************************************************/
50 /** Static initializer for the empty list entry structure */
51 #define DLIST_INIT(dlist) \
52  { \
53  .prev = &dlist, \
54  .next = &dlist, \
55  }
56 
57 /**********************************************************************/
58 /** Static initializer for the empty list entry structure, typed version */
59 #define DLIST_INIT_TYPED(dlist) \
60  (struct dlist_t) DLIST_INIT(dlist)
61 
62 /**********************************************************************/
63 /**
64  * Linked list entry.
65  */
66 struct dlist_t {
67  struct dlist_t *prev; /**< Pointer to the previous entry in the list */
68  struct dlist_t *next; /**< Pointer to the next entry in the list */
69 };
70 
71 /**********************************************************************/
72 /**
73  * Initialize a linked list head
74  * @param list pointer to the linked list head
75  */
76 static inline void dlist_init(struct dlist_t *list)
77 {
78  list->prev = list->next = list;
79 }
80 
81 /**********************************************************************/
82 /**
83  * Insert a new list entry between two in a list
84  * @param list a new list entry to be inserted
85  * @param prev left-hand neighbor of the inserted entry
86  * @param next right-hand neighbor of the inserted entry
87  */
88 static inline void dlist_insert(struct dlist_t *list, struct dlist_t *prev, struct dlist_t *next)
89 {
90  list->prev = prev;
91  list->next = next;
92  prev->next = next->prev = list;
93 }
94 
95 /**********************************************************************/
96 /**
97  * Remove an entry from the list
98  * @param list the entry to be removed
99  */
100 static inline struct dlist_t *dlist_del(struct dlist_t *list)
101 {
102  list->prev->next = list->next;
103  list->next->prev = list->prev;
104  dlist_init(list);
105  return list;
106 }
107 
108 /**********************************************************************/
109 /**
110  * Insert a new list entry to the tail of the list
111  * @param list head of the list
112  * @param item new entry to be inserted
113  */
114 static inline void dlist_append(struct dlist_t *list, struct dlist_t *item)
115 {
116  dlist_insert(item, list->prev, list);
117 }
118 
119 /**********************************************************************/
120 /**
121  * Check if the list is empty
122  * @param list head of the list
123  * @return true if list is empty, false if not
124  */
125 static inline int dlist_is_empty(struct dlist_t *list)
126 {
127  return list->next == list;
128 }
129 
130 /**********************************************************************/
131 /**
132  * Check if list contains exactly one entry (except list head)
133  * @param list head of the list
134  * @return true if list is singular, false if not
135  */
136 static inline int dlist_is_singular(struct dlist_t *list)
137 {
138  return !dlist_is_empty(list) && (list->next == list->prev);
139 }
140 
141 /** @} */ /* dlist */
142 
143 #endif /* EVE_DLIST_H_INCLUDED */
static void dlist_insert(struct dlist_t *list, struct dlist_t *prev, struct dlist_t *next)
Definition: dlist.h:88
static int dlist_is_empty(struct dlist_t *list)
Definition: dlist.h:125
static void dlist_init(struct dlist_t *list)
Definition: dlist.h:76
struct dlist_t * next
Definition: dlist.h:68
struct dlist_t * prev
Definition: dlist.h:67
static int dlist_is_singular(struct dlist_t *list)
Definition: dlist.h:136
Definition: dlist.h:66
static void dlist_append(struct dlist_t *list, struct dlist_t *item)
Definition: dlist.h:114
static struct dlist_t * dlist_del(struct dlist_t *list)
Definition: dlist.h:100