1 /* 2 * Copyright (c) 2024 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 #ifndef RAWHEAP_TRANSLATE_STRING_HASHMAP_H 17 #define RAWHEAP_TRANSLATE_STRING_HASHMAP_H 18 19 #include "ecmascript/dfx/hprof/rawheap_translate/common.h" 20 #include "ecmascript/dfx/hprof/rawheap_translate/utils.h" 21 22 namespace rawheap_translate { 23 // An Implementation for Native StringTable without Auto Mem-Management 24 // To make sure when using String, it still stays where it was. 25 class StringHashMap { 26 public: StringHashMap()27 explicit StringHashMap() 28 { 29 } 30 ~StringHashMap()31 ~StringHashMap() 32 { 33 } 34 35 /* 36 * Get all keys sorted by insert order 37 */ GetOrderedKeyStorage()38 const std::vector<StringKey> &GetOrderedKeyStorage() const 39 { 40 return orderedKey_; 41 } 42 /* 43 * Get string by its hash key 44 */ 45 std::string GetStringByKey(StringKey key) const; 46 StringKey GetKeyByStringId(StringId stringId) const; 47 StringId InsertStrAndGetStringId(const std::string &cstrArg); GetCapcity()48 size_t GetCapcity() const 49 { 50 return orderedKey_.size(); 51 } 52 53 static constexpr uint32_t CUSTOM_STRID_START = 3; 54 55 private: 56 StringKey GenerateStringKey(const std::string &cstr) const; 57 58 std::vector<StringKey> orderedKey_; // Used for Serialize Order 59 size_t index_ {2}; // 2: Offset the String-Table Header 60 std::unordered_map<StringKey, StringId> indexMap_; 61 std::unordered_map<StringKey, std::string> hashmap_; 62 }; 63 } // namespace rawheap_translate 64 #endif // RAWHEAP_TRANSLATE_STRING_HASHMAP_H 65