边学边用linux-源代码篇
目录
部分Linux源代码赏析,侧重在系统实现中用到的重要的数据结构和算法
常见宏
-
内存相关
// 返回x所指向的内存区域的起始cacheline的边界地址 #define L1_CACHE_ALIGN(x) __ALIGN_KERNEL(x, L1_CACHE_BYTES)
// 局部数据,将数据分配到程序.data段,起始位置对齐 ____cacheline_aligned
// (下划线少了2个)全局数据,其他同上 __cacheline_aligned
// 提供给list_head,计算所属结构体 #define list_entry(ptr, type, member) \ container_of(ptr, type, member) // 从指定地址ptr,计算所属结构体的首地址 #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) // 通过0地址计算字段偏移量 #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
数据结构
- 通用
// 通用双向链表的指针域 struct list_head { struct list_head *next, *prev; };
// 哈希列表中的槽 struct hlist_head { struct hlist_node *first; }; // 哈希列表槽中的双向链表节点 struct hlist_node { // pprev指向前驱的next指针 struct hlist_node *next, **pprev; };