Linux 内核list使用
来源:互联网 发布:淘宝卷皮折扣网 编辑:程序博客网 时间:2024/05/17 12:23
源码:
#ifndef LIST_H
#define LIST_H 1struct 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);//删除
- Linux 内核list使用
- Linux内核---60.linux内核链表list的使用
- linux内核list模块的使用
- linux内核list实现
- linux内核list解析
- linux内核list.h
- linux内核中的list
- linux内核list.h
- 在用户程序中使用linux内核list(zz)
- 在用户程序中使用linux内核list(原创)
- 使用linux 内核中代码之--list
- 用户空间使用linux内核链表list编程
- linux内核中的list详解
- Linux内核list&hlist解读
- linux 内核之 list.h
- linux list 内核链表
- Linux内核中的list链表
- Linux内核list/hlist解读
- vs打开utf8编码文件
- update-rc.d的具体用法
- 平衡二叉树
- fusetool 里面的 ActivatingAnimation 使用理解
- 处理iOS键盘通知
- Linux 内核list使用
- mysql 点赞取消点赞语句
- Android Studio查看类结构的快捷键
- CodeForces 572A,B,C
- iOS开发-如何把广告,图片通过代码加到Screen上
- softmax regression 推导
- java中对日期的处理
- Dubbo学习过程、使用经验分享及实现原理简单介绍
- mysql备份和恢复