[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))])
相关推荐
AI视觉网奇5 分钟前
3d数字人 ue blender 绑定衣服对齐 2026
学习·ue5
Nan_Shu_6149 分钟前
学习: Blender 基础篇
学习·blender
Hello_Embed25 分钟前
libmodbus 移植 STM32(USB 串口后端篇)
笔记·stm32·单片机·嵌入式·freertos·libmodbus
张祥64228890435 分钟前
RTKLIB源码和理论结合分析笔记三
笔记
日更嵌入式的打工仔43 分钟前
0欧电阻作用
笔记
生活很暖很治愈44 分钟前
Linux——孤儿进程&进程调度&大O(1)调度
linux·服务器·ubuntu
HalvmånEver1 小时前
Linux:线程同步
linux·运维·服务器·线程·同步
奶茶精Gaaa1 小时前
工具分享--json在线转换工具
学习
Zach_yuan1 小时前
自定义协议:实现网络计算器
linux·服务器·开发语言·网络
wdfk_prog1 小时前
[Linux]学习笔记系列 -- [drivers][I2C]I2C
linux·笔记·学习