• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 ECMASCRIPT_HPROF_STRING_HASHMAP_H
17 #define ECMASCRIPT_HPROF_STRING_HASHMAP_H
18 
19 #include "ecmascript/js_thread.h"
20 #include "ecmascript/mem/c_containers.h"
21 #include "ecmascript/mem/c_string.h"
22 #include "ecmascript/mem/heap.h"
23 #include "os/mem.h"
24 
25 namespace panda::ecmascript {
26 using StringKey = uint64_t;
27 using StringId = uint64_t;
28 
29 // An Implementation for Native StringTable without Auto Mem-Management
30 // To make sure when using String, it still stays where it was.
31 class StringHashMap {
32 public:
StringHashMap(const Heap * heap)33     explicit StringHashMap(const Heap *heap) : heap_(heap)
34     {
35         ASSERT(heap_ != nullptr);
36     }
~StringHashMap()37     ~StringHashMap()
38     {
39         Clear();
40     }
41     NO_MOVE_SEMANTIC(StringHashMap);
42     NO_COPY_SEMANTIC(StringHashMap);
43     /*
44      * The ID is the seat number in JSON file Range from 0~string_table_.size()
45      */
46     StringId GetStringId(const CString *string) const;
47     /*
48      * Get all keys sorted by insert order
49      */
GetOrderedKeyStorage()50     const CVector<StringKey> &GetOrderedKeyStorage() const
51     {
52         return orderedKey_;
53     }
54     /*
55      * Get string by its hash key
56      */
57     CString *GetStringByKey(StringKey key) const;
GetCapcity()58     size_t GetCapcity() const
59     {
60         ASSERT(orderedKey_.size() == hashmap_.size());
61         return orderedKey_.size();
62     }
63     /*
64      * For external call to use this StringTable
65      */
66     CString *GetString(CString as);
67 
68 private:
69     StringKey GenerateStringKey(const CString *string) const;
70     CString *FindOrInsertString(CString *string);
71     CString *FormatString(CString *string) const;
72     /*
73      * Free all memory
74      */
75     void Clear();
76     const Heap *heap_;
77     CVector<StringKey> orderedKey_;  // Used for Serialize Order
78     size_t index_{2};  // 2: Offset the String-Table Header
79     CUnorderedMap<StringKey, StringId> indexMap_;
80     CUnorderedMap<StringKey, CString *> hashmap_;
81 };
82 }  // namespace panda::ecmascript
83 #endif  // ECMASCRIPT_HPROF_STRING_HASHMAP_H
84