linux内核链表提取

list.h
#ifndef IF_LIST
#define IF_LIST

#define LIST_HEAD_INIT(x) list_head x={&x, &x}
#define LIST_HEAD_INIT_STATIC(x) static LIST_HEAD_INIT(x)

struct __list_head
{
	struct __list_head* prev;
	struct __list_head* next;
};

typedef struct __list_head list_head;

inline int list_empty(list_head* head);
inline void list_add(list_head* head, list_head* t);
inline void list_add_tail(list_head* head, list_head* t);
inline void list_del(list_head* t);
inline list_head* list_first(list_head* t);
inline list_head* list_takefirst(list_head* head);
inline int list_count(list_head* t);
inline void list_init(list_head* head);

#define list_entry(p, t, list) /
	(t*)((char*)p - (char*)(&(((t*)0)->list)))

#define list_for_each(p, x) /
	for(p=(x)->next;p!=(x);p=p->next)


#endif
list.c
#include "list.h"

inline int list_empty(list_head* head)
{
	return head->next == head;
}

inline void list_add(list_head* head, list_head* t)
{
	list_head* tmp = head->next;
	t->prev = head;
	t->next = tmp;
	tmp->prev = t;
	head->next = t;
}

inline void list_add_tail(list_head* head, list_head* t)
{
	t->prev = head->prev;
	t->next = head;
	t->prev->next = t;
	head->prev = t;
}

inline void list_del(list_head* t)
{
	t->prev->next = t->next;
	t->next->prev = t->prev;
}

inline list_head* list_first(list_head* t)
{
	return list_empty(t)?0:t->next;
}

inline list_head* list_takefirst(list_head* head)
{
	list_head* ret = head->next;
	list_del(ret);
	return ret;
}

inline int list_count(list_head* t)
{
	int ret = 0;
	list_head* head = t;
	while(t->next != head)
	{
		t=t->next;
		++ret;
	}
	return ret;
}

inline void list_init(list_head* head)
{
	head->next = head->prev = head;
}
测试代码
main.c
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值