about summary refs log tree commit diff
path: root/absl/flags/internal/flag.cc
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2020-03-26T15·48-0700
committerXiaoyi Zhang <zhangxy@google.com>2020-03-26T15·51-0400
commit79e0dc11514df035a8d07a356f9ee1800fb2160c (patch)
tree3e345e19f1cc496e127ec00dcebb67d7a311c3b5 /absl/flags/internal/flag.cc
parent132d791b40b0f99464e033bc624cfeac7aa68894 (diff)
Export of internal Abseil changes
--
990253454819ce26ff1dda9ab4bbc145b61d01e4 by Xiaoyi Zhang <zhangxy@google.com>:

Import github PR https://github.com/abseil/abseil-cpp/pull/645

PiperOrigin-RevId: 303119797

--
5ac845cb7929b7d1eaf59a309afd811db5001175 by Abseil Team <absl-team@google.com>:

Fix internal exception spec compatibility error

PiperOrigin-RevId: 303104081

--
3290595dd866eecab3c7044e2e3ca0adb74f1bf5 by Gennadiy Rozental <rogeeff@google.com>:

Use FlagValue<T> to represent the value of a flag. Place it directly after
FlagImpl and use a computed offset refer to it.

The offset is computed based on the assumption that the `value_` data member
is placed directly after the impl_ data member in Flag<T>.

This change will allow us to migrate to `T`-specific storage in the generic case.

This change decreases the overhead for int flags by 32 bytes.

PiperOrigin-RevId: 303038099

--
f2b37722cd7a6d3a60ef9713f0d2bbff56f3ddbf by Derek Mauro <dmauro@google.com>:

Minor correctness fix for an ABSL_HAVE_BUILTIN conditional

PiperOrigin-RevId: 302980666

--
39c079a6141ae1c5728af8bf33a39c8aff9deb9f by Abseil Team <absl-team@google.com>:

Use ABSL_HARDENING_ASSERT in b-tree and SwissTable iterators.

PiperOrigin-RevId: 302970075

--
9668a044e080c789df32bcaa1ffb5100831cd9fa by Benjamin Barenblat <bbaren@google.com>:

Correct `add_subdirectory` line in CMake googletest support

Commit bcefbdcdf6ad85046ccacee0aeffba5404d3e528 added support for building with CMake against a local googletest checkout, but I missed a line when constructing the diff. Change the `add_subdirectory` line to reference the correct directories.

PiperOrigin-RevId: 302947488

--
0a3c10fabf80a43ca69ab8b1570030e55f2be741 by Andy Soffer <asoffer@google.com>:

Remove unused distribution format traits.

PiperOrigin-RevId: 302896176

--
0478f2f6270e5ed64c0e28ec09556ca90b2d46a9 by Samuel Benzaquen <sbenza@google.com>:

Fix for CWG:2310.

PiperOrigin-RevId: 302734089

--
3cb978dda5cae5905affdc0914dcc2d27671ed11 by Samuel Benzaquen <sbenza@google.com>:

Fix the Allocate/Deallocate functions to use the same underlying allocator type.

PiperOrigin-RevId: 302721804

--
ae38d3984fb68b4e3ddc165fa8d5c24d5936be52 by Matthew Brown <matthewbr@google.com>:

Internal Change

PiperOrigin-RevId: 302717314

--
7357cf7abd03cc60b6e82b5f28a8e34935c3b4dc by Andy Getzendanner <durandal@google.com>:

Fix typo: s/ABSL_HARDENED_ASSERT/ABSL_HARDENING_ASSERT/

PiperOrigin-RevId: 302532164
GitOrigin-RevId: 990253454819ce26ff1dda9ab4bbc145b61d01e4
Change-Id: Ie595a221c16e1e7e1255ad42e029b646c5f3e11d
Diffstat (limited to 'absl/flags/internal/flag.cc')
-rw-r--r--absl/flags/internal/flag.cc82
1 files changed, 53 insertions, 29 deletions
diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc
index 56a5ed2bc468..5b4499abe47e 100644
--- a/absl/flags/internal/flag.cc
+++ b/absl/flags/internal/flag.cc
@@ -25,6 +25,7 @@
 #include <vector>
 
 #include "absl/base/attributes.h"
+#include "absl/base/casts.h"
 #include "absl/base/config.h"
 #include "absl/base/const_init.h"
 #include "absl/base/optimization.h"
@@ -135,18 +136,18 @@ void FlagImpl::Init() {
       (*default_value_.gen_func)(), DynValueDeleter{op_});
   switch (ValueStorageKind()) {
     case FlagValueStorageKind::kHeapAllocated:
-      value_.dynamic = init_value.release();
+      HeapAllocatedValue() = init_value.release();
       break;
     case FlagValueStorageKind::kOneWordAtomic: {
       int64_t atomic_value;
-      std::memcpy(&atomic_value, init_value.get(), flags_internal::Sizeof(op_));
-      value_.one_word_atomic.store(atomic_value, std::memory_order_release);
+      std::memcpy(&atomic_value, init_value.get(), Sizeof(op_));
+      OneWordValue().store(atomic_value, std::memory_order_release);
       break;
     }
     case FlagValueStorageKind::kTwoWordsAtomic: {
       AlignedTwoWords atomic_value{0, 0};
-      std::memcpy(&atomic_value, init_value.get(), flags_internal::Sizeof(op_));
-      value_.two_words_atomic.store(atomic_value, std::memory_order_release);
+      std::memcpy(&atomic_value, init_value.get(), Sizeof(op_));
+      TwoWordsValue().store(atomic_value, std::memory_order_release);
       break;
     }
   }
@@ -198,18 +199,18 @@ std::unique_ptr<void, DynValueDeleter> FlagImpl::MakeInitValue() const {
 void FlagImpl::StoreValue(const void* src) {
   switch (ValueStorageKind()) {
     case FlagValueStorageKind::kHeapAllocated:
-      flags_internal::Copy(op_, src, value_.dynamic);
+      Copy(op_, src, HeapAllocatedValue());
       break;
     case FlagValueStorageKind::kOneWordAtomic: {
-      int64_t one_word_val;
-      std::memcpy(&one_word_val, src, flags_internal::Sizeof(op_));
-      value_.one_word_atomic.store(one_word_val, std::memory_order_release);
+      int64_t one_word_val = 0;
+      std::memcpy(&one_word_val, src, Sizeof(op_));
+      OneWordValue().store(one_word_val, std::memory_order_release);
       break;
     }
     case FlagValueStorageKind::kTwoWordsAtomic: {
       AlignedTwoWords two_words_val{0, 0};
-      std::memcpy(&two_words_val, src, flags_internal::Sizeof(op_));
-      value_.two_words_atomic.store(two_words_val, std::memory_order_release);
+      std::memcpy(&two_words_val, src, Sizeof(op_));
+      TwoWordsValue().store(two_words_val, std::memory_order_release);
       break;
     }
   }
@@ -258,17 +259,19 @@ std::string FlagImpl::CurrentValue() const {
   switch (ValueStorageKind()) {
     case FlagValueStorageKind::kHeapAllocated: {
       absl::MutexLock l(guard);
-      return flags_internal::Unparse(op_, value_.dynamic);
+      return flags_internal::Unparse(op_, HeapAllocatedValue());
     }
     case FlagValueStorageKind::kOneWordAtomic: {
       const auto one_word_val =
-          value_.one_word_atomic.load(std::memory_order_acquire);
-      return flags_internal::Unparse(op_, &one_word_val);
+          absl::bit_cast<std::array<char, sizeof(int64_t)>>(
+              OneWordValue().load(std::memory_order_acquire));
+      return flags_internal::Unparse(op_, one_word_val.data());
     }
     case FlagValueStorageKind::kTwoWordsAtomic: {
       const auto two_words_val =
-          value_.two_words_atomic.load(std::memory_order_acquire);
-      return flags_internal::Unparse(op_, &two_words_val);
+          absl::bit_cast<std::array<char, sizeof(AlignedTwoWords)>>(
+              TwoWordsValue().load(std::memory_order_acquire));
+      return flags_internal::Unparse(op_, two_words_val.data());
     }
   }
 
@@ -317,18 +320,18 @@ std::unique_ptr<FlagStateInterface> FlagImpl::SaveState() {
   switch (ValueStorageKind()) {
     case FlagValueStorageKind::kHeapAllocated: {
       return absl::make_unique<FlagState>(
-          this, flags_internal::Clone(op_, value_.dynamic), modified,
+          this, flags_internal::Clone(op_, HeapAllocatedValue()), modified,
           on_command_line, counter_);
     }
     case FlagValueStorageKind::kOneWordAtomic: {
       return absl::make_unique<FlagState>(
-          this, value_.one_word_atomic.load(std::memory_order_acquire),
-          modified, on_command_line, counter_);
+          this, OneWordValue().load(std::memory_order_acquire), modified,
+          on_command_line, counter_);
     }
     case FlagValueStorageKind::kTwoWordsAtomic: {
       return absl::make_unique<FlagState>(
-          this, value_.two_words_atomic.load(std::memory_order_acquire),
-          modified, on_command_line, counter_);
+          this, TwoWordsValue().load(std::memory_order_acquire), modified,
+          on_command_line, counter_);
     }
   }
   return nullptr;
@@ -359,6 +362,28 @@ bool FlagImpl::RestoreState(const FlagState& flag_state) {
   return true;
 }
 
+template <typename StorageT>
+typename StorageT::value_type& FlagImpl::OffsetValue() const {
+  char* p = reinterpret_cast<char*>(const_cast<FlagImpl*>(this));
+  // The offset is deduced via Flag value type specific op_.
+  size_t offset = flags_internal::ValueOffset(op_);
+
+  return reinterpret_cast<StorageT*>(p + offset)->value;
+}
+
+void*& FlagImpl::HeapAllocatedValue() const {
+  assert(ValueStorageKind() == FlagValueStorageKind::kHeapAllocated);
+  return OffsetValue<FlagHeapAllocatedValue>();
+}
+std::atomic<int64_t>& FlagImpl::OneWordValue() const {
+  assert(ValueStorageKind() == FlagValueStorageKind::kOneWordAtomic);
+  return OffsetValue<FlagOneWordValue>();
+}
+std::atomic<AlignedTwoWords>& FlagImpl::TwoWordsValue() const {
+  assert(ValueStorageKind() == FlagValueStorageKind::kTwoWordsAtomic);
+  return OffsetValue<FlagTwoWordsValue>();
+}
+
 // Attempts to parse supplied `value` string using parsing routine in the `flag`
 // argument. If parsing successful, this function replaces the dst with newly
 // parsed value. In case if any error is encountered in either step, the error
@@ -383,20 +408,19 @@ void FlagImpl::Read(void* dst) const {
   switch (ValueStorageKind()) {
     case FlagValueStorageKind::kHeapAllocated: {
       absl::MutexLock l(guard);
-
-      flags_internal::CopyConstruct(op_, value_.dynamic, dst);
+      flags_internal::CopyConstruct(op_, HeapAllocatedValue(), dst);
       break;
     }
     case FlagValueStorageKind::kOneWordAtomic: {
-      const auto one_word_val =
-          value_.one_word_atomic.load(std::memory_order_acquire);
-      std::memcpy(dst, &one_word_val, flags_internal::Sizeof(op_));
+      const int64_t one_word_val =
+          OneWordValue().load(std::memory_order_acquire);
+      std::memcpy(dst, &one_word_val, Sizeof(op_));
       break;
     }
     case FlagValueStorageKind::kTwoWordsAtomic: {
-      const auto two_words_val =
-          value_.two_words_atomic.load(std::memory_order_acquire);
-      std::memcpy(dst, &two_words_val, flags_internal::Sizeof(op_));
+      const AlignedTwoWords two_words_val =
+          TwoWordsValue().load(std::memory_order_acquire);
+      std::memcpy(dst, &two_words_val, Sizeof(op_));
       break;
     }
   }