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 #include "ecmascript/dfx/hprof/string_hashmap.h" 17 18 #include "ecmascript/mem/native_area_allocator.h" 19 20 namespace panda::ecmascript { FindOrInsertString(const CString * cstr)21CString *StringHashMap::FindOrInsertString(const CString *cstr) 22 { 23 StringKey key = GenerateStringKey(cstr); 24 auto it = hashmap_.find(key); 25 if (it != hashmap_.end()) { 26 return it->second; 27 } else { // NOLINT(readability-else-after-return) 28 index_++; 29 auto *newStr = const_cast<NativeAreaAllocator *>( 30 vm_->GetNativeAreaAllocator())->New<CString>(cstr->c_str()); 31 hashmap_.emplace(key, newStr); 32 orderedKey_.emplace_back(key); 33 indexMap_.emplace(key, index_); 34 return newStr; 35 } 36 } 37 GetStringId(const CString * cstr) const38StringId StringHashMap::GetStringId(const CString *cstr) const 39 { 40 auto it = indexMap_.find(GenerateStringKey(cstr)); 41 return it != indexMap_.end() ? it->second : 1; // "" 42 } 43 GetStringByKey(StringKey key) const44CString *StringHashMap::GetStringByKey(StringKey key) const 45 { 46 auto it = hashmap_.find(key); 47 if (it != hashmap_.end()) { 48 return it->second; 49 } 50 return nullptr; 51 } 52 GenerateStringKey(const CString * cstr) const53StringKey StringHashMap::GenerateStringKey(const CString *cstr) const 54 { 55 return std::hash<CString>{} (*cstr); 56 } 57 GetString(const CString & cstr)58CString *StringHashMap::GetString(const CString &cstr) 59 { 60 return FindOrInsertString(&cstr); 61 } 62 Clear()63void StringHashMap::Clear() 64 { 65 auto *alloctor = const_cast<NativeAreaAllocator *>(vm_->GetNativeAreaAllocator()); 66 for (auto &[_, cstr] : hashmap_) { 67 if (cstr != nullptr) { 68 alloctor->Delete(cstr); 69 } 70 } 71 } 72 } // namespace panda::ecmascript 73