博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
openwrt使用list
阅读量:6269 次
发布时间:2019-06-22

本文共 4394 字,大约阅读时间需要 14 分钟。

openwrt中用到双向无头链表,实际应用时应在外部定义实体链表头,后续可直接应用链表函数(宏定义已将链表头排除在外):

static struct list_head timeouts = LIST_HEAD_INIT(timeouts);

static struct list_head processes = LIST_HEAD_INIT(processes);

与linux相同,定义如下:

#ifndef _LINUX_LIST_H_#define _LINUX_LIST_H_#include 
#include
#define prefetch(x)#ifndef container_of#define container_of(ptr, type, member) \ ({ \ const typeof(((type *)NULL)->member) *__mptr = (ptr); \ (type *)((char *)__mptr - offsetof(type, member)); \ })#endifstruct list_head { struct list_head *next; struct list_head *prev;};

初始化

#define LIST_HEAD_INIT(name) {&(name), &(name)}#undef LIST_HEAD#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)static inline voidINIT_LIST_HEAD(struct list_head *list){    list->next = list->prev = list;}

list属性

static inline boollist_empty(const struct list_head *head){    return (head->next == head);}static inline boollist_is_first(const struct list_head *list,    const struct list_head *head){    return (list->prev == head);}static inline boollist_is_last(const struct list_head *list,    const struct list_head *head){    return (list->next == head);}

list常用操作--增add

static inline void_list_add(struct list_head *_new, struct list_head *prev,    struct list_head *next){    prev->next = _new;    _new->prev = prev;    _new->next = next;    next->prev = _new;}static inline voidlist_add(struct list_head *_new, struct list_head *head){    _list_add(_new, head, head->next);}static inline voidlist_add_tail(struct list_head *_new, struct list_head, *head){    _list_add(_new, head->prev, head);}

list常用操作--删del

static inline void_list_del(struct list_head *entry){    entry->next->prev = entry->prev;    entry->prev->next = entry->next;}static inline voidlist_del(struct list_head *entry){    _list_del(entry);    entry->next = entry->prev = NULL;}static inline voidlist_del_init(struct list_head *entry){    _list_del(entry);    INIT_LIST_HEAD(entry);}

list常用操作--改move

static inline voidlist_move(struct list_head *list, struct list_head * head){    _list_del(list);    list_add(list, head);}static inline voidlist_move_tail(struct list_head *list, struct list_head *head){    _list_del(list);    list_add_tail(list, head);}

list常用操作--查

#define list_entry(ptr, type, field) container_of(ptr, type, field)#define list_first_entry(ptr, type, field) list_entry((ptr)->next, type, field)#define list_last_entry(ptr, type, field) list_entry((ptr)->prev, type, field)#define list_for_each(p, head) \    for(p=(head)->next; p!=(head); p=p->next)#define list_for_each_safe(p, n, head) \    for(p = (head)->next, n=p->next; p!= (head); p=n, n=p->next)#define list_for_each_entry(p, h, field) \    for(p = list_first_entry(h, typeof(*p), field); \        &p->field != (h); \        p = list_entry(p->field.next, typeof(*p), field))#define list_for_each_entry_safe(p, n, h, field) \    for(p = list_first_entry(h, typeof(*p), field), \        n = list_entry(p->field.next, typeof(*p), field); \        &p->field != (h); \        p = n, n = list_entry(n->field.next, typeof(*p), field))#define list_for_each_entry_reverse(p, h, field) \    for(p = list_last_entry(h, typeof(*p), field); \        &p->field != (h); \        p = list_entry(p->field.prev, typeof(*p), field))#define list_for_each_prev(p, h) \    for(p = (h)->prev; p != (h); p = p->prev)#define list_for_each_prev_safe(p, n, h) \    for(p = (h)->prev, n = p->prev; p != (h); \        p = n, n = p->prev)

list常用操作--拼接splice

static inline void_list_splice(const struct list_head *list, struct list_head *prev, struct list_head *next){    struct list_head *first, last;    if(list_empty(list))        return ;    first = list->next;    last = list->prev;    first->prev = prev;    prev->next = first;    last->next = next;    next->prev = last;}static inline voidlist_splice(const struct list_head *list, struct list_head *head){    _list_splice(list, head, head->next);}static inline voidlist_splice_tail(struct list_head *list, struct list_head *head){    _list_splice(list, head->prev, head);}static inline voidlist_splice_init(struct list_head *list, struct list_head *head){    _list_splice(list, head, head->next);    INIT_LIST_HEAD(list);}static inline voidlist_splice_tail_init(struct list_head *list, struct list_head *head){    _list_splice(list, head->prev, head);    INIT_LIST_HEAD(list);}#endif

摘自:libubox

转载地址:http://mnppa.baihongyu.com/

你可能感兴趣的文章
android 打开各种文件(setDataAndType)转:
查看>>
补交:最最原始的第一次作业(当时没有选上课,所以不知道)
查看>>
Vue实例初始化的选项配置对象详解
查看>>
PLM产品技术的发展趋势 来源:e-works 作者:清软英泰 党伟升 罗先海 耿坤瑛
查看>>
vue part3.3 小案例ajax (axios) 及页面异步显示
查看>>
软件测试(二)之 Failure, Error & Fault
查看>>
浅谈MVC3自定义分页
查看>>
.net中ashx文件有什么用?功能有那些,一般用在什么情况下?
查看>>
select、poll、epoll之间的区别总结[整理]【转】
查看>>
CSS基础知识(上)
查看>>
PHP中常见的面试题2(附答案)
查看>>
角色权限分配
查看>>
明小子动力上传拿webshell.zip
查看>>
ES6 Module export与import复合使用
查看>>
第三篇、image 设置圆角的几种方式
查看>>
关于Vs2010 C#使用DirectX的问题
查看>>
EPP(Eclipse PHP)语法高亮仿EditPlus配置
查看>>
OA账号架构权限的问题
查看>>
030——VUE中鼠标语义修饰符
查看>>
python编辑csv
查看>>