|
| 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 | + |
| 6 | +#pragma once |
| 7 | + |
| 8 | +namespace Js |
| 9 | +{ |
| 10 | + // given bucket size, calculate how many pointers can be hold in AuxPtrFix structure |
| 11 | + constexpr uint8 CalcMaxCount(const uint8 size) |
| 12 | + { |
| 13 | + return (size - 1) / (1 + sizeof(void*)); |
| 14 | + } |
| 15 | + // Use fixed size structure to save pointers |
| 16 | + // AuxPtrsFix(16 bytes or 32 bytes) layout: |
| 17 | + // max count metadata(init'd type) pointers array |
| 18 | + // -------------------------------------------------------------------------------------------------- |
| 19 | + // AuxPtr16 on x64 1 byte 1 bytes 8 bytes to hold up to 1 pointer |
| 20 | + // AuxPtr32 on x64 1 byte 3 bytes 24 bytes to hold up to 3 pointers |
| 21 | + // AuxPtr16 on x86 1 byte 3 bytes 12 bytes to hold up to 3 pointers |
| 22 | + // AuxPtr32 on x64 1 byte 6 bytes 24 bytes to hold up to 6 pointers |
| 23 | + template<typename FieldsEnum, uint8 size, uint8 _MaxCount = CalcMaxCount(size)> |
| 24 | + struct AuxPtrsFix |
| 25 | + { |
| 26 | + static const uint8 MaxCount = _MaxCount; |
| 27 | + uint8 count; // always saving maxCount |
| 28 | + FieldsEnum type[MaxCount]; // save instantiated pointer enum |
| 29 | + WriteBarrierPtr<void> ptr[MaxCount]; // save instantiated pointer address |
| 30 | + AuxPtrsFix(); |
| 31 | + AuxPtrsFix(AuxPtrsFix<FieldsEnum, 16>* ptr16); // called when promoting from AuxPtrs16 to AuxPtrs32 |
| 32 | + void* Get(FieldsEnum e); |
| 33 | + bool Set(FieldsEnum e, void* p); |
| 34 | + }; |
| 35 | + |
| 36 | + // Use flexible size structure to save pointers. when pointer count exceeds AuxPtrsFix<FieldsEnum, 32>::MaxCount, |
| 37 | + // it will promote to this structure to save the pointers |
| 38 | + // Layout: |
| 39 | + // count array of positions array of instantiated pointers |
| 40 | + // ---------------------------------------------------------------- |
| 41 | + // 1 byte FieldsEnum::Max bytes dynamic array depending on how many pointers has been instantiated |
| 42 | + template<class T, typename FieldsEnum> |
| 43 | + struct AuxPtrs |
| 44 | + { |
| 45 | + typedef AuxPtrsFix<FieldsEnum, 16> AuxPtrs16; |
| 46 | + typedef AuxPtrsFix<FieldsEnum, 32> AuxPtrs32; |
| 47 | + typedef AuxPtrs<T, FieldsEnum> AuxPtrsT; |
| 48 | + uint8 count; // save instantiated pointers count |
| 49 | + uint8 capacity; // save number of pointers can be hold in current instance of AuxPtrs |
| 50 | + FieldsEnum offsets[FieldsEnum::Max]; // save position of each instantiated pointers, if not instantiate, it's invalid |
| 51 | + WriteBarrierPtr<void> ptrs[1]; // instantiated pointer addresses |
| 52 | + AuxPtrs(uint8 capacity, AuxPtrs32* ptr32); // called when promoting from AuxPtrs32 to AuxPtrs |
| 53 | + AuxPtrs(uint8 capacity, AuxPtrs* ptr); // called when expanding (i.e. promoting from AuxPtrs to bigger AuxPtrs) |
| 54 | + void* Get(FieldsEnum e); |
| 55 | + bool Set(FieldsEnum e, void* p); |
| 56 | + static void AllocAuxPtrFix(T* _this, uint8 size, Recycler* recycler); |
| 57 | + static void AllocAuxPtr(T* _this, uint8 count, Recycler* recycler); |
| 58 | + static void* GetAuxPtr(const T* _this, FieldsEnum e); |
| 59 | + static void SetAuxPtr(T* _this, FieldsEnum e, void* ptr, Recycler* recycler); |
| 60 | + }; |
| 61 | + |
| 62 | + |
| 63 | + template<typename FieldsEnum, uint8 size, uint8 _MaxCount> |
| 64 | + AuxPtrsFix<FieldsEnum, size, _MaxCount>::AuxPtrsFix() |
| 65 | + { |
| 66 | + static_assert(_MaxCount == AuxPtrsFix<FieldsEnum, 16>::MaxCount, "Should only be called on AuxPtrsFix<FieldsEnum, 16>"); |
| 67 | + this->count = AuxPtrsFix<FieldsEnum, 16>::MaxCount; |
| 68 | + for (uint8 i = 0; i < count; i++) |
| 69 | + { |
| 70 | + this->type[i] = FieldsEnum::Invalid; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + template<typename FieldsEnum, uint8 size, uint8 _MaxCount> |
| 75 | + AuxPtrsFix<FieldsEnum, size, _MaxCount>::AuxPtrsFix(AuxPtrsFix<FieldsEnum, 16>* ptr16) |
| 76 | + { |
| 77 | + static_assert(_MaxCount == AuxPtrsFix<FieldsEnum, 32>::MaxCount, "Should only be called on AuxPtrsFix<FieldsEnum, 32>"); |
| 78 | + this->count = AuxPtrsFix<FieldsEnum, 32>::MaxCount; |
| 79 | + for (uint8 i = 0; i < AuxPtrsFix<FieldsEnum, 16>::MaxCount; i++) |
| 80 | + { |
| 81 | + this->type[i] = ptr16->type[i]; |
| 82 | + this->ptr[i] = ptr16->ptr[i]; |
| 83 | + } |
| 84 | + for (uint8 i = AuxPtrsFix<FieldsEnum, 16>::MaxCount; i < count; i++) |
| 85 | + { |
| 86 | + this->type[i] = FieldsEnum::Invalid; |
| 87 | + } |
| 88 | + } |
| 89 | + template<typename FieldsEnum, uint8 size, uint8 _MaxCount> |
| 90 | + inline void* AuxPtrsFix<FieldsEnum, size, _MaxCount>::Get(FieldsEnum e) |
| 91 | + { |
| 92 | + Assert(count == _MaxCount); |
| 93 | + for (uint8 i = 0; i < _MaxCount; i++) // using _MaxCount instead of count so compiler can optimize in case _MaxCount is 1. |
| 94 | + { |
| 95 | + if (type[i] == e) |
| 96 | + { |
| 97 | + return ptr[i]; |
| 98 | + } |
| 99 | + } |
| 100 | + return nullptr; |
| 101 | + } |
| 102 | + template<typename FieldsEnum, uint8 size, uint8 _MaxCount> |
| 103 | + inline bool AuxPtrsFix<FieldsEnum, size, _MaxCount>::Set(FieldsEnum e, void* p) |
| 104 | + { |
| 105 | + Assert(count == _MaxCount); |
| 106 | + for (uint8 i = 0; i < _MaxCount; i++) |
| 107 | + { |
| 108 | + if (type[i] == e || type[i] == FieldsEnum::Invalid) |
| 109 | + { |
| 110 | + ptr[i] = p; |
| 111 | + type[i] = e; |
| 112 | + return true; |
| 113 | + } |
| 114 | + } |
| 115 | + return false; |
| 116 | + } |
| 117 | + template<class T, typename FieldsEnum> |
| 118 | + AuxPtrs<T, FieldsEnum>::AuxPtrs(uint8 capacity, AuxPtrs32* ptr32) |
| 119 | + { |
| 120 | + Assert(ptr32->count >= AuxPtrs32::MaxCount); |
| 121 | + this->count = ptr32->count; |
| 122 | + this->capacity = capacity; |
| 123 | + memset(offsets, (uint8)FieldsEnum::Invalid, (uint8)FieldsEnum::Max); |
| 124 | + for (uint8 i = 0; i < ptr32->count; i++) |
| 125 | + { |
| 126 | + offsets[(uint8)ptr32->type[i]] = (FieldsEnum)i; |
| 127 | + ptrs[i] = ptr32->ptr[i]; |
| 128 | + } |
| 129 | + } |
| 130 | + template<class T, typename FieldsEnum> |
| 131 | + AuxPtrs<T, FieldsEnum>::AuxPtrs(uint8 capacity, AuxPtrs* ptr) |
| 132 | + { |
| 133 | + memcpy(this, ptr, sizeof(AuxPtrs) + (ptr->count - 1)*sizeof(void*)); |
| 134 | + this->capacity = capacity; |
| 135 | + } |
| 136 | + template<class T, typename FieldsEnum> |
| 137 | + inline void* AuxPtrs<T, FieldsEnum>::Get(FieldsEnum e) |
| 138 | + { |
| 139 | + uint8 u = (uint8)e; |
| 140 | + return offsets[u] == FieldsEnum::Invalid ? nullptr : ptrs[(uint8)offsets[u]]; |
| 141 | + } |
| 142 | + template<class T, typename FieldsEnum> |
| 143 | + inline bool AuxPtrs<T, FieldsEnum>::Set(FieldsEnum e, void* p) |
| 144 | + { |
| 145 | + uint8 u = (uint8)e; |
| 146 | + if (offsets[u] != FieldsEnum::Invalid) |
| 147 | + { |
| 148 | + ptrs[(uint8)offsets[u]] = p; |
| 149 | + return true; |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + if (count == capacity) |
| 154 | + { |
| 155 | + // need to expand |
| 156 | + return false; |
| 157 | + } |
| 158 | + else |
| 159 | + { |
| 160 | + offsets[u] = (FieldsEnum)count++; |
| 161 | + ptrs[(uint8)offsets[u]] = p; |
| 162 | + return true; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + template<class T, typename FieldsEnum> |
| 168 | + void AuxPtrs<T, FieldsEnum>::AllocAuxPtrFix(T* _this, uint8 size, Recycler* recycler) |
| 169 | + { |
| 170 | + if (size == 16) |
| 171 | + { |
| 172 | + _this->auxPtrs = (AuxPtrs<T, FieldsEnum>*)RecyclerNewWithBarrierStructZ(recycler, AuxPtrs16); |
| 173 | + } |
| 174 | + else if (size == 32) |
| 175 | + { |
| 176 | + _this->auxPtrs = (AuxPtrs<T, FieldsEnum>*)RecyclerNewWithBarrierPlusZ(recycler, 0, AuxPtrs32, (AuxPtrs16*)(void*)_this->auxPtrs); |
| 177 | + } |
| 178 | + else |
| 179 | + { |
| 180 | + Assert(false); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + template<class T, typename FieldsEnum> |
| 185 | + void AuxPtrs<T, FieldsEnum>::AllocAuxPtr(T* _this, uint8 count, Recycler* recycler) |
| 186 | + { |
| 187 | + Assert(count >= AuxPtrs32::MaxCount); |
| 188 | + auto requestSize = sizeof(AuxPtrs<T, FieldsEnum>) + (count - 1)*sizeof(void*); |
| 189 | + auto allocSize = ::Math::Align<uint8>((uint8)requestSize, 16); |
| 190 | + auto capacity = (uint8)((allocSize - offsetof(AuxPtrsT, ptrs)) / sizeof(void*)); |
| 191 | + |
| 192 | + if (_this->auxPtrs->count != AuxPtrs32::MaxCount) // expanding |
| 193 | + { |
| 194 | + _this->auxPtrs = RecyclerNewWithBarrierPlusZ(recycler, allocSize - sizeof(AuxPtrsT), AuxPtrsT, capacity, _this->auxPtrs); |
| 195 | + } |
| 196 | + else // promoting from AuxPtrs32 |
| 197 | + { |
| 198 | + _this->auxPtrs = RecyclerNewWithBarrierPlusZ(recycler, allocSize - sizeof(AuxPtrsT), AuxPtrsT, capacity, (AuxPtrs32*)(void*)_this->auxPtrs); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + template<class T, typename FieldsEnum> |
| 203 | + inline void* AuxPtrs<T, FieldsEnum>::GetAuxPtr(const T* _this, FieldsEnum e) |
| 204 | + { |
| 205 | + if (_this->auxPtrs == nullptr) |
| 206 | + { |
| 207 | + return nullptr; |
| 208 | + } |
| 209 | + if (_this->auxPtrs->count == AuxPtrs16::MaxCount) |
| 210 | + { |
| 211 | + return ((AuxPtrs16*)(void*)_this->auxPtrs)->Get(e); |
| 212 | + } |
| 213 | + if (_this->auxPtrs->count == AuxPtrs32::MaxCount) |
| 214 | + { |
| 215 | + return ((AuxPtrs32*)(void*)_this->auxPtrs)->Get(e); |
| 216 | + } |
| 217 | + return _this->auxPtrs->Get(e); |
| 218 | + } |
| 219 | + template<class T, typename FieldsEnum> |
| 220 | + void AuxPtrs<T, FieldsEnum>::SetAuxPtr(T* _this, FieldsEnum e, void* ptr, Recycler* recycler) |
| 221 | + { |
| 222 | + if (ptr == nullptr && GetAuxPtr(_this, e) == nullptr) |
| 223 | + { |
| 224 | + return; |
| 225 | + } |
| 226 | + if (_this->auxPtrs == nullptr) |
| 227 | + { |
| 228 | + AuxPtrs<FunctionProxy, FieldsEnum>::AllocAuxPtrFix(_this, 16, recycler); |
| 229 | + bool ret = ((AuxPtrs16*)(void*)_this->auxPtrs)->Set(e, ptr); |
| 230 | + Assert(ret); |
| 231 | + return; |
| 232 | + } |
| 233 | + if (_this->auxPtrs->count == AuxPtrs16::MaxCount) |
| 234 | + { |
| 235 | + bool ret = ((AuxPtrs16*)(void*)_this->auxPtrs)->Set(e, ptr); |
| 236 | + if (ret) |
| 237 | + { |
| 238 | + return; |
| 239 | + } |
| 240 | + else |
| 241 | + { |
| 242 | + AuxPtrs<FunctionProxy, FieldsEnum>::AllocAuxPtrFix(_this, 32, recycler); |
| 243 | + } |
| 244 | + } |
| 245 | + if (_this->auxPtrs->count == AuxPtrs32::MaxCount) |
| 246 | + { |
| 247 | + bool ret = ((AuxPtrs32*)(void*)_this->auxPtrs)->Set(e, ptr); |
| 248 | + if (ret) |
| 249 | + { |
| 250 | + return; |
| 251 | + } |
| 252 | + else |
| 253 | + { |
| 254 | + AuxPtrs<FunctionProxy, FieldsEnum>::AllocAuxPtr(_this, AuxPtrs32::MaxCount + 1, recycler); |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + bool ret = _this->auxPtrs->Set(e, ptr); |
| 259 | + if (!ret) |
| 260 | + { |
| 261 | + AuxPtrs<FunctionProxy, FieldsEnum>::AllocAuxPtr(_this, _this->auxPtrs->count + 1, recycler); |
| 262 | + ret = _this->auxPtrs->Set(e, ptr); |
| 263 | + Assert(ret); |
| 264 | + } |
| 265 | + } |
| 266 | +} |
0 commit comments