• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/aot_version.h"
20 #include "ecmascript/common.h"
21 #include "ecmascript/snapshot/mem/encode_bit.h"
22 #include "ecmascript/snapshot/mem/snapshot_processor.h"
23 #include "ecmascript/mem/c_string.h"
24 
25 #include "libpandabase/macros.h"
26 #include "libpandafile/file.h"
27 
28 namespace panda::ecmascript {
29 class Program;
30 class EcmaVM;
31 class JSPandaFile;
32 
33 class PUBLIC_API Snapshot final {
34 public:
Snapshot(EcmaVM * vm)35     explicit Snapshot(EcmaVM *vm) : vm_(vm) {}
36     ~Snapshot() = default;
37 
38     void Serialize(const CString &fileName = "./snapshot");
39     void Serialize(TaggedObject *objectHeader, const panda_file::File *pf, const CString &fileName = "./snapshot");
40     void Serialize(uintptr_t startAddr, size_t size, const CString &fileName = "./snapshot");
41     void SerializeBuiltins(const CString &fileName = "./snapshot");
42     const JSPandaFile *Deserialize(SnapshotType type, const CString &snapshotFile, bool isBuiltins = false);
43 
44 private:
45     struct SnapShotHeader : public base::FileHeader {
46     public:
SnapShotHeaderSnapShotHeader47         SnapShotHeader() : base::FileHeader(AOTFileVersion::AI_VERSION) {}
48 
VerifySnapShotHeader49         bool Verify()
50         {
51             return VerifyInner("snapshot file", AOTFileVersion::AI_VERSION);
52         }
53 
54         uint32_t oldSpaceObjSize;
55         uint32_t nonMovableObjSize;
56         uint32_t machineCodeObjSize;
57         uint32_t snapshotObjSize;
58         uint32_t hugeObjSize;
59         uint32_t stringSize;
60         uint32_t pandaFileBegin;
61         uint32_t rootObjectSize;
62     };
63 
64 private:
65     size_t AlignUpPageSize(size_t spaceSize);
66     void WriteToFile(std::fstream &writer, const panda_file::File *pf, size_t size, SnapshotProcessor &processor);
67 
68     NO_MOVE_SEMANTIC(Snapshot);
69     NO_COPY_SEMANTIC(Snapshot);
70 
71     EcmaVM *vm_;
72 };
73 }  // namespace panda::ecmascript
74 #endif  // ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_H
75