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