contributed by < Kevin-Shih
>
先分開來看各個 function 的用途:
若有找到則回傳該節點,否則回傳 NULL。
假設 hash 後的地址為 2
:
find_key()
會以 pointer p
如上圖依序走訪 1
、5
、-2
,並比較 key
值是否與輸入的 @key
相等,若相等則回傳該 hash_key
的位址。
會將新的 node kn
插到原先的 fisrt 前面。
n->next = first;
n->prev = &h->first;
n->next
接到原先的第一個 node ,first
可以為 NULL
。pprev
指回 n
。#19 的 first->pprev = &n->next;
是將 first->pprev
指向存放 n->next
這個 pointer 的位址,而非 n->next
這個 pointer 中存放的位址。 因此該行作用相當於將 first->pprev
指回 n
。 (用 containerof()
即可取得 n
的位址)
head->first
指向新的 "first" 也就是 n
。n->pprev
指向 head
。同 #19 的 first->pprev = &n->next;
#21 將 n->pprev
指向存放 &h->first
這個 pointer 的位址。 (用 containerof()
即可取得 head
的位址)
看懂 #18~20 的是將新的節點 n 放在 hlist 中的第一個位置就很好推論出 #17 的 AAA 是將 n->next
指向原先的 first
、#21 的 BBB 則是將 n->pprev
指向 &head->first
。
節錄自linux/hash.h:
可以發現 GOLDEN_RATIO_PRIME 在 32 bit 系統(unsigned long 為 32 bit)上為 0x61C88647
而在 64 bit 系統上為 0x61C8864680B583EBull
,兩者皆非時會 error。