]> www.infradead.org Git - users/hch/xfsprogs.git/commitdiff
libfrog: add xarray emulation
authorChristoph Hellwig <hch@lst.de>
Mon, 21 Oct 2024 00:10:40 +0000 (17:10 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Thu, 31 Oct 2024 22:45:02 +0000 (15:45 -0700)
Implement the simple parts of the kernel xarray API on-top of the libfrog
radix-tree.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
libfrog/radix-tree.h

index dad5f5b72039e3b4b71198c0a4934beba390d937..fe896134eeb283f3721d25540f91429a7e527f8f 100644 (file)
@@ -63,4 +63,39 @@ int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
 static inline int radix_tree_preload(int gfp_mask) { return 0; }
 static inline void radix_tree_preload_end(void) { }
 
+/*
+ * Emulation of the kernel xarray API.  Note that unlike the kernel
+ * xarray, there is no internal locking so code using this should not
+ * allow concurrent operations in userspace.
+ */
+struct xarray {
+       struct radix_tree_root  r;
+};
+
+static inline void xa_init(struct xarray *xa)
+{
+       INIT_RADIX_TREE(&xa->r, GFP_KERNEL);
+}
+
+static inline void *xa_load(struct xarray *xa, unsigned long index)
+{
+       return radix_tree_lookup(&xa->r, index);
+}
+
+static inline void *xa_erase(struct xarray *xa, unsigned long index)
+{
+       return radix_tree_delete(&xa->r, index);
+}
+
+static inline int xa_insert(struct xarray *xa, unsigned long index, void *entry,
+               unsigned int gfp)
+{
+       int error;
+
+       error = radix_tree_insert(&xa->r, index, entry);
+       if (error == -EEXIST)
+               return -EBUSY;
+       return error;
+}
+
 #endif /* __LIBFROG_RADIX_TREE_H__ */