#include <stdlib.h>
#include <stdio.h>
struct list_head {
struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/*插入链表头部*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
/*插入链表尾部*/
static inline void list_add_tail(struct list_head *new,
struct list_head *head)
{
__list_add(new, head->prev, head);
}
static inline void __list_del(struct list_head * prev,
struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/*删除链表*/
static inline void list_del(struct list_head *entry)
{
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
/*移动到链表头部*/
static inline void list_move(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/*移动到链表尾部*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
/*链表合并*/
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) /*not empty*/
__list_splice(list, head, head->next);
}
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head->prev, head);
}
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head, head->next);
INIT_LIST_HEAD(list);
}
}
static inline void list_splice_tail_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head->prev, head);
INIT_LIST_HEAD(list);
}
}
#define offset_of(type, memb) \
((unsigned long)(&((type *)0)->memb))
#define container_of(obj, type, memb) \
((type *)(((char *)obj) - offset_of(type, memb)))
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/*正向遍历*/
#define list_for_each_entry(pos, head, member)\
for (pos = list_entry((head)->next, typeof(*pos), member);\
pos->member.next, &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/*反向遍历*/
#define list_for_each_entry_reverse(pos, head, member)\
for (pos = list_entry((head)->prev, typeof(*pos), member);\
pos->member.prev, &pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
struct list_head nf_sockopts;
struct nf_sockopt_ops
{
struct list_head list;
int data;
};
void nf_register_sockopt(struct nf_sockopt_ops *reg)
{
struct nf_sockopt_ops *ops;
list_for_each_entry(ops, &nf_sockopts, list) {
// printf("%d\n",ops->data);
}
list_add(®->list, &nf_sockopts);
}
void nf_unregister_sockopt(struct nf_sockopt_ops *reg)
{
list_del(®->list);
}
int main(void)
{
struct nf_sockopt_ops test1,test2,*ops;
test1.data = 1;
test2.data = 2;
INIT_LIST_HEAD(&nf_sockopts);
nf_register_sockopt(&test1);
nf_register_sockopt(&test2);
list_for_each_entry(ops, &nf_sockopts, list) {
printf("%d\n",ops->data);
}
nf_unregister_sockopt(&test1);
nf_unregister_sockopt(&test2);
return 0;
}
结果:
2
1