【发布时间】:2013-06-21 17:50:12
【问题描述】:
linux内核中rb_node的定义如下:
struct rb_node {
unsigned long __rb_parent_color;
struct rb_node *rb_right;
struct rb_node *rb_left;
} __attribute__((aligned(sizeof(long))));
#define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
#define rb_color(r) ((r)->rb_parent_color & 1)
#define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
#define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
我的问题是关于__rb_parent_color,其中最后一位是颜色,其余的是指向其父级的指针。
我了解到有人说 __rb_parent_color 的最后 2 位因为 aligned(sizeof(long)) 而没有用,但为什么呢?
不是sizeof(struct rb_node *) 4 还是不是sizeof(unsigned long) 4?即使它们不相等,aligned 是否应该在 Byte 中,如果不对齐,至少会有一个完整的 Byte 是无用的?
【问题讨论】:
标签: c struct linux-kernel binary-tree red-black-tree