所有的list函数见 include/linux/list.h

自己从 include/linux/list.h 拷贝了一些函数到自己的list.c中, 然后练习了一下。

没有别的目的,就是想熟练一下。毕竟linux内核代码中试用了大量的list函数。

list的函数太方便使用了。

文件:list.c

  1. #include <stdio.h>
  2. // #include <linux/list.h>
  3.  
  4. struct list_head {
  5. struct list_head *next, *prev;
  6. };
  7.  
  8. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  9. #define LIST_HEAD(name) \
  10. struct list_head name = LIST_HEAD_INIT(name)
  11.  
  12. static inline void INIT_LIST_HEAD(struct list_head *list)
  13. {
  14. list->next = list;
  15. list->prev = list;
  16. }
  17.  
  18. static inline void __list_add(struct list_head *new,
  19. struct list_head *prev,
  20. struct list_head *next) //ËüÖ»Êǽ«newÌí¼Óµ½prevºÍnextÖ®¼ä
  21. {
  22. next->prev = new;
  23. new->next = next;
  24. new->prev = prev;
  25. prev->next = new;
  26. }
  27.  
  28. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  29. {
  30. __list_add(new, head->prev, head);
  31. }
  32.  
  33. static inline int list_empty(const struct list_head *head)
  34. {
  35. return head->next == head;
  36. }
  37.  
  38. #undef offsetof
  39. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  40.  
  41. /**
  42. * * container_of - cast a member of a structure out to the containing structure
  43. * * @ptr: the pointer to the member.
  44. * * @type: the type of the container struct this is embedded in.
  45. * * @member: the name of the member within the struct.
  46. * *
  47. * */
  48. #define container_of(ptr, type, member) ({ \
  49. const typeof( ((type *))->member ) *__mptr = (ptr); \
  50. (type *)( (char *)__mptr - offsetof(type,member) );})
  51.  
  52. #define list_entry(ptr, type, member) \
  53. container_of(ptr, type, member)
  54.  
  55. /**
  56. * * list_for_each_entry - iterate over list of given type
  57. * * @pos: the type * to use as a loop cursor.
  58. * * @head: the head for your list.
  59. * * @member: the name of the list_struct within the struct.
  60. * */
  61. #define list_for_each_entry(pos, head, member) \
  62. for (pos = list_entry((head)->next, typeof(*pos), member); \
  63. &pos->member != (head); \
  64. pos = list_entry(pos->member.next, typeof(*pos), member))
  65.  
  66. #define list_for_each(pos, head) \
  67. for (pos = (head)->next; pos != (head); pos = pos->next)
  68.  
  69. struct devlist {
  70. struct list_head list;
  71. char * name;
  72. };
  73.  
  74. struct devlist dev0 = {
  75. .list = LIST_HEAD_INIT(dev0.list),
  76. .name = NULL,
  77. };
  78.  
  79. /* static initiate head list*/
  80. // LIST_HEAD(hl);
  81. struct list_head hl = LIST_HEAD_INIT(hl);
  82.  
  83. void main(void) {
  84. /* dynamic initiate head list. */
  85. INIT_LIST_HEAD(&hl);
  86. struct list_head *l;
  87. struct devlist * pdev = NULL;
  88. int empty = list_empty(&hl);
  89. if(empty)
  90. printf("the device list is empty!\n");
  91. struct devlist dev1;
  92. dev1.name = "device1";
  93. list_add_tail(&dev1.list, &hl);
  94. struct devlist edev1 = {{}, "entry device1"};
  95. list_add_tail(&edev1.list, &dev0.list);
  96. struct devlist dev2 = {{}, "device2"};
  97. list_add_tail(&dev2.list, &hl);
  98. struct devlist edev2 = {{}, "entry device2"};
  99. list_add_tail(&edev2.list, &dev0.list);
  100.  
  101. empty = list_empty(&hl);
  102. if(!empty)
  103. printf("the device list is not empty!\n");
  104.  
  105. list_for_each_entry(pdev, &dev0.list, list){
  106. printf("the device name is %s\n", pdev->name);
  107. }
  108.  
  109. list_for_each(l, &hl) {
  110. // for (l = (&hl)->next; l != (&hl); l = l->next){
  111. // printf("l is: %p, head is: %p \n", l, &hl);
  112. pdev = list_entry(l, typeof(dev0), list);
  113. printf("the device name is %s\n", pdev->name);
  114. }
  115. }

输出结果:

  1. $ ./list
  2. the device list is empty!
  3. the device list is not empty!
  4. the device name is entry device1
  5. the device name is entry device2
  6. the device name is device1
  7. the device name is device2

练习一下linux中的list函数。的更多相关文章

  1. 深入解析Linux中的fork函数

    1.定义 #include <unistd.h> #include<sys/types.h> pid_t fork( void ); pid_t 是一个宏定义,其实质是int, ...

  2. 如何测试Linux 中的wait函数能不能等待子进程的子进程?

    #include <stdio.h> #include <stdlib.h> int main() { pid_t pid = fork(); switch(pid) { : ...

  3. [fork]Linux中的fork函数详解

    ---------------------------------------------------------------------------------------------------- ...

  4. 关于linux中的system函数

    Linux下使用system()函数一定要谨慎 https://blog.csdn.net/senen_wakk/article/details/51496322 system()正确应用 https ...

  5. Linux中的入口函数main

    main()函数,想必大家都不陌生了,从刚开始写程序的时候,大家便开始写main(),我们都知道main是程序的入口.那main作为一个函数,又是谁调用的它,它是怎么被调用的,返回给谁,返回的又是什么 ...

  6. Linux中的读函数与块高速缓存

    为了提高Linux块设备读写的效率,Unix会在内存中建立块高速缓存,块高速缓存存储了系统最近读的数据块和刚刚写入的数据块,也就是说IO访问其实是和块高速缓存打交道的(直接IO除外),块高速缓存会适时 ...

  7. Unix/Linux中的fork函数

    fork函数介绍 一个现有进程可以调用fork函数创建一个新进程.该函数定义如下: #include <unistd.h> pid_t fork(void); // 返回:若成功则在子进程 ...

  8. linux中内核延时函数 (转)

    第一类延时函数原型是:(忙等) void ndelay(unsigned long nsecs); void udelay(unsigned long usecs); void mdelay(unsi ...

  9. linux中字符串转换函数 simple_strtoul【转】

    转自:http://blog.csdn.net/tommy_wxie/article/details/7480087 Linux内核中提供的一些字符串转换函数: lib/vsprintf.c [htm ...

随机推荐

  1. Flink Program Guide (9) -- StateBackend : Fault Tolerance(Basic API Concepts -- For Java)

    State Backends 本文翻译自文档Streaming Guide / Fault Tolerance / StateBackend ----------------------------- ...

  2. 《pigcms v6.2最新完美至尊版无任何限制,小猪微信源码多用户微信营销服务平台系统》

    <pigcms v6.2最新完美至尊版无任何限制,小猪微信源码多用户微信营销服务平台系统> 前两天分享了套小猪CMS(PigCms)多用户微信营销服务平台系统V6.1完美破解至尊版带微用户 ...

  3. ubuntu下lnmp的安装

    适用于ubuntu14.04和源是14.04的ubuntu上安装nginx(我学在线Moodle工作室---注这里安装的是最新版的nginx,并且解决pathinfo问题,特别适用于Moodle安装) ...

  4. 开源代码——Crouton

    开源代码——Crouton 一个可随意定位置的带色Toast——开源代码Crouton的简单使用   今天在公司要求的代码中,要求显示的提示能够更加具有多样化,而不是简单的Toast字样,第一想法肯定 ...

  5. 超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上

    超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上 阅读目录 Deft简介 Deft 核心类介绍 Deft 3分钟即可上手使用 其他可选的配置参数 性能测试 Demo代码下载 回到顶 ...

  6. 安卓使用spinner控件和pull解析实现全国省市县的三级联动

    实现该功能主要有两个难点: 1.XML文件的Pull解析.由于XML文件的结构较为复杂,所以解析时要注意,划分XML文件的结构,根据结构建立相应的和集合.本例中需要建立三个类和三个对象.三个类分别为, ...

  7. 普林斯顿大学算法课 Algorithm Part I 学习资源

    网友笔记参考 果壳Mooc首页 revilwang的专栏 白色咖啡 Weiran Liu的渣技术小专栏 Bug表:http://findbugs.sourceforge.net/bugDescript ...

  8. Mac 下 Chrome 快捷键大全

    1. 标签页和窗口快捷键 ⌘-N 打开新窗口. ⌘-T 打开新标签页. ⌘-Shift-N 在隐身模式下打开新窗口. 按 ⌘-O,然后选择文件. 在 Chrome 浏览器中打开计算机中的文件. 按住  ...

  9. 原生网络请求以及AFN网络请求/异步下载

    这里对网络请求方式做一个总结. 原生方式同步GET请求: NSString *urlStr = @"http://apis.juhe.cn/mobile/get?phone=13429667 ...

  10. 数据挖掘(七):Apriori算法:频繁模式挖掘

    1 算法思想 算法使用频繁项集性质的先验知识.Apriori使用一种称作逐层搜索的迭代方法,k项集用于探索(k+1)项集.首先,通过扫描数据库,累积每个项的计数,并收集满足最小支持度的项,找出频繁1项 ...