diff options
author | Michel Lespinasse <walken@google.com> | 2012-10-08 19:30:37 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-09 03:22:32 -0400 |
commit | bf7ad8eeab995710c766df49c9c69a8592ca0216 (patch) | |
tree | 737988d677b8ea408a44a58a949cc0e8eda02440 | |
parent | ea5272f5c94fb2ee62f4f15a5b88eef6184cd506 (diff) |
rbtree: move some implementation details from rbtree.h to rbtree.c
rbtree users must use the documented APIs to manipulate the tree
structure. Low-level helpers to manipulate node colors and parenthood are
not part of that API, so move them to lib/rbtree.c
[dwmw2@infradead.org: fix jffs2 build issue due to renamed __rb_parent_color field]
Signed-off-by: Michel Lespinasse <walken@google.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Acked-by: David Woodhouse <David.Woodhouse@intel.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Daniel Santos <daniel.santos@pobox.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | fs/jffs2/readinode.c | 13 | ||||
-rw-r--r-- | include/linux/rbtree.h | 34 | ||||
-rw-r--r-- | lib/rbtree.c | 20 |
3 files changed, 36 insertions, 31 deletions
diff --git a/fs/jffs2/readinode.c b/fs/jffs2/readinode.c index 1ea349fff68..ae81b01e6fd 100644 --- a/fs/jffs2/readinode.c +++ b/fs/jffs2/readinode.c | |||
@@ -394,8 +394,11 @@ static int jffs2_add_tn_to_tree(struct jffs2_sb_info *c, | |||
394 | } | 394 | } |
395 | 395 | ||
396 | /* Trivial function to remove the last node in the tree. Which by definition | 396 | /* Trivial function to remove the last node in the tree. Which by definition |
397 | has no right-hand -- so can be removed just by making its only child (if | 397 | has no right-hand child — so can be removed just by making its left-hand |
398 | any) take its place under its parent. */ | 398 | child (if any) take its place under its parent. Since this is only done |
399 | when we're consuming the whole tree, there's no need to use rb_erase() | ||
400 | and let it worry about adjusting colours and balancing the tree. That | ||
401 | would just be a waste of time. */ | ||
399 | static void eat_last(struct rb_root *root, struct rb_node *node) | 402 | static void eat_last(struct rb_root *root, struct rb_node *node) |
400 | { | 403 | { |
401 | struct rb_node *parent = rb_parent(node); | 404 | struct rb_node *parent = rb_parent(node); |
@@ -412,12 +415,12 @@ static void eat_last(struct rb_root *root, struct rb_node *node) | |||
412 | link = &parent->rb_right; | 415 | link = &parent->rb_right; |
413 | 416 | ||
414 | *link = node->rb_left; | 417 | *link = node->rb_left; |
415 | /* Colour doesn't matter now. Only the parent pointer. */ | ||
416 | if (node->rb_left) | 418 | if (node->rb_left) |
417 | node->rb_left->rb_parent_color = node->rb_parent_color; | 419 | node->rb_left->__rb_parent_color = node->__rb_parent_color; |
418 | } | 420 | } |
419 | 421 | ||
420 | /* We put this in reverse order, so we can just use eat_last */ | 422 | /* We put the version tree in reverse order, so we can use the same eat_last() |
423 | function that we use to consume the tmpnode tree (tn_root). */ | ||
421 | static void ver_insert(struct rb_root *ver_root, struct jffs2_tmp_dnode_info *tn) | 424 | static void ver_insert(struct rb_root *ver_root, struct jffs2_tmp_dnode_info *tn) |
422 | { | 425 | { |
423 | struct rb_node **link = &ver_root->rb_node; | 426 | struct rb_node **link = &ver_root->rb_node; |
diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h index 2049087c43b..bf836a2c6a3 100644 --- a/include/linux/rbtree.h +++ b/include/linux/rbtree.h | |||
@@ -32,37 +32,19 @@ | |||
32 | #include <linux/kernel.h> | 32 | #include <linux/kernel.h> |
33 | #include <linux/stddef.h> | 33 | #include <linux/stddef.h> |
34 | 34 | ||
35 | struct rb_node | 35 | struct rb_node { |
36 | { | 36 | unsigned long __rb_parent_color; |
37 | unsigned long rb_parent_color; | ||
38 | #define RB_RED 0 | ||
39 | #define RB_BLACK 1 | ||
40 | struct rb_node *rb_right; | 37 | struct rb_node *rb_right; |
41 | struct rb_node *rb_left; | 38 | struct rb_node *rb_left; |
42 | } __attribute__((aligned(sizeof(long)))); | 39 | } __attribute__((aligned(sizeof(long)))); |
43 | /* The alignment might seem pointless, but allegedly CRIS needs it */ | 40 | /* The alignment might seem pointless, but allegedly CRIS needs it */ |
44 | 41 | ||
45 | struct rb_root | 42 | struct rb_root { |
46 | { | ||
47 | struct rb_node *rb_node; | 43 | struct rb_node *rb_node; |
48 | }; | 44 | }; |
49 | 45 | ||
50 | 46 | ||
51 | #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3)) | 47 | #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3)) |
52 | #define rb_color(r) ((r)->rb_parent_color & 1) | ||
53 | #define rb_is_red(r) (!rb_color(r)) | ||
54 | #define rb_is_black(r) rb_color(r) | ||
55 | #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0) | ||
56 | #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0) | ||
57 | |||
58 | static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p) | ||
59 | { | ||
60 | rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p; | ||
61 | } | ||
62 | static inline void rb_set_color(struct rb_node *rb, int color) | ||
63 | { | ||
64 | rb->rb_parent_color = (rb->rb_parent_color & ~1) | color; | ||
65 | } | ||
66 | 48 | ||
67 | #define RB_ROOT (struct rb_root) { NULL, } | 49 | #define RB_ROOT (struct rb_root) { NULL, } |
68 | #define rb_entry(ptr, type, member) container_of(ptr, type, member) | 50 | #define rb_entry(ptr, type, member) container_of(ptr, type, member) |
@@ -70,8 +52,10 @@ static inline void rb_set_color(struct rb_node *rb, int color) | |||
70 | #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL) | 52 | #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL) |
71 | 53 | ||
72 | /* 'empty' nodes are nodes that are known not to be inserted in an rbree */ | 54 | /* 'empty' nodes are nodes that are known not to be inserted in an rbree */ |
73 | #define RB_EMPTY_NODE(node) ((node)->rb_parent_color == (unsigned long)(node)) | 55 | #define RB_EMPTY_NODE(node) \ |
74 | #define RB_CLEAR_NODE(node) ((node)->rb_parent_color = (unsigned long)(node)) | 56 | ((node)->__rb_parent_color == (unsigned long)(node)) |
57 | #define RB_CLEAR_NODE(node) \ | ||
58 | ((node)->__rb_parent_color = (unsigned long)(node)) | ||
75 | 59 | ||
76 | 60 | ||
77 | extern void rb_insert_color(struct rb_node *, struct rb_root *); | 61 | extern void rb_insert_color(struct rb_node *, struct rb_root *); |
@@ -98,7 +82,7 @@ extern void rb_replace_node(struct rb_node *victim, struct rb_node *new, | |||
98 | static inline void rb_link_node(struct rb_node * node, struct rb_node * parent, | 82 | static inline void rb_link_node(struct rb_node * node, struct rb_node * parent, |
99 | struct rb_node ** rb_link) | 83 | struct rb_node ** rb_link) |
100 | { | 84 | { |
101 | node->rb_parent_color = (unsigned long )parent; | 85 | node->__rb_parent_color = (unsigned long)parent; |
102 | node->rb_left = node->rb_right = NULL; | 86 | node->rb_left = node->rb_right = NULL; |
103 | 87 | ||
104 | *rb_link = node; | 88 | *rb_link = node; |
diff --git a/lib/rbtree.c b/lib/rbtree.c index fe43c8c5f52..ccada9abe6f 100644 --- a/lib/rbtree.c +++ b/lib/rbtree.c | |||
@@ -23,6 +23,24 @@ | |||
23 | #include <linux/rbtree.h> | 23 | #include <linux/rbtree.h> |
24 | #include <linux/export.h> | 24 | #include <linux/export.h> |
25 | 25 | ||
26 | #define RB_RED 0 | ||
27 | #define RB_BLACK 1 | ||
28 | |||
29 | #define rb_color(r) ((r)->__rb_parent_color & 1) | ||
30 | #define rb_is_red(r) (!rb_color(r)) | ||
31 | #define rb_is_black(r) rb_color(r) | ||
32 | #define rb_set_red(r) do { (r)->__rb_parent_color &= ~1; } while (0) | ||
33 | #define rb_set_black(r) do { (r)->__rb_parent_color |= 1; } while (0) | ||
34 | |||
35 | static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p) | ||
36 | { | ||
37 | rb->__rb_parent_color = rb_color(rb) | (unsigned long)p; | ||
38 | } | ||
39 | static inline void rb_set_color(struct rb_node *rb, int color) | ||
40 | { | ||
41 | rb->__rb_parent_color = (rb->__rb_parent_color & ~1) | color; | ||
42 | } | ||
43 | |||
26 | static void __rb_rotate_left(struct rb_node *node, struct rb_root *root) | 44 | static void __rb_rotate_left(struct rb_node *node, struct rb_root *root) |
27 | { | 45 | { |
28 | struct rb_node *right = node->rb_right; | 46 | struct rb_node *right = node->rb_right; |
@@ -255,7 +273,7 @@ void rb_erase(struct rb_node *node, struct rb_root *root) | |||
255 | rb_set_parent(old->rb_right, node); | 273 | rb_set_parent(old->rb_right, node); |
256 | } | 274 | } |
257 | 275 | ||
258 | node->rb_parent_color = old->rb_parent_color; | 276 | node->__rb_parent_color = old->__rb_parent_color; |
259 | node->rb_left = old->rb_left; | 277 | node->rb_left = old->rb_left; |
260 | rb_set_parent(old->rb_left, node); | 278 | rb_set_parent(old->rb_left, node); |
261 | 279 | ||