|
| 1 | +//------------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (C) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. |
| 4 | +//------------------------------------------------------------------------------------------------------- |
| 5 | +#pragma once |
| 6 | + |
| 7 | +template<class T, typename CountT = T::CounterFields> |
| 8 | +struct Counter |
| 9 | +{ |
| 10 | + uint8 fieldSize; |
| 11 | + union { |
| 12 | + uint8 fields1[1]; |
| 13 | + uint16 fields2[1]; |
| 14 | + uint32 fields4[1]; |
| 15 | + int8 signedFields1[1]; |
| 16 | + int16 signedFields2[1]; |
| 17 | + int32 signedFields4[1]; |
| 18 | + }; |
| 19 | + |
| 20 | + |
| 21 | + Counter(uint8 size) |
| 22 | + { |
| 23 | + fieldSize = size; |
| 24 | + } |
| 25 | + |
| 26 | + static void AllocCounters(T* _this, uint8 newSize); |
| 27 | + |
| 28 | + uint32 Get(CountT typeEnum) const |
| 29 | + { |
| 30 | + uint8 type = static_cast<uint8>(typeEnum); |
| 31 | + if (fieldSize == 1) |
| 32 | + { |
| 33 | + return this->fields1[type]; |
| 34 | + } |
| 35 | + else if (fieldSize == 2) |
| 36 | + { |
| 37 | + return this->fields2[type]; |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + Assert(fieldSize == 4); |
| 42 | + return this->fields4[type]; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + int32 GetSigned(CountT typeEnum) const |
| 47 | + { |
| 48 | + uint8 type = static_cast<uint8>(typeEnum); |
| 49 | + if (fieldSize == 1) |
| 50 | + { |
| 51 | + return this->signedFields1[type]; |
| 52 | + } |
| 53 | + else if (fieldSize == 2) |
| 54 | + { |
| 55 | + return this->signedFields2[type]; |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + Assert(fieldSize == 4); |
| 60 | + return this->signedFields4[type]; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + uint32 Set(CountT typeEnum, uint32 val, T* _this) |
| 65 | + { |
| 66 | + uint8 type = static_cast<uint8>(typeEnum); |
| 67 | + if (fieldSize == 1) |
| 68 | + { |
| 69 | + if (val <= UINT8_MAX) |
| 70 | + { |
| 71 | + return fields1[type] = static_cast<uint8>(val); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + AllocCounters(_this, val <= UINT16_MAX ? 2 : 4); |
| 76 | + } |
| 77 | + return _this->counters->Set(typeEnum, val, _this); |
| 78 | + } |
| 79 | + |
| 80 | + if (fieldSize == 2) |
| 81 | + { |
| 82 | + if (val <= UINT16_MAX) |
| 83 | + { |
| 84 | + return fields2[type] = static_cast<uint16>(val); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + AllocCounters(_this, 4); |
| 89 | + } |
| 90 | + return _this->counters->Set(typeEnum, val, _this); |
| 91 | + } |
| 92 | + |
| 93 | + Assert(fieldSize == 4); |
| 94 | + return fields4[type] = val; |
| 95 | + } |
| 96 | + |
| 97 | + int32 SetSigned(CountT typeEnum, int32 val, T* _this) |
| 98 | + { |
| 99 | + uint8 type = static_cast<uint8>(typeEnum); |
| 100 | + if (fieldSize == 1) |
| 101 | + { |
| 102 | + if (val <= INT8_MAX && val>=INT8_MIN) |
| 103 | + { |
| 104 | + return signedFields1[type] = static_cast<uint8>(val); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + AllocCounters(_this, (val <= INT16_MAX && val >= INT16_MIN) ? 2 : 4); |
| 109 | + } |
| 110 | + return _this->counters->SetSigned(typeEnum, val, _this); |
| 111 | + } |
| 112 | + |
| 113 | + if (fieldSize == 2) |
| 114 | + { |
| 115 | + if (val <= INT16_MAX && val >= INT16_MIN) |
| 116 | + { |
| 117 | + return signedFields2[type] = static_cast<uint16>(val); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + AllocCounters(_this, 4); |
| 122 | + } |
| 123 | + return _this->counters->SetSigned(typeEnum, val, _this); |
| 124 | + } |
| 125 | + |
| 126 | + Assert(fieldSize == 4); |
| 127 | + return signedFields4[type] = val; |
| 128 | + } |
| 129 | + |
| 130 | + uint32 Increase(CountT typeEnum, T* _this) |
| 131 | + { |
| 132 | + uint8 type = static_cast<uint8>(typeEnum); |
| 133 | + if (fieldSize == 1) |
| 134 | + { |
| 135 | + if (fields1[type] < UINT8_MAX) |
| 136 | + { |
| 137 | + return fields1[type]++; |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + AllocCounters(_this, fields1[type] < UINT16_MAX ? 2 : 4); |
| 142 | + } |
| 143 | + return _this->counters->Increase(typeEnum, _this); |
| 144 | + } |
| 145 | + |
| 146 | + if (fieldSize == 2) |
| 147 | + { |
| 148 | + if (fields1[type] < UINT16_MAX) |
| 149 | + { |
| 150 | + return fields2[type]++; |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + AllocCounters(_this, 4); |
| 155 | + } |
| 156 | + return _this->counters->Increase(typeEnum, _this); |
| 157 | + } |
| 158 | + |
| 159 | + Assert(fieldSize == 4); |
| 160 | + return fields4[type]++; |
| 161 | + } |
| 162 | +}; |
| 163 | + |
| 164 | + |
| 165 | +template<class T, typename CountT> |
| 166 | +void Counter<T, CountT>::AllocCounters(T* _this, uint8 newSize) |
| 167 | +{ |
| 168 | + typedef Counter<T, CountT> CounterT; |
| 169 | + const uint8 signedStart = static_cast<uint8>(CountT::SignedFieldsStart); |
| 170 | + const uint8 max = static_cast<uint8>(CountT::Max); |
| 171 | + auto plusSize = (max - 1)*newSize; |
| 172 | + CounterT* newCounters = RecyclerNewPlusLeafZ(_this->GetRecycler(), plusSize, CounterT, newSize); |
| 173 | + |
| 174 | + if (_this->counters != nullptr) |
| 175 | + { |
| 176 | + uint8 i = 0; |
| 177 | + if (_this->counters->fieldSize == 1) |
| 178 | + { |
| 179 | + if (newSize == 2) |
| 180 | + { |
| 181 | + for (; i < signedStart; i++) |
| 182 | + { |
| 183 | + newCounters->fields2[i] = _this->counters->fields1[i]; |
| 184 | + } |
| 185 | + for (; i < max; i++) |
| 186 | + { |
| 187 | + newCounters->signedFields2[i] = _this->counters->signedFields1[i]; |
| 188 | + } |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + for (; i < signedStart; i++) |
| 193 | + { |
| 194 | + newCounters->fields4[i] = _this->counters->fields1[i]; |
| 195 | + } |
| 196 | + for (; i < max; i++) |
| 197 | + { |
| 198 | + newCounters->signedFields4[i] = _this->counters->signedFields1[i]; |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + else if (_this->counters->fieldSize == 2) |
| 203 | + { |
| 204 | + for (; i < signedStart; i++) |
| 205 | + { |
| 206 | + newCounters->fields4[i] = _this->counters->fields2[i]; |
| 207 | + } |
| 208 | + for (; i < max; i++) |
| 209 | + { |
| 210 | + newCounters->signedFields4[i] = _this->counters->signedFields2[i]; |
| 211 | + } |
| 212 | + } |
| 213 | + else |
| 214 | + { |
| 215 | + Assert(false); |
| 216 | + } |
| 217 | + |
| 218 | + } |
| 219 | + _this->counters = newCounters; |
| 220 | +} |
0 commit comments