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_ASHMEM_H
17 #define OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_ASHMEM_H
18 
19 #include <memory>
20 #include <shared_mutex>
21 #include <string>
22 #include <sys/ioctl.h>
23 #include <unistd.h>
24 
25 #include <linux/ashmem.h>
26 
27 #include "ashmem.h"
28 #include "purgeable_mem_builder.h"
29 #include "purgeable_mem_base.h"
30 #include "ux_page_table.h"
31 
32 #ifndef ASHMEM_SET_PURGEABLE
33 #define ASHMEM_SET_PURGEABLE                   _IO(__ASHMEMIOC, 11)
34 #endif
35 #ifndef ASHMEM_GET_PURGEABLE
36 #define ASHMEM_GET_PURGEABLE                   _IO(__ASHMEMIOC, 12)
37 #endif
38 #ifndef PURGEABLE_ASHMEM_IS_PURGED
39 #define PURGEABLE_ASHMEM_IS_PURGED             _IO(__ASHMEMIOC, 13)
40 #endif
41 #ifndef PURGEABLE_ASHMEM_REBUILD_SUCCESS
42 #define PURGEABLE_ASHMEM_REBUILD_SUCCESS       _IO(__ASHMEMIOC, 14)
43 #endif
44 
45 namespace OHOS {
46 namespace PurgeableMem {
47 class PurgeableAshMem : public PurgeableMemBase {
48 public:
49     PurgeableAshMem(size_t dataSize, std::unique_ptr<PurgeableMemBuilder> builder);
50     PurgeableAshMem(std::unique_ptr<PurgeableMemBuilder> builder);
51     ~PurgeableAshMem() override;
52     int GetAshmemFd();
53     void ResizeData(size_t newSize) override;
54     bool ChangeAshmemData(size_t size, int fd, void *data);
55 
56 protected:
57     int ashmemFd_;
58     int isSupport_;
59     bool isChange_;
60     ashmem_pin pin_ = { static_cast<uint32_t>(0), static_cast<uint32_t>(0) };
61     bool Pin() override;
62     bool Unpin() override;
63     bool IsPurged() override;
64     int GetPinStatus() const override;
65     bool CreatePurgeableData();
66     void AfterRebuildSucc() override;
67     std::string ToString() const override;
68 };
69 } /* namespace PurgeableMem */
70 } /* namespace OHOS */
71 #endif /* OHOS_UTILS_MEMORY_LIBPURGEABLEMEM_CPP_INCLUDE_PURGEABLE_ASHMEM_H */
72