From 1813d0d186d9308c2f0eff11ee65c2492fcf2874 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Sat, 1 Jun 2024 19:30:33 +0200 Subject: [PATCH] radix_tree: add radix_tree_lookup/insert_ptr Add simple 'wrapper' inline functions to insert or return ptr lookup value. (So the user doesn't need to deal with 'union radix_value' locally and also it makes easier to translage some lvm2 functions to use radix_tree). Note: If the stored 'value' would NULL, it cannot be recognized from a case of 'not found'. So usable only when 'values' stored with tree are not NULL. --- base/data-struct/radix-tree.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/base/data-struct/radix-tree.h b/base/data-struct/radix-tree.h index bfc2a0d0c..7f74769f7 100644 --- a/base/data-struct/radix-tree.h +++ b/base/data-struct/radix-tree.h @@ -59,6 +59,19 @@ void radix_tree_iterate(struct radix_tree *rt, const void *key, size_t keylen, bool radix_tree_is_well_formed(struct radix_tree *rt); void radix_tree_dump(struct radix_tree *rt, FILE *out); +// Shortcut for ptr value return +// Note: if value would be NULL, it's same result for not/found case. +static inline void *radix_tree_lookup_ptr(struct radix_tree *rt, const void *key, size_t keylen) +{ + union radix_value v; + return radix_tree_lookup(rt, key, keylen, &v) ? v.ptr : NULL; +} + +static inline bool radix_tree_insert_ptr(struct radix_tree *rt, const void *key, size_t keylen, void *ptr) +{ + union radix_value v = { .ptr = ptr }; + return radix_tree_insert(rt, key, keylen, v); +} //---------------------------------------------------------------- #endif