Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
STLAllocators.h
Go to the documentation of this file.
1/* Copyright (C) 2021 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23#ifndef INCLUDED_STL_ALLOCATORS
24#define INCLUDED_STL_ALLOCATORS
25
26#include <memory>
27#include <type_traits>
28
29/**
30 * Adapt a 0 A.D.-style allocator for usage in STL containers.
31 * Use 'Backend' as an underlying allocator.
32 */
33template<typename T, class Backend>
35{
36 template<typename A, class B>
37 friend class STLAllocator;
38public:
39 using value_type = T;
40 using pointer = T*;
41 using is_always_equal = std::false_type;
42
43 STLAllocator() : allocator(std::make_shared<Backend>())
44 {
45 }
46
47 template<typename V>
49 {
50 }
51
52 template<typename V>
53 struct rebind
54 {
56 };
57
58 T* allocate(size_t n)
59 {
60 return static_cast<T*>(allocator->allocate(n * sizeof(T), nullptr, alignof(T)));
61 }
62
63 void deallocate(T* ptr, const size_t n)
64 {
65 return allocator->deallocate(static_cast<void*>(ptr), n * sizeof(T));
66 }
67
68private:
69 std::shared_ptr<Backend> allocator;
70};
71
72
73/**
74 * Proxies allocation to another allocator.
75 * This allows a single allocator to serve multiple STL containers.
76 */
77template<typename T, class Backend>
79{
80 template<typename A, class B>
81 friend class ProxyAllocator;
82public:
83 using value_type = T;
84 using pointer = T*;
85 using is_always_equal = std::false_type;
86
88 {
89 }
90
91 template<typename V>
93 {
94 }
95
96 template<typename V>
97 struct rebind
98 {
100 };
101
102 T* allocate(size_t n)
103 {
104 return static_cast<T*>(allocator.allocate(n * sizeof(T), nullptr, alignof(T)));
105 }
106
107 void deallocate(T* ptr, const size_t n)
108 {
109 return allocator.deallocate(static_cast<void*>(ptr), n * sizeof(T));
110 }
111
112private:
114};
115
116#endif // INCLUDED_STL_ALLOCATORS
Proxies allocation to another allocator.
Definition: STLAllocators.h:79
T value_type
Definition: STLAllocators.h:83
std::false_type is_always_equal
Definition: STLAllocators.h:85
T * pointer
Definition: STLAllocators.h:84
void deallocate(T *ptr, const size_t n)
Definition: STLAllocators.h:107
ProxyAllocator(const ProxyAllocator< V, Backend > &proxy)
Definition: STLAllocators.h:92
Backend & allocator
Definition: STLAllocators.h:113
ProxyAllocator(Backend &alloc)
Definition: STLAllocators.h:87
T * allocate(size_t n)
Definition: STLAllocators.h:102
Adapt a 0 A.D.
Definition: STLAllocators.h:35
void deallocate(T *ptr, const size_t n)
Definition: STLAllocators.h:63
T * pointer
Definition: STLAllocators.h:40
STLAllocator(const STLAllocator< V, Backend > &proxy)
Definition: STLAllocators.h:48
STLAllocator()
Definition: STLAllocators.h:43
std::false_type is_always_equal
Definition: STLAllocators.h:41
T value_type
Definition: STLAllocators.h:39
std::shared_ptr< Backend > allocator
Definition: STLAllocators.h:69
T * allocate(size_t n)
Definition: STLAllocators.h:58
Backend
Definition: Backend.h:28
Definition: ShaderDefines.cpp:31
#define T(string_literal)
Definition: secure_crt.cpp:77
Definition: STLAllocators.h:98
Definition: STLAllocators.h:54