[Linux]学习笔记系列 -- hashtable


title: hashtable

categories:

  • linux
  • include
    tags:
  • linux
  • include
    abbrlink: 14dfa8ed
    date: 2025-10-03 09:01:49

https://github.com/wdfk-prog/linux-study

文章目录

  • include/linux/hashtable.h
    • [DEFINE_HASHTABLE 定义哈希表](#DEFINE_HASHTABLE 定义哈希表)
    • [hlist_for_each_entry 遍历哈希表](#hlist_for_each_entry 遍历哈希表)
    • [hash_add 将一个对象添加到哈希表](#hash_add 将一个对象添加到哈希表)

include/linux/hashtable.h

DEFINE_HASHTABLE 定义哈希表

c 复制代码
#define DEFINE_HASHTABLE(name, bits)						\
	struct hlist_head name[1 << (bits)] =					\
			{ [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }

hlist_for_each_entry 遍历哈希表

c 复制代码
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)

#define hlist_for_each(pos, head) \
	for (pos = (head)->first; pos ; pos = pos->next)

#define hlist_for_each_safe(pos, n, head) \
	for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
	     pos = n)

#define hlist_entry_safe(ptr, type, member) \
	({ typeof(ptr) ____ptr = (ptr); \
	   ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
	})

/**
 * hlist_for_each_entry - 遍历给定类型的列表
 * @pos:	用作循环游标的类型 *。
 * @head:	您列表的开头。
 * @member:	结构体内 hlist_node 的名称。
 */
#define hlist_for_each_entry(pos, head, member)				\
	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
	     pos;							\
	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))


/**
 * hash_for_each_possible - 遍历所有可能哈希到同一桶的对象 
 * @name: 要遍历的哈希表 * @obj: 用作每个条目的循环游标的类型* 
 * @member: 结构体内 hlist_node 的名称 
 * @key: 要遍历的对象的键
 */
#define hash_for_each_possible(name, obj, member, key)			\
	hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)

hash_add 将一个对象添加到哈希表

c 复制代码
/**
 * hlist_add_head - add a new entry at the beginning of the hlist
 * @n: new entry to be added
 * @h: hlist head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
	struct hlist_node *first = h->first;
	WRITE_ONCE(n->next, first);
	if (first)
		WRITE_ONCE(first->pprev, &n->next);
	WRITE_ONCE(h->first, n);
	WRITE_ONCE(n->pprev, &h->first);
}

/**
 * hash_add - 将一个对象添加到哈希表 
 * @hashtable: 要添加到的哈希表 
 * @node: 要添加的对象的 &struct hlist_node 
 * @key: 要添加的对象的键
 */
#define hash_add(hashtable, node, key)						\
	hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
相关推荐
CheungChunChiu2 小时前
Linux 内核动态打印机制详解
android·linux·服务器·前端·ubuntu
BlueBirdssh4 小时前
linux 内核通过 dts 设备树 配置pcie 控制器 各种参数和中断等, 那freeRTOS 是通过直接设置PCIe寄存器吗
linux
小目标一个亿4 小时前
Windows平台Nginx配置web账号密码验证
linux·前端·nginx
2501_942326444 小时前
寒假高效记忆法助力学习飞跃
学习
Aotman_4 小时前
Element-UI Message Box弹窗 使用$confirm方法自定义模版内容,修改默认样式
linux·运维·前端
计算机程序设计小李同学4 小时前
基于SSM框架的动画制作及分享网站设计
java·前端·后端·学习·ssm
深情的小陈同学4 小时前
工作学习笔记 —— 支持手机端的添加表单行操作
笔记·学习·ai编程
xiangshi_yan5 小时前
内核学习之路【4/100】-io
学习
am心6 小时前
学习笔记-小程序-导入商品浏览功能实现
笔记·学习