Pages

2011-08-13

Store extra info in pointer

A pointer is at least align to 4 bytes, in all architectures.
That means, the lowest 2 bits are always 0 :)
For example in kernel standard rbtree (include/linux/rbtree.h),

struct rb_node
{
    unsigned long  rb_parent_color;
#define    RB_RED       0
#define    RB_BLACK    1
    struct rb_node *rb_right;
    struct rb_node *rb_left;
} __attribute__((aligned(sizeof(long))));



rb_parent_color is a pointer to it's parent, while bit 0 represents the color of the parent.


They've defined a macro to say hello world to it's parent,
#define rb_parent(r)   ((struct rb_node *)((r)->rb_parent_color & ~3))

No comments:

Post a Comment