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