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 33namespace panda::ecmascript { 34template<typename T> 35class PUBLIC_API ChunkVector : public std::vector<T, ChunkAllocator<T>> { 36public: 37 explicit ChunkVector(Chunk *chunk) : std::vector<T, ChunkAllocator<T>>(ChunkAllocator<T>(chunk)) {} 38 39 ChunkVector(size_t size, Chunk *chunk) : std::vector<T, ChunkAllocator<T>>(size, T(), ChunkAllocator<T>(chunk)) {} 40 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 50template<typename T> 51class PUBLIC_API ChunkDeque : public std::deque<T, ChunkAllocator<T>> { 52public: 53 explicit ChunkDeque(Chunk *chunk) : std::deque<T, ChunkAllocator<T>>(ChunkAllocator<T>(chunk)) {} 54 ~ChunkDeque() = default; 55}; 56 57template<typename T> 58class PUBLIC_API ChunkQueue : public std::queue<T, ChunkDeque<T>> { 59public: 60 explicit ChunkQueue(Chunk *chunk) : std::queue<T, ChunkDeque<T>>(ChunkDeque<T>(chunk)) {} 61 ~ChunkQueue() = default; 62}; 63 64template<typename T> 65class PUBLIC_API ChunkStack : public std::stack<T, ChunkDeque<T>> { 66public: 67 explicit ChunkStack(Chunk *chunk) : std::stack<T, ChunkDeque<T>>(ChunkDeque<T>(chunk)) {} 68 ~ChunkStack() = default; 69}; 70 71template<typename K, typename Compare = std::less<K>> 72class PUBLIC_API ChunkSet : public std::set<K, Compare, ChunkAllocator<K>> { 73public: 74 // Constructs an empty set. 75 explicit ChunkSet(Chunk *chunk) 76 : std::set<K, Compare, ChunkAllocator<K>>(Compare(), ChunkAllocator<K>(chunk)) 77 { 78 } 79 ~ChunkSet() = default; 80}; 81 82template<typename K, typename V, typename Compare = std::less<K>> 83class PUBLIC_API ChunkMap : public std::map<K, V, Compare, ChunkAllocator<std::pair<const K, V>>> { 84public: 85 // Constructs an empty map. 86 explicit ChunkMap(Chunk *chunk) 87 : std::map<K, V, Compare, ChunkAllocator<std::pair<const K, V>>>(Compare(), 88 ChunkAllocator<std::pair<const K, V>>(chunk)) 89 { 90 } 91 ~ChunkMap() = default; 92}; 93 94template<typename K, typename V, typename Hash = std::hash<K>, typename KeyEqual = std::equal_to<K>> 95class PUBLIC_API ChunkUnorderedMap : public std::unordered_map<K, V, Hash, KeyEqual, 96 ChunkAllocator<std::pair<const K, V>>> { 97public: 98 // NOLINTNEXTLINE(readability-magic-numbers) 99 explicit ChunkUnorderedMap(Chunk *chunk, size_t bucket_count = 100) 100 : std::unordered_map<K, V, Hash, KeyEqual, ChunkAllocator<std::pair<const K, V>>>( 101 bucket_count, Hash(), KeyEqual(), ChunkAllocator<std::pair<const K, V>>(chunk)) 102 { 103 } 104 ~ChunkUnorderedMap() = default; 105 NO_COPY_SEMANTIC(ChunkUnorderedMap); 106 NO_MOVE_SEMANTIC(ChunkUnorderedMap); 107}; 108 109template<typename K, typename V, typename Compare = std::less<K>> 110class PUBLIC_API ChunkMultimap : public std::multimap<K, V, Compare, ChunkAllocator<std::pair<const K, V>>> { 111public: 112 // Constructs an empty multimap. 113 explicit ChunkMultimap(Chunk *chunk) 114 : std::multimap<K, V, Compare, ChunkAllocator<std::pair<const K, V>>>( 115 Compare(), ChunkAllocator<std::pair<const K, V>>(chunk)) 116 { 117 } 118 ~ChunkMultimap() = default; 119 NO_COPY_SEMANTIC(ChunkMultimap); 120 NO_MOVE_SEMANTIC(ChunkMultimap); 121}; 122} // namespace panda::ecmascript 123 124#endif // LIBPANDABASE_ECMASCRIPT_MEM_CONTAINERS_H 125