Linux 内核list使用

来源:互联网 发布:淘宝卷皮折扣网 编辑:程序博客网 时间:2024/05/17 12:23

源码:

#ifndef LIST_H

#define LIST_H 1


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 void INIT_LIST_HEAD(struct list_head *list)
{


list->next = list;


list->prev = list;


}


static inline 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)


{


__list_del(entry->prev, entry->next);


}


static inline void list_replace(struct list_head *old, struct list_head *new)
{


new->next = old->next;


new->next->prev = new;


new->prev = old->prev;


new->prev->next = new;


}


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 int list_is_last(const struct list_head *list, const struct list_head *head)
{


return list->next == head;


}


static inline int list_empty(const struct list_head *head)
{


return head->next == head;


}


static inline int list_is_singular(const struct list_head *head)
{


return !list_empty(head) && (head->next == head->prev);


}


#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)


#define container_of(ptr, type, member) \
({ const typeof( ((type *)0)->member ) *__mptr = (ptr);\
(type *)( (char *)__mptr - offsetof(type,member) );})


#define list_entry(ptr, type, member) \
container_of(ptr, type, member)




#define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next)


#endif


Linux内核list在内核的使用非常多.定义也比较精简.它只是实现了list算法.并不存在数据区域.所以如果要使用list必须自己重新定义struct 并在struct内部包含list的结构,

比如:

typedef struct msg_buf_s
{
struct list_headlist;/*此处就是list用于实现链接的*/
int len;
char buf[1024];
}msg_buf_struct;

另外还需要一个头:



typedef struct
{
struct list_headlist_head;/*list head 入口*/


pthread_mutex_t q_lock;
int init_nums;//缓冲区初始化数量
int frm_nums;//记录缓冲区使用数量
}Msg_Buf_Queue_head;

//-------------------------------------------------------------------------------------------------------

Msg_Buf_Queue_head qhead;

Msg_Buf_Queue_head *p_qhead = &q_head;


INIT_LIST_HEAD(&p_qhead->list_head);//用来初始化的链表头的

list_add_tail(ptmp,&p_qhead->list_head);//添加到list 尾部


list_for_each(ptmp,  &p_qhead->list_head);//

{

msg_buf_struct *pmsg;
pmsg = list_entry(ptmp, msg_buf_struct, list);//获取节点.通过struct 成员获取结构体的指针.container_of的应用欧冠

}

list_del(ptmp);//删除









0 0
原创粉丝点击