GNU Radio's FOSPHOR Package
llist.h
Go to the documentation of this file.
1 /*
2  * llist.h
3  *
4  * Simple double-linked list. Interface similar to the linux kernel
5  * one, but heavily simplified and rewritten to compile on other compilers
6  * than GCC.
7  *
8  * Copyright (C) 2013-2014 Sylvain Munaut
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef __FOSPHOR_LLIST_H__
25 #define __FOSPHOR_LLIST_H__
26 
27 /*! \defgroup llist
28  * @{
29  */
30 
31 /*! \file llist.h
32  * \brief Simple double-linked list
33  */
34 
35 #include <stddef.h>
36 
37 
38 struct llist_head {
39  struct llist_head *next, *prev;
40 };
41 
42 #define LLIST_HEAD_INIT(name) { &(name), &(name) }
43 
44 #define LLIST_HEAD(name) \
45  struct llist_head name = LLIST_HEAD_INIT(name)
46 
47 /**
48  * \brief Add a new entry after the specified head
49  * \param[in] new new entry to be added
50  * \param[in] head llist head to add it after
51  */
52 static inline void llist_add(struct llist_head *_new, struct llist_head *head)
53 {
54  head->next->prev = _new;
55  _new->next = head->next;
56  _new->prev = head;
57  head->next = _new;
58 }
59 
60 /**
61  * \brief Deletes entry from llist
62  * \param[in] entry the element to delete from the llist
63  */
64 static inline void llist_del(struct llist_head *entry)
65 {
66  entry->next->prev = entry->prev;
67  entry->prev->next = entry->next;
68  entry->next = (struct llist_head *)0;
69  entry->prev = (struct llist_head *)0;
70 }
71 
72 /**
73  * \brief Get the struct for this entry
74  * \param[in] ptr the &struct llist_head pointer
75  * \param[in] type the type of the struct this is embedded in
76  * \param[in] member the name of the llist_struct within the struct
77  * \returns The struct for this entry
78  */
79 #define llist_entry(ptr, type, member) \
80  ((type *)( (char *)(ptr) - offsetof(type, member) ))
81 
82 /**
83  * \brief Iterate over llist of given type
84  * \param[in] type the type of the loop counter
85  * \param[out] pos the type * to use as a loop counter
86  * \param[in] head the head for your llist
87  * \param[in] member the name of the llist_struct within the struct
88  */
89 #define llist_for_each_entry(type, pos, head, member) \
90  for (pos = llist_entry((head)->next, type, member); \
91  &pos->member != (head); \
92  pos = llist_entry(pos->member.next, type, member))
93 
94 /*! @} */
95 
96 #endif /* __FOSPHOR_LLIST_H__ */
struct llist_head * prev
Definition: llist.h:39
Definition: llist.h:38
static void llist_del(struct llist_head *entry)
Deletes entry from llist.
Definition: llist.h:64
static void llist_add(struct llist_head *_new, struct llist_head *head)
Add a new entry after the specified head.
Definition: llist.h:52
struct llist_head * next
Definition: llist.h:39