Linux内核Hash Table结构及神奇的container_of宏

Linux内核Hash Table结构

近期在学习linux内核netfilter框架中有关conntrack和分片重组相关的代码过程中,顺便学习到了内核中hash表的结构。在此记录一下。
内核中hash表的定义:

		struct  hlist_head{
				struct hlist_node *first;
		}
		
		struct  hlist_node {
		        struct hlist_node *next,**pprev;
		} 
  1. hlist_head表示哈希表的头结点。哈希表中每一个entry(list_entry)所对应的都是一个链表(hlist).hlist_head结构体只有一个域,即first。First指针指向该hlist链表的第一个结点。
  2. hlist_node结构体有两个域,next和pprev。
    (1)next指向下个hlist_node结点,倘若该结点是链表的最后一个节点,next则指向NULL
    (2)pprev是一个二级指针,它指向前一个节点的next指针。

更详细的内核Hash Table相关的

注意:hlist_node hash表节点的结构体中只有其他节点的指针信息,即只有表本身所需的结构,没有额外空间存储Value信息。那么使用hash记录信息是,信息存储到哪了呢?或者说找到了hash节点,又怎么能找到相关联的信息呢?
内核提供了container_of宏来解决上面提出的疑问。具体实现是:hash节点(hlist_node)可作为另一结构体A的成员,A中的其他成员用于存储必要的信息。通过Hash表查询到hlist_node的地址后,container_of宏可以通过hlist_node的地址得到其父结构体A的地址。

container_of宏

container_of宏定义:

/**
 * 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) ({				\
	void *__mptr = (void *)(ptr);					\
	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
			 !__same_type(*(ptr), void),			\
			 "pointer type mismatch in container_of()");	\
	((type *)(__mptr - offsetof(type, member))); })

未完待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值