• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 #ifndef PANDA_RUNTIME_MEM_PANDA_CONTAINERS_H
16 #define PANDA_RUNTIME_MEM_PANDA_CONTAINERS_H
17 
18 #include <deque>
19 #include <forward_list>
20 #include <list>
21 #include <map>
22 #include <queue>
23 #include <set>
24 #include <stack>
25 #include <unordered_map>
26 #include <unordered_set>
27 #include <vector>
28 #include "libpandabase/utils/small_vector.h"
29 #include "runtime/mem/allocator_adapter.h"
30 
31 namespace panda {
32 
33 template <class T>
34 using PandaForwardList = std::forward_list<T, mem::AllocatorAdapter<T>>;
35 // Thread local version of PandaForwardList
36 template <class T>
37 using PandaForwardListTL = std::forward_list<T, mem::AllocatorAdapter<T, mem::AllocScope::LOCAL>>;
38 
39 template <class T>
40 using PandaList = std::list<T, mem::AllocatorAdapter<T>>;
41 // Thread local version of PandaList
42 template <class T>
43 using PandaListTL = std::list<T, mem::AllocatorAdapter<T, mem::AllocScope::LOCAL>>;
44 
45 template <class T>
46 using PandaDeque = std::deque<T, mem::AllocatorAdapter<T>>;
47 // Thread local version of PandaDeque
48 template <class T>
49 using PandaDequeTL = std::deque<T, mem::AllocatorAdapter<T, mem::AllocScope::LOCAL>>;
50 
51 template <class T, class PandaContainer = PandaDeque<T>>
52 using PandaQueue = std::queue<T, PandaContainer>;
53 // Thread local version of PandaQueue
54 template <class T, class PandaContainer = PandaDequeTL<T>>
55 using PandaQueueTL = std::queue<T, PandaContainer>;
56 
57 template <class T, class PandaContainer = PandaDeque<T>>
58 using PandaStack = std::stack<T, PandaContainer>;
59 // Thread local version of PandaStack
60 template <class T, class PandaContainer = PandaDequeTL<T>>
61 using PandaStackTL = std::stack<T, PandaContainer>;
62 
63 template <class Key, class KeyLess = std::less<Key>>
64 using PandaSet = std::set<Key, KeyLess, mem::AllocatorAdapter<Key>>;
65 // Thread local version of PandaSet
66 template <class Key, class KeyLess = std::less<Key>>
67 using PandaSetTL = std::set<Key, KeyLess, mem::AllocatorAdapter<Key, mem::AllocScope::LOCAL>>;
68 
69 template <class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
70 using PandaUnorderedSet = std::unordered_set<Key, Hash, KeyEqual, mem::AllocatorAdapter<Key>>;
71 // Thread local version of PandaUnorderedSet
72 template <class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
73 using PandaUnorderedSetTL = std::unordered_set<Key, Hash, KeyEqual, mem::AllocatorAdapter<Key, mem::AllocScope::LOCAL>>;
74 
75 template <class T>
76 using PandaVector = std::vector<T, mem::AllocatorAdapter<T>>;
77 // Thread local version of PandaVector
78 template <class T>
79 using PandaVectorTL = std::vector<T, mem::AllocatorAdapter<T, mem::AllocScope::LOCAL>>;
80 
81 // For small vector with static allocation support
82 static constexpr size_t STATIC_CAPACITY_DEFAULT = 8;
83 template <typename T, size_t N = STATIC_CAPACITY_DEFAULT>
84 using PandaSmallVector = SmallVector<T, N, mem::AllocatorAdapter<T>>;
85 template <typename T, size_t N = STATIC_CAPACITY_DEFAULT>
86 using PandaSmallVectorTL = SmallVector<T, N, mem::AllocatorAdapter<T, mem::AllocScope::LOCAL>>;
87 
88 template <class T, class Container = PandaVector<T>, class Compare = std::less<typename Container::value_type>>
89 using PandaPriorityQueue = std::priority_queue<T, Container, Compare>;
90 // Thread local version of PandaPriorityQueue
91 template <class T, class Container = PandaVectorTL<T>, class Compare = std::less<typename Container::value_type>>
92 using PandaPriorityQueueTL = std::priority_queue<T, Container, Compare>;
93 
94 template <class Key, class T, class Compare = std::less<>>
95 using PandaMap = std::map<Key, T, Compare, mem::AllocatorAdapter<std::pair<const Key, T>>>;
96 // Thread local version of PandaMap
97 template <class Key, class T, class Compare = std::less<>>
98 using PandaMapTL = std::map<Key, T, Compare, mem::AllocatorAdapter<std::pair<const Key, T>, mem::AllocScope::LOCAL>>;
99 
100 template <class Key, class T, class Compare = std::less<>>
101 using PandaMultiMap = std::multimap<Key, T, Compare, mem::AllocatorAdapter<std::pair<const Key, T>>>;
102 // Thread local version of PandaMultiMap
103 template <class Key, class T, class Compare = std::less<>>
104 using PandaMultiMapTL =
105     std::multimap<Key, T, Compare, mem::AllocatorAdapter<std::pair<const Key, T>, mem::AllocScope::LOCAL>>;
106 
107 template <class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
108 using PandaUnorderedMap = std::unordered_map<Key, T, Hash, KeyEqual, mem::AllocatorAdapter<std::pair<const Key, T>>>;
109 // Thread local version of PandaUnorderedMap
110 template <class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
111 using PandaUnorderedMapTL =
112     std::unordered_map<Key, T, Hash, KeyEqual, mem::AllocatorAdapter<std::pair<const Key, T>, mem::AllocScope::LOCAL>>;
113 
114 template <class Key, class Value, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
115 using PandaUnorderedMultiMap =
116     std::unordered_multimap<Key, Value, Hash, KeyEqual, mem::AllocatorAdapter<std::pair<const Key, Value>>>;
117 // Thread local version of PandaUnorderedMultiMap
118 template <class Key, class Value, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
119 using PandaUnorderedMultiMapTL =
120     std::unordered_multimap<Key, Value, Hash, KeyEqual,
121                             mem::AllocatorAdapter<std::pair<const Key, Value>, mem::AllocScope::LOCAL>>;
122 }  // namespace panda
123 
124 #endif  // PANDA_RUNTIME_MEM_PANDA_CONTAINERS_H
125