1 /* 2 * Copyright (c) 2021 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_ENV_H 17 #define ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_ENV_H 18 19 #include <unordered_map> 20 21 #include "ecmascript/js_thread.h" 22 #include "ecmascript/mem/visitor.h" 23 24 namespace panda::ecmascript { 25 class EcmaVM; 26 27 class SnapshotEnv final { 28 public: SnapshotEnv(EcmaVM * vm)29 explicit SnapshotEnv(EcmaVM *vm) : vm_(vm) {} 30 ~SnapshotEnv() = default; 31 32 void AddGlobalConstToMap(); 33 ClearEnvMap()34 void ClearEnvMap() 35 { 36 rootObjectMap_.clear(); 37 } 38 39 void Iterate(RootVisitor &v, VMRootVisitType type); 40 Push(JSTaggedType objectAddr,uint32_t index)41 void Push(JSTaggedType objectAddr, uint32_t index) 42 { 43 rootObjectMap_.emplace(objectAddr, index); 44 } 45 Remove(JSTaggedType objectAddr)46 void Remove(JSTaggedType objectAddr) 47 { 48 rootObjectMap_.erase(objectAddr); 49 } 50 FindEnvObjectIndex(JSTaggedType objectAddr)51 uint32_t FindEnvObjectIndex(JSTaggedType objectAddr) const 52 { 53 if (rootObjectMap_.find(objectAddr) != rootObjectMap_.end()) { 54 return rootObjectMap_.find(objectAddr)->second; 55 } 56 return MAX_UINT_32; 57 } 58 59 JSTaggedType RelocateRootObjectAddr(uint32_t index); 60 61 static constexpr size_t MAX_UINT_32 = 0xFFFFFFFF; 62 63 private: 64 NO_MOVE_SEMANTIC(SnapshotEnv); 65 NO_COPY_SEMANTIC(SnapshotEnv); 66 67 void InitGlobalConst(); 68 void InitGlobalEnv(); 69 70 EcmaVM *vm_; 71 std::unordered_map<JSTaggedType, uint32_t> rootObjectMap_; 72 std::atomic<uint32_t> multiThreadCheckValue_ {0}; 73 }; 74 } // namespace panda::ecmascript 75 #endif // ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_ENV_H 76