[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_一辰22 分钟前
AI测试工程笔记 05:AI评测实践(从数据集到自动评测闭环)
人工智能·笔记·功能测试·自动化·ai编程
BestOrNothing_20151 小时前
(2)联想拯救者安装 Ubuntu 双系统前的 BIOS 设置全过程
linux·bios·拯救者·ubuntu22.04·联想lenovo
23.1 小时前
【Linux】grep命令终极指南
linux
巨斧空间掌门1 小时前
JDK17 下载 windows Linux
linux·运维·服务器
AI+程序员在路上1 小时前
CANopen 协议:介绍、调试命令与应用
linux·c语言·开发语言·网络
learndiary2 小时前
2026.03.12~2026.03.19制作的共7个视频及简介
linux·视频·学习日记小店
JiMoKuangXiangQu2 小时前
Linux:ARM64 中断处理简析
linux·arm64 中断
Edward111111112 小时前
3月20包装类
学习
小生不才yz2 小时前
【Makefile 专家之路 | 函数篇】11. 终极奥义:eval 函数——动态生成规则的“核武器”
linux
皮卡蛋炒饭.3 小时前
进程得控制
linux·运维·服务器