易截截图软件、单文件、免安装、纯绿色、仅160KB

Linux内核中 遍历链表


最近在看一个开源的代码,一段代码看了很久都没有看懂,Google了一下,尽然是linux中链表的遍历。
遍历链表:
遍历链表list_for_each是一个宏,展开了就是一个for循环
#define list_for_each(pos, head) \
        for (pos = (head)->next; prefetch(pos->next), pos != (head); \
                pos = pos->next)
其中prefech(pos->next)是处理器预取pos->next,通过处理器的预取功能,对for循环进行了优化。
··访问数据操作:
#define list_entry(ptr, type, member) \
        container_of(ptr, type, member)
#define container_of(ptr, type, member) ({                      \
        const typeof(((type *)0)->member) * __mptr = (ptr);     \
        (type *)((char *)__mptr - offsetof(type, member)); } )
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
container_of是一个有点复杂的宏,使用了C扩展关键字typeof,还有不好理解的求结构体成员
变量偏移.....
·((type *)0)是将0强制转化为type *,也就是指向type类型的指针
·((type *)0)->member是访问type结构中的member成员
·const typeof( ((type *)0)->member ) *__mptr 定义一个与((type *)0)->member同种类型的
指针变量(const变量)
·const typeof( ((type *)0)->member ) *__mptr=(ptr)对常量指针变量__mptr赋值
__mptr=ptr
·((size_t) &((TYPE *)0)->MEMBER得到member在type结构体中的偏移量,
offsetof(type,member)的展开效果
·(char *)__mptr - offsetof(type,member) 将__mptr强制转化为char类型指针,也就是__mptr
的地址,然后减去member在结构体中的偏移量,的到的自然就是结构体起始地址(偏移量求的很巧妙,
又很巧妙的获得结构体起始地址)。
·(type *)( (char *)__mptr - offsetof(type,member) )最后再强制转化为(type *)类型


相关文档:

Linux程序参数

Linux程序参数包括两部分: 命令行参数和环境变量
命令行参数传递给mian函数  int mian (int argc,char *argv[])
环境变量  extern char *environ[]
获得环境变量的函数: const char* getenv(const char* name)
设置环境变量的函数:
        (1)int putent (const c ......

linux设备驱动开发详解——globalmem



 
linux设备驱动开发详解——globalmem
收藏



常见的两种错误:
1.在插入模块的时候出现如下问题:
# insmod globalmem.ko insmod: error inserting 'globalmem.ko': -1
Device or res ......

linux LVM基本操作

1.增加硬件
增加的ide硬盘前缀为hd,scsi硬盘前缀为sd;第一块硬盘为hda或sda,第二块硬盘为hdb或sdb,以此类推。
2.用pvcreate创建physical volumn
pvcreate /dev/sdb1 /dev/sdb2
此处所用的分区是第二步中操作的类型为8e的分区,pvcreate的参数可以为1个或多个;创建完成后可以用pvdisplay -v来查看创建的情况,用pvre ......

如何开启关闭linux防火墙

如何开启关闭linux防火墙
 
 
  重启后生效
  开启:chkconfig iptables on
  关闭:chkconfig iptables off
  即时生效,重启后失效
  开启:service iptables start
  关闭:service iptables stop
  或
  关闭: /etc/rc.d/init.d/iptables stop
  启动: /etc/rc.d/init.d/ip ......

[转帖]Linux中使用搜狗浏览器的代理为其他程序加速

wine 一个1.1版本的搜狗浏览器:
 $ ln -s
文件夹路径/SogouExplorer/ .wine/drive_c/Program\ Files/SogouExplorer
 $ wine "C:\Program
Files\SogouExplorer\SogouExplorer.exe" -proxy
PID:8
PORT:8081
CPORT:8082
PAC:http://127.0.0.1:8082/proxy.pac?t=1349
RET:SUCCESS

可在 Fi ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号