Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Linux中的红黑树_C_Pointers_Linux Kernel_Red Black Tree - Fatal编程技术网

C Linux中的红黑树

C Linux中的红黑树,c,pointers,linux-kernel,red-black-tree,C,Pointers,Linux Kernel,Red Black Tree,我正在从事一个Linux内核项目,该项目涉及使用rbtree.h中定义的rb_树。以下是我存储在树中的结构: struct source_store{ sector_t source; sector_t cache; struct rb_node * node; } 为了从树中检索对象,我执行以下操作: struct rb_node * parent = root->rb_node; struct source_store * store = rb_entry(p

我正在从事一个Linux内核项目,该项目涉及使用rbtree.h中定义的rb_树。以下是我存储在树中的结构:

struct source_store{
    sector_t source;
    sector_t cache;
    struct rb_node * node;
}
为了从树中检索对象,我执行以下操作:

struct rb_node * parent = root->rb_node;
struct source_store * store = rb_entry(parent, struct source_store, node);
但是,在编译时,出现以下错误:

warning: initialization from incompatible pointer type

另外,当我从树中检索struts时,存储在source和cache字段中的数字是不同的。例如,我将数字512存储在源字段中,当我稍后检索结构时,它将是一个可笑的大数字,如16810075660910329857。据我所知,扇区是一个长的无符号整数。为什么存储的数字会改变?为什么指针类型不兼容?

您应该将
结构源存储定义为:

struct source_store{
    sector_t source;
    sector_t cache;
    struct rb_node node; // not a pointer to node
}
这是因为
rb\u条目被定义为

#define rb_entry(ptr, type, member) container_of(ptr, type, member)
这只是一些简单的偏移量计算

#define container_of(ptr, type, member) ({             /
         const typeof( ((type *)0)->member ) *__mptr = (ptr);  /   <--error happens here
         (type *)( (char *)__mptr - offsetof(type,member) );})
#定义(ptr、类型、成员)({/

const typeof(((type*)0)->member)*\uu mptr=(ptr);/如何将
struct source\u store
作为第二个参数传递-它是一个数据类型而不是变量。另外,
rb\u条目的原型是什么?根据本教程:,必须传入类型。原型是:rb\u条目(指针、类型、成员);其中member是包含结构中节点的名称。rb_条目只是()的容器_的包装器。这是因为
rb_entry
是一个宏,而不是一个函数。这就是为什么您需要在问题中提供这些信息-rb_entry的定义。@user93353 Linux内核中充满了此类宏。在我看来,假设当Linux内核g的存在是一个合理的假设。