1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

radix_tree: debug updates

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).
This commit is contained in:
Zdenek Kabelac 2024-06-02 00:23:14 +02:00
parent 88681f05f1
commit 1e2a3445d9
2 changed files with 55 additions and 8 deletions

View 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++)

View 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);
}
//----------------------------------------------------------------