|
| 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 | +#pragma warning(push) |
| 8 | +#pragma warning(disable:6200) // C6200: Index is out of valid index range, compiler complains here we use variable length array |
| 9 | + |
| 10 | +namespace Js |
| 11 | +{ |
| 12 | + template<class T, typename CountT = T::CounterFields> |
| 13 | + struct CompactCounters |
| 14 | + { |
| 15 | + uint8 fieldSize; |
| 16 | +#if DBG |
| 17 | + mutable bool bgThreadCallStarted; |
| 18 | + bool isCleaningUp; |
| 19 | +#endif |
| 20 | + union { |
| 21 | + uint8 u8Fields[CountT::Max]; |
| 22 | + int8 i8Fields[CountT::Max]; |
| 23 | + WriteBarrierPtr<uint16> u16Fields; |
| 24 | + WriteBarrierPtr<uint32> u32Fields; |
| 25 | + WriteBarrierPtr<int16> i16Fields; |
| 26 | + WriteBarrierPtr<int32> i32Fields; |
| 27 | + }; |
| 28 | + |
| 29 | + CompactCounters() |
| 30 | + :fieldSize(1) |
| 31 | +#if DBG |
| 32 | + , bgThreadCallStarted(false), isCleaningUp(false) |
| 33 | +#endif |
| 34 | + { |
| 35 | + memset(u8Fields, 0, (uint8)CountT::Max); |
| 36 | + } |
| 37 | + |
| 38 | + void AllocCounters(T* host, uint8 newSize); |
| 39 | + |
| 40 | + uint32 Get(CountT typeEnum) const |
| 41 | + { |
| 42 | +#if DBG |
| 43 | + if (ThreadContext::GetContextForCurrentThread() == nullptr) |
| 44 | + { |
| 45 | + bgThreadCallStarted = true; |
| 46 | + } |
| 47 | +#endif |
| 48 | + uint8 type = static_cast<uint8>(typeEnum); |
| 49 | + uint8 localFieldSize = fieldSize; |
| 50 | + uint32 value = 0; |
| 51 | + if (localFieldSize == 1) |
| 52 | + { |
| 53 | + value = this->u8Fields[type]; |
| 54 | + } |
| 55 | + else if (localFieldSize == 2) |
| 56 | + { |
| 57 | + value = this->u16Fields[type]; |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + Assert(localFieldSize == 4); |
| 62 | + value = this->u32Fields[type]; |
| 63 | + } |
| 64 | + |
| 65 | + return value; |
| 66 | + } |
| 67 | + |
| 68 | + int32 GetSigned(CountT typeEnum) const |
| 69 | + { |
| 70 | +#if DBG |
| 71 | + if (ThreadContext::GetContextForCurrentThread() == nullptr) |
| 72 | + { |
| 73 | + bgThreadCallStarted = true; |
| 74 | + } |
| 75 | +#endif |
| 76 | + |
| 77 | + uint8 type = static_cast<uint8>(typeEnum); |
| 78 | + uint8 localFieldSize = fieldSize; |
| 79 | + int32 value = 0; |
| 80 | + if (localFieldSize == 1) |
| 81 | + { |
| 82 | + value = this->i8Fields[type]; |
| 83 | + } |
| 84 | + else if (localFieldSize == 2) |
| 85 | + { |
| 86 | + value = this->i16Fields[type]; |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + Assert(localFieldSize == 4); |
| 91 | + value = this->i32Fields[type]; |
| 92 | + } |
| 93 | + return value; |
| 94 | + } |
| 95 | + |
| 96 | + uint32 Set(CountT typeEnum, uint32 val, T* host) |
| 97 | + { |
| 98 | + Assert(bgThreadCallStarted == false || isCleaningUp == true); |
| 99 | + |
| 100 | + uint8 type = static_cast<uint8>(typeEnum); |
| 101 | + if (fieldSize == 1) |
| 102 | + { |
| 103 | + if (val <= UINT8_MAX) |
| 104 | + { |
| 105 | + return this->u8Fields[type] = static_cast<uint8>(val); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + AllocCounters(host, val <= UINT16_MAX ? 2 : 4); |
| 110 | + } |
| 111 | + return this->Set(typeEnum, val, host); |
| 112 | + } |
| 113 | + |
| 114 | + if (fieldSize == 2) |
| 115 | + { |
| 116 | + if (val <= UINT16_MAX) |
| 117 | + { |
| 118 | + return this->u16Fields[type] = static_cast<uint16>(val); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + AllocCounters(host, 4); |
| 123 | + } |
| 124 | + return this->Set(typeEnum, val, host); |
| 125 | + } |
| 126 | + |
| 127 | + Assert(fieldSize == 4); |
| 128 | + return this->u32Fields[type] = val; |
| 129 | + } |
| 130 | + |
| 131 | + int32 SetSigned(CountT typeEnum, int32 val, T* host) |
| 132 | + { |
| 133 | + Assert(bgThreadCallStarted == false || isCleaningUp == true); |
| 134 | + |
| 135 | + uint8 type = static_cast<uint8>(typeEnum); |
| 136 | + if (fieldSize == 1) |
| 137 | + { |
| 138 | + if (val <= INT8_MAX && val >= INT8_MIN) |
| 139 | + { |
| 140 | + return this->i8Fields[type] = static_cast<uint8>(val); |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + AllocCounters(host, (val <= INT16_MAX && val >= INT16_MIN) ? 2 : 4); |
| 145 | + } |
| 146 | + return this->SetSigned(typeEnum, val, host); |
| 147 | + } |
| 148 | + |
| 149 | + if (fieldSize == 2) |
| 150 | + { |
| 151 | + if (val <= INT16_MAX && val >= INT16_MIN) |
| 152 | + { |
| 153 | + return this->i16Fields[type] = static_cast<uint16>(val); |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + AllocCounters(host, 4); |
| 158 | + } |
| 159 | + return this->SetSigned(typeEnum, val, host); |
| 160 | + } |
| 161 | + |
| 162 | + Assert(fieldSize == 4); |
| 163 | + return this->i32Fields[type] = val; |
| 164 | + } |
| 165 | + |
| 166 | + uint32 Increase(CountT typeEnum, T* host) |
| 167 | + { |
| 168 | + Assert(bgThreadCallStarted == false); |
| 169 | + |
| 170 | + uint8 type = static_cast<uint8>(typeEnum); |
| 171 | + if (fieldSize == 1) |
| 172 | + { |
| 173 | + if (this->u8Fields[type] < UINT8_MAX) |
| 174 | + { |
| 175 | + return this->u8Fields[type]++; |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + AllocCounters(host, 2); |
| 180 | + } |
| 181 | + return this->Increase(typeEnum, host); |
| 182 | + } |
| 183 | + |
| 184 | + if (fieldSize == 2) |
| 185 | + { |
| 186 | + if (this->u16Fields[type] < UINT16_MAX) |
| 187 | + { |
| 188 | + return this->u16Fields[type]++; |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + AllocCounters(host, 4); |
| 193 | + } |
| 194 | + return this->Increase(typeEnum, host); |
| 195 | + } |
| 196 | + |
| 197 | + Assert(fieldSize == 4); |
| 198 | + return this->u32Fields[type]++; |
| 199 | + } |
| 200 | + }; |
| 201 | + |
| 202 | + |
| 203 | + template<class T, typename CountT> |
| 204 | + void CompactCounters<T, CountT>::AllocCounters(T* host, uint8 newSize) |
| 205 | + { |
| 206 | + Assert(ThreadContext::GetContextForCurrentThread() || ThreadContext::GetCriticalSection()->IsLocked()); |
| 207 | + |
| 208 | + typedef CompactCounters<T, CountT> CounterT; |
| 209 | + Assert(host->GetRecycler() != nullptr); |
| 210 | + |
| 211 | + const uint8 signedStart = static_cast<uint8>(CountT::SignedFieldsStart); |
| 212 | + const uint8 max = static_cast<uint8>(CountT::Max); |
| 213 | + |
| 214 | + void* newFieldsArray = nullptr; |
| 215 | + if (newSize == 2) |
| 216 | + { |
| 217 | + newFieldsArray = RecyclerNewArrayLeafZ(host->GetRecycler(), uint16, max); |
| 218 | + } |
| 219 | + else |
| 220 | + { |
| 221 | + Assert(newSize == 4); |
| 222 | + newFieldsArray = RecyclerNewArrayLeafZ(host->GetRecycler(), uint32, max); |
| 223 | + } |
| 224 | + |
| 225 | + uint8 i = 0; |
| 226 | + if (this->fieldSize == 1) |
| 227 | + { |
| 228 | + if (newSize == 2) |
| 229 | + { |
| 230 | + for (; i < signedStart; i++) |
| 231 | + { |
| 232 | + ((uint16*)newFieldsArray)[i] = this->u8Fields[i]; |
| 233 | + } |
| 234 | + for (; i < max; i++) |
| 235 | + { |
| 236 | + ((int16*)newFieldsArray)[i] = this->i8Fields[i]; |
| 237 | + } |
| 238 | + } |
| 239 | + else |
| 240 | + { |
| 241 | + for (; i < signedStart; i++) |
| 242 | + { |
| 243 | + ((uint32*)newFieldsArray)[i] = this->u8Fields[i]; |
| 244 | + } |
| 245 | + for (; i < max; i++) |
| 246 | + { |
| 247 | + ((int32*)newFieldsArray)[i] = this->i8Fields[i]; |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + else if (this->fieldSize == 2) |
| 252 | + { |
| 253 | + for (; i < signedStart; i++) |
| 254 | + { |
| 255 | + ((uint32*)newFieldsArray)[i] = this->u16Fields[i]; |
| 256 | + } |
| 257 | + for (; i < max; i++) |
| 258 | + { |
| 259 | + ((int32*)newFieldsArray)[i] = this->i16Fields[i]; |
| 260 | + } |
| 261 | + } |
| 262 | + else |
| 263 | + { |
| 264 | + Assert(false); |
| 265 | + } |
| 266 | + |
| 267 | + this->fieldSize = newSize; |
| 268 | + this->u16Fields = (uint16*)newFieldsArray; |
| 269 | + |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +#pragma warning(pop) |
0 commit comments