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_SET_H 17 #define ECMASCRIPT_MEM_FREE_OBJECT_SET_H 18 19 #include <cstdint> 20 21 #include "libpandabase/macros.h" 22 #include "ecmascript/mem/jit_fort_memdesc.h" 23 24 namespace panda::ecmascript { 25 using SetType = int8_t; 26 27 class FreeObject; 28 29 template <typename T> 30 class FreeObjectList; 31 32 template <typename T> 33 class FreeObjectSet { 34 public: FreeObjectSet(SetType type)35 explicit FreeObjectSet(SetType type) : setType_(type) 36 { 37 Rebuild(); 38 } FreeObjectSet(SetType type,MemDescPool * pool)39 explicit FreeObjectSet(SetType type, MemDescPool *pool) : setType_(type), memDescPool_(pool) 40 { 41 Rebuild(); 42 } 43 ~FreeObjectSet() = default; 44 Empty()45 inline bool Empty() const 46 { 47 return available_ == 0; 48 } 49 Available()50 inline size_t Available() const 51 { 52 return available_; 53 } 54 MaxAvailableFreeSize()55 inline size_t MaxAvailableFreeSize() const 56 { 57 return maxAvailableFreeSize_; 58 } 59 60 void Free(uintptr_t begin, size_t size); 61 62 void Rebuild(); 63 64 T *LookupSmallFreeObject(size_t size); 65 T *LookupLargeFreeObject(size_t size); 66 T *ObtainSmallFreeObject(size_t size); 67 T *ObtainLargeFreeObject(size_t size); 68 69 NO_COPY_SEMANTIC(FreeObjectSet); 70 NO_MOVE_SEMANTIC(FreeObjectSet); 71 72 static constexpr SetType INVALID_SET_TYPE = -1; 73 74 private: 75 FreeObjectSet *next_ = nullptr; 76 FreeObjectSet *prev_ = nullptr; 77 SetType setType_ = INVALID_SET_TYPE; 78 size_t available_ = 0; 79 size_t maxAvailableFreeSize_ = 0; 80 uint64_t isAdded_ = 0; // 0: not added, 1: is added 81 T *freeObject_ = nullptr; 82 MemDescPool *memDescPool_ {nullptr}; 83 friend class FreeObjectList<T>; 84 }; 85 } // namespace panda::ecmascript 86 #endif // ECMASCRIPT_MEM_FREE_OBJECT_SET_H 87