1 /* 2 * Copyright (c) 2023 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 OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_RESOURCE_MANAGER_H 17 #define OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_RESOURCE_MANAGER_H 18 19 #include <list> 20 #include <mutex> 21 #include <string> 22 #include <unordered_map> 23 24 #include "ffrt.h" 25 26 namespace OHOS { 27 namespace PurgeableMem { 28 const std::string THREAD_POOL_NAME = "PurgeThread"; 29 class PurgeableMemBase; 30 31 class PurgeableResourceManager { 32 public: 33 PurgeableResourceManager(const PurgeableResourceManager&) = delete; 34 PurgeableResourceManager& operator=(const PurgeableResourceManager&) = delete; 35 ~PurgeableResourceManager(); 36 37 static PurgeableResourceManager &GetInstance(); 38 void BeginAccessPurgeableMem(); 39 void EndAccessPurgeableMem(); 40 void AddResource(std::shared_ptr<PurgeableMemBase> resourcePtr); 41 void RemoveResource(std::shared_ptr<PurgeableMemBase> resourcePtr); 42 void SetRecentUsedResource(std::shared_ptr<PurgeableMemBase> resourcePtr); 43 void SetLruCacheCapacity(int32_t capacity); 44 void Clear(); 45 void RemoveLastResource(); 46 void ShowLruCache() const; 47 48 private: 49 PurgeableResourceManager(); 50 int32_t GetThreadPoolTaskNumFromSysPara() const; 51 int32_t GetLruCacheCapacityFromSysPara() const; 52 void ChangeDataValid(std::shared_ptr<PurgeableMemBase> resourcePtr, bool isVaild) const; 53 void AddResourceInner(std::shared_ptr<PurgeableMemBase> resourcePtr); 54 void RemoveResourceInner(std::shared_ptr<PurgeableMemBase> resourcePtr); 55 class LruCache { 56 public: 57 /* 58 * Visited: visit the cache entry with the given key. 59 * If the entry is found, it will be move to the most-recent position in the cache. 60 */ 61 void Visited(std::shared_ptr<PurgeableMemBase> key); 62 63 /* 64 * Insert: insert the PurgeableMemBase key in the lrucache. 65 * Input: @key: ptr of PurgeableMemBase. 66 */ 67 void Insert(std::shared_ptr<PurgeableMemBase> key); 68 69 /* 70 * Erase: erase the PurgeableMemBase key in the lrucache. 71 * Input: @key: ptr of PurgeableMemBase. 72 */ 73 void Erase(std::shared_ptr<PurgeableMemBase> key); 74 75 /* 76 * SetCapacity: set the capacity of the lrucache. 77 * Input: the capacity of lrucache. 78 */ 79 void SetCapacity(int32_t capacity); 80 81 /* 82 * Clear: clear the resourcePtrList and positionMap of the lrucache. 83 */ 84 void Clear(); 85 86 using ListSharedPtrIterator = std::list<std::shared_ptr<PurgeableMemBase>>::iterator; 87 std::list<std::shared_ptr<PurgeableMemBase>> GetResourcePtrList() const; 88 std::shared_ptr<PurgeableMemBase> GetLastResourcePtr() const; 89 size_t Size() const; 90 91 private: 92 int32_t lruCacheCapacity_; 93 std::list<std::shared_ptr<PurgeableMemBase>> resourcePtrList_; 94 std::unordered_map<std::shared_ptr<PurgeableMemBase>, ListSharedPtrIterator> positionMap_; 95 }; 96 97 mutable ffrt::mutex lruCacheMutex_; 98 LruCache lruCache_; 99 }; 100 } /* namespace PurgeableMem */ 101 } /* namespace OHOS */ 102 #endif /* OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_RESOURCE_MANAGER_H */