libnl 1.1

include/netlink/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 
00022 static inline void __nl_list_add(struct nl_list_head *obj,
00023                                  struct nl_list_head *prev,
00024                                  struct nl_list_head *next)
00025 {
00026         prev->next = obj;
00027         obj->prev = prev;
00028         next->prev = obj;
00029         obj->next = next;
00030 }
00031 
00032 static inline void nl_list_add_tail(struct nl_list_head *obj,
00033                                     struct nl_list_head *head)
00034 {
00035         __nl_list_add(obj, head->prev, head);
00036 }
00037 
00038 static inline void nl_list_add_head(struct nl_list_head *obj,
00039                                     struct nl_list_head *head)
00040 {
00041         __nl_list_add(obj, head, head->next);
00042 }
00043 
00044 static inline void nl_list_del(struct nl_list_head *obj)
00045 {
00046         obj->next->prev = obj->prev;
00047         obj->prev->next = obj->next;
00048 }
00049 
00050 static inline int nl_list_empty(struct nl_list_head *head)
00051 {
00052         return head->next == head;
00053 }
00054 
00055 #define nl_container_of(ptr, type, member) ({                   \
00056         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
00057         (type *)( (char *)__mptr - ((size_t) &((type *)0)->member));})
00058 
00059 #define nl_list_entry(ptr, type, member) \
00060         nl_container_of(ptr, type, member)
00061 
00062 #define nl_list_at_tail(pos, head, member) \
00063         ((pos)->member.next == (head))
00064 
00065 #define nl_list_at_head(pos, head, member) \
00066         ((pos)->member.prev == (head))
00067 
00068 #define NL_LIST_HEAD(name) \
00069         struct nl_list_head name = { &(name), &(name) }
00070 
00071 #define nl_list_for_each_entry(pos, head, member)                               \
00072         for (pos = nl_list_entry((head)->next, typeof(*pos), member);   \
00073              &(pos)->member != (head);  \
00074              (pos) = nl_list_entry((pos)->member.next, typeof(*(pos)), member))
00075 
00076 #define nl_list_for_each_entry_safe(pos, n, head, member)                       \
00077         for (pos = nl_list_entry((head)->next, typeof(*pos), member),   \
00078                 n = nl_list_entry(pos->member.next, typeof(*pos), member);      \
00079              &(pos)->member != (head);                                  \
00080              pos = n, n = nl_list_entry(n->member.next, typeof(*n), member))
00081 
00082 #define nl_init_list_head(head) \
00083         do { (head)->next = (head); (head)->prev = (head); } while (0)
00084 
00085 #endif