]> sourceware.org Git - lvm2.git/commitdiff
radix_tree: debug updates
authorZdenek Kabelac <zkabelac@redhat.com>
Sat, 1 Jun 2024 22:23:14 +0000 (00:23 +0200)
committerZdenek Kabelac <zkabelac@redhat.com>
Mon, 3 Jun 2024 13:30:05 +0000 (15:30 +0200)
Some updates to _dump()  debugging function so the printed result
can be more easily examined by human.

Also print 'prefix' as string when all chars are printable.

Add 'simple' variant of _dump also to 'simple' version of radix tree
(which is however normally not compiled).

base/data-struct/radix-tree-adaptive.c
base/data-struct/radix-tree-simple.c

index f03c00f27af55858fedd3f9b49f706fa927092a4..4c87495c24f14b8944602c448942025cbbe2ce0c 100644 (file)
@@ -19,6 +19,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <ctype.h>
 
 //----------------------------------------------------------------
 
@@ -500,7 +501,7 @@ static struct lookup_result _lookup_prefix(struct value *v, const uint8_t *kb, c
        struct node48 *n48;
        struct node256 *n256;
 
-       if (kb == ke)
+       if (kb == ke || !kb) /* extra check for !kb for coverity */
                return (struct lookup_result) {.v = v, .kb = kb};
 
        switch (v->type) {
@@ -1218,6 +1219,7 @@ static void _dump(FILE *out, struct value v, unsigned indent)
        struct node16 *n16;
        struct node48 *n48;
        struct node256 *n256;
+       unsigned printable;
 
        if (v.type == UNSET)
                return;
@@ -1242,9 +1244,22 @@ static void _dump(FILE *out, struct value v, unsigned indent)
 
        case PREFIX_CHAIN:
                pc = v.value.ptr;
-               fprintf(out, "<prefix: ");
+               fprintf(out, "<prefix(%u): ", pc->len);
+               printable = 1;
                for (i = 0; i < pc->len; i++)
-                       fprintf(out, "%x.", (unsigned) *(pc->prefix + i));
+                       if (!isprint(pc->prefix[i])) {
+                               printable = 0;
+                               break;
+                       }
+               if (printable)
+                       fputc('"', out);
+               for (i = 0; i < pc->len; i++)
+                       if (printable)
+                               fprintf(out, "%c", pc->prefix[i]);
+                       else
+                               fprintf(out, "%02x.", (unsigned) *(pc->prefix + i));
+               if (printable)
+                       fputc('"', out);
                fprintf(out, ">\n");
                _dump(out, pc->child, indent + 1);
                break;
@@ -1253,7 +1268,7 @@ static void _dump(FILE *out, struct value v, unsigned indent)
                n4 = v.value.ptr;
                fprintf(out, "<n4: ");
                for (i = 0; i < n4->nr_entries; i++)
-                       fprintf(out, "%x ", (unsigned) n4->keys[i]);
+                       fprintf(out, "%02x ", (unsigned) n4->keys[i]);
                fprintf(out, ">\n");
 
                for (i = 0; i < n4->nr_entries; i++)
@@ -1264,7 +1279,7 @@ static void _dump(FILE *out, struct value v, unsigned indent)
                n16 = v.value.ptr;
                fprintf(out, "<n16: ");
                for (i = 0; i < n16->nr_entries; i++)
-                       fprintf(out, "%x ", (unsigned) n16->keys[i]);
+                       fprintf(out, "%02x ", (unsigned) n16->keys[i]);
                fprintf(out, ">\n");
 
                for (i = 0; i < n16->nr_entries; i++)
@@ -1276,7 +1291,7 @@ static void _dump(FILE *out, struct value v, unsigned indent)
                fprintf(out, "<n48: ");
                for (i = 0; i < 256; i++)
                        if (n48->keys[i] < 48)
-                               fprintf(out, "%x ", i);
+                               fprintf(out, "%02x ", i);
                fprintf(out, ">\n");
 
                for (i = 0; i < n48->nr_entries; i++) {
@@ -1290,7 +1305,7 @@ static void _dump(FILE *out, struct value v, unsigned indent)
                fprintf(out, "<n256: ");
                for (i = 0; i < 256; i++)
                        if (n256->values[i].type != UNSET)
-                               fprintf(out, "%x ", i);
+                               fprintf(out, "%02x ", i);
                fprintf(out, ">\n");
 
                for (i = 0; i < 256; i++)
index d2dc0ee18e5be8d218eb7c068d195c8192eaa4da..d89c16bde491eb53ff2a9882c074b78db95f39ea 100644 (file)
@@ -18,6 +18,7 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <ctype.h>
 
 //----------------------------------------------------------------
 // This implementation is based around nested binary trees.  Very
@@ -37,6 +38,7 @@ struct node {
 struct radix_tree {
        radix_value_dtr dtr;
        void *dtr_context;
+       unsigned nr_entries;
 
        struct node *root;
 };
@@ -156,7 +158,11 @@ bool radix_tree_insert(struct radix_tree *rt, const void *key, size_t keylen,
        const uint8_t *kb = key;
        const uint8_t *ke = kb + keylen;
 
-       return _insert(&rt->root, kb, ke, v);
+       if (!_insert(&rt->root, kb, ke, v))
+               return false;
+
+       rt->nr_entries++;
+       return true;
 }
 
 bool radix_tree_remove(struct radix_tree *rt, const void *key, size_t keylen)
@@ -169,6 +175,8 @@ bool radix_tree_remove(struct radix_tree *rt, const void *key, size_t keylen)
        if (!n || !n->has_value)
                return false;
 
+       rt->nr_entries--;
+
        if (rt->dtr)
            rt->dtr(rt->dtr_context, n->value);
 
@@ -259,8 +267,32 @@ bool radix_tree_is_well_formed(struct radix_tree *rt)
        return true;
 }
 
+static void _dump(FILE *out, struct node *n, unsigned indent)
+{
+       unsigned i;
+
+       if (!n)
+               return;
+
+       _dump(out, n->left, indent + 1);
+
+       for (i = 0; i < 2 * indent; i++)
+               fprintf(out, " ");
+
+       if (n->has_value) {
+               fprintf(out, "value: %llu\n", n->value.n);
+       } else {
+               fprintf(out, "key: '%c' [0x%02x] %u\n",
+                       isprint(n->key) ? n->key : ' ', n->key, indent);
+       }
+
+       _dump(out, n->center, indent + 1);
+       _dump(out, n->right, indent + 1);
+}
+
 void radix_tree_dump(struct radix_tree *rt, FILE *out)
 {
+       _dump(out, rt->root, 0);
 }
 
 //----------------------------------------------------------------
This page took 0.045528 seconds and 5 git commands to generate.