contributed by < yuwen
>
這題主要是解釋了 Linux Kernal 中 hashmap 的實作,可以從講義得知定義為
hlist_head
結構first
,並沒有 pprev
。因為在使用 hash table
時並不會有針對 tail 的操作,且記憶體會減少很多。hlist_node
結構pprev
是 indirect pointer ,因為如果和 list_head
一樣使用單純的指標的話在 delete node 時需要考慮 list 的 head 或是 NULL 兩種狀況,因此使用 **pprev
直接存取上一個 node 所在的位址。hash_key
結構這是在計算 hash table 的 size 。由 MAP_HASH_SIZE()
巨集來取得,值為輸入值乘以 2 ,並會紀錄在 map_t
的變數名稱 bits
中。
map_init()
的功能是為了動態配置出 table 結構,並將每個 hlist_head 的 first 成員都初始化為 NULL 。
void *map_get
可以透過 static struct hash_key *find_key
取得 hash_key
中的 address,最後回傳 address of data。
在 find_key
中的 hash
function 是利用 golden ratio marco 來定義,參考 tinyynoob同學的 g