diff mbox series

[1/6] btrfs: add helper for bit enumeration

Message ID 8ac93de3eef16c454a9cdd9770515ee7c5bd229a.1665492943.git.dsterba@suse.com (mailing list archive)
State New, archived
Headers show
Series Enumerated bits in enum | expand

Commit Message

David Sterba Oct. 11, 2022, 12:58 p.m. UTC
Define helper macro that can be used in enum {} to utilize the automatic
increment to define all bits without directly defining the values or
using additional linear bits.

1. capture the sequence value, N
2. use the value to define the given enum with N-th bit set
3. reset the sequence back to N

Use for enums that do not require fixed values for symbolic names (like
for on-disk structures):

enum {
	ENUM_BIT(FIRST),
	ENUM_BIT(SECOND),
	ENUM_BIT(THIRD)
};

Where the values would be 0x1, 0x2 and 0x4.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/misc.h | 8 ++++++++
 1 file changed, 8 insertions(+)
diff mbox series

Patch

diff --git a/fs/btrfs/misc.h b/fs/btrfs/misc.h
index f9850edfd726..69826ccd6644 100644
--- a/fs/btrfs/misc.h
+++ b/fs/btrfs/misc.h
@@ -10,6 +10,14 @@ 
 
 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
 
+/*
+ * Enumerate bits using enum autoincrement. Define the @name as the n-th bit.
+ */
+#define ENUM_BIT(name)                                  \
+	__ ## name ## _BIT,                             \
+	name = (1U << __ ## name ## _BIT),              \
+	__ ## name ## _SEQ = __ ## name ## _BIT
+
 static inline void cond_wake_up(struct wait_queue_head *wq)
 {
 	/*