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)21 CString *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) const38 StringId 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) const44 CString *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
GetStringAndIdPair(StringKey key) const53 std::pair<uint64_t, CString *> StringHashMap::GetStringAndIdPair(StringKey key) const
54 {
55 auto it = hashmap_.find(key);
56 if (it != hashmap_.end()) {
57 return {indexMap_.at(key), it->second};
58 }
59 return {1, nullptr}; // 1 : invalid id
60 }
61
InsertStrAndGetStringId(const CString &cstrArg)62 StringId StringHashMap::InsertStrAndGetStringId(const CString &cstrArg)
63 {
64 CString *cstr = const_cast<CString *>(&cstrArg);
65 StringKey key = GenerateStringKey(cstr);
66 auto it = indexMap_.find(key);
67 if (it != indexMap_.end()) {
68 return it->second;
69 } else { // NOLINT(readability-else-after-return)
70 index_++;
71 auto *newStr = const_cast<NativeAreaAllocator *>(
72 vm_->GetNativeAreaAllocator())->New<CString>(cstr->c_str());
73 hashmap_.emplace(key, newStr);
74 orderedKey_.emplace_back(key);
75 indexMap_.emplace(key, index_);
76 return index_;
77 }
78 }
79
GenerateStringKey(const CString *cstr) const80 StringKey StringHashMap::GenerateStringKey(const CString *cstr) const
81 {
82 return std::hash<CString>{} (*cstr);
83 }
84
GetString(const CString &cstr)85 CString *StringHashMap::GetString(const CString &cstr)
86 {
87 return FindOrInsertString(&cstr);
88 }
89
Clear()90 void StringHashMap::Clear()
91 {
92 auto *alloctor = const_cast<NativeAreaAllocator *>(vm_->GetNativeAreaAllocator());
93 for (auto &[_, cstr] : hashmap_) {
94 if (cstr != nullptr) {
95 alloctor->Delete(cstr);
96 }
97 }
98 }
99 } // namespace panda::ecmascript
100