[PATCH] bitfields: add bitfields API

From: Vegard Nossum
Date: Wed Aug 27 2008 - 12:37:16 EST


It turns out that there is an inherent conflict between the way GCC
encodes bitfield operations and the way kmemcheck detects uses of
uninitialized memory; since bitfields span one or more bytes,
accessing only a single bit may generate a load of the whole byte.
This happens regardless of whether we actually want to preserve the
contents of the rest of the bitfield or not.

The solution to the problem is to initialize all the fields at once,
which means that GCC can now optimize out the initial load. This can
actually even save a few bytes on certain architectures, although
this is not the main purpose of the patch. On other architectures,
the generated assembly code will be slightly longer; therefore, the
patch should do nothing in those cases.

This new header defines one macro for defining bitfields,
DEFINE_BITFIELD, and one macro for annotating the places where the
bitfield is being initialized, bitfield_begin_init. See the next
patch in the series for an example of how to use these macros.

Signed-off-by: Vegard Nossum <vegard.nossum@xxxxxxxxx>
---
include/linux/bitfield.h | 26 ++++++++++++++++++++++++++
1 files changed, 26 insertions(+), 0 deletions(-)
create mode 100644 include/linux/bitfield.h

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
new file mode 100644
index 0000000..2d02ba3
--- /dev/null
+++ b/include/linux/bitfield.h
@@ -0,0 +1,26 @@
+#ifndef LINUX_BITFIELD_H
+#define LINUX_BITFIELD_H
+
+#define DEFINE_BITFIELD(type, name, fields...) \
+ union { \
+ struct { \
+ type _fields; \
+ } name; \
+ struct { \
+ type fields; \
+ }; \
+ };
+
+/*
+ * NOTE: This macro should NOT be used to initialize the bitfield! Its
+ * purpose is to inform the compiler that the bitfield is currently
+ * uninitialized and that the other bits do not have to be preserved on
+ * subsequent changes to the bitfield.
+ */
+#ifdef CONFIG_ARCH_WANT_BITFIELD_INITIALIZERS
+#define bitfield_begin_init(name) ((name)._fields = 0)
+#else
+#define bitfield_begin_init(name)
+#endif
+
+#endif
--
1.5.5.1


--ibTvN161/egqYuK8
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="0002-net-use-bitfields-API-in-skbuff.patch"