list.h

00001 /*
00002  * netlink/list.h       Netlink List Utilities
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 #ifndef NETLINK_LIST_H_
00013 #define NETLINK_LIST_H_
00014 
00015 struct nl_list_head
00016 {
00017         struct nl_list_head *   next;
00018         struct nl_list_head *   prev;
00019 };
00020 
00021 static inline void NL_INIT_LIST_HEAD(struct nl_list_head *list)
00022 {
00023         list->next = list;
00024         list->prev = list;
00025 }
00026 
00027 static inline void __nl_list_add(struct nl_list_head *obj,
00028                                  struct nl_list_head *prev,
00029                                  struct nl_list_head *next)
00030 {
00031         prev->next = obj;
00032         obj->prev = prev;
00033         next->prev = obj;
00034         obj->next = next;
00035 }
00036 
00037 static inline void nl_list_add_tail(struct nl_list_head *obj,
00038                                     struct nl_list_head *head)
00039 {
00040         __nl_list_add(obj, head->prev, head);
00041 }
00042 
00043 static inline void nl_list_add_head(struct nl_list_head *obj,
00044                                     struct nl_list_head *head)
00045 {
00046         __nl_list_add(obj, head, head->next);
00047 }
00048 
00049 static inline void nl_list_del(struct nl_list_head *obj)
00050 {
00051         obj->next->prev = obj->prev;
00052         obj->prev->next = obj->next;
00053 }
00054 
00055 static inline int nl_list_empty(struct nl_list_head *head)
00056 {
00057         return head->next == head;
00058 }
00059 
00060 #define nl_container_of(ptr, type, member) ({                   \
00061         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
00062         (type *)( (char *)__mptr - ((size_t) &((type *)0)->member));})
00063 
00064 #define nl_list_entry(ptr, type, member) \
00065         nl_container_of(ptr, type, member)
00066 
00067 #define nl_list_at_tail(pos, head, member) \
00068         ((pos)->member.next == (head))
00069 
00070 #define nl_list_at_head(pos, head, member) \
00071         ((pos)->member.prev == (head))
00072 
00073 #define NL_LIST_HEAD(name) \
00074         struct nl_list_head name = { &(name), &(name) }
00075 
00076 #define nl_list_first_entry(head, type, member)                 \
00077         nl_list_entry((head)->next, type, member)
00078 
00079 #define nl_list_for_each_entry(pos, head, member)                               \
00080         for (pos = nl_list_entry((head)->next, typeof(*pos), member);   \
00081              &(pos)->member != (head);  \
00082              (pos) = nl_list_entry((pos)->member.next, typeof(*(pos)), member))
00083 
00084 #define nl_list_for_each_entry_safe(pos, n, head, member)                       \
00085         for (pos = nl_list_entry((head)->next, typeof(*pos), member),   \
00086                 n = nl_list_entry(pos->member.next, typeof(*pos), member);      \
00087              &(pos)->member != (head);                                  \
00088              pos = n, n = nl_list_entry(n->member.next, typeof(*n), member))
00089 
00090 #define nl_init_list_head(head) \
00091         do { (head)->next = (head); (head)->prev = (head); } while (0)
00092 
00093 #endif