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