From ed49cb094b7320b7782227afbe3d3fb7d61ecc3b Mon Sep 17 00:00:00 2001 From: Andrew Morton Date: Sat, 12 Apr 2003 12:54:20 -0700 Subject: [PATCH] [PATCH] radix_tree_delete API improvement radix_tree_delete() currently returns 0 on success, -ENOENT if there was nothing to delete. But it is more useful to return the address of the deleted item on success and NULL if there was no matching item. It can potentially save a lookup+delete operation. --- include/linux/radix-tree.h | 2 +- lib/radix-tree.c | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/linux/radix-tree.h b/include/linux/radix-tree.h index f4a78d52b5ceb..c32a45fd1f0d0 100644 --- a/include/linux/radix-tree.h +++ b/include/linux/radix-tree.h @@ -43,7 +43,7 @@ do { \ extern int radix_tree_insert(struct radix_tree_root *, unsigned long, void *); extern void *radix_tree_lookup(struct radix_tree_root *, unsigned long); -extern int radix_tree_delete(struct radix_tree_root *, unsigned long); +extern void *radix_tree_delete(struct radix_tree_root *, unsigned long); extern unsigned int radix_tree_gang_lookup(struct radix_tree_root *root, void **results, unsigned long first_index, unsigned int max_items); diff --git a/lib/radix-tree.c b/lib/radix-tree.c index 3d0183a4127de..cdfb8c6a97f1d 100644 --- a/lib/radix-tree.c +++ b/lib/radix-tree.c @@ -349,15 +349,18 @@ EXPORT_SYMBOL(radix_tree_gang_lookup); * @index: index key * * Remove the item at @index from the radix tree rooted at @root. + * + * Returns the address of the deleted item, or NULL if it was not present. */ -int radix_tree_delete(struct radix_tree_root *root, unsigned long index) +void *radix_tree_delete(struct radix_tree_root *root, unsigned long index) { struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path; unsigned int height, shift; + void *ret = NULL; height = root->height; if (index > radix_tree_maxindex(height)) - return -ENOENT; + goto out; shift = (height-1) * RADIX_TREE_MAP_SHIFT; pathp->node = NULL; @@ -365,7 +368,7 @@ int radix_tree_delete(struct radix_tree_root *root, unsigned long index) while (height > 0) { if (*pathp->slot == NULL) - return -ENOENT; + goto out; pathp[1].node = *pathp[0].slot; pathp[1].slot = (struct radix_tree_node **) @@ -375,8 +378,9 @@ int radix_tree_delete(struct radix_tree_root *root, unsigned long index) height--; } - if (*pathp[0].slot == NULL) - return -ENOENT; + ret = *pathp[0].slot; + if (ret == NULL) + goto out; *pathp[0].slot = NULL; while (pathp[0].node && --pathp[0].node->count == 0) { @@ -387,8 +391,8 @@ int radix_tree_delete(struct radix_tree_root *root, unsigned long index) if (root->rnode == NULL) root->height = 0; /* Empty tree, we can reset the height */ - - return 0; +out: + return ret; } EXPORT_SYMBOL(radix_tree_delete); -- 2.53.0