root / Whitix / branches / hybrid / include / llist.h

Revision 1, 2.8 kB (checked in by mtw07, 6 months ago)

Initial import of projects.

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
24/* Most of this logic is adapted from Linux. It's great and flexible, kudos to them */
25
26struct ListHead
27{
28        struct ListHead* prev,*next;
29};
30
31#define LIST_HEAD_INIT(name) { &(name), &(name) }
32#define INIT_LIST_HEAD(ptr) do { (ptr)->next=(ptr); (ptr)->prev=(ptr); } while(0)
33#define LIST_HEAD(name) struct ListHead name=LIST_HEAD_INIT(name)
34
35#define offsetof(type,member) (unsigned int)(&(((type*)0)->member))
36
37static inline void DoListAdd(struct ListHead* new,struct ListHead* prev,struct ListHead* next)
38{
39        next->prev=new;
40        new->next=next;
41        new->prev=prev;
42        prev->next=new;
43}
44
45static inline void ListAdd(struct ListHead* new,struct ListHead* head)
46{
47        DoListAdd(new,head,head->next);
48}
49
50/* Adds ListHead to the prev of head, which is the end of the list */
51
52static inline void ListAddTail(struct ListHead* new,struct ListHead* head)
53{
54        DoListAdd(new,head->prev,head);
55}
56
57static inline void DoListRemove(struct ListHead* prev,struct ListHead* next)
58{
59        next->prev=prev;
60        prev->next=next;
61}
62
63static inline void ListRemove(struct ListHead* entry)
64{
65        DoListRemove(entry->prev,entry->next);
66}
67
68static inline void ListRemoveInit(struct ListHead* entry)
69{
70        DoListRemove(entry->prev,entry->next);
71        INIT_LIST_HEAD(entry);
72}
73
74static inline int ListEmpty(struct ListHead* head)
75{
76        return (head->next == head);
77}
78
79/* Minus the address of the list_struct within the struct by the start of the struct */
80#define ListEntry(ptr,type,member) \
81        ((type*)(((char*)ptr)-offsetof(type,member)))
82
83#define ListForEachEntry(pos,head,member) \
84        for (pos=ListEntry((head)->next,typeof(*pos),member); \
85         &(pos->member) != head; \
86         pos=ListEntry(pos->member.next, typeof(*pos), member))
87
88#define ListForEachEntrySafe(pos,n,head,member) \
89        for (pos=ListEntry((head)->next,typeof(*pos),member), \
90        n=ListEntry(pos->member.next,typeof(*pos),member); \
91        &pos->member!=(head); \
92        pos=n,n=ListEntry(n->member.next,typeof(*n),member))
93
94#define ListForEach(pos,head) \
95        for (pos=head; pos != head; pos=pos->next)
96
97#endif
Note: See TracBrowser for help on using the browser.