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 ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_H
17#define ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_H
18
19#include "ecmascript/compiler/aot_file/aot_version.h"
20#include "ecmascript/common.h"
21#include "ecmascript/mem/c_string.h"
22#include "ecmascript/snapshot/mem/encode_bit.h"
23#include "ecmascript/snapshot/mem/snapshot_processor.h"
24
25#include "libpandabase/macros.h"
26
27namespace panda::ecmascript {
28class Program;
29class EcmaVM;
30class JSPandaFile;
31
32class PUBLIC_API Snapshot {
33public:
34    explicit Snapshot(EcmaVM *vm) : vm_(vm) {}
35    ~Snapshot() = default;
36
37    void Serialize(const CString &fileName = "./snapshot");
38    void Serialize(TaggedObject *objectHeader, const JSPandaFile *jsPandaFile, const CString &fileName = "./snapshot");
39    void Serialize(uintptr_t startAddr, size_t size, const CString &fileName = "./snapshot");
40    void SerializeBuiltins(const CString &fileName = "./snapshot");
41    bool DeserializeInternal(SnapshotType type, const CString &snapshotFile, SnapshotProcessor &processor,
42                             MemMap &fileMap);
43    bool Deserialize(SnapshotType type, const CString &snapshotFile, bool isBuiltins = false);
44#if defined(CROSS_PLATFORM) && defined(ANDROID_PLATFORM)
45    bool Deserialize(SnapshotType type, const CString &snapshotFile, [[maybe_unused]] std::function<bool
46        (std::string fileName, uint8_t **buff, size_t *buffSize)> ReadAOTCallBack, bool isBuiltins = false);
47#endif
48
49protected:
50    struct SnapShotHeader : public base::FileHeaderBase {
51    public:
52        explicit SnapShotHeader(const VersionType &lastVersion) : base::FileHeaderBase(lastVersion) {}
53
54        bool Verify(const VersionType &lastVersion) const
55        {
56            return VerifyVersion("snapshot file", lastVersion, AOTFileVersion::AI_STRICT_MATCH);
57        }
58
59        uint32_t oldSpaceObjSize {0};
60        uint32_t nonMovableObjSize {0};
61        uint32_t machineCodeObjSize {0};
62        uint32_t snapshotObjSize {0};
63        uint32_t hugeObjSize {0};
64        uint32_t stringSize {0};
65        uint32_t pandaFileBegin {0};
66        uint32_t rootObjectSize {0};
67    };
68
69    virtual const base::FileHeaderBase::VersionType &GetLastVersion() const
70    {
71        return AOTFileVersion::AI_VERSION;
72    }
73
74private:
75    size_t AlignUpPageSize(size_t spaceSize);
76    void WriteToFile(std::fstream &writer, const JSPandaFile *jsPandaFile, size_t size, SnapshotProcessor &processor);
77
78    NO_MOVE_SEMANTIC(Snapshot);
79    NO_COPY_SEMANTIC(Snapshot);
80
81    EcmaVM *vm_;
82};
83}  // namespace panda::ecmascript
84#endif  // ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_H
85