1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_MEM_FREE_OBJECT_LIST_H 17 #define ECMASCRIPT_MEM_FREE_OBJECT_LIST_H 18 19 #include <cstddef> 20 21 #include "ecmascript/mem/free_object_set.h" 22 #include "utils/span.h" 23 24 namespace panda::ecmascript { 25 class FreeObjectList { 26 public: 27 FreeObjectList(); 28 ~FreeObjectList(); 29 30 FreeObject *Allocate(size_t size); 31 32 FreeObject *LookupSuitableFreeObject(size_t size); 33 34 void Free(uintptr_t start, size_t size, bool isAdd = true); 35 36 void Rebuild(); 37 38 bool AddSet(FreeObjectSet *set); 39 40 void RemoveSet(FreeObjectSet *set); 41 42 void Merge(FreeObjectList *list); 43 44 template<class Callback> 45 void EnumerateSets(const Callback &cb) const; 46 47 template<class Callback> 48 void EnumerateSets(SetType type, const Callback &cb) const; 49 50 template<class Callback> 51 void EnumerateTopAndLastSets(const Callback &cb) const; 52 53 NO_COPY_SEMANTIC(FreeObjectList); 54 NO_MOVE_SEMANTIC(FreeObjectList); 55 GetFreeObjectSize()56 size_t GetFreeObjectSize() const 57 { 58 return available_; 59 } GetWastedSize()60 size_t GetWastedSize() const 61 { 62 return wasted_; 63 } DecrementWastedSize(size_t size)64 void DecrementWastedSize(size_t size) 65 { 66 wasted_ -= size; 67 } IncrementWastedSize(size_t size)68 void IncrementWastedSize(size_t size) 69 { 70 wasted_ += size; 71 } 72 NumberOfSets()73 static int NumberOfSets() 74 { 75 return NUMBER_OF_SETS; 76 } 77 78 private: 79 static constexpr int NUMBER_OF_SETS = 39; 80 static constexpr size_t MIN_SIZE = 16; 81 static constexpr size_t SMALL_SET_MAX_SIZE = 256; 82 static constexpr size_t LARGE_SET_MAX_SIZE = 65536; 83 static constexpr size_t HUGE_SET_MAX_SIZE = 255 * 1024; 84 static constexpr int SMALL_SET_MAX_INDEX = 29; 85 static constexpr int NUMBER_OF_LAST_LARGE = NUMBER_OF_SETS - 2; 86 static constexpr int NUMBER_OF_LAST_HUGE = NUMBER_OF_SETS - 1; 87 static constexpr size_t INTERVAL_OFFSET = 3; 88 static constexpr size_t LOG2_OFFSET = 21; 89 static constexpr size_t MAX_BIT_OF_SIZET = sizeof(size_t) << INTERVAL_OFFSET; 90 const int smallSetOffsetIndex = 2; 91 92 inline SetType SelectSetType(size_t size) const; 93 94 inline void SetNoneEmptyBit(SetType type); 95 inline void ClearNoneEmptyBit(SetType type); 96 inline size_t CalcNextNoneEmptyIndex(SetType start); 97 98 size_t available_ = 0; 99 size_t wasted_ = 0; 100 uint64_t noneEmptySetBitMap_; 101 Span<FreeObjectSet *> sets_ {}; 102 Span<FreeObjectSet *> lastSets_ {}; 103 }; 104 } // namespace panda::ecmascript 105 #endif // ECMASCRIPT_MEM_FREE_OBJECT_LIST_H 106