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

本文共 4452 字,大约阅读时间需要 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

转载于:https://www.cnblogs.com/embedded-linux/p/8013144.html

你可能感兴趣的文章
poj 1331 Multiply
查看>>
严重: 文档无效: 找不到语法。 at (null:2:19)
查看>>
tomcat7的数据库连接池tomcatjdbc的25个优势
查看>>
Html 小插件5 百度搜索代码2
查看>>
nodejs-Path模块
查看>>
P1107 最大整数
查看>>
监控CPU和内存的使用
查看>>
Ubuntu14.04设置开机自启动程序
查看>>
多进程与多线程的区别
查看>>
Ubuntu(虚拟机)下安装Qt5.5.1
查看>>
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
查看>>
个人寒假作业项目《印象笔记》第一天
查看>>
java 常用命令
查看>>
CodeForces Round #545 Div.2
查看>>
卷积中的参数
查看>>
51nod1076 (边双连通)
查看>>
Item 9: Avoid Conversion Operators in Your APIs(Effective C#)
查看>>
学习Spring Boot:(二十八)Spring Security 权限认证
查看>>
IT学习神器——慕课网App获App Store、Android应用市场重磅推荐
查看>>
深入浅出JavaScript(2)—ECMAScript
查看>>