From 9284f39b8b851dfbe8d8dd514def166fd454982e Mon Sep 17 00:00:00 2001 From: Tudor Bosman Date: Thu, 13 Dec 2012 10:31:51 -0800 Subject: [PATCH] Silence -Wuninitialized in some cases Test Plan: compiles Reviewed By: philipp@fb.com FB internal diff: D657623 --- folly/Bits.h | 2 +- folly/experimental/Bits.h | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/folly/Bits.h b/folly/Bits.h index 38d35723..a88e6349 100644 --- a/folly/Bits.h +++ b/folly/Bits.h @@ -498,7 +498,7 @@ template struct Unaligned< T, typename std::enable_if::value>::type> { - Unaligned() { } // uninitialized + Unaligned() = default; // uninitialized /* implicit */ Unaligned(T v) : value(v) { } T value; } __attribute__((packed)); diff --git a/folly/experimental/Bits.h b/folly/experimental/Bits.h index e3ec6eb6..214bd66a 100644 --- a/folly/experimental/Bits.h +++ b/folly/experimental/Bits.h @@ -40,12 +40,21 @@ namespace detail { template struct BitsTraits; // Partial specialization for Unaligned, where T is unsigned integral +// loadRMW is the same as load, but it indicates that it loads for a +// read-modify-write operation (we write back the bits we won't change); +// silence the GCC warning in that case. template struct BitsTraits, typename std::enable_if< (std::is_integral::value && std::is_unsigned::value)>::type> { typedef T UnderlyingType; static T load(const Unaligned& x) { return x.value; } static void store(Unaligned& x, T v) { x.value = v; } + static T loadRMW(const Unaligned& x) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wuninitialized" + return x.value; +#pragma GCC diagnostic pop + } }; // Partial specialization for T, where T is unsigned integral @@ -55,6 +64,12 @@ struct BitsTraits inline void Bits::set(T* p, size_t bit) { T& block = p[blockIndex(bit)]; - Traits::store(block, Traits::load(block) | (one << bitOffset(bit))); + Traits::store(block, Traits::loadRMW(block) | (one << bitOffset(bit))); } template inline void Bits::clear(T* p, size_t bit) { T& block = p[blockIndex(bit)]; - Traits::store(block, Traits::load(block) & ~(one << bitOffset(bit))); + Traits::store(block, Traits::loadRMW(block) & ~(one << bitOffset(bit))); } template @@ -201,7 +216,7 @@ template inline void Bits::innerSet(T* p, size_t offset, size_t count, UnderlyingType value) { // Mask out bits and set new value - UnderlyingType v = Traits::load(*p); + UnderlyingType v = Traits::loadRMW(*p); v &= ~(((one << count) - 1) << offset); v |= (value << offset); Traits::store(*p, v); -- 2.34.1