1f0bfeaa8Sopenharmony_ci/* 2f0bfeaa8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3f0bfeaa8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f0bfeaa8Sopenharmony_ci * you may not use this file except in compliance with the License. 5f0bfeaa8Sopenharmony_ci * You may obtain a copy of the License at 6f0bfeaa8Sopenharmony_ci * 7f0bfeaa8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f0bfeaa8Sopenharmony_ci * 9f0bfeaa8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f0bfeaa8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f0bfeaa8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f0bfeaa8Sopenharmony_ci * See the License for the specific language governing permissions and 13f0bfeaa8Sopenharmony_ci * limitations under the License. 14f0bfeaa8Sopenharmony_ci */ 15f0bfeaa8Sopenharmony_ci 16f0bfeaa8Sopenharmony_ci#ifndef OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_MEM_BASE_H 17f0bfeaa8Sopenharmony_ci#define OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_MEM_BASE_H 18f0bfeaa8Sopenharmony_ci 19f0bfeaa8Sopenharmony_ci#ifndef OHOS_MAXIMUM_PURGEABLE_MEMORY 20f0bfeaa8Sopenharmony_ci#define OHOS_MAXIMUM_PURGEABLE_MEMORY ((1024) * (1024) * (1024)) /* 1G */ 21f0bfeaa8Sopenharmony_ci#endif /* OHOS_MAXIMUM_PURGEABLE_MEMORY */ 22f0bfeaa8Sopenharmony_ci 23f0bfeaa8Sopenharmony_ci#include <memory> /* unique_ptr */ 24f0bfeaa8Sopenharmony_ci#include <shared_mutex> /* shared_mutex */ 25f0bfeaa8Sopenharmony_ci#include <string> 26f0bfeaa8Sopenharmony_ci 27f0bfeaa8Sopenharmony_ci#include "purgeable_mem_builder.h" 28f0bfeaa8Sopenharmony_ci#include "ux_page_table.h" 29f0bfeaa8Sopenharmony_ci 30f0bfeaa8Sopenharmony_cinamespace OHOS { 31f0bfeaa8Sopenharmony_cinamespace PurgeableMem { 32f0bfeaa8Sopenharmony_ciclass PurgeableMemBase { 33f0bfeaa8Sopenharmony_cipublic: 34f0bfeaa8Sopenharmony_ci /* 35f0bfeaa8Sopenharmony_ci * BeginRead: begin read the PurgeableMem obj. 36f0bfeaa8Sopenharmony_ci * Return: return true if the obj's content is present. 37f0bfeaa8Sopenharmony_ci * If content is purged(no present), system will recover its data, 38f0bfeaa8Sopenharmony_ci * return false if content is purged and recover failed. 39f0bfeaa8Sopenharmony_ci * While return true if content recover success. 40f0bfeaa8Sopenharmony_ci * OS cannot reclaim the memory of the obj's content when this 41f0bfeaa8Sopenharmony_ci * function return true, until EndRead() is called. 42f0bfeaa8Sopenharmony_ci * 43f0bfeaa8Sopenharmony_ci * Attension: the return value must be recevied and handled, 44f0bfeaa8Sopenharmony_ci * since the visiting of this object with the failure result 45f0bfeaa8Sopenharmony_ci * will cause unsuspected result. 46f0bfeaa8Sopenharmony_ci * For example: 47f0bfeaa8Sopenharmony_ci * if (BeginRead()) { 48f0bfeaa8Sopenharmony_ci * // visit this object 49f0bfeaa8Sopenharmony_ci * EndRead(); 50f0bfeaa8Sopenharmony_ci * } 51f0bfeaa8Sopenharmony_ci */ 52f0bfeaa8Sopenharmony_ci bool BeginRead(); 53f0bfeaa8Sopenharmony_ci 54f0bfeaa8Sopenharmony_ci /* 55f0bfeaa8Sopenharmony_ci * EndRead: end read the PurgeableMem obj. 56f0bfeaa8Sopenharmony_ci * OS may reclaim the memory of its content 57f0bfeaa8Sopenharmony_ci * at a later time when this function returns. 58f0bfeaa8Sopenharmony_ci */ 59f0bfeaa8Sopenharmony_ci void EndRead(); 60f0bfeaa8Sopenharmony_ci 61f0bfeaa8Sopenharmony_ci /* 62f0bfeaa8Sopenharmony_ci * BeginRead: begin write the PurgeableMem obj. 63f0bfeaa8Sopenharmony_ci * Return: return true if the obj's content is present. 64f0bfeaa8Sopenharmony_ci * If content is purged(no present), system will recover its data, 65f0bfeaa8Sopenharmony_ci * return false if content is purged and recover failed. 66f0bfeaa8Sopenharmony_ci * While return true if content recover success. 67f0bfeaa8Sopenharmony_ci * OS cannot reclaim the memory of the obj's content when this 68f0bfeaa8Sopenharmony_ci * function return true, until EndWrite() is called. 69f0bfeaa8Sopenharmony_ci * 70f0bfeaa8Sopenharmony_ci * Attension: the return value must be recevied and handled, 71f0bfeaa8Sopenharmony_ci * since the visiting of this object with the failure result 72f0bfeaa8Sopenharmony_ci * will cause unsuspected result. 73f0bfeaa8Sopenharmony_ci * For example: 74f0bfeaa8Sopenharmony_ci * if (BeginWrite()) { 75f0bfeaa8Sopenharmony_ci * // visit this object 76f0bfeaa8Sopenharmony_ci * EndWrite(); 77f0bfeaa8Sopenharmony_ci * } 78f0bfeaa8Sopenharmony_ci */ 79f0bfeaa8Sopenharmony_ci 80f0bfeaa8Sopenharmony_ci bool BeginWrite(); 81f0bfeaa8Sopenharmony_ci 82f0bfeaa8Sopenharmony_ci /* 83f0bfeaa8Sopenharmony_ci * EndWrite: end write the PurgeableMem obj. 84f0bfeaa8Sopenharmony_ci * OS may reclaim the memory of its content 85f0bfeaa8Sopenharmony_ci * at a later time when this function returns. 86f0bfeaa8Sopenharmony_ci */ 87f0bfeaa8Sopenharmony_ci void EndWrite(); 88f0bfeaa8Sopenharmony_ci 89f0bfeaa8Sopenharmony_ci /* 90f0bfeaa8Sopenharmony_ci * ModifyContentByBuilder: append a PurgeableMemBuilder obj to the PurgeableMem obj. 91f0bfeaa8Sopenharmony_ci * Input: @modifier: unique_ptr of PurgeableMemBuilder, it will modify content of this obj. 92f0bfeaa8Sopenharmony_ci * Return: modify result, true is success, while false is fail. 93f0bfeaa8Sopenharmony_ci * This function should be protected by BeginWrite()/EndWrite(). 94f0bfeaa8Sopenharmony_ci */ 95f0bfeaa8Sopenharmony_ci bool ModifyContentByBuilder(std::unique_ptr<PurgeableMemBuilder> modifier); 96f0bfeaa8Sopenharmony_ci 97f0bfeaa8Sopenharmony_ci /* 98f0bfeaa8Sopenharmony_ci * GetContent: get content ptr of the PurgeableMem obj. 99f0bfeaa8Sopenharmony_ci * Return: return the content ptr, which is start address of the obj's content. 100f0bfeaa8Sopenharmony_ci * This function should be protected by BeginRead()/EndRead() 101f0bfeaa8Sopenharmony_ci * or BeginWrite()/EndWrite(). 102f0bfeaa8Sopenharmony_ci */ 103f0bfeaa8Sopenharmony_ci void *GetContent(); 104f0bfeaa8Sopenharmony_ci 105f0bfeaa8Sopenharmony_ci /* 106f0bfeaa8Sopenharmony_ci * GetContentSize: get content size of the PurgeableMem obj. 107f0bfeaa8Sopenharmony_ci * Return: return content size of the obj's content. 108f0bfeaa8Sopenharmony_ci */ 109f0bfeaa8Sopenharmony_ci size_t GetContentSize(); 110f0bfeaa8Sopenharmony_ci 111f0bfeaa8Sopenharmony_ci /* 112f0bfeaa8Sopenharmony_ci * ResizeData: resize size of the PurgeableMem obj. 113f0bfeaa8Sopenharmony_ci */ 114f0bfeaa8Sopenharmony_ci virtual void ResizeData(size_t newSize); 115f0bfeaa8Sopenharmony_ci void SetRebuildSuccessCallback(std::function<void()> &callback); 116f0bfeaa8Sopenharmony_ci bool IsDataValid(); 117f0bfeaa8Sopenharmony_ci void SetDataValid(bool target); 118f0bfeaa8Sopenharmony_ci 119f0bfeaa8Sopenharmony_ci PurgeableMemBase(); 120f0bfeaa8Sopenharmony_ci virtual ~PurgeableMemBase(); 121f0bfeaa8Sopenharmony_ci PurgeableMemBase(const PurgeableMemBase&) = delete; 122f0bfeaa8Sopenharmony_ci PurgeableMemBase& operator = (PurgeableMemBase&) = delete; 123f0bfeaa8Sopenharmony_ci PurgeableMemBase(PurgeableMemBase&&) noexcept = delete; 124f0bfeaa8Sopenharmony_ci PurgeableMemBase& operator = (PurgeableMemBase&&) noexcept = delete; 125f0bfeaa8Sopenharmony_ci virtual int GetPinStatus() const; 126f0bfeaa8Sopenharmony_ci virtual bool Pin(); 127f0bfeaa8Sopenharmony_ci 128f0bfeaa8Sopenharmony_ciprotected: 129f0bfeaa8Sopenharmony_ci void *dataPtr_ = nullptr; 130f0bfeaa8Sopenharmony_ci std::mutex dataLock_; 131f0bfeaa8Sopenharmony_ci bool isDataValid_ {true}; 132f0bfeaa8Sopenharmony_ci size_t dataSizeInput_ = 0; 133f0bfeaa8Sopenharmony_ci std::unique_ptr<PurgeableMemBuilder> builder_ = nullptr; 134f0bfeaa8Sopenharmony_ci unsigned int buildDataCount_ = 0; 135f0bfeaa8Sopenharmony_ci bool BuildContent(); 136f0bfeaa8Sopenharmony_ci bool IfNeedRebuild(); 137f0bfeaa8Sopenharmony_ci virtual bool Unpin(); 138f0bfeaa8Sopenharmony_ci virtual bool IsPurged(); 139f0bfeaa8Sopenharmony_ci virtual void AfterRebuildSucc(); 140f0bfeaa8Sopenharmony_ci virtual std::string ToString() const; 141f0bfeaa8Sopenharmony_ci}; 142f0bfeaa8Sopenharmony_ci} /* namespace PurgeableMem */ 143f0bfeaa8Sopenharmony_ci} /* namespace OHOS */ 144f0bfeaa8Sopenharmony_ci#endif /* OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_MEM_BASE_H */ 145