File indexing completed on 2024-11-15 09:01:14
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef ABSL_STRINGS_INTERNAL_CORDZ_UPDATE_TRACKER_H_
0016 #define ABSL_STRINGS_INTERNAL_CORDZ_UPDATE_TRACKER_H_
0017
0018 #include <atomic>
0019 #include <cstdint>
0020
0021 #include "absl/base/config.h"
0022
0023 namespace absl {
0024 ABSL_NAMESPACE_BEGIN
0025 namespace cord_internal {
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 class CordzUpdateTracker {
0038 public:
0039
0040 enum MethodIdentifier {
0041 kUnknown,
0042 kAppendCord,
0043 kAppendCordBuffer,
0044 kAppendExternalMemory,
0045 kAppendString,
0046 kAssignCord,
0047 kAssignString,
0048 kClear,
0049 kConstructorCord,
0050 kConstructorString,
0051 kCordReader,
0052 kFlatten,
0053 kGetAppendBuffer,
0054 kGetAppendRegion,
0055 kMakeCordFromExternal,
0056 kMoveAppendCord,
0057 kMoveAssignCord,
0058 kMovePrependCord,
0059 kPrependCord,
0060 kPrependCordBuffer,
0061 kPrependString,
0062 kRemovePrefix,
0063 kRemoveSuffix,
0064 kSetExpectedChecksum,
0065 kSubCord,
0066
0067
0068 kNumMethods,
0069 };
0070
0071
0072 constexpr CordzUpdateTracker() noexcept : values_{} {}
0073
0074
0075 CordzUpdateTracker(const CordzUpdateTracker& rhs) noexcept { *this = rhs; }
0076
0077
0078 CordzUpdateTracker& operator=(const CordzUpdateTracker& rhs) noexcept {
0079 for (int i = 0; i < kNumMethods; ++i) {
0080 values_[i].store(rhs.values_[i].load(std::memory_order_relaxed),
0081 std::memory_order_relaxed);
0082 }
0083 return *this;
0084 }
0085
0086
0087 int64_t Value(MethodIdentifier method) const {
0088 return values_[method].load(std::memory_order_relaxed);
0089 }
0090
0091
0092 void LossyAdd(MethodIdentifier method, int64_t n = 1) {
0093 auto& value = values_[method];
0094 value.store(value.load(std::memory_order_relaxed) + n,
0095 std::memory_order_relaxed);
0096 }
0097
0098
0099 void LossyAdd(const CordzUpdateTracker& src) {
0100 for (int i = 0; i < kNumMethods; ++i) {
0101 MethodIdentifier method = static_cast<MethodIdentifier>(i);
0102 if (int64_t value = src.Value(method)) {
0103 LossyAdd(method, value);
0104 }
0105 }
0106 }
0107
0108 private:
0109
0110
0111 class Counter : public std::atomic<int64_t> {
0112 public:
0113 constexpr Counter() noexcept : std::atomic<int64_t>(0) {}
0114 };
0115
0116 Counter values_[kNumMethods];
0117 };
0118
0119 }
0120 ABSL_NAMESPACE_END
0121 }
0122
0123 #endif