Skip to content

Commit

Permalink
btrfs-progs: kernel-lib: add rbtree_types.h from linux
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kdave committed May 12, 2022
1 parent df77a23 commit c1b24d7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 3 additions & 13 deletions kernel-lib/rbtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <btrfs/kerncompat.h>
#include <btrfs/rbtree_types.h>
#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)
Expand Down
42 changes: 42 additions & 0 deletions kernel-lib/rbtree_types.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c1b24d7

Please sign in to comment.