struct hlist_head {
struct hlist_node *first;
};
struct hlist_node {
struct hlist_node *next, **pprev;
};
一个是以名字为索引的哈希表,一个是以index为索引的哈希表
#define NETDEV_HASHBITS 8
static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
根据设备 名称 或者 index 计算出在hash表中的slot
static inline struct hlist_head *dev_name_hash(const char *name)
{
unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
}
static inline struct hlist_head *dev_index_hash(int ifindex)
{
return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
}
添加至哈希表的表头
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
struct hlist_node *first = h->first; 找到哈希表的表头
n->next = first; 需要添加的节点的下一个指向表头
if (first)
first->pprev = &n->next; 要是第一个存在,择第一个的前一个指向现在这个
h->first = n; 现在表头变成这个
n->pprev = &h->first; 指向了自己?
}