【推荐阅读】
忙了好久,总有有时间看内核,最近研究下了LINUX内核“页面回收”时候采用的PST树算法。
PST是 radix tree和heap tree的综合体。而且LINUX的实现和传统的算法还不一样,甚是花了不少时间啊。废话少说了,先上图。
下面是插入操作:还是先上图再帖代码
struct prio_tree_node *prio_tree_insert(struct prio_tree_root *root,struct prio_tree_node *node)
{
获取insert_node的radix和heap
get_index(root, node, &radix_index, &heap_index);
1 如果树是空
2 要insert_node的heap大于root的maxindex,则要先"扩展"树
if (prio_tree_empty(root) ||heap_index > prio_tree_maxindex(root->index_bits))
return prio_tree_expand(root, node, heap_index);
从root节点开始比较
cur = root->prio_tree_node;
获取mask掩码
mask = 1UL << (root->index_bits - 1);
while (mask) {
获取cur_node的radix和heap
get_index(root, cur, &r_index, &h_index);
如果insert_node已经存在,则直接返回当前node
if (r_index == radix_index && h_index == heap_index)
return