• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_DATA_MANAGER_H
16 #define ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_DATA_MANAGER_H
17 
18 #include "ecmascript/compiler/aot_file/an_file_info.h"
19 #include "ecmascript/compiler/aot_file/stub_file_info.h"
20 #include "ecmascript/platform/map.h"
21 
22 namespace panda::ecmascript {
23 class AnFileDataManager {
24 public:
25     enum class Type : uint8_t {
26         STUB = 0,
27         AOT,
28     };
29 
30     static AnFileDataManager *GetInstance();
31     ~AnFileDataManager();
32 
33     bool SafeLoad(const std::string &fileName, Type type);
34     uint32_t SafeGetFileInfoIndex(const std::string &fileName);
35     std::shared_ptr<AnFileInfo> SafeGetAnFileInfo(uint32_t index);
36     std::shared_ptr<StubFileInfo> SafeGetStubFileInfo();
37     bool SafeTryReadLock();
38     bool SafeInsideStub(uintptr_t pc);
39     bool SafeInsideAOT(uintptr_t pc);
40     AOTFileInfo::CallSiteInfo SafeCalCallSiteInfo(uintptr_t retAddr);
41     static void DestroyFileMapMem(MemMap &fileMapMem);
42     void SafeDestroyAllData();
43     void SafeDestroyAnData(const std::string &fileName);
44 
GetDir()45     const std::string &GetDir() const
46     {
47         return anDir_;
48     }
49 
IsEnable()50     bool IsEnable() const
51     {
52         return anEnable_;
53     }
54 
55     void Dump() const DUMP_API_ATTR;
56 
57     // only main thread call this, only call once, no need to lock
SetDir(std::string dir)58     void SetDir(std::string dir)
59     {
60         anDir_ = std::move(dir);
61     }
62 
SetEnable(bool enable)63     void SetEnable(bool enable)
64     {
65         anEnable_ = enable;
66     }
67 
68 private:
69     AnFileDataManager() = default;
70     std::shared_ptr<AnFileInfo> UnsafeFind(const std::string &fileName) const;
71     bool UnsafeLoadFromAOT(const std::string &fileName);
72     bool UnsafeLoadFromStub();
73     uint32_t UnSafeGetFileInfoIndex(const std::string &fileName);
UnSafeGetAnFileInfo(uint32_t index)74     std::shared_ptr<AnFileInfo> UnSafeGetAnFileInfo(uint32_t index)
75     {
76         return loadedAn_.at(index);
77     }
78 
79     os::memory::RWLock lock_ {};
80     std::unordered_map<std::string, uint32_t> anFileNameToIndexMap_ {};
81     std::vector<std::shared_ptr<AnFileInfo>> loadedAn_ {};
82     std::shared_ptr<StubFileInfo> loadedStub_ {nullptr};
83     std::string anDir_;
84     bool anEnable_ {false};
85     NO_COPY_SEMANTIC(AnFileDataManager);
86     NO_MOVE_SEMANTIC(AnFileDataManager);
87 };
88 }  // namespace panda::ecmascript
89 #endif  // ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_DATA_MANAGER_H
90