14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_JSPANDAFILE_ABC_BUFFER_CACHE_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_JSPANDAFILE_ABC_BUFFER_CACHE_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include <string> 204514f5e3Sopenharmony_ci#include "ecmascript/ecma_context.h" 214514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h" 224514f5e3Sopenharmony_ci 234514f5e3Sopenharmony_cinamespace panda::ecmascript { 244514f5e3Sopenharmony_ci 254514f5e3Sopenharmony_cienum AbcBufferType { NORMAL_BUFFER, SECURE_BUFFER }; 264514f5e3Sopenharmony_cistruct AbcBufferInfo { 274514f5e3Sopenharmony_ci void *buffer_ ; 284514f5e3Sopenharmony_ci size_t size_; 294514f5e3Sopenharmony_ci AbcBufferType bufferType_; 304514f5e3Sopenharmony_ci 314514f5e3Sopenharmony_ci AbcBufferInfo(void *buffer, size_t size, AbcBufferType bufferType) 324514f5e3Sopenharmony_ci : buffer_(buffer), size_(size), bufferType_(bufferType) {} 334514f5e3Sopenharmony_ci AbcBufferInfo() 344514f5e3Sopenharmony_ci : buffer_(nullptr), size_(0), bufferType_(AbcBufferType::NORMAL_BUFFER) {} 354514f5e3Sopenharmony_ci}; 364514f5e3Sopenharmony_ci 374514f5e3Sopenharmony_ci/* 384514f5e3Sopenharmony_ci * If JSPandafileManager's loadedJSPandaFiles_ delete cache during sharedgc process, 394514f5e3Sopenharmony_ci * buffer cannot get in later module LoadJsPandafile process, which will cause crash. 404514f5e3Sopenharmony_ci * This map can help use buffer to get *pf again. 414514f5e3Sopenharmony_ci */ 424514f5e3Sopenharmony_ciclass AbcBufferCache { 434514f5e3Sopenharmony_cipublic: 444514f5e3Sopenharmony_ci AbcBufferCache() = default; 454514f5e3Sopenharmony_ci ~AbcBufferCache() 464514f5e3Sopenharmony_ci { 474514f5e3Sopenharmony_ci abcBufferMap_.clear(); 484514f5e3Sopenharmony_ci } 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_ci void AddAbcBufferToCache(const CString &fileName, const void *buffer, size_t size, AbcBufferType bufferType) 514514f5e3Sopenharmony_ci { 524514f5e3Sopenharmony_ci abcBufferMap_.emplace(fileName, AbcBufferInfo(const_cast<void *>(buffer), size, bufferType)); 534514f5e3Sopenharmony_ci } 544514f5e3Sopenharmony_ci 554514f5e3Sopenharmony_ci void DeleteAbcBufferFromCache(const CString &fileName) 564514f5e3Sopenharmony_ci { 574514f5e3Sopenharmony_ci auto iter = abcBufferMap_.find(fileName); 584514f5e3Sopenharmony_ci if (iter == abcBufferMap_.end()) { 594514f5e3Sopenharmony_ci return; 604514f5e3Sopenharmony_ci } 614514f5e3Sopenharmony_ci abcBufferMap_.erase(iter); 624514f5e3Sopenharmony_ci } 634514f5e3Sopenharmony_ci 644514f5e3Sopenharmony_ci AbcBufferInfo FindJSPandaFileInAbcBufferCache(const CString &fileName) const 654514f5e3Sopenharmony_ci { 664514f5e3Sopenharmony_ci auto iter = abcBufferMap_.find(fileName); 674514f5e3Sopenharmony_ci if (iter == abcBufferMap_.end()) { 684514f5e3Sopenharmony_ci return AbcBufferInfo(); 694514f5e3Sopenharmony_ci } 704514f5e3Sopenharmony_ci return iter->second; 714514f5e3Sopenharmony_ci } 724514f5e3Sopenharmony_ci 734514f5e3Sopenharmony_ciprivate: 744514f5e3Sopenharmony_ci std::unordered_map<CString, AbcBufferInfo> abcBufferMap_; 754514f5e3Sopenharmony_ci}; 764514f5e3Sopenharmony_ci 774514f5e3Sopenharmony_ciclass AbcBufferCacheScope { 784514f5e3Sopenharmony_cipublic: 794514f5e3Sopenharmony_ci AbcBufferCacheScope(JSThread *thread, const CString &filename, const void *buffer, 804514f5e3Sopenharmony_ci size_t size, AbcBufferType bufferType): filename_(filename) 814514f5e3Sopenharmony_ci { 824514f5e3Sopenharmony_ci abcBufferCache_ = thread->GetCurrentEcmaContext()->GetAbcBufferCache(); 834514f5e3Sopenharmony_ci ASSERT(abcBufferCache_ != nullptr); 844514f5e3Sopenharmony_ci abcBufferCache_->AddAbcBufferToCache(filename_, buffer, size, bufferType); 854514f5e3Sopenharmony_ci } 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_ci ~AbcBufferCacheScope() 884514f5e3Sopenharmony_ci { 894514f5e3Sopenharmony_ci ASSERT(abcBufferCache_ != nullptr); 904514f5e3Sopenharmony_ci abcBufferCache_->DeleteAbcBufferFromCache(filename_); 914514f5e3Sopenharmony_ci } 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_ciprivate: 944514f5e3Sopenharmony_ci const CString filename_; 954514f5e3Sopenharmony_ci AbcBufferCache *abcBufferCache_ {nullptr}; 964514f5e3Sopenharmony_ci}; 974514f5e3Sopenharmony_ci} // namespace panda::ecmascript 984514f5e3Sopenharmony_ci#endif // ECMASCRIPT_JSPANDAFILE_ABC_BUFFER_CACHE_H