Files
scoutfs/utils/src/avl.c
Zach Brown d87e2e0166 scoutfs-utils: add btree insertion for mkfs
Use little helpers to insert items into new single block btrees for
mkfs.  We're about to insert a whole bunch more items.

Signed-off-by: Zach Brown <zab@versity.com>
2020-10-26 15:19:41 -07:00

49 lines
1015 B
C

#include "sparse.h"
#include "util.h"
#include "format.h"
#include "avl.h"
static struct scoutfs_avl_node *node_ptr(struct scoutfs_avl_root *root,
__le16 off)
{
return off ? (void *)root + le16_to_cpu(off) : NULL;
}
__le16 avl_node_off(struct scoutfs_avl_root *root,
struct scoutfs_avl_node *node)
{
if (!node)
return 0;
return cpu_to_le16((void *)node - (void *)root);
}
struct scoutfs_avl_node *avl_first(struct scoutfs_avl_root *root)
{
struct scoutfs_avl_node *node = node_ptr(root, root->node);
while (node && node->left)
node = node_ptr(root, node->left);
return node;
}
struct scoutfs_avl_node *avl_next(struct scoutfs_avl_root *root,
struct scoutfs_avl_node *node)
{
struct scoutfs_avl_node *parent;
if (node->right) {
node = node_ptr(root, node->right);
while (node->left)
node = node_ptr(root, node->left);
return node;
}
while ((parent = node_ptr(root, node->parent)) &&
node == node_ptr(root, parent->right))
node = parent;
return parent;
}