练习一下linux中的list函数。
所有的list函数见 include/linux/list.h
自己从 include/linux/list.h 拷贝了一些函数到自己的list.c中, 然后练习了一下。
没有别的目的,就是想熟练一下。毕竟linux内核代码中试用了大量的list函数。
list的函数太方便使用了。
文件:list.c
#include <stdio.h>
// #include <linux/list.h> 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) //ËüÖ»Êǽ«newÌí¼Óµ½prevºÍnextÖ®¼ä
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
} static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
} static inline int list_empty(const struct list_head *head)
{
return head->next == head;
} #undef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) /**
* * container_of - cast a member of a structure out to the containing structure
* * @ptr: the pointer to the member.
* * @type: the type of the container struct this is embedded in.
* * @member: the name of the member within the struct.
* *
* */
#define container_of(ptr, type, member) ({ \
const typeof( ((type *))->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );}) #define list_entry(ptr, type, member) \
container_of(ptr, type, member) /**
* * list_for_each_entry - iterate over list of given type
* * @pos: the type * to use as a loop cursor.
* * @head: the head for your list.
* * @member: the name of the list_struct within the struct.
* */
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member)) #define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next) struct devlist {
struct list_head list;
char * name;
}; struct devlist dev0 = {
.list = LIST_HEAD_INIT(dev0.list),
.name = NULL,
}; /* static initiate head list*/
// LIST_HEAD(hl);
struct list_head hl = LIST_HEAD_INIT(hl); void main(void) {
/* dynamic initiate head list. */
INIT_LIST_HEAD(&hl);
struct list_head *l;
struct devlist * pdev = NULL;
int empty = list_empty(&hl);
if(empty)
printf("the device list is empty!\n");
struct devlist dev1;
dev1.name = "device1";
list_add_tail(&dev1.list, &hl);
struct devlist edev1 = {{}, "entry device1"};
list_add_tail(&edev1.list, &dev0.list);
struct devlist dev2 = {{}, "device2"};
list_add_tail(&dev2.list, &hl);
struct devlist edev2 = {{}, "entry device2"};
list_add_tail(&edev2.list, &dev0.list); empty = list_empty(&hl);
if(!empty)
printf("the device list is not empty!\n"); list_for_each_entry(pdev, &dev0.list, list){
printf("the device name is %s\n", pdev->name);
} list_for_each(l, &hl) {
// for (l = (&hl)->next; l != (&hl); l = l->next){
// printf("l is: %p, head is: %p \n", l, &hl);
pdev = list_entry(l, typeof(dev0), list);
printf("the device name is %s\n", pdev->name);
}
}
输出结果:
$ ./list
the device list is empty!
the device list is not empty!
the device name is entry device1
the device name is entry device2
the device name is device1
the device name is device2
练习一下linux中的list函数。的更多相关文章
- 深入解析Linux中的fork函数
1.定义 #include <unistd.h> #include<sys/types.h> pid_t fork( void ); pid_t 是一个宏定义,其实质是int, ...
- 如何测试Linux 中的wait函数能不能等待子进程的子进程?
#include <stdio.h> #include <stdlib.h> int main() { pid_t pid = fork(); switch(pid) { : ...
- [fork]Linux中的fork函数详解
---------------------------------------------------------------------------------------------------- ...
- 关于linux中的system函数
Linux下使用system()函数一定要谨慎 https://blog.csdn.net/senen_wakk/article/details/51496322 system()正确应用 https ...
- Linux中的入口函数main
main()函数,想必大家都不陌生了,从刚开始写程序的时候,大家便开始写main(),我们都知道main是程序的入口.那main作为一个函数,又是谁调用的它,它是怎么被调用的,返回给谁,返回的又是什么 ...
- Linux中的读函数与块高速缓存
为了提高Linux块设备读写的效率,Unix会在内存中建立块高速缓存,块高速缓存存储了系统最近读的数据块和刚刚写入的数据块,也就是说IO访问其实是和块高速缓存打交道的(直接IO除外),块高速缓存会适时 ...
- Unix/Linux中的fork函数
fork函数介绍 一个现有进程可以调用fork函数创建一个新进程.该函数定义如下: #include <unistd.h> pid_t fork(void); // 返回:若成功则在子进程 ...
- linux中内核延时函数 (转)
第一类延时函数原型是:(忙等) void ndelay(unsigned long nsecs); void udelay(unsigned long usecs); void mdelay(unsi ...
- linux中字符串转换函数 simple_strtoul【转】
转自:http://blog.csdn.net/tommy_wxie/article/details/7480087 Linux内核中提供的一些字符串转换函数: lib/vsprintf.c [htm ...
随机推荐
- 小米4c刷机
[测评] 试用小米4c最新波兰开发版 http://www.miui.com/thread-3048163-1-1.html [教程] 小米4C刷第三方recovery 以及ROOT教程 http:/ ...
- 如何使用getopt()函数解析参数
最近在写程序的过程中,把一部分时间都花费在程序对参数的处理上.今天听了学长说到getopt函数,才发现原来c里面还有一个专门解决参数处理的函数,查询了相关资料,这里简单总结一下. 使用int main ...
- 学习使用Free RTOS ,移植最新的STM32 v3.5固件库
最近拿到了一块万利的EK-STM32F的学习板,打算先用它来熟悉下STM32的编程环境,不过在这过程中和一个同事聊的时候觉得如果只调用STM32的库写程序,可能在芯片资源的利用上面可能就会差一点,在这 ...
- 2014第13周四Webservice概念问题记
晚上回来看网页学习了这两天一直疑惑的两个问题: 1.REST和SOAP架构下的Webservice的区别? 2.axis2和CXF的区别. 大部分是理论,暂时摘录一下,以后有更多实践后再回顾. 一.R ...
- Linux下设置开机自启动Tomcat
方法一: linux 下tomcat开机自启动修改Tomcat/bin/startup.sh 为: export JAVA_HOME=/usr/java/j2sdk1..2_08 export CLA ...
- 递推计数-hdu-4747-Mex
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4747 题目大意: 给n个数,求所有区间内没有出现的最小非负整数和. 解题思路: 首先感谢大神博客:h ...
- 【COM学习】之二、HRESULT,GUID
HRESULT 来向用户报告各种情况. 他的值位于 WINERROR.H中 QueryInterface返回一个HRESULT值. HRESULT不是一个句柄,他是一个可分成三个域的32位值. ...
- jQuery学习笔记(一)——基础选择器、过滤选择器、表单选择器
$()就是jQuery中的函数,它的功能是获得()中指定的标签元素.如演示样例中$("p")会得到一组P标签元素,当中"p"表示CSS中的标签选择器.$()中的 ...
- js中 ===与==
js里面,==比较的是参数的值,不会比较参数的类型,===需要先比较参数的类型是否一致,然后才会去比较值比如,if(3 == "3")这个会返回true,if(3 === &quo ...
- js的this几种用法
1.普通的函数调用 此时指的是全局对象 function aaa(){ this.x=1;}aaa();alert(x) 2.对象内的方法this调用 此时指的是上一级对象 var aaa={ zz: ...