xref: /aosp_15_r20/external/angle/third_party/abseil-cpp/absl/flags/internal/flag.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2019 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef ABSL_FLAGS_INTERNAL_FLAG_H_
17 #define ABSL_FLAGS_INTERNAL_FLAG_H_
18 
19 #include <stddef.h>
20 #include <stdint.h>
21 
22 #include <atomic>
23 #include <cstring>
24 #include <memory>
25 #include <string>
26 #include <type_traits>
27 #include <typeinfo>
28 
29 #include "absl/base/attributes.h"
30 #include "absl/base/call_once.h"
31 #include "absl/base/casts.h"
32 #include "absl/base/config.h"
33 #include "absl/base/optimization.h"
34 #include "absl/base/thread_annotations.h"
35 #include "absl/flags/commandlineflag.h"
36 #include "absl/flags/config.h"
37 #include "absl/flags/internal/commandlineflag.h"
38 #include "absl/flags/internal/registry.h"
39 #include "absl/flags/internal/sequence_lock.h"
40 #include "absl/flags/marshalling.h"
41 #include "absl/meta/type_traits.h"
42 #include "absl/strings/string_view.h"
43 #include "absl/synchronization/mutex.h"
44 #include "absl/utility/utility.h"
45 
46 namespace absl {
47 ABSL_NAMESPACE_BEGIN
48 
49 ///////////////////////////////////////////////////////////////////////////////
50 // Forward declaration of absl::Flag<T> public API.
51 namespace flags_internal {
52 template <typename T>
53 class Flag;
54 }  // namespace flags_internal
55 
56 template <typename T>
57 using Flag = flags_internal::Flag<T>;
58 
59 template <typename T>
60 ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag);
61 
62 template <typename T>
63 void SetFlag(absl::Flag<T>* flag, const T& v);
64 
65 template <typename T, typename V>
66 void SetFlag(absl::Flag<T>* flag, const V& v);
67 
68 template <typename U>
69 const CommandLineFlag& GetFlagReflectionHandle(const absl::Flag<U>& f);
70 
71 ///////////////////////////////////////////////////////////////////////////////
72 // Flag value type operations, eg., parsing, copying, etc. are provided
73 // by function specific to that type with a signature matching FlagOpFn.
74 
75 namespace flags_internal {
76 
77 enum class FlagOp {
78   kAlloc,
79   kDelete,
80   kCopy,
81   kCopyConstruct,
82   kSizeof,
83   kFastTypeId,
84   kRuntimeTypeId,
85   kParse,
86   kUnparse,
87   kValueOffset,
88 };
89 using FlagOpFn = void* (*)(FlagOp, const void*, void*, void*);
90 
91 // Forward declaration for Flag value specific operations.
92 template <typename T>
93 void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3);
94 
95 // Allocate aligned memory for a flag value.
Alloc(FlagOpFn op)96 inline void* Alloc(FlagOpFn op) {
97   return op(FlagOp::kAlloc, nullptr, nullptr, nullptr);
98 }
99 // Deletes memory interpreting obj as flag value type pointer.
Delete(FlagOpFn op,void * obj)100 inline void Delete(FlagOpFn op, void* obj) {
101   op(FlagOp::kDelete, nullptr, obj, nullptr);
102 }
103 // Copies src to dst interpreting as flag value type pointers.
Copy(FlagOpFn op,const void * src,void * dst)104 inline void Copy(FlagOpFn op, const void* src, void* dst) {
105   op(FlagOp::kCopy, src, dst, nullptr);
106 }
107 // Construct a copy of flag value in a location pointed by dst
108 // based on src - pointer to the flag's value.
CopyConstruct(FlagOpFn op,const void * src,void * dst)109 inline void CopyConstruct(FlagOpFn op, const void* src, void* dst) {
110   op(FlagOp::kCopyConstruct, src, dst, nullptr);
111 }
112 // Makes a copy of flag value pointed by obj.
Clone(FlagOpFn op,const void * obj)113 inline void* Clone(FlagOpFn op, const void* obj) {
114   void* res = flags_internal::Alloc(op);
115   flags_internal::CopyConstruct(op, obj, res);
116   return res;
117 }
118 // Returns true if parsing of input text is successful.
Parse(FlagOpFn op,absl::string_view text,void * dst,std::string * error)119 inline bool Parse(FlagOpFn op, absl::string_view text, void* dst,
120                   std::string* error) {
121   return op(FlagOp::kParse, &text, dst, error) != nullptr;
122 }
123 // Returns string representing supplied value.
Unparse(FlagOpFn op,const void * val)124 inline std::string Unparse(FlagOpFn op, const void* val) {
125   std::string result;
126   op(FlagOp::kUnparse, val, &result, nullptr);
127   return result;
128 }
129 // Returns size of flag value type.
Sizeof(FlagOpFn op)130 inline size_t Sizeof(FlagOpFn op) {
131   // This sequence of casts reverses the sequence from
132   // `flags_internal::FlagOps()`
133   return static_cast<size_t>(reinterpret_cast<intptr_t>(
134       op(FlagOp::kSizeof, nullptr, nullptr, nullptr)));
135 }
136 // Returns fast type id corresponding to the value type.
FastTypeId(FlagOpFn op)137 inline FlagFastTypeId FastTypeId(FlagOpFn op) {
138   return reinterpret_cast<FlagFastTypeId>(
139       op(FlagOp::kFastTypeId, nullptr, nullptr, nullptr));
140 }
141 // Returns fast type id corresponding to the value type.
RuntimeTypeId(FlagOpFn op)142 inline const std::type_info* RuntimeTypeId(FlagOpFn op) {
143   return reinterpret_cast<const std::type_info*>(
144       op(FlagOp::kRuntimeTypeId, nullptr, nullptr, nullptr));
145 }
146 // Returns offset of the field value_ from the field impl_ inside of
147 // absl::Flag<T> data. Given FlagImpl pointer p you can get the
148 // location of the corresponding value as:
149 //      reinterpret_cast<char*>(p) + ValueOffset().
ValueOffset(FlagOpFn op)150 inline ptrdiff_t ValueOffset(FlagOpFn op) {
151   // This sequence of casts reverses the sequence from
152   // `flags_internal::FlagOps()`
153   return static_cast<ptrdiff_t>(reinterpret_cast<intptr_t>(
154       op(FlagOp::kValueOffset, nullptr, nullptr, nullptr)));
155 }
156 
157 // Returns an address of RTTI's typeid(T).
158 template <typename T>
GenRuntimeTypeId()159 inline const std::type_info* GenRuntimeTypeId() {
160 #ifdef ABSL_INTERNAL_HAS_RTTI
161   return &typeid(T);
162 #else
163   return nullptr;
164 #endif
165 }
166 
167 ///////////////////////////////////////////////////////////////////////////////
168 // Flag help auxiliary structs.
169 
170 // This is help argument for absl::Flag encapsulating the string literal pointer
171 // or pointer to function generating it as well as enum descriminating two
172 // cases.
173 using HelpGenFunc = std::string (*)();
174 
175 template <size_t N>
176 struct FixedCharArray {
177   char value[N];
178 
179   template <size_t... I>
FromLiteralStringFixedCharArray180   static constexpr FixedCharArray<N> FromLiteralString(
181       absl::string_view str, absl::index_sequence<I...>) {
182     return (void)str, FixedCharArray<N>({{str[I]..., '\0'}});
183   }
184 };
185 
186 template <typename Gen, size_t N = Gen::Value().size()>
HelpStringAsArray(int)187 constexpr FixedCharArray<N + 1> HelpStringAsArray(int) {
188   return FixedCharArray<N + 1>::FromLiteralString(
189       Gen::Value(), absl::make_index_sequence<N>{});
190 }
191 
192 template <typename Gen>
HelpStringAsArray(char)193 constexpr std::false_type HelpStringAsArray(char) {
194   return std::false_type{};
195 }
196 
197 union FlagHelpMsg {
FlagHelpMsg(const char * help_msg)198   constexpr explicit FlagHelpMsg(const char* help_msg) : literal(help_msg) {}
FlagHelpMsg(HelpGenFunc help_gen)199   constexpr explicit FlagHelpMsg(HelpGenFunc help_gen) : gen_func(help_gen) {}
200 
201   const char* literal;
202   HelpGenFunc gen_func;
203 };
204 
205 enum class FlagHelpKind : uint8_t { kLiteral = 0, kGenFunc = 1 };
206 
207 struct FlagHelpArg {
208   FlagHelpMsg source;
209   FlagHelpKind kind;
210 };
211 
212 extern const char kStrippedFlagHelp[];
213 
214 // These two HelpArg overloads allows us to select at compile time one of two
215 // way to pass Help argument to absl::Flag. We'll be passing
216 // AbslFlagHelpGenFor##name as Gen and integer 0 as a single argument to prefer
217 // first overload if possible. If help message is evaluatable on constexpr
218 // context We'll be able to make FixedCharArray out of it and we'll choose first
219 // overload. In this case the help message expression is immediately evaluated
220 // and is used to construct the absl::Flag. No additional code is generated by
221 // ABSL_FLAG Otherwise SFINAE kicks in and first overload is dropped from the
222 // consideration, in which case the second overload will be used. The second
223 // overload does not attempt to evaluate the help message expression
224 // immediately and instead delays the evaluation by returning the function
225 // pointer (&T::NonConst) generating the help message when necessary. This is
226 // evaluatable in constexpr context, but the cost is an extra function being
227 // generated in the ABSL_FLAG code.
228 template <typename Gen, size_t N>
HelpArg(const FixedCharArray<N> & value)229 constexpr FlagHelpArg HelpArg(const FixedCharArray<N>& value) {
230   return {FlagHelpMsg(value.value), FlagHelpKind::kLiteral};
231 }
232 
233 template <typename Gen>
HelpArg(std::false_type)234 constexpr FlagHelpArg HelpArg(std::false_type) {
235   return {FlagHelpMsg(&Gen::NonConst), FlagHelpKind::kGenFunc};
236 }
237 
238 ///////////////////////////////////////////////////////////////////////////////
239 // Flag default value auxiliary structs.
240 
241 // Signature for the function generating the initial flag value (usually
242 // based on default value supplied in flag's definition)
243 using FlagDfltGenFunc = void (*)(void*);
244 
245 union FlagDefaultSrc {
FlagDefaultSrc(FlagDfltGenFunc gen_func_arg)246   constexpr explicit FlagDefaultSrc(FlagDfltGenFunc gen_func_arg)
247       : gen_func(gen_func_arg) {}
248 
249 #define ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE(T, name) \
250   T name##_value;                                  \
251   constexpr explicit FlagDefaultSrc(T value) : name##_value(value) {}  // NOLINT
252   ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE)
253 #undef ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE
254 
255   void* dynamic_value;
256   FlagDfltGenFunc gen_func;
257 };
258 
259 enum class FlagDefaultKind : uint8_t {
260   kDynamicValue = 0,
261   kGenFunc = 1,
262   kOneWord = 2  // for default values UP to one word in size
263 };
264 
265 struct FlagDefaultArg {
266   FlagDefaultSrc source;
267   FlagDefaultKind kind;
268 };
269 
270 // This struct and corresponding overload to InitDefaultValue are used to
271 // facilitate usage of {} as default value in ABSL_FLAG macro.
272 // TODO(rogeeff): Fix handling types with explicit constructors.
273 struct EmptyBraces {};
274 
275 template <typename T>
InitDefaultValue(T t)276 constexpr T InitDefaultValue(T t) {
277   return t;
278 }
279 
280 template <typename T>
InitDefaultValue(EmptyBraces)281 constexpr T InitDefaultValue(EmptyBraces) {
282   return T{};
283 }
284 
285 template <typename ValueT, typename GenT,
286           typename std::enable_if<std::is_integral<ValueT>::value, int>::type =
287               ((void)GenT{}, 0)>
DefaultArg(int)288 constexpr FlagDefaultArg DefaultArg(int) {
289   return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kOneWord};
290 }
291 
292 template <typename ValueT, typename GenT>
DefaultArg(char)293 constexpr FlagDefaultArg DefaultArg(char) {
294   return {FlagDefaultSrc(&GenT::Gen), FlagDefaultKind::kGenFunc};
295 }
296 
297 ///////////////////////////////////////////////////////////////////////////////
298 // Flag storage selector traits. Each trait indicates what kind of storage kind
299 // to use for the flag value.
300 
301 template <typename T>
302 using FlagUseValueAndInitBitStorage =
303     std::integral_constant<bool, std::is_trivially_copyable<T>::value &&
304                                      std::is_default_constructible<T>::value &&
305                                      (sizeof(T) < 8)>;
306 
307 template <typename T>
308 using FlagUseOneWordStorage =
309     std::integral_constant<bool, std::is_trivially_copyable<T>::value &&
310                                      (sizeof(T) <= 8)>;
311 
312 template <class T>
313 using FlagUseSequenceLockStorage =
314     std::integral_constant<bool, std::is_trivially_copyable<T>::value &&
315                                      (sizeof(T) > 8)>;
316 
317 enum class FlagValueStorageKind : uint8_t {
318   kValueAndInitBit = 0,
319   kOneWordAtomic = 1,
320   kSequenceLocked = 2,
321   kHeapAllocated = 3,
322 };
323 
324 // This constexpr function returns the storage kind for the given flag value
325 // type.
326 template <typename T>
StorageKind()327 static constexpr FlagValueStorageKind StorageKind() {
328   return FlagUseValueAndInitBitStorage<T>::value
329              ? FlagValueStorageKind::kValueAndInitBit
330          : FlagUseOneWordStorage<T>::value
331              ? FlagValueStorageKind::kOneWordAtomic
332          : FlagUseSequenceLockStorage<T>::value
333              ? FlagValueStorageKind::kSequenceLocked
334              : FlagValueStorageKind::kHeapAllocated;
335 }
336 
337 // This is a base class for the storage classes used by kOneWordAtomic and
338 // kValueAndInitBit storage kinds. It literally just stores the one word value
339 // as an atomic. By default, it is initialized to a magic value that is unlikely
340 // a valid value for the flag value type.
341 struct FlagOneWordValue {
UninitializedFlagOneWordValue342   constexpr static int64_t Uninitialized() {
343     return static_cast<int64_t>(0xababababababababll);
344   }
345 
FlagOneWordValueFlagOneWordValue346   constexpr FlagOneWordValue() : value(Uninitialized()) {}
FlagOneWordValueFlagOneWordValue347   constexpr explicit FlagOneWordValue(int64_t v) : value(v) {}
348   std::atomic<int64_t> value;
349 };
350 
351 // This class represents a memory layout used by kValueAndInitBit storage kind.
352 template <typename T>
353 struct alignas(8) FlagValueAndInitBit {
354   T value;
355   // Use an int instead of a bool to guarantee that a non-zero value has
356   // a bit set.
357   uint8_t init;
358 };
359 
360 // This class implements an aligned pointer with two options stored via masks
361 // in unused bits of the pointer value (due to alignment requirement).
362 //  - IsUnprotectedReadCandidate - indicates that the value can be switched to
363 //    unprotected read without a lock.
364 //  - HasBeenRead - indicates that the value has been read at least once.
365 //  - AllowsUnprotectedRead - combination of the two options above and indicates
366 //    that the value can now be read without a lock.
367 // Further details of these options and their use is covered in the description
368 // of the FlagValue<T, FlagValueStorageKind::kHeapAllocated> specialization.
369 class MaskedPointer {
370  public:
371   using mask_t = uintptr_t;
372   using ptr_t = void*;
373 
RequiredAlignment()374   static constexpr int RequiredAlignment() { return 4; }
375 
MaskedPointer()376   constexpr MaskedPointer() : ptr_(nullptr) {}
MaskedPointer(ptr_t rhs)377   constexpr explicit MaskedPointer(ptr_t rhs) : ptr_(rhs) {}
378   MaskedPointer(ptr_t rhs, bool is_candidate);
379 
380   MaskedPointer(const MaskedPointer& rhs) = default;
381   MaskedPointer& operator=(const MaskedPointer& rhs) = default;
382 
Ptr()383   void* Ptr() const {
384     return reinterpret_cast<void*>(reinterpret_cast<mask_t>(ptr_) &
385                                    kPtrValueMask);
386   }
AllowsUnprotectedRead()387   bool AllowsUnprotectedRead() const {
388     return (reinterpret_cast<mask_t>(ptr_) & kAllowsUnprotectedRead) ==
389            kAllowsUnprotectedRead;
390   }
391   bool IsUnprotectedReadCandidate() const;
392   bool HasBeenRead() const;
393 
394   void Set(FlagOpFn op, const void* src, bool is_candidate);
395   void MarkAsRead();
396 
397  private:
398   // Masks
399   // Indicates that the flag value either default or originated from command
400   // line.
401   static constexpr mask_t kUnprotectedReadCandidate = 0x1u;
402   // Indicates that flag has been read.
403   static constexpr mask_t kHasBeenRead = 0x2u;
404   static constexpr mask_t kAllowsUnprotectedRead =
405       kUnprotectedReadCandidate | kHasBeenRead;
406   static constexpr mask_t kPtrValueMask = ~kAllowsUnprotectedRead;
407 
408   void ApplyMask(mask_t mask);
409   bool CheckMask(mask_t mask) const;
410 
411   ptr_t ptr_;
412 };
413 
414 // This class implements a type erased storage of the heap allocated flag value.
415 // It is used as a base class for the storage class for kHeapAllocated storage
416 // kind. The initial_buffer is expected to have an alignment of at least
417 // MaskedPointer::RequiredAlignment(), so that the bits used by the
418 // MaskedPointer to store masks are set to 0. This guarantees that value starts
419 // in an uninitialized state.
420 struct FlagMaskedPointerValue {
FlagMaskedPointerValueFlagMaskedPointerValue421   constexpr explicit FlagMaskedPointerValue(MaskedPointer::ptr_t initial_buffer)
422       : value(MaskedPointer(initial_buffer)) {}
423 
424   std::atomic<MaskedPointer> value;
425 };
426 
427 // This is the forward declaration for the template that represents a storage
428 // for the flag values. This template is expected to be explicitly specialized
429 // for each storage kind and it does not have a generic default
430 // implementation.
431 template <typename T,
432           FlagValueStorageKind Kind = flags_internal::StorageKind<T>()>
433 struct FlagValue;
434 
435 // This specialization represents the storage of flag values types with the
436 // kValueAndInitBit storage kind. It is based on the FlagOneWordValue class
437 // and relies on memory layout in FlagValueAndInitBit<T> to indicate that the
438 // value has been initialized or not.
439 template <typename T>
440 struct FlagValue<T, FlagValueStorageKind::kValueAndInitBit> : FlagOneWordValue {
441   constexpr FlagValue() : FlagOneWordValue(0) {}
442   bool Get(const SequenceLock&, T& dst) const {
443     int64_t storage = value.load(std::memory_order_acquire);
444     if (ABSL_PREDICT_FALSE(storage == 0)) {
445       // This assert is to ensure that the initialization inside FlagImpl::Init
446       // is able to set init member correctly.
447       static_assert(offsetof(FlagValueAndInitBit<T>, init) == sizeof(T),
448                     "Unexpected memory layout of FlagValueAndInitBit");
449       return false;
450     }
451     dst = absl::bit_cast<FlagValueAndInitBit<T>>(storage).value;
452     return true;
453   }
454 };
455 
456 // This specialization represents the storage of flag values types with the
457 // kOneWordAtomic storage kind. It is based on the FlagOneWordValue class
458 // and relies on the magic uninitialized state of default constructed instead of
459 // FlagOneWordValue to indicate that the value has been initialized or not.
460 template <typename T>
461 struct FlagValue<T, FlagValueStorageKind::kOneWordAtomic> : FlagOneWordValue {
462   constexpr FlagValue() : FlagOneWordValue() {}
463   bool Get(const SequenceLock&, T& dst) const {
464     int64_t one_word_val = value.load(std::memory_order_acquire);
465     if (ABSL_PREDICT_FALSE(one_word_val == FlagOneWordValue::Uninitialized())) {
466       return false;
467     }
468     std::memcpy(&dst, static_cast<const void*>(&one_word_val), sizeof(T));
469     return true;
470   }
471 };
472 
473 // This specialization represents the storage of flag values types with the
474 // kSequenceLocked storage kind. This storage is used by trivially copyable
475 // types with size greater than 8 bytes. This storage relies on uninitialized
476 // state of the SequenceLock to indicate that the value has been initialized or
477 // not. This storage also provides lock-free read access to the underlying
478 // value once it is initialized.
479 template <typename T>
480 struct FlagValue<T, FlagValueStorageKind::kSequenceLocked> {
481   bool Get(const SequenceLock& lock, T& dst) const {
482     return lock.TryRead(&dst, value_words, sizeof(T));
483   }
484 
485   static constexpr int kNumWords =
486       flags_internal::AlignUp(sizeof(T), sizeof(uint64_t)) / sizeof(uint64_t);
487 
488   alignas(T) alignas(
489       std::atomic<uint64_t>) std::atomic<uint64_t> value_words[kNumWords];
490 };
491 
492 // This specialization represents the storage of flag values types with the
493 // kHeapAllocated storage kind. This is a storage of last resort and is used
494 // if none of other storage kinds are applicable.
495 //
496 // Generally speaking the values with this storage kind can't be accessed
497 // atomically and thus can't be read without holding a lock. If we would ever
498 // want to avoid the lock, we'd need to leak the old value every time new flag
499 // value is being set (since we are in danger of having a race condition
500 // otherwise).
501 //
502 // Instead of doing that, this implementation attempts to cater to some common
503 // use cases by allowing at most 2 values to be leaked - default value and
504 // value set from the command line.
505 //
506 // This specialization provides an initial buffer for the first flag value. This
507 // is where the default value is going to be stored. We attempt to reuse this
508 // buffer if possible, including storing the value set from the command line
509 // there.
510 //
511 // As long as we only read this value, we can access it without a lock (in
512 // practice we still use the lock for the very first read to be able set
513 // "has been read" option on this flag).
514 //
515 // If flag is specified on the command line we store the parsed value either
516 // in the internal buffer (if the default value never been read) or we leak the
517 // default value and allocate the new storage for the parse value. This value is
518 // also a candidate for an unprotected read. If flag is set programmatically
519 // after the command line is parsed, the storage for this value is going to be
520 // leaked. Note that in both scenarios we are not going to have a real leak.
521 // Instead we'll store the leaked value pointers in the internal freelist to
522 // avoid triggering the memory leak checker complains.
523 //
524 // If the flag is ever set programmatically, it stops being the candidate for an
525 // unprotected read, and any follow up access to the flag value requires a lock.
526 // Note that if the value if set programmatically before the command line is
527 // parsed, we can switch back to enabling unprotected reads for that value.
528 template <typename T>
529 struct FlagValue<T, FlagValueStorageKind::kHeapAllocated>
530     : FlagMaskedPointerValue {
531   // We const initialize the value with unmasked pointer to the internal buffer,
532   // making sure it is not a candidate for unprotected read. This way we can
533   // ensure Init is done before any access to the flag value.
534   constexpr FlagValue() : FlagMaskedPointerValue(&buffer[0]) {}
535 
536   bool Get(const SequenceLock&, T& dst) const {
537     MaskedPointer ptr_value = value.load(std::memory_order_acquire);
538 
539     if (ABSL_PREDICT_TRUE(ptr_value.AllowsUnprotectedRead())) {
540       ::new (static_cast<void*>(&dst)) T(*static_cast<T*>(ptr_value.Ptr()));
541       return true;
542     }
543     return false;
544   }
545 
546   alignas(MaskedPointer::RequiredAlignment()) alignas(
547       T) char buffer[sizeof(T)]{};
548 };
549 
550 ///////////////////////////////////////////////////////////////////////////////
551 // Flag callback auxiliary structs.
552 
553 // Signature for the mutation callback used by watched Flags
554 // The callback is noexcept.
555 // TODO(rogeeff): add noexcept after C++17 support is added.
556 using FlagCallbackFunc = void (*)();
557 
558 struct FlagCallback {
559   FlagCallbackFunc func;
560   absl::Mutex guard;  // Guard for concurrent callback invocations.
561 };
562 
563 ///////////////////////////////////////////////////////////////////////////////
564 // Flag implementation, which does not depend on flag value type.
565 // The class encapsulates the Flag's data and access to it.
566 
567 struct DynValueDeleter {
568   explicit DynValueDeleter(FlagOpFn op_arg = nullptr);
569   void operator()(void* ptr) const;
570 
571   FlagOpFn op;
572 };
573 
574 class FlagState;
575 
576 // These are only used as constexpr global objects.
577 // They do not use a virtual destructor to simplify their implementation.
578 // They are not destroyed except at program exit, so leaks do not matter.
579 #if defined(__GNUC__) && !defined(__clang__)
580 #pragma GCC diagnostic push
581 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
582 #endif
583 class FlagImpl final : public CommandLineFlag {
584  public:
585   constexpr FlagImpl(const char* name, const char* filename, FlagOpFn op,
586                      FlagHelpArg help, FlagValueStorageKind value_kind,
587                      FlagDefaultArg default_arg)
588       : name_(name),
589         filename_(filename),
590         op_(op),
591         help_(help.source),
592         help_source_kind_(static_cast<uint8_t>(help.kind)),
593         value_storage_kind_(static_cast<uint8_t>(value_kind)),
594         def_kind_(static_cast<uint8_t>(default_arg.kind)),
595         modified_(false),
596         on_command_line_(false),
597         callback_(nullptr),
598         default_value_(default_arg.source),
599         data_guard_{} {}
600 
601   // Constant access methods
602   int64_t ReadOneWord() const ABSL_LOCKS_EXCLUDED(*DataGuard());
603   bool ReadOneBool() const ABSL_LOCKS_EXCLUDED(*DataGuard());
604   void Read(void* dst) const override ABSL_LOCKS_EXCLUDED(*DataGuard());
605   void Read(bool* value) const ABSL_LOCKS_EXCLUDED(*DataGuard()) {
606     *value = ReadOneBool();
607   }
608   template <typename T,
609             absl::enable_if_t<flags_internal::StorageKind<T>() ==
610                                   FlagValueStorageKind::kOneWordAtomic,
611                               int> = 0>
612   void Read(T* value) const ABSL_LOCKS_EXCLUDED(*DataGuard()) {
613     int64_t v = ReadOneWord();
614     std::memcpy(value, static_cast<const void*>(&v), sizeof(T));
615   }
616   template <typename T,
617             typename std::enable_if<flags_internal::StorageKind<T>() ==
618                                         FlagValueStorageKind::kValueAndInitBit,
619                                     int>::type = 0>
620   void Read(T* value) const ABSL_LOCKS_EXCLUDED(*DataGuard()) {
621     *value = absl::bit_cast<FlagValueAndInitBit<T>>(ReadOneWord()).value;
622   }
623 
624   // Mutating access methods
625   void Write(const void* src) ABSL_LOCKS_EXCLUDED(*DataGuard());
626 
627   // Interfaces to operate on callbacks.
628   void SetCallback(const FlagCallbackFunc mutation_callback)
629       ABSL_LOCKS_EXCLUDED(*DataGuard());
630   void InvokeCallback() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
631 
632   // Used in read/write operations to validate source/target has correct type.
633   // For example if flag is declared as absl::Flag<int> FLAGS_foo, a call to
634   // absl::GetFlag(FLAGS_foo) validates that the type of FLAGS_foo is indeed
635   // int. To do that we pass the assumed type id (which is deduced from type
636   // int) as an argument `type_id`, which is in turn is validated against the
637   // type id stored in flag object by flag definition statement.
638   void AssertValidType(FlagFastTypeId type_id,
639                        const std::type_info* (*gen_rtti)()) const;
640 
641  private:
642   template <typename T>
643   friend class Flag;
644   friend class FlagState;
645 
646   // Ensures that `data_guard_` is initialized and returns it.
647   absl::Mutex* DataGuard() const
648       ABSL_LOCK_RETURNED(reinterpret_cast<absl::Mutex*>(data_guard_));
649   // Returns heap allocated value of type T initialized with default value.
650   std::unique_ptr<void, DynValueDeleter> MakeInitValue() const
651       ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
652   // Flag initialization called via absl::call_once.
653   void Init();
654 
655   // Offset value access methods. One per storage kind. These methods to not
656   // respect const correctness, so be very careful using them.
657 
658   // This is a shared helper routine which encapsulates most of the magic. Since
659   // it is only used inside the three routines below, which are defined in
660   // flag.cc, we can define it in that file as well.
661   template <typename StorageT>
662   StorageT* OffsetValue() const;
663 
664   // The same as above, but used for sequencelock-protected storage.
665   std::atomic<uint64_t>* AtomicBufferValue() const;
666 
667   // This is an accessor for a value stored as one word atomic. Returns a
668   // mutable reference to an atomic value.
669   std::atomic<int64_t>& OneWordValue() const;
670 
671   std::atomic<MaskedPointer>& PtrStorage() const;
672 
673   // Attempts to parse supplied `value` string. If parsing is successful,
674   // returns new value. Otherwise returns nullptr.
675   std::unique_ptr<void, DynValueDeleter> TryParse(absl::string_view value,
676                                                   std::string& err) const
677       ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
678   // Stores the flag value based on the pointer to the source.
679   void StoreValue(const void* src, ValueSource source)
680       ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
681 
682   // Copy the flag data, protected by `seq_lock_` into `dst`.
683   //
684   // REQUIRES: ValueStorageKind() == kSequenceLocked.
685   void ReadSequenceLockedData(void* dst) const
686       ABSL_LOCKS_EXCLUDED(*DataGuard());
687 
688   FlagHelpKind HelpSourceKind() const {
689     return static_cast<FlagHelpKind>(help_source_kind_);
690   }
691   FlagValueStorageKind ValueStorageKind() const {
692     return static_cast<FlagValueStorageKind>(value_storage_kind_);
693   }
694   FlagDefaultKind DefaultKind() const
695       ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard()) {
696     return static_cast<FlagDefaultKind>(def_kind_);
697   }
698 
699   // CommandLineFlag interface implementation
700   absl::string_view Name() const override;
701   std::string Filename() const override;
702   std::string Help() const override;
703   FlagFastTypeId TypeId() const override;
704   bool IsSpecifiedOnCommandLine() const override
705       ABSL_LOCKS_EXCLUDED(*DataGuard());
706   std::string DefaultValue() const override ABSL_LOCKS_EXCLUDED(*DataGuard());
707   std::string CurrentValue() const override ABSL_LOCKS_EXCLUDED(*DataGuard());
708   bool ValidateInputValue(absl::string_view value) const override
709       ABSL_LOCKS_EXCLUDED(*DataGuard());
710   void CheckDefaultValueParsingRoundtrip() const override
711       ABSL_LOCKS_EXCLUDED(*DataGuard());
712 
713   int64_t ModificationCount() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
714 
715   // Interfaces to save and restore flags to/from persistent state.
716   // Returns current flag state or nullptr if flag does not support
717   // saving and restoring a state.
718   std::unique_ptr<FlagStateInterface> SaveState() override
719       ABSL_LOCKS_EXCLUDED(*DataGuard());
720 
721   // Restores the flag state to the supplied state object. If there is
722   // nothing to restore returns false. Otherwise returns true.
723   bool RestoreState(const FlagState& flag_state)
724       ABSL_LOCKS_EXCLUDED(*DataGuard());
725 
726   bool ParseFrom(absl::string_view value, FlagSettingMode set_mode,
727                  ValueSource source, std::string& error) override
728       ABSL_LOCKS_EXCLUDED(*DataGuard());
729 
730   // Immutable flag's state.
731 
732   // Flags name passed to ABSL_FLAG as second arg.
733   const char* const name_;
734   // The file name where ABSL_FLAG resides.
735   const char* const filename_;
736   // Type-specific operations vtable.
737   const FlagOpFn op_;
738   // Help message literal or function to generate it.
739   const FlagHelpMsg help_;
740   // Indicates if help message was supplied as literal or generator func.
741   const uint8_t help_source_kind_ : 1;
742   // Kind of storage this flag is using for the flag's value.
743   const uint8_t value_storage_kind_ : 2;
744 
745   uint8_t : 0;  // The bytes containing the const bitfields must not be
746                 // shared with bytes containing the mutable bitfields.
747 
748   // Mutable flag's state (guarded by `data_guard_`).
749 
750   // def_kind_ is not guard by DataGuard() since it is accessed in Init without
751   // locks.
752   uint8_t def_kind_ : 2;
753   // Has this flag's value been modified?
754   bool modified_ : 1 ABSL_GUARDED_BY(*DataGuard());
755   // Has this flag been specified on command line.
756   bool on_command_line_ : 1 ABSL_GUARDED_BY(*DataGuard());
757 
758   // Unique tag for absl::call_once call to initialize this flag.
759   absl::once_flag init_control_;
760 
761   // Sequence lock / mutation counter.
762   flags_internal::SequenceLock seq_lock_;
763 
764   // Optional flag's callback and absl::Mutex to guard the invocations.
765   FlagCallback* callback_ ABSL_GUARDED_BY(*DataGuard());
766   // Either a pointer to the function generating the default value based on the
767   // value specified in ABSL_FLAG or pointer to the dynamically set default
768   // value via SetCommandLineOptionWithMode. def_kind_ is used to distinguish
769   // these two cases.
770   FlagDefaultSrc default_value_;
771 
772   // This is reserved space for an absl::Mutex to guard flag data. It will be
773   // initialized in FlagImpl::Init via placement new.
774   // We can't use "absl::Mutex data_guard_", since this class is not literal.
775   // We do not want to use "absl::Mutex* data_guard_", since this would require
776   // heap allocation during initialization, which is both slows program startup
777   // and can fail. Using reserved space + placement new allows us to avoid both
778   // problems.
779   alignas(absl::Mutex) mutable char data_guard_[sizeof(absl::Mutex)];
780 };
781 #if defined(__GNUC__) && !defined(__clang__)
782 #pragma GCC diagnostic pop
783 #endif
784 
785 ///////////////////////////////////////////////////////////////////////////////
786 // The Flag object parameterized by the flag's value type. This class implements
787 // flag reflection handle interface.
788 
789 template <typename T>
790 class Flag {
791  public:
792   constexpr Flag(const char* name, const char* filename, FlagHelpArg help,
793                  const FlagDefaultArg default_arg)
794       : impl_(name, filename, &FlagOps<T>, help,
795               flags_internal::StorageKind<T>(), default_arg),
796         value_() {}
797 
798   // CommandLineFlag interface
799   absl::string_view Name() const { return impl_.Name(); }
800   std::string Filename() const { return impl_.Filename(); }
801   std::string Help() const { return impl_.Help(); }
802   // Do not use. To be removed.
803   bool IsSpecifiedOnCommandLine() const {
804     return impl_.IsSpecifiedOnCommandLine();
805   }
806   std::string DefaultValue() const { return impl_.DefaultValue(); }
807   std::string CurrentValue() const { return impl_.CurrentValue(); }
808 
809  private:
810   template <typename, bool>
811   friend class FlagRegistrar;
812   friend class FlagImplPeer;
813 
814   T Get() const {
815     // See implementation notes in CommandLineFlag::Get().
816     union U {
817       T value;
818       U() {}
819       ~U() { value.~T(); }
820     };
821     U u;
822 
823 #if !defined(NDEBUG)
824     impl_.AssertValidType(base_internal::FastTypeId<T>(), &GenRuntimeTypeId<T>);
825 #endif
826 
827     if (ABSL_PREDICT_FALSE(!value_.Get(impl_.seq_lock_, u.value))) {
828       impl_.Read(&u.value);
829     }
830     return std::move(u.value);
831   }
832   void Set(const T& v) {
833     impl_.AssertValidType(base_internal::FastTypeId<T>(), &GenRuntimeTypeId<T>);
834     impl_.Write(&v);
835   }
836 
837   // Access to the reflection.
838   const CommandLineFlag& Reflect() const { return impl_; }
839 
840   // Flag's data
841   // The implementation depends on value_ field to be placed exactly after the
842   // impl_ field, so that impl_ can figure out the offset to the value and
843   // access it.
844   FlagImpl impl_;
845   FlagValue<T> value_;
846 };
847 
848 ///////////////////////////////////////////////////////////////////////////////
849 // Trampoline for friend access
850 
851 class FlagImplPeer {
852  public:
853   template <typename T, typename FlagType>
854   static T InvokeGet(const FlagType& flag) {
855     return flag.Get();
856   }
857   template <typename FlagType, typename T>
858   static void InvokeSet(FlagType& flag, const T& v) {
859     flag.Set(v);
860   }
861   template <typename FlagType>
862   static const CommandLineFlag& InvokeReflect(const FlagType& f) {
863     return f.Reflect();
864   }
865 };
866 
867 ///////////////////////////////////////////////////////////////////////////////
868 // Implementation of Flag value specific operations routine.
869 template <typename T>
870 void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3) {
871   struct AlignedSpace {
872     alignas(MaskedPointer::RequiredAlignment()) alignas(T) char buf[sizeof(T)];
873   };
874   using Allocator = std::allocator<AlignedSpace>;
875   switch (op) {
876     case FlagOp::kAlloc: {
877       Allocator alloc;
878       return std::allocator_traits<Allocator>::allocate(alloc, 1);
879     }
880     case FlagOp::kDelete: {
881       T* p = static_cast<T*>(v2);
882       p->~T();
883       Allocator alloc;
884       std::allocator_traits<Allocator>::deallocate(
885           alloc, reinterpret_cast<AlignedSpace*>(p), 1);
886       return nullptr;
887     }
888     case FlagOp::kCopy:
889       *static_cast<T*>(v2) = *static_cast<const T*>(v1);
890       return nullptr;
891     case FlagOp::kCopyConstruct:
892       new (v2) T(*static_cast<const T*>(v1));
893       return nullptr;
894     case FlagOp::kSizeof:
895       return reinterpret_cast<void*>(static_cast<uintptr_t>(sizeof(T)));
896     case FlagOp::kFastTypeId:
897       return const_cast<void*>(base_internal::FastTypeId<T>());
898     case FlagOp::kRuntimeTypeId:
899       return const_cast<std::type_info*>(GenRuntimeTypeId<T>());
900     case FlagOp::kParse: {
901       // Initialize the temporary instance of type T based on current value in
902       // destination (which is going to be flag's default value).
903       T temp(*static_cast<T*>(v2));
904       if (!absl::ParseFlag<T>(*static_cast<const absl::string_view*>(v1), &temp,
905                               static_cast<std::string*>(v3))) {
906         return nullptr;
907       }
908       *static_cast<T*>(v2) = std::move(temp);
909       return v2;
910     }
911     case FlagOp::kUnparse:
912       *static_cast<std::string*>(v2) =
913           absl::UnparseFlag<T>(*static_cast<const T*>(v1));
914       return nullptr;
915     case FlagOp::kValueOffset: {
916       // Round sizeof(FlagImp) to a multiple of alignof(FlagValue<T>) to get the
917       // offset of the data.
918       size_t round_to = alignof(FlagValue<T>);
919       size_t offset = (sizeof(FlagImpl) + round_to - 1) / round_to * round_to;
920       return reinterpret_cast<void*>(offset);
921     }
922   }
923   return nullptr;
924 }
925 
926 ///////////////////////////////////////////////////////////////////////////////
927 // This class facilitates Flag object registration and tail expression-based
928 // flag definition, for example:
929 // ABSL_FLAG(int, foo, 42, "Foo help").OnUpdate(NotifyFooWatcher);
930 struct FlagRegistrarEmpty {};
931 template <typename T, bool do_register>
932 class FlagRegistrar {
933  public:
934   constexpr explicit FlagRegistrar(Flag<T>& flag, const char* filename)
935       : flag_(flag) {
936     if (do_register)
937       flags_internal::RegisterCommandLineFlag(flag_.impl_, filename);
938   }
939 
940   FlagRegistrar OnUpdate(FlagCallbackFunc cb) && {
941     flag_.impl_.SetCallback(cb);
942     return *this;
943   }
944 
945   // Makes the registrar die gracefully as an empty struct on a line where
946   // registration happens. Registrar objects are intended to live only as
947   // temporary.
948   constexpr operator FlagRegistrarEmpty() const { return {}; }  // NOLINT
949 
950  private:
951   Flag<T>& flag_;  // Flag being registered (not owned).
952 };
953 
954 ///////////////////////////////////////////////////////////////////////////////
955 // Test only API
956 uint64_t NumLeakedFlagValues();
957 
958 }  // namespace flags_internal
959 ABSL_NAMESPACE_END
960 }  // namespace absl
961 
962 #endif  // ABSL_FLAGS_INTERNAL_FLAG_H_
963