3

我正在尝试使用来自linux/rbtree.h. 我可以在内核中的独立空间(例如模块)中正确插入红/黑树,但是当我尝试让相同的代码与or中rb_root声明的函数一起运行时,每次尝试插入时都会得到一个。task_structtask_struct->files_structSEGFAULT

这是一些代码:

在 task_struct 中,我rb_root为我的树创建了一个结构(不是指针)。在init_task.hINIT_TASK(tsk)中,我将其设置为等于RB_ROOT。要进行插入,我使用以下代码:

rb_insert(&(current->fd_tree), &rbnode);

这就是问题发生的地方。

我的插入命令是所有 RBTree 内核文档中记录的标准插入:

  int my_insert(struct rb_root *root, struct mytype *data)
  {
    struct rb_node **new = &(root->rb_node), *parent = NULL;

    /* Figure out where to put new node */
    while (*new) {
        struct mytype *this = container_of(*new, struct mytype, node);
        int result = strcmp(data->keystring, this->keystring);

        parent = *new;
        if (result < 0)
            new = &((*new)->rb_left);
        else if (result > 0)
            new = &((*new)->rb_right);
        else
            return FALSE;
    }

    /* Add new node and rebalance tree. */
    rb_link_node(&data->node, parent, new);
    rb_insert_color(&data->node, root);

    return TRUE;
  }

有什么我想念的吗?

如果我在task_struct? 如果我rb_root在模块内部制作这个插件可以正常工作。但是,一旦我将实际的树根放入task_struct或什至 中task_struct->files_struct,我就会得到一个SEGFAULT. 不能在这些结构中添加根节点吗?

非常感谢任何提示。我已经尝试了几乎所有我能想到的东西。


编辑:

SEGFAULT尝试打印和访问树的任何行时,我在下一行得到一个。通过这一行,您应该了解我如何处理指针。rb_entry并且rb_first是内核中已经可用的方法。current是指向任务结构(当前工作进程)的指针,树是我的根节点(不是指针),它是任务结构的成员(我添加了)。rb_first需要传递一个指针*rb_root。我做错了。

printk(KERN_CRIT "node=%d\n", rb_entry(rb_first(&(current->tree)), struct rb_tree_struct, node)->fd_key);
4

1 回答 1

0

可能是根和/或数据的指针值不是您所期望的吗?添加可能有用

printk("%s: root=%p data=%p\n", __func__, root, data);

while()循环之前。

于 2010-04-20T16:24:28.053 回答