root/Whitix/trunk/include/llist.h

Revision 2002, 4.7 KB (checked in by mwhitworth, 3 years ago)

Add ListMove and ListMoveTail functions.

Line 
1/*  This file is part of Whitix.
2 *
3 *  Whitix is free software; you can redistribute it and/or modify
4 *  it under the terms of the GNU General Public License as published by
5 *  the Free Software Foundation; either version 2 of the License, or
6 *  (at your option) any later version.
7 *
8 *  Whitix is distributed in the hope that it will be useful,
9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *  GNU General Public License for more details.
12 *
13 *  You should have received a copy of the GNU General Public License
14 *  along with Whitix; if not, write to the Free Software
15 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16 *
17 */
18
19#ifndef LLIST_H
20#define LLIST_H
21
22#include <typedefs.h>
23
24struct ListHead
25{
26        struct ListHead* next, *prev;
27};
28
29/***********************************************************************
30 *
31 * MACRO:               LIST_HEAD_INIT
32 *
33 * DESCRIPTION: Construct a list head at compile time. Not used generally,
34 *                              except in LIST_HEAD.
35 *
36 * PARAMETERS:  name - name of the list head variable.
37 *
38 * EXAMPLE:             struct ListHead head = LIST_HEAD_INIT(head)
39 *
40 ***********************************************************************/
41
42#define LIST_HEAD_INIT(name) { &(name), &(name) }
43
44/***********************************************************************
45 *
46 * MACRO:               LIST_HEAD_INIT
47 *
48 * DESCRIPTION: Construct a list head at runtime.
49 *
50 * PARAMETERS:  ptr - address (typically) of the list head.
51 *
52 * EXAMPLE:             INIT_LIST_HEAD(&head)
53 *
54 ***********************************************************************/
55
56#define INIT_LIST_HEAD(ptr) do { (ptr)->next=(ptr); (ptr)->prev=(ptr); } while(0)
57
58/***********************************************************************
59 *
60 * MACRO:               LIST_HEAD
61 *
62 * DESCRIPTION: Construct a list head at compile time.
63 *
64 * PARAMETERS:  name - name of the list head variable to construct
65 *
66 * EXAMPLE:             LIST_HEAD(threadList)
67 *
68 ***********************************************************************/
69
70#define LIST_HEAD(name) struct ListHead name=LIST_HEAD_INIT(name)
71
72static inline void DoListAdd(struct ListHead* new,struct ListHead* prev,struct ListHead* next)
73{
74        next->prev=new;
75        new->next=next;
76        new->prev=prev;
77        prev->next=new;
78}
79
80static inline void ListAdd(struct ListHead* new,struct ListHead* head)
81{
82        DoListAdd(new,head,head->next);
83}
84
85/* Adds ListHead to the prev of head, which is the end of the list */
86
87static inline void ListAddTail(struct ListHead* new,struct ListHead* head)
88{
89        DoListAdd(new,head->prev,head);
90}
91
92static inline void DoListRemove(struct ListHead* prev,struct ListHead* next)
93{
94        next->prev=prev;
95        prev->next=next;
96}
97
98static inline void ListRemove(struct ListHead* entry)
99{
100        DoListRemove(entry->prev,entry->next);
101}
102
103static inline void ListRemoveInit(struct ListHead* entry)
104{
105        DoListRemove(entry->prev,entry->next);
106        INIT_LIST_HEAD(entry);
107}
108
109static inline int ListEmpty(struct ListHead* head)
110{
111        return (head->next == head);
112}
113
114static inline void DoListSpliceTail(struct ListHead* list, struct ListHead* head)
115{
116        struct ListHead* first = list->next;
117        struct ListHead* last = list->prev;
118        struct ListHead* at = head->prev;
119
120        first->prev = at;
121        at->next = first;
122
123        last->next = head;
124        head->prev = last;
125}
126
127static inline void ListSpliceTail(struct ListHead* list, struct ListHead* head)
128{
129        if (!ListEmpty(list))
130                DoListSpliceTail(list, head);   
131}
132
133static inline void ListSpliceTailInit(struct ListHead* list, struct ListHead* head)
134{
135        if (!ListEmpty(list))
136        {
137                DoListSpliceTail(list, head);
138                INIT_LIST_HEAD(list);
139        }
140}
141
142static inline void ListMove(struct ListHead* list, struct ListHead* head)
143{
144        DoListRemove(list->prev, list->next);
145        ListAdd(list, head);
146}
147
148static inline void ListMoveTail(struct ListHead* list, struct ListHead* head)
149{
150        DoListRemove(list->prev, list->next);
151        ListAddTail(list, head);
152}
153
154/* Minus the address of the list_struct within the struct by the start of the struct */
155#define ListEntry(ptr,type,member) \
156        ((type*)(((char*)ptr) - OffsetOf(type,member)))
157
158#define ListForEachEntry(pos,head,member) \
159        for (pos=ListEntry((head)->next,typeof(*pos),member); \
160         &(pos->member) != head; \
161         pos=ListEntry(pos->member.next, typeof(*pos), member))
162
163#define ListForEachEntryReverse(pos, head, member) \
164        for (pos=ListEntry((head)->prev, typeof(*pos), member); \
165        &(pos->member) != head;                                                                 \
166        pos = ListEntry(pos->member.prev, typeof(*pos), member))
167
168#define ListForEachEntrySafe(pos,n,head,member) \
169        for (pos=ListEntry((head)->next,typeof(*pos),member), \
170        n=ListEntry(pos->member.next,typeof(*pos),member); \
171        &pos->member!=(head); \
172        pos=n,n=ListEntry(n->member.next,typeof(*n),member))
173
174#define ListForEach(pos,head) \
175        for (pos=head; pos != head; pos=pos->next)
176
177#endif
Note: See TracBrowser for help on using the browser.