[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))])
相关推荐
酒鼎5 小时前
学习笔记(4)HTML5新特性(第3章)- WebSocket
笔记·学习·html5
郝亚军6 小时前
ubuntu-18.04.6-desktop-amd64安装步骤
linux·运维·ubuntu
-Springer-6 小时前
STM32 学习 —— 个人学习笔记2-2(新建工程)
笔记·stm32·学习
Konwledging6 小时前
kernel-devel_kernel-headers_libmodules
linux
Web极客码6 小时前
CentOS 7.x如何快速升级到CentOS 7.9
linux·运维·centos
tb_first6 小时前
万字超详细苍穹外卖学习笔记4
java·spring boot·笔记·学习·spring·mybatis
日更嵌入式的打工仔6 小时前
C内存布局
笔记
一位赵6 小时前
小练2 选择题
linux·运维·windows
卡布叻_星星7 小时前
达梦数据库笔记之使用教程以及不推荐迁移选择小写
笔记
山岚的运维笔记7 小时前
SQL Server笔记 -- 第15章:INSERT INTO
java·数据库·笔记·sql·microsoft·sqlserver