应用层调用内核中的链表操作.
来源:互联网 发布:手机自动连接移动网络 编辑:程序博客网 时间:2024/05/17 22:58
今天上班没有事做,自己折腾了一下应用层使用内核链表的用法.因为内核中的list.h文件中包含了汇编和依赖了其他文件,直接包含内核的list.h文件编译出大量的错误,要使用下面的list.h.
网上有大量的链表中函数的讲解,所以我直接上程序.
list.h 代码:
#ifndef _LINUX_LIST_H #define _LINUX_LIST_H #include <stdio.h> #include <stdlib.h> #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) ); } ) static inline void prefetch(const void *x) {;} static inline void prefetchw(const void *x) {;} #define LIST_POISON1 ((void *) 0x00100100) #define LIST_POISON2 ((void *) 0x00200200) 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) #define INIT_LIST_HEAD(ptr) do { \ (ptr)->next = (ptr); (ptr)->prev = (ptr); \ } while (0) /* * Insert a new entry between two known consecutive entries. * * This is only for internal list manipulation where we know * the prev/next entries already! */ 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; } /** * list_add - add a new entry * @new: new entry to be added * @head: list head to add it after * * Insert a new entry after the specified head. * This is good for implementing stacks. */ static inline void list_add(struct list_head *new, struct list_head *head) { __list_add(new, head, head->next); } /** * list_add_tail - add a new entry * @new: new entry to be added * @head: list head to add it before * * Insert a new entry before the specified head. * This is useful for implementing queues. */ 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); entry->next = LIST_POISON1; entry->prev = LIST_POISON2; } static inline void list_del_init(struct list_head *entry) { __list_del(entry->prev, entry->next); INIT_LIST_HEAD(entry); } 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_empty(const struct list_head *head) { return head->next == head; } static inline int list_empty_careful(const struct list_head *head) { struct list_head *next = head->next; return (next == head) && (next == head->prev); } static inline void __list_splice(struct list_head *list, struct list_head *head) { struct list_head *first = list->next; struct list_head *last = list->prev; struct list_head *at = head->next; first->prev = head; head->next = first; last->next = at; at->prev = last; } /** * list_splice - join two lists * @list: the new list to add. * @head: the place to add it in the first list. */ static inline void list_splice(struct list_head *list, struct list_head *head) { if (!list_empty(list)) __list_splice(list, head); } /** * list_splice_init - join two lists and reinitialise the emptied list. * @list: the new list to add. * @head: the place to add it in the first list. * * The list at @list is reinitialised */ static inline void list_splice_init(struct list_head *list, struct list_head *head) { if (!list_empty(list)) { __list_splice(list, head); INIT_LIST_HEAD(list); } } #define list_entry(ptr, type, member) container_of(ptr, type, member) #define list_for_each(pos, head) \ for (pos = (head)->next; prefetch(pos->next), pos != (head); \ pos = pos->next) #define __list_for_each(pos, head) \ for (pos = (head)->next; pos != (head); pos = pos->next) #define list_for_each_prev(pos, head) \ for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ pos = pos->prev) #define list_for_each_safe(pos, n, head) \ for (pos = (head)->next, n = pos->next; pos != (head); \ pos = n, n = pos->next) #define list_for_each_entry(pos, head, member) \ for (pos = list_entry((head)->next, typeof(*pos), member); \ prefetch(pos->member.next), &pos->member != (head); \ pos = list_entry(pos->member.next, typeof(*pos), member)) #define list_for_each_entry_reverse(pos, head, member) \ for (pos = list_entry((head)->prev, typeof(*pos), member); \ prefetch(pos->member.prev), &pos->member != (head); \ pos = list_entry(pos->member.prev, typeof(*pos), member)) #define list_prepare_entry(pos, head, member) \ ((pos) ? : list_entry(head, typeof(*pos), member)) #define list_for_each_entry_continue(pos, head, member) \ for (pos = list_entry(pos->member.next, typeof(*pos), member); \ prefetch(pos->member.next), &pos->member != (head); \ pos = list_entry(pos->member.next, typeof(*pos), member)) #define list_for_each_entry_safe(pos, n, head, member) \ for (pos = list_entry((head)->next, typeof(*pos), member), \ n = list_entry(pos->member.next, typeof(*pos), member); \ &pos->member != (head); \ pos = n, n = list_entry(n->member.next, typeof(*n), member)) //HASH LIST struct hlist_head { struct hlist_node *first; }; struct hlist_node { struct hlist_node *next, **pprev; }; #define HLIST_HEAD_INIT { .first = NULL } #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL) static inline int hlist_unhashed(const struct hlist_node *h) { return !h->pprev; } static inline int hlist_empty(const struct hlist_head *h) { return !h->first; } static inline void __hlist_del(struct hlist_node *n) { struct hlist_node *next = n->next; struct hlist_node **pprev = n->pprev; *pprev = next; if (next) next->pprev = pprev; } static inline void hlist_del(struct hlist_node *n) { __hlist_del(n); n->next = LIST_POISON1; n->pprev = LIST_POISON2; } static inline void hlist_del_init(struct hlist_node *n) { if (n->pprev) { __hlist_del(n); INIT_HLIST_NODE(n); } } static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) { struct hlist_node *first = h->first; n->next = first; if (first) first->pprev = &n->next; h->first = n; n->pprev = &h->first; } /* next must be != NULL */ static inline void hlist_add_before(struct hlist_node *n, struct hlist_node *next) { n->pprev = next->pprev; n->next = next; next->pprev = &n->next; *(n->pprev) = n; } static inline void hlist_add_after(struct hlist_node *n, struct hlist_node *next) { next->next = n->next; n->next = next; next->pprev = &n->next; if(next->next) next->next->pprev = &next->next; } #define hlist_entry(ptr, type, member) container_of(ptr,type,member) #define hlist_for_each(pos, head) \ for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ pos = pos->next) #define hlist_for_each_safe(pos, n, head) \ for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ pos = n) #define hlist_for_each_entry(tpos, pos, head, member) \ for (pos = (head)->first; \ pos && ({ prefetch(pos->next); 1;}) && \ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ pos = pos->next) #define hlist_for_each_entry_continue(tpos, pos, member) \ for (pos = (pos)->next; \ pos && ({ prefetch(pos->next); 1;}) && \ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ pos = pos->next) #define hlist_for_each_entry_from(tpos, pos, member) \ for (; pos && ({ prefetch(pos->next); 1;}) && \ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ pos = pos->next) #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ for (pos = (head)->first; \ pos && ({ n = pos->next; 1; }) && \ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ pos = n) #endif
以上的list.h中的代码是网上找到,感谢那位大神帮忙将内核中的list.h整理出来.我复制下来编译代码会出现没有NULL定义,所以我加了头文件进去了,当然也可以自己定义一个NULL.你可以将list.h拷贝到你的任意工程中使用.以下是我使用链表的代码.
stuinfo.h文件中的代码:
#ifndef __LIST_STU__#define __LIST_STU__#include "list.h"struct stuinfo{ char name[30]; int age; char addr[50]; struct list_head list;};struct stuinfo *init_stuinfo(void);int insertstuinfo(char name[30],int age,char addr[50],struct list_head *head);void iterator_stuinfolist(struct list_head *head);struct stuinfo *find_stuinfo_by_name(char *name,struct list_head *head);void del_stuinfo(char *name,struct list_head *head);#endifstuinfo.c文件中的代码
#include "stuinfo.h"#include <string.h>#include <stdlib.h>struct stuinfo *init_stuinfo(void){ struct stuinfo *tstuinfo; tstuinfo = (struct stuinfo *)malloc(sizeof(struct stuinfo)); memset(tstuinfo,0,sizeof(struct stuinfo)); INIT_LIST_HEAD(&tstuinfo->list); return tstuinfo;}int insertstuinfo(char name[30],int age,char addr[50],struct list_head *head)//插入链表{ struct stuinfo *ttmpstuinfo; ttmpstuinfo = (struct stuinfo *)malloc(sizeof(struct stuinfo)); if(ttmpstuinfo == NULL) return 1; strcpy(ttmpstuinfo->name,name); ttmpstuinfo->age = age; strcpy(ttmpstuinfo->addr,addr); list_add_tail(&ttmpstuinfo->list,head); return 0;}void iterator_stuinfolist(struct list_head *head)//遍历整个链表{ struct list_head *pos; struct stuinfo *tmp; list_for_each(pos,head){ tmp = list_entry(pos,struct stuinfo,list); printf("tmp->name:%s \t tmp->age:%d \t tmp->addr :%s\n",tmp->name,tmp->age,tmp->addr); }}<pre name="code" class="cpp">struct stuinfo *find_stuinfo_by_name(char *name,struct list_head *head)//通过名字查找stuinfo节点{ struct list_head *pos; struct stuinfo *tmp; list_for_each(pos,head){ tmp = list_entry(pos,struct stuinfo,list); if(strcmp(tmp->name,name) == 0) { return tmp; } } return NULL;}void del_stuinfo(char *name,struct list_head *head)//通过名字删除stuinfo节点{ struct list_head *listtmp; struct list_head *pos; struct stuinfo *tmp; list_for_each(pos,head){ tmp = list_entry(pos,struct stuinfo,list); if(strcmp(tmp->name,name) == 0) { //listtmp = pos->prev; //如果想让list_for_each继续去掉注释并将return ;语句注释掉 list_del(pos); //pos = listtmp; free(tmp); tmp = NULL; return ; } }}
main.c文件中的代码:
#include "stuinfo.h"#include <stdio.h>#include <unistd.h>int main(int argc,char* argv[]){ struct stuinfo *tstuinfo; struct stuinfo *findstuinfo; tstuinfo = init_stuinfo(); if (insertstuinfo("xiaoli",18,"beijing",&tstuinfo->list)) { printf("insert error!\n"); exit(0); } if (insertstuinfo("xiaoming",14,"guangzhou",&tstuinfo->list)) { printf("insert error!\n"); exit(0); } if (insertstuinfo("xiaobai",21,"shanghai",&tstuinfo->list)) { printf("insert error!\n"); exit(0); } if (insertstuinfo("xiaohei",25,"hubei",&tstuinfo->list)) { printf("insert error!\n"); exit(0); } iterator_stuinfolist(&tstuinfo->list); findstuinfo = find_stuinfo_by_name("xiaobai",&tstuinfo->list); if(findstuinfo == NULL) printf("not find the name of xiaobai\n"); else { printf("findstuinfo->name:%s \t findstuinfo->age:%d \t findstuinfo->addr :%s\n",findstuinfo->name,findstuinfo->age,findstuinfo->addr); del_stuinfo(findstuinfo->name,&tstuinfo->list); } iterator_stuinfolist(&tstuinfo->list); return 0;}
最后编译代码: gcc -o main main.c stuinfo.c执行./main可以看到结果如下:tmp->name:xiaoli tmp->age:18 tmp->addr :beijing
tmp->name:xiaoming tmp->age:14 tmp->addr :guangzhoutmp->name:xiaobai tmp->age:21 tmp->addr :shanghai
tmp->name:xiaohei tmp->age:25 tmp->addr :hubei
findstuinfo->name:xiaobai findstuinfo->age:21 findstuinfo->addr :shanghai
tmp->name:xiaoli tmp->age:18 tmp->addr :beijing
tmp->name:xiaoming tmp->age:14 tmp->addr :guangzhou
tmp->name:xiaohei tmp->age:25 tmp->addr :hubei
以上的操作是应用层使用内核的双链表的插入,查找,删除,遍历的例子.以下这篇文章有讲链表的函数使用还有一个链表使用的驱动程序,http://blog.chinaunix.net/uid-27037833-id-3237153.html
1 1
- 应用层调用内核中的链表操作.
- 在应用层使用内核链表
- 内核---内核应用层
- 应用层与驱动层通信---内核中的加法运算
- Android Webkit内核应用层函数调用方法
- LINUX系统调用原理-既应用层如何调用内核层函数之软件中断
- 应用调用驱动层
- 内核层与应用层通信详解
- 应用层和内核层通信
- 内核层读写应用层文件
- 应用层 和 内核通讯
- 应用层对i2c通用驱动接口与eeprom的i2c驱动接口从应用层到内核层调用区别和联系
- Android应用层操作GPIO
- Android应用层操作GPIO
- 了解安卓架构(linux内核层、系统运行库层、应用框架层、应用层)
- linux 内核与应用层的接口
- Windbg内核调试下调试应用层
- 内核ko中打印应用层数据
- POJ 2451 Uyuw's Concert 半平面交求面积
- Linux忘记 root密码的解决办法
- 线性插值——如何根据两点确定一条线段
- HTML 分组无序列表和有序列表
- Hibernate--HQL的使用(包括表的连接)
- 应用层调用内核中的链表操作.
- NGUI FontMaker 简单使用,以及在文字源文件丢失的情况下,怎么根据UIFont预制获取全部文字
- 前端开发和设计必备的Chrome插件
- PHP之——PDO_MYSQL扩展安装步骤
- HexString类型的颜色的使用
- C语言学习之关键字第十讲
- JQuery前端插件的一些使用
- 阿里云CentOS6.5挂载数据盘以及搭建LAMP环境
- java垃圾回收机制