/** * hash_add - add an object to a hashtable * @hashtable: hashtable to add to * @node: the &struct hlist_node of the object to be added * @key: the key of the object to be added */ hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
hash_min 这个宏用于判断这个元素将进入哪个存储桶。
iterate all elements
1 2 3 4 5 6 7 8 9 10
/** * hash_for_each - iterate over a hashtable * @name: hashtable to iterate * @bkt: integer to use as bucket loop cursor * @obj: the type * to use as a loop cursor for each entry * @member: the name of the hlist_node within the struct */ for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\ (bkt)++)\ hlist_for_each_entry(obj, &name[bkt], member)