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, [[maybe_unused]] std::function<bool 34 (std::string fileName, uint8_t **buff, size_t *buffSize)> cb = nullptr); 35 uint32_t SafeGetFileInfoIndex(const std::string &fileName); 36 std::shared_ptr<AnFileInfo> SafeGetAnFileInfo(uint32_t index); 37 std::shared_ptr<StubFileInfo> SafeGetStubFileInfo(); 38 bool SafeTryReadLock(); 39 bool SafeInsideStub(uintptr_t pc); 40 bool SafeInsideAOT(uintptr_t pc); 41 AOTFileInfo::CallSiteInfo SafeCalCallSiteInfo(uintptr_t retAddr, bool isDeopt); 42 static void DestroyFileMapMem(MemMap &fileMapMem); 43 void SafeDestroyAllData(); 44 void SafeDestroyAnData(const std::string &fileName); 45 GetDir() const46 const std::string &GetDir() const 47 { 48 return anDir_; 49 } 50 IsEnable() const51 bool IsEnable() const 52 { 53 return anEnable_; 54 } 55 56 void Dump() const DUMP_API_ATTR; 57 58 // only main thread call this, only call once, no need to lock SetDir(std::string dir)59 void SetDir(std::string dir) 60 { 61 anDir_ = std::move(dir); 62 } 63 SetEnable(bool enable)64 void SetEnable(bool enable) 65 { 66 anEnable_ = enable; 67 } 68 69 private: 70 AnFileDataManager() = default; 71 std::shared_ptr<AnFileInfo> UnsafeFind(const std::string &fileName) const; 72 bool UnsafeLoadFromAOTInternal(const std::string &fileName, std::shared_ptr<AnFileInfo> &info); 73 bool UnsafeLoadFromAOT(const std::string &fileName); 74 #if defined(CROSS_PLATFORM) && defined(ANDROID_PLATFORM) 75 bool UnsafeLoadFromAOT(const std::string &fileName, 76 std::function<bool(std::string fileName, uint8_t **buff, size_t *buffSize)> cb); 77 #endif 78 bool UnsafeLoadFromStub(const std::string &fileName); 79 uint32_t UnSafeGetFileInfoIndex(const std::string &fileName); UnSafeGetAnFileInfo(uint32_t index)80 std::shared_ptr<AnFileInfo> UnSafeGetAnFileInfo(uint32_t index) 81 { 82 return loadedAn_.at(index); 83 } 84 85 RWLock lock_ {}; 86 std::unordered_map<std::string, uint32_t> anFileNameToIndexMap_ {}; 87 std::vector<std::shared_ptr<AnFileInfo>> loadedAn_ {}; 88 89 std::shared_ptr<StubFileInfo> loadedStub_ {nullptr}; 90 std::string anDir_; 91 bool anEnable_ {false}; 92 NO_COPY_SEMANTIC(AnFileDataManager); 93 NO_MOVE_SEMANTIC(AnFileDataManager); 94 }; 95 } // namespace panda::ecmascript 96 #endif // ECMASCRIPT_COMPILER_AOT_FILE_AN_FILE_DATA_MANAGER_H 97