1 /* 2 * Copyright (c) 2022 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_H 17 #define OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_MEM_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 "ux_page_table.h" 25 26 namespace OHOS { 27 namespace PurgeableMem { 28 class PurgeableMem { 29 public: 30 /* 31 * Constructor of class PurgeableMem. 32 * Input: @dataSize: data size of this PurgeableMem obj's data content. 33 * Input: @builder: using @builder to recover user data when this obj's content is purged. 34 * @builder should be a unique_ptr to avolid memory manage chaos(memory leak). 35 */ 36 PurgeableMem(size_t dataSize, std::unique_ptr<PurgeableMemBuilder> builder); 37 38 /* 39 * Destructor of class PurgeableMem. 40 * It will free the memory of builders associated with this PurgeableMem obj. 41 */ 42 ~PurgeableMem(); 43 44 /* 45 * BeginRead: begin read the PurgeableMem obj. 46 * Return: return true if the obj's content is present. 47 * If content is purged(no present), system will recover its data, 48 * return false if content is purged and recover failed. 49 * While return true if content recover success. 50 * OS cannot reclaim the memory of the obj's content when this 51 * function return true, until EndRead() is called. 52 */ 53 bool BeginRead(); 54 55 /* 56 * EndRead: end read the PurgeableMem obj. 57 * OS may reclaim the memory of its content 58 * at a later time when this function returns. 59 */ 60 void EndRead(); 61 62 /* 63 * BeginWrite: begin write the PurgeableMem obj. 64 * Return: return true if the obj's content is present. 65 * if content is purged(no present), system will recover its data, 66 * return false if content is purged and recover failed. 67 * While return true if content recover success. 68 * OS cannot reclaim the memory of the obj's content when this 69 * function return true, until EndWrite() is called. 70 */ 71 bool BeginWrite(); 72 73 /* 74 * EndWrite: end write the PurgeableMem obj. 75 * OS may reclaim the memory of its content 76 * at a later time when this function returns. 77 */ 78 void EndWrite(); 79 80 /* 81 * GetContent: get content ptr of the PurgeableMem obj. 82 * Return: return the content ptr, which is start address of the obj's content. 83 * This function should be protected by BeginRead()/EndRead() 84 * or BeginWrite()/EndWrite(). 85 */ 86 void *GetContent(); 87 88 /* 89 * GetContentSize: get content size of the PurgeableMem obj. 90 * Return: return content size of the obj's content. 91 */ 92 size_t GetContentSize(); 93 94 /* 95 * ModifyContentByBuilder: append a PurgeableMemBuilder obj to the PurgeableMem obj. 96 * Input: @modifier: unique_ptr of PurgeableMemBuilder, it will modify content of this obj. 97 * Return: modify result, true is success, while false is fail. 98 * This function should be protected by BeginWrite()/EndWrite(). 99 */ 100 bool ModifyContentByBuilder(std::unique_ptr<PurgeableMemBuilder> modifier); 101 102 private: 103 void *dataPtr_ = nullptr; 104 size_t dataSizeInput_; 105 std::unique_ptr<PurgeableMemBuilder> builder_ = nullptr; 106 std::unique_ptr<UxPageTable> pageTable_ = nullptr; 107 std::shared_mutex rwlock_; 108 unsigned int buildDataCount_; 109 110 bool IsPurged_(); 111 bool BuildContent_(); 112 std::string ToString_() const; 113 }; 114 } /* namespace PurgeableMem */ 115 } /* namespace OHOS */ 116 #endif /* OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_MEM_H */ 117