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 27 namespace panda::ecmascript { 28 class Program; 29 class EcmaVM; 30 class JSPandaFile; 31 32 class PUBLIC_API Snapshot { 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 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 Deserialize(SnapshotType type, const CString &snapshotFile, bool isBuiltins = false); 42 43 protected: 44 struct SnapShotHeader : public base::FileHeaderBase { 45 public: SnapShotHeaderSnapShotHeader46 explicit SnapShotHeader(const VersionType &lastVersion) : base::FileHeaderBase(lastVersion) {} 47 VerifySnapShotHeader48 bool Verify(const VersionType &lastVersion) const 49 { 50 return VerifyVersion("snapshot file", lastVersion, AOTFileVersion::AI_STRICT_MATCH); 51 } 52 53 uint32_t oldSpaceObjSize {0}; 54 uint32_t nonMovableObjSize {0}; 55 uint32_t machineCodeObjSize {0}; 56 uint32_t snapshotObjSize {0}; 57 uint32_t hugeObjSize {0}; 58 uint32_t stringSize {0}; 59 uint32_t pandaFileBegin {0}; 60 uint32_t rootObjectSize {0}; 61 }; 62 GetLastVersion()63 virtual const base::FileHeaderBase::VersionType &GetLastVersion() const 64 { 65 return AOTFileVersion::AI_VERSION; 66 } 67 68 private: 69 size_t AlignUpPageSize(size_t spaceSize); 70 void WriteToFile(std::fstream &writer, const JSPandaFile *jsPandaFile, size_t size, SnapshotProcessor &processor); 71 72 NO_MOVE_SEMANTIC(Snapshot); 73 NO_COPY_SEMANTIC(Snapshot); 74 75 EcmaVM *vm_; 76 }; 77 } // namespace panda::ecmascript 78 #endif // ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_H 79