From: Seth Jennings <sjenning@linux.vnet.ibm.com>
To: Cody P Schafer <cody@linux.vnet.ibm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
LKML <linux-kernel@vger.kernel.org>,
Linux MM <linux-mm@kvack.org>,
David Woodhouse <David.Woodhouse@intel.com>,
Rik van Riel <riel@redhat.com>,
Michel Lespinasse <walken@google.com>
Subject: Re: [PATCH 1/5] rbtree: add postorder iteration functions
Date: Mon, 29 Jul 2013 10:01:47 -0500 [thread overview]
Message-ID: <20130729150147.GA4381@variantweb.net> (raw)
In-Reply-To: <1374873223-25557-2-git-send-email-cody@linux.vnet.ibm.com>
On Fri, Jul 26, 2013 at 02:13:39PM -0700, Cody P Schafer wrote:
> Add postorder iteration functions for rbtree. These are useful for
> safely freeing an entire rbtree without modifying the tree at all.
>
> Signed-off-by: Cody P Schafer <cody@linux.vnet.ibm.com>
> ---
> include/linux/rbtree.h | 4 ++++
> lib/rbtree.c | 40 ++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 44 insertions(+)
>
> diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
> index 0022c1b..2879e96 100644
> --- a/include/linux/rbtree.h
> +++ b/include/linux/rbtree.h
> @@ -68,6 +68,10 @@ extern struct rb_node *rb_prev(const struct rb_node *);
> extern struct rb_node *rb_first(const struct rb_root *);
> extern struct rb_node *rb_last(const struct rb_root *);
>
> +/* Postorder iteration - always visit the parent after it's children */
s/it's/its/
> +extern struct rb_node *rb_first_postorder(const struct rb_root *);
> +extern struct rb_node *rb_next_postorder(const struct rb_node *);
> +
> /* Fast replacement of a single node without remove/rebalance/add/rebalance */
> extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
> struct rb_root *root);
> diff --git a/lib/rbtree.c b/lib/rbtree.c
> index c0e31fe..65f4eff 100644
> --- a/lib/rbtree.c
> +++ b/lib/rbtree.c
> @@ -518,3 +518,43 @@ void rb_replace_node(struct rb_node *victim, struct rb_node *new,
> *new = *victim;
> }
> EXPORT_SYMBOL(rb_replace_node);
> +
> +static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
> +{
> + for (;;) {
> + if (node->rb_left)
> + node = node->rb_left;
Assigning to an argument passed as const seems weird to me. I would
think it shouldn't compile but it does. I guess my understanding of
const is incomplete.
> + else if (node->rb_right)
> + node = node->rb_right;
> + else
> + return (struct rb_node *)node;
> + }
> +}
> +
> +struct rb_node *rb_next_postorder(const struct rb_node *node)
> +{
> + const struct rb_node *parent;
> + if (!node)
> + return NULL;
> + parent = rb_parent(node);
Again here.
Seth
> +
> + /* If we're sitting on node, we've already seen our children */
> + if (parent && node == parent->rb_left && parent->rb_right) {
> + /* If we are the parent's left node, go to the parent's right
> + * node then all the way down to the left */
> + return rb_left_deepest_node(parent->rb_right);
> + } else
> + /* Otherwise we are the parent's right node, and the parent
> + * should be next */
> + return (struct rb_node *)parent;
> +}
> +EXPORT_SYMBOL(rb_next_postorder);
> +
> +struct rb_node *rb_first_postorder(const struct rb_root *root)
> +{
> + if (!root->rb_node)
> + return NULL;
> +
> + return rb_left_deepest_node(root->rb_node);
> +}
> +EXPORT_SYMBOL(rb_first_postorder);
> --
> 1.8.3.4
>
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2013-07-30 15:23 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-26 21:13 [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Cody P Schafer
2013-07-26 21:13 ` [PATCH 1/5] rbtree: add postorder iteration functions Cody P Schafer
2013-07-29 15:01 ` Seth Jennings [this message]
2013-07-29 17:32 ` Cody P Schafer
2013-07-26 21:13 ` [PATCH 2/5] rbtree: add rbtree_postorder_for_each_entry_safe() helper Cody P Schafer
2013-07-29 15:06 ` Seth Jennings
2013-07-29 17:41 ` Cody P Schafer
2013-07-26 21:13 ` [PATCH 3/5] rbtree_test: add test for postorder iteration Cody P Schafer
2013-07-26 21:13 ` [PATCH 4/5] rbtree: allow tests to run as builtin Cody P Schafer
2013-07-26 21:13 ` [PATCH 5/5] mm/zswap: use postorder iteration when destroying rbtree Cody P Schafer
2013-07-29 15:08 ` Seth Jennings
2013-07-29 15:11 ` [PATCH 0/5] Add rbtree postorder iteration functions, runtime tests, and update zswap to use Seth Jennings
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130729150147.GA4381@variantweb.net \
--to=sjenning@linux.vnet.ibm.com \
--cc=David.Woodhouse@intel.com \
--cc=akpm@linux-foundation.org \
--cc=cody@linux.vnet.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=riel@redhat.com \
--cc=walken@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).