From c1b24d742f5635d16cbbd4bade0affe21b22fb03 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 12 May 2022 12:32:18 +0200 Subject: [PATCH] btrfs-progs: kernel-lib: add rbtree_types.h from linux In order to use rb_root_cached we need to sync with kernel sources. Copy the file from linux.git/include/linux/rbtree_types.h and update so it's C++ protected for inclusion to libbtrfs and remove duplicate definitions. Signed-off-by: David Sterba --- Makefile | 2 +- kernel-lib/rbtree.h | 16 +++------------ kernel-lib/rbtree_types.h | 42 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 kernel-lib/rbtree_types.h diff --git a/Makefile b/Makefile index af4908f9..9b7c8d32 100644 --- a/Makefile +++ b/Makefile @@ -213,7 +213,7 @@ libbtrfs_objects = \ crypto/crc32c.o libbtrfs_headers = libbtrfs/send-stream.h libbtrfs/send-utils.h kernel-shared/send.h kernel-lib/rbtree.h \ - kernel-lib/list.h kerncompat.h \ + kernel-lib/list.h kernel-lib/rbtree_types.h kerncompat.h \ common/extent-cache.h kernel-shared/extent_io.h ioctl.h \ kernel-shared/ctree.h version.h libbtrfsutil_major := $(shell sed -rn 's/^\#define BTRFS_UTIL_VERSION_MAJOR ([0-9])+$$/\1/p' libbtrfsutil/btrfsutil.h) diff --git a/kernel-lib/rbtree.h b/kernel-lib/rbtree.h index 6d689075..5f8e1330 100644 --- a/kernel-lib/rbtree.h +++ b/kernel-lib/rbtree.h @@ -28,31 +28,21 @@ #ifndef _LINUX_RBTREE_H #define _LINUX_RBTREE_H + #if BTRFS_FLAT_INCLUDES #include "kerncompat.h" +#include "kernel-lib/rbtree_types.h" #else #include +#include #endif /* BTRFS_FLAT_INCLUDES */ #ifdef __cplusplus extern "C" { #endif -struct rb_node { - unsigned long __rb_parent_color; - struct rb_node *rb_right; - struct rb_node *rb_left; -} __attribute__((aligned(sizeof(long)))); - /* The alignment might seem pointless, but allegedly CRIS needs it */ - -struct rb_root { - struct rb_node *rb_node; -}; - - #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3)) -#define RB_ROOT (struct rb_root) { NULL, } #define rb_entry(ptr, type, member) container_of(ptr, type, member) #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL) diff --git a/kernel-lib/rbtree_types.h b/kernel-lib/rbtree_types.h new file mode 100644 index 00000000..550d193d --- /dev/null +++ b/kernel-lib/rbtree_types.h @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef _LINUX_RBTREE_TYPES_H +#define _LINUX_RBTREE_TYPES_H + +#ifdef __cplusplus +extern "C" { +#endif + +struct rb_node { + unsigned long __rb_parent_color; + struct rb_node *rb_right; + struct rb_node *rb_left; +} __attribute__((aligned(sizeof(long)))); +/* The alignment might seem pointless, but allegedly CRIS needs it */ + +struct rb_root { + struct rb_node *rb_node; +}; + +/* + * Leftmost-cached rbtrees. + * + * We do not cache the rightmost node based on footprint + * size vs number of potential users that could benefit + * from O(1) rb_last(). Just not worth it, users that want + * this feature can always implement the logic explicitly. + * Furthermore, users that want to cache both pointers may + * find it a bit asymmetric, but that's ok. + */ +struct rb_root_cached { + struct rb_root rb_root; + struct rb_node *rb_leftmost; +}; + +#define RB_ROOT (struct rb_root) { NULL, } +#define RB_ROOT_CACHED (struct rb_root_cached) { {NULL, }, NULL } + +#ifdef __cplusplus +} +#endif + +#endif