14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_FREE_OBJECT_LIST_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_FREE_OBJECT_LIST_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include <cstddef> 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ci#include "ecmascript/mem/free_object_set.h" 224514f5e3Sopenharmony_ci#include "ecmascript/mem/mem_common.h" 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_ci#include "libpandabase/utils/span.h" 254514f5e3Sopenharmony_ci 264514f5e3Sopenharmony_cinamespace panda::ecmascript { 274514f5e3Sopenharmony_ciclass JitFort; 284514f5e3Sopenharmony_citemplate <typename T> 294514f5e3Sopenharmony_ciclass FreeObjectList { 304514f5e3Sopenharmony_cipublic: 314514f5e3Sopenharmony_ci FreeObjectList(JitFort* fort = nullptr); 324514f5e3Sopenharmony_ci ~FreeObjectList(); 334514f5e3Sopenharmony_ci 344514f5e3Sopenharmony_ci T *Allocate(size_t size); 354514f5e3Sopenharmony_ci 364514f5e3Sopenharmony_ci T *LookupSuitableFreeObject(size_t size); 374514f5e3Sopenharmony_ci 384514f5e3Sopenharmony_ci void Free(uintptr_t start, size_t size, bool isAdd = true); 394514f5e3Sopenharmony_ci 404514f5e3Sopenharmony_ci void Rebuild(); 414514f5e3Sopenharmony_ci 424514f5e3Sopenharmony_ci bool MatchFreeObjectInSet(FreeObjectSet<T> *set, size_t size); 434514f5e3Sopenharmony_ci 444514f5e3Sopenharmony_ci bool AddSet(FreeObjectSet<T> *set); 454514f5e3Sopenharmony_ci 464514f5e3Sopenharmony_ci void RemoveSet(FreeObjectSet<T> *set); 474514f5e3Sopenharmony_ci void Merge(FreeObjectList *list); 484514f5e3Sopenharmony_ci 494514f5e3Sopenharmony_ci template<class Callback> 504514f5e3Sopenharmony_ci void EnumerateSets(const Callback &cb) const; 514514f5e3Sopenharmony_ci 524514f5e3Sopenharmony_ci template<class Callback> 534514f5e3Sopenharmony_ci void EnumerateSets(SetType type, const Callback &cb) const; 544514f5e3Sopenharmony_ci 554514f5e3Sopenharmony_ci template<class Callback> 564514f5e3Sopenharmony_ci void EnumerateTopAndLastSets(const Callback &cb) const; 574514f5e3Sopenharmony_ci 584514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(FreeObjectList); 594514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(FreeObjectList); 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci size_t GetFreeObjectSize() const 624514f5e3Sopenharmony_ci { 634514f5e3Sopenharmony_ci return available_; 644514f5e3Sopenharmony_ci } 654514f5e3Sopenharmony_ci size_t GetWastedSize() const 664514f5e3Sopenharmony_ci { 674514f5e3Sopenharmony_ci return wasted_; 684514f5e3Sopenharmony_ci } 694514f5e3Sopenharmony_ci void DecreaseWastedSize(size_t size) 704514f5e3Sopenharmony_ci { 714514f5e3Sopenharmony_ci wasted_ -= size; 724514f5e3Sopenharmony_ci } 734514f5e3Sopenharmony_ci void IncreaseWastedSize(size_t size) 744514f5e3Sopenharmony_ci { 754514f5e3Sopenharmony_ci wasted_ += size; 764514f5e3Sopenharmony_ci } 774514f5e3Sopenharmony_ci 784514f5e3Sopenharmony_ci static int NumberOfSets() 794514f5e3Sopenharmony_ci { 804514f5e3Sopenharmony_ci return NUMBER_OF_SETS; 814514f5e3Sopenharmony_ci } 824514f5e3Sopenharmony_ci 834514f5e3Sopenharmony_ci template<typename U> 844514f5e3Sopenharmony_ci void FreeImpl(U* region, uintptr_t start, size_t size, bool isAdd); 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_ciprivate: 874514f5e3Sopenharmony_ci static constexpr int NUMBER_OF_SETS = 39; 884514f5e3Sopenharmony_ci static constexpr size_t MIN_SIZE = 16; 894514f5e3Sopenharmony_ci static constexpr size_t SMALL_SET_MAX_SIZE = 256; 904514f5e3Sopenharmony_ci static constexpr size_t LARGE_SET_MAX_SIZE = 65536; 914514f5e3Sopenharmony_ci static constexpr size_t HUGE_SET_MAX_SIZE = 255_KB; 924514f5e3Sopenharmony_ci static constexpr int SMALL_SET_MAX_INDEX = 29; 934514f5e3Sopenharmony_ci static constexpr int NUMBER_OF_LAST_LARGE = NUMBER_OF_SETS - 2; 944514f5e3Sopenharmony_ci static constexpr int NUMBER_OF_LAST_HUGE = NUMBER_OF_SETS - 1; 954514f5e3Sopenharmony_ci static constexpr size_t INTERVAL_OFFSET = 3; 964514f5e3Sopenharmony_ci static constexpr size_t LOG2_OFFSET = 21; 974514f5e3Sopenharmony_ci static constexpr size_t MAX_BIT_OF_SIZET = sizeof(size_t) << INTERVAL_OFFSET; 984514f5e3Sopenharmony_ci const int smallSetOffsetIndex = 2; 994514f5e3Sopenharmony_ci 1004514f5e3Sopenharmony_ci inline SetType SelectSetType(size_t size) const 1014514f5e3Sopenharmony_ci { 1024514f5e3Sopenharmony_ci if (size < SMALL_SET_MAX_SIZE) { 1034514f5e3Sopenharmony_ci if (UNLIKELY(size < MIN_SIZE)) { 1044514f5e3Sopenharmony_ci return FreeObjectSet<T>::INVALID_SET_TYPE; 1054514f5e3Sopenharmony_ci } 1064514f5e3Sopenharmony_ci return (size >> INTERVAL_OFFSET) - smallSetOffsetIndex; 1074514f5e3Sopenharmony_ci } 1084514f5e3Sopenharmony_ci if (size < LARGE_SET_MAX_SIZE) { 1094514f5e3Sopenharmony_ci#ifdef PANDA_TARGET_64 1104514f5e3Sopenharmony_ci return MAX_BIT_OF_SIZET - __builtin_clzll(size) + LOG2_OFFSET; 1114514f5e3Sopenharmony_ci#else 1124514f5e3Sopenharmony_ci return MAX_BIT_OF_SIZET - __builtin_clzl(size) + LOG2_OFFSET; 1134514f5e3Sopenharmony_ci#endif 1144514f5e3Sopenharmony_ci } 1154514f5e3Sopenharmony_ci if (size >= HUGE_SET_MAX_SIZE) { 1164514f5e3Sopenharmony_ci return NUMBER_OF_LAST_HUGE; 1174514f5e3Sopenharmony_ci } 1184514f5e3Sopenharmony_ci 1194514f5e3Sopenharmony_ci return NUMBER_OF_LAST_LARGE; 1204514f5e3Sopenharmony_ci } 1214514f5e3Sopenharmony_ci inline void SetNoneEmptyBit(SetType type) 1224514f5e3Sopenharmony_ci { 1234514f5e3Sopenharmony_ci noneEmptySetBitMap_ |= 1ULL << static_cast<uint32_t>(type); 1244514f5e3Sopenharmony_ci } 1254514f5e3Sopenharmony_ci inline void ClearNoneEmptyBit(SetType type) 1264514f5e3Sopenharmony_ci { 1274514f5e3Sopenharmony_ci noneEmptySetBitMap_ &= ~(1ULL << static_cast<uint32_t>(type)); 1284514f5e3Sopenharmony_ci } 1294514f5e3Sopenharmony_ci inline size_t CalcNextNoneEmptyIndex(SetType start) 1304514f5e3Sopenharmony_ci { 1314514f5e3Sopenharmony_ci return __builtin_ffsll(noneEmptySetBitMap_ >> static_cast<uint32_t>(start)) + start - 1; 1324514f5e3Sopenharmony_ci } 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ci size_t available_ = 0; 1354514f5e3Sopenharmony_ci size_t wasted_ = 0; 1364514f5e3Sopenharmony_ci uint64_t noneEmptySetBitMap_ = 0; 1374514f5e3Sopenharmony_ci Span<FreeObjectSet<T> *> sets_ {}; 1384514f5e3Sopenharmony_ci Span<FreeObjectSet<T> *> lastSets_ {}; 1394514f5e3Sopenharmony_ci JitFort *jitFort_ {nullptr}; 1404514f5e3Sopenharmony_ci}; 1414514f5e3Sopenharmony_ci} // namespace panda::ecmascript 1424514f5e3Sopenharmony_ci#endif // ECMASCRIPT_MEM_FREE_OBJECT_LIST_H 143