1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5#pragma once
6
7#include "new.h"
8
9namespace jitstd
10{
11
12template <typename T>
13class allocator;
14
15template <>
16class allocator<void>
17{
18public:
19 typedef size_t size_type;
20 typedef ptrdiff_t difference_type;
21 typedef void* pointer;
22 typedef const void* const_pointer;
23 typedef void value_type;
24
25 template <typename U>
26 struct rebind
27 {
28 typedef allocator<U> allocator;
29 };
30
31private:
32 allocator();
33
34public:
35 inline allocator(CompAllocator alloc);
36
37 template <typename U>
38 inline allocator(const allocator<U>& alloc);
39
40 inline allocator(const allocator& alloc);
41
42 template <typename U>
43 inline allocator& operator=(const allocator<U>& alloc);
44
45private:
46 CompAllocator m_alloc;
47 template <typename U>
48 friend class allocator;
49};
50
51allocator<void>::allocator(CompAllocator alloc)
52 : m_alloc(alloc)
53{
54}
55
56allocator<void>::allocator(const allocator& alloc)
57 : m_alloc(alloc.m_alloc)
58{
59}
60
61template <typename U>
62allocator<void>::allocator(const allocator<U>& alloc)
63 : m_alloc(alloc.m_alloc)
64{
65}
66
67template <typename U>
68allocator<void>& allocator<void>::operator=(const allocator<U>& alloc)
69{
70 m_alloc = alloc.m_alloc;
71 return *this;
72}
73
74template <typename T>
75class allocator
76{
77public:
78 typedef size_t size_type;
79 typedef ptrdiff_t difference_type;
80 typedef T* pointer;
81 typedef T& reference;
82 typedef const T* const_pointer;
83 typedef const T& const_reference;
84 typedef T value_type;
85
86private:
87 allocator();
88public:
89 allocator(CompAllocator alloc);
90
91 template <typename U>
92 allocator(const allocator<U>& alloc);
93
94 allocator(const allocator& alloc);
95
96 template <typename U>
97 allocator& operator=(const allocator<U>& alloc);
98
99 pointer address(reference val);
100 const_pointer address(const_reference val) const;
101 pointer allocate(size_type count, allocator<void>::const_pointer hint = nullptr);
102 void construct(pointer ptr, const_reference val);
103 void deallocate(pointer ptr, size_type size);
104 void destroy(pointer ptr);
105 size_type max_size() const;
106 template <typename U>
107 struct rebind
108 {
109 typedef allocator<U> allocator;
110 };
111
112private:
113 CompAllocator m_alloc;
114 template <typename U>
115 friend class allocator;
116};
117
118} // end of namespace jitstd
119
120
121namespace jitstd
122{
123
124template <typename T>
125allocator<T>::allocator(CompAllocator alloc)
126 : m_alloc(alloc)
127{
128}
129
130template <typename T>
131template <typename U>
132allocator<T>::allocator(const allocator<U>& alloc)
133 : m_alloc(alloc.m_alloc)
134{
135}
136
137template <typename T>
138allocator<T>::allocator(const allocator<T>& alloc)
139 : m_alloc(alloc.m_alloc)
140{
141}
142
143template <typename T>
144template <typename U>
145allocator<T>& allocator<T>::operator=(const allocator<U>& alloc)
146{
147 m_alloc = alloc.m_alloc;
148 return *this;
149}
150
151template <typename T>
152typename allocator<T>::pointer allocator<T>::address(reference val)
153{
154 return &val;
155}
156
157template <typename T>
158typename allocator<T>::const_pointer allocator<T>::address(const_reference val) const
159{
160 return &val;
161}
162
163template <typename T>
164T* allocator<T>::allocate(size_type count, allocator<void>::const_pointer hint)
165{
166 return m_alloc.allocate<value_type>(count);
167}
168
169template <typename T>
170void allocator<T>::construct(pointer ptr, const_reference val)
171{
172 new (ptr, placement_t()) value_type(val);
173}
174
175template <typename T>
176void allocator<T>::deallocate(pointer ptr, size_type size)
177{
178 m_alloc.deallocate(ptr);
179}
180
181template <typename T>
182void allocator<T>::destroy(pointer ptr)
183{
184 ptr->~T();
185}
186
187template <typename T>
188typename allocator<T>::size_type allocator<T>::max_size() const
189{
190 return (size_type) -1;
191}
192
193} // end of namespace jitstd
194