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