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 LIBPANDABASE_ECMASCRIPT_MEM_CONTAINERS_H 17 #define LIBPANDABASE_ECMASCRIPT_MEM_CONTAINERS_H 18 19 #include <deque> 20 #include <list> 21 #include <stack> 22 #include <queue> 23 #include <vector> 24 #include <set> 25 #include <string> 26 #include <map> 27 #include <unordered_map> 28 #include <unordered_set> 29 #include <functional> 30 31 #include "ecmascript/mem/chunk_allocator.h" 32 33 namespace panda::ecmascript { 34 template<typename T> 35 class PUBLIC_API ChunkVector : public std::vector<T, ChunkAllocator<T>> { 36 public: ChunkVector(Chunk * chunk)37 explicit ChunkVector(Chunk *chunk) : std::vector<T, ChunkAllocator<T>>(ChunkAllocator<T>(chunk)) {} 38 ChunkVector(size_t size,Chunk * chunk)39 ChunkVector(size_t size, Chunk *chunk) : std::vector<T, ChunkAllocator<T>>(size, T(), ChunkAllocator<T>(chunk)) {} 40 ChunkVector(size_t size,T def,Chunk * chunk)41 ChunkVector(size_t size, T def, Chunk *chunk) 42 : std::vector<T, ChunkAllocator<T>>(size, def, ChunkAllocator<T>(chunk)) 43 { 44 } 45 ~ChunkVector() = default; 46 NO_COPY_SEMANTIC(ChunkVector); 47 NO_MOVE_SEMANTIC(ChunkVector); 48 }; 49 50 template<typename T> 51 class PUBLIC_API ChunkDeque : public std::deque<T, ChunkAllocator<T>> { 52 public: ChunkDeque(Chunk * chunk)53 explicit ChunkDeque(Chunk *chunk) : std::deque<T, ChunkAllocator<T>>(ChunkAllocator<T>(chunk)) {} 54 ~ChunkDeque() = default; 55 }; 56 57 template<typename T> 58 class PUBLIC_API ChunkQueue : public std::queue<T, ChunkDeque<T>> { 59 public: ChunkQueue(Chunk * chunk)60 explicit ChunkQueue(Chunk *chunk) : std::queue<T, ChunkDeque<T>>(ChunkDeque<T>(chunk)) {} 61 ~ChunkQueue() = default; 62 }; 63 64 template<typename K, typename Compare = std::less<K>> 65 class PUBLIC_API ChunkSet : public std::set<K, Compare, ChunkAllocator<K>> { 66 public: 67 // Constructs an empty set. ChunkSet(Chunk * chunk)68 explicit ChunkSet(Chunk *chunk) 69 : std::set<K, Compare, ChunkAllocator<K>>(Compare(), ChunkAllocator<K>(chunk)) 70 { 71 } 72 ~ChunkSet() = default; 73 }; 74 75 template<typename K, typename V, typename Compare = std::less<K>> 76 class PUBLIC_API ChunkMap : public std::map<K, V, Compare, ChunkAllocator<std::pair<const K, V>>> { 77 public: 78 // Constructs an empty map. ChunkMap(Chunk * chunk)79 explicit ChunkMap(Chunk *chunk) 80 : std::map<K, V, Compare, ChunkAllocator<std::pair<const K, V>>>(Compare(), 81 ChunkAllocator<std::pair<const K, V>>(chunk)) 82 { 83 } 84 ~ChunkMap() = default; 85 }; 86 87 template<typename K, typename V, typename Hash = std::hash<K>, typename KeyEqual = std::equal_to<K>> 88 class PUBLIC_API ChunkUnorderedMap : public std::unordered_map<K, V, Hash, KeyEqual, ChunkAllocator<std::pair<const K, V>>> { 89 public: 90 // NOLINTNEXTLINE(readability-magic-numbers) 91 explicit ChunkUnorderedMap(Chunk *chunk, size_t bucket_count = 100) 92 : std::unordered_map<K, V, Hash, KeyEqual, ChunkAllocator<std::pair<const K, V>>>( 93 bucket_count, Hash(), KeyEqual(), ChunkAllocator<std::pair<const K, V>>(chunk)) 94 { 95 } 96 ~ChunkUnorderedMap() = default; 97 NO_COPY_SEMANTIC(ChunkUnorderedMap); 98 NO_MOVE_SEMANTIC(ChunkUnorderedMap); 99 }; 100 101 template<typename K, typename V, typename Compare = std::less<K>> 102 class PUBLIC_API ChunkMultimap : public std::multimap<K, V, Compare, ChunkAllocator<std::pair<const K, V>>> { 103 public: 104 // Constructs an empty multimap. ChunkMultimap(Chunk * chunk)105 explicit ChunkMultimap(Chunk *chunk) 106 : std::multimap<K, V, Compare, ChunkAllocator<std::pair<const K, V>>>( 107 Compare(), ChunkAllocator<std::pair<const K, V>>(chunk)) 108 { 109 } 110 ~ChunkMultimap() = default; 111 NO_COPY_SEMANTIC(ChunkMultimap); 112 NO_MOVE_SEMANTIC(ChunkMultimap); 113 }; 114 } // namespace panda::ecmascript 115 116 #endif // LIBPANDABASE_ECMASCRIPT_MEM_CONTAINERS_H 117