Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2691485
  • 博文数量: 505
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 2514
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-23 18:24
文章分类

全部博文(505)

文章存档

2019年(12)

2018年(15)

2017年(1)

2016年(17)

2015年(14)

2014年(93)

2013年(233)

2012年(108)

2011年(1)

2009年(11)

分类: C/C++

2013-01-23 08:40:59

    之前上《linux操作系统》课程的时候,陈老师安排的一个作业就是分析linux内核中的头文件list.h,但是之前我没有认真做,都是完成任务式的去做。
    今天比较伤心些,一个同学问我list.h中的
  1. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  2. #define LIST_HEAD(name) \
  3.     struct list_head name = LIST_HEAD_INIT(name)
    宏代表什么含义,一时没有给她解释清楚,于是下决心把这个list.h认真的分析一边,给她一个满意的答复。由于内容比较多,我把list.h头文件的分析分为了几个部分。
(一)结构体定义
  1. struct list_head {
  2.     struct list_head *next, *prev;
  3. };
  4. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  5. #define LIST_HEAD(name) \
  6.     struct list_head name = LIST_HEAD_INIT(name)
申请一个变量LIST_HEAD(temp)等价于这个:
struct list_head temp = {&(temp), &(temp)}
附带知识:
  1. 结构体赋值:
  2. 1、对成员赋值
  3. 例如结构体struct st1 {
  4. int a;
  5. int b;
  6. int c;
  7. }
  8. 1.1 用{}形式
  9. struct st1 st1 = {1,2,3);
  10. 1.2 linux kernel风格.
  11. struct st1 st1 = {
  12. .a = 1;
  13. .b = 2;
  14. };
  15. //注 此风格(即在成员变量之前加点“.”),可以不按成员变量的顺序进行赋值。如可以为
  16. struct st1 st1 = {
  17. .c = 3;
  18. .a = 1;
  19. .b = 2;
  20. };
  21. 2 对整体赋值.
  22. struct st1 a, b;
  23. b = a;
  24. 3 结构体作为函数返回值对另一个结构体赋值.
  25. struct st1 func1();
  26. struct st1 a = func1();
(二)结构体初始化
结构体初始化函数:
  1. static inline void INIT_LIST_HEAD(struct list_head *list)
  2. {
  3.     list->next = list;
  4.     list->prev = list;
  5. }
初始化结构体list指向自己本身。
对于(一)结构体定义和(二)结构体初始化来说,最终的效果是一样的,都是将一个
struct list_head变量指向自己本身。
可以写一个小的程序测试一下
  1. /*test.c*/
  2. #include <stdio.h>
  3. struct list_head {
  4.     struct list_head *next, *prev;
  5. };
  6. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  7. #define LIST_HEAD(name) \
  8.     struct list_head name = LIST_HEAD_INIT(name)
  9. static inline void INIT_LIST_HEAD(struct list_head *list)
  10. {
  11.     list->next = list;
  12.     list->prev = list;
  13. }
  14. int main()
  15. {
  16.     LIST_HEAD(temp);
  17.     printf("%p %p %p\n", (&temp)->prev, (&temp)->next, &temp);
  18.     INIT_LIST_HEAD(&temp);
  19.     printf("%p %p %p\n", (&temp)->prev, (&temp)->next, &temp);
  20.     return 0;
  21. }
  22. 运行结果:
  23. ^_^[sunny@sunny-laptop ~/DS]11$ ./a.out
  24. 0xbf8191a8 0xbf8191a8 0xbf8191a8
  25. 0xbf8191a8 0xbf8191a8 0xbf8191a8
  26. ^_^[sunny@sunny-laptop ~/DS]12$
可以看出他们完毕之后的地址都是一样的。
(三)增加结点
附带知识:
  1. 内联函数 inline
  2. 在c 中,为了解决一些频繁调用的小函数而大量消耗栈空间或者是叫栈内存的问题,特别的引入了inline修饰符,表示为内联函数。
  3. 内联函数使用inline关键字定义,
  4. 并且函数体和声明必须结合在一起,
  5. 否则编译器将他作为普通函数对待。
  6. inline void function(int x); //仅仅是声明函数,没有任何效果
  7. inline void function(int x) //正确
  8. {
  9. return x;
  10. }
增加结点的话,有两种方式:头插法和尾插法。
我们调用的话就调用
static inline void list_add(struct list_head *new, struct list_head *head);
static inline void list_add_tail(struct list_head *new, struct list_head *head);
这两个接口。
头插法:
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
尾插法:
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
真正的实现插入:
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(new, prev, next):表示在prev和next之间添加一个新的节点new
所以对于list_add()中的__list_add(new, head, head->next)表示的在head和
head->next之间加入一个新的节点,是头插法。
对于list_add_tail()中的__list_add(new, head->prev, head)表示在head->prev(双向循环链表的最后一个结点)和head之间添加一个新的结点。


阅读(595) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~