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_MEM_BASE_H 17 #define OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_MEM_BASE_H 18 19 #include <memory> /* unique_ptr */ 20 #include <shared_mutex> /* shared_mutex */ 21 #include <string> 22 23 #include "purgeable_mem_builder.h" 24 #include "purgeable_resource_manager.h" 25 #include "ux_page_table.h" 26 #include "ffrt.h" 27 28 namespace OHOS { 29 namespace PurgeableMem { 30 class PurgeableMemBase { 31 public: 32 /* 33 * BeginRead: begin read the PurgeableMem obj. 34 * Return: return true if the obj's content is present. 35 * If content is purged(no present), system will recover its data, 36 * return false if content is purged and recover failed. 37 * While return true if content recover success. 38 * OS cannot reclaim the memory of the obj's content when this 39 * function return true, until EndRead() is called. 40 */ 41 bool BeginRead(); 42 43 /* 44 * EndRead: end read the PurgeableMem obj. 45 * OS may reclaim the memory of its content 46 * at a later time when this function returns. 47 */ 48 void EndRead(); 49 50 /* 51 * BeginRead: begin read the PurgeableMem obj. 52 * Return: return true if the obj's content is present. 53 * If content is purged(no present), system will recover its data, 54 * return false if content is purged and recover failed. 55 * While return true if content recover success. 56 * OS cannot reclaim the memory of the obj's content when this 57 * function return true, until EndRead() is called. 58 */ 59 60 bool BeginWrite(); 61 62 /* 63 * EndWrite: end write the PurgeableMem obj. 64 * OS may reclaim the memory of its content 65 * at a later time when this function returns. 66 */ 67 void EndWrite(); 68 69 /* 70 * ModifyContentByBuilder: append a PurgeableMemBuilder obj to the PurgeableMem obj. 71 * Input: @modifier: unique_ptr of PurgeableMemBuilder, it will modify content of this obj. 72 * Return: modify result, true is success, while false is fail. 73 * This function should be protected by BeginWrite()/EndWrite(). 74 */ 75 bool ModifyContentByBuilder(std::unique_ptr<PurgeableMemBuilder> modifier); 76 77 /* 78 * GetContent: get content ptr of the PurgeableMem obj. 79 * Return: return the content ptr, which is start address of the obj's content. 80 * This function should be protected by BeginRead()/EndRead() 81 * or BeginWrite()/EndWrite(). 82 */ 83 void *GetContent(); 84 85 /* 86 * GetContentSize: get content size of the PurgeableMem obj. 87 * Return: return content size of the obj's content. 88 */ 89 size_t GetContentSize(); 90 91 /* 92 * ResizeData: resize size of the PurgeableMem obj. 93 */ 94 virtual void ResizeData(size_t newSize); 95 void SetRebuildSuccessCallback(std::function<void()> &callback); 96 bool IsDataValid(); 97 void SetDataValid(bool target); 98 bool BeginReadWithDataLock(); 99 void EndReadWithDataLock(); 100 101 PurgeableMemBase(); 102 virtual ~PurgeableMemBase(); 103 PurgeableMemBase(const PurgeableMemBase&) = delete; 104 PurgeableMemBase& operator = (PurgeableMemBase&) = delete; 105 PurgeableMemBase(PurgeableMemBase&&) noexcept = delete; 106 PurgeableMemBase& operator = (PurgeableMemBase&&) noexcept = delete; 107 108 protected: 109 void *dataPtr_ = nullptr; 110 ffrt::mutex dataLock_; 111 bool isDataValid_ {true}; 112 size_t dataSizeInput_ = 0; 113 std::unique_ptr<PurgeableMemBuilder> builder_ = nullptr; 114 std::shared_mutex rwlock_; 115 unsigned int buildDataCount_ = 0; 116 bool BuildContent_(); 117 bool IfNeedRebuild_(); 118 virtual bool Pin(); 119 virtual bool Unpin(); 120 virtual bool IsPurged(); 121 virtual int GetPinStatus() const; 122 virtual void AfterRebuildSucc(); 123 virtual std::string ToString() const; 124 friend class PurgeableResourceManager; 125 }; 126 } /* namespace PurgeableMem */ 127 } /* namespace OHOS */ 128 #endif /* OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_MEM_BASE_H */ 129