lib/btree: simplify btree_{lookup|update}
authorwuchi <wuchi.zero@gmail.com>
Tue, 7 Jun 2022 13:35:56 +0000 (21:35 +0800)
committerakpm <akpm@linux-foundation.org>
Fri, 17 Jun 2022 02:58:21 +0000 (19:58 -0700)
btree_{lookup|update} both need to look up node by key, using the common
parts(add function btree_lookup_node) to simplify code.

Link: https://lkml.kernel.org/r/20220607133556.34732-1-wuchi.zero@gmail.com
Signed-off-by: wuchi <wuchi.zero@gmail.com>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
lib/btree.c

index b4cf08a5c267891a30f88c85fcb43c9e0a47b0e9..a82100c73b5597fe934b0b20297452ccabd06fe1 100644 (file)
@@ -238,7 +238,7 @@ static int keyzero(struct btree_geo *geo, unsigned long *key)
        return 1;
 }
 
-void *btree_lookup(struct btree_head *head, struct btree_geo *geo,
+static void *btree_lookup_node(struct btree_head *head, struct btree_geo *geo,
                unsigned long *key)
 {
        int i, height = head->height;
@@ -257,7 +257,16 @@ void *btree_lookup(struct btree_head *head, struct btree_geo *geo,
                if (!node)
                        return NULL;
        }
+       return node;
+}
 
+void *btree_lookup(struct btree_head *head, struct btree_geo *geo,
+               unsigned long *key)
+{
+       int i;
+       unsigned long *node;
+
+       node = btree_lookup_node(head, geo, key);
        if (!node)
                return NULL;
 
@@ -271,23 +280,10 @@ EXPORT_SYMBOL_GPL(btree_lookup);
 int btree_update(struct btree_head *head, struct btree_geo *geo,
                 unsigned long *key, void *val)
 {
-       int i, height = head->height;
-       unsigned long *node = head->node;
-
-       if (height == 0)
-               return -ENOENT;
-
-       for ( ; height > 1; height--) {
-               for (i = 0; i < geo->no_pairs; i++)
-                       if (keycmp(geo, node, i, key) <= 0)
-                               break;
-               if (i == geo->no_pairs)
-                       return -ENOENT;
-               node = bval(geo, node, i);
-               if (!node)
-                       return -ENOENT;
-       }
+       int i;
+       unsigned long *node;
 
+       node = btree_lookup_node(head, geo, key);
        if (!node)
                return -ENOENT;