1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef MIN_ALLOCATOR_H
11 #define MIN_ALLOCATOR_H
18 #include "test_macros.h"
26 bare_allocator() TEST_NOEXCEPT {}
29 bare_allocator(bare_allocator<U>) TEST_NOEXCEPT {}
31 T* allocate(std::size_t n)
33 return static_cast<T*>(::operator new(n*sizeof(T)));
36 void deallocate(T* p, std::size_t)
38 return ::operator delete(static_cast<void*>(p));
41 friend bool operator==(bare_allocator, bare_allocator) {return true;}
42 friend bool operator!=(bare_allocator x, bare_allocator y) {return !(x == y);}
47 class no_default_allocator
49 #if TEST_STD_VER >= 11
50 no_default_allocator() = delete;
52 no_default_allocator();
54 struct construct_tag {};
55 explicit no_default_allocator(construct_tag) {}
58 static no_default_allocator create() {
60 return no_default_allocator(tag);
67 no_default_allocator(no_default_allocator<U>) TEST_NOEXCEPT {}
69 T* allocate(std::size_t n)
71 return static_cast<T*>(::operator new(n*sizeof(T)));
74 void deallocate(T* p, std::size_t)
76 return ::operator delete(static_cast<void*>(p));
79 friend bool operator==(no_default_allocator, no_default_allocator) {return true;}
80 friend bool operator!=(no_default_allocator x, no_default_allocator y) {return !(x == y);}
83 struct malloc_allocator_base {
84 static size_t alloc_count;
85 static size_t dealloc_count;
86 static bool disable_default_constructor;
88 static size_t outstanding_alloc() {
89 assert(alloc_count >= dealloc_count);
90 return (alloc_count - dealloc_count);
94 assert(outstanding_alloc() == 0);
95 disable_default_constructor = false;
102 size_t malloc_allocator_base::alloc_count = 0;
103 size_t malloc_allocator_base::dealloc_count = 0;
104 bool malloc_allocator_base::disable_default_constructor = false;
108 class malloc_allocator : public malloc_allocator_base
111 typedef T value_type;
113 malloc_allocator() TEST_NOEXCEPT { assert(!disable_default_constructor); }
116 malloc_allocator(malloc_allocator<U>) TEST_NOEXCEPT {}
118 T* allocate(std::size_t n)
121 return static_cast<T*>(std::malloc(n*sizeof(T)));
124 void deallocate(T* p, std::size_t)
127 std::free(static_cast<void*>(p));
130 friend bool operator==(malloc_allocator, malloc_allocator) {return true;}
131 friend bool operator!=(malloc_allocator x, malloc_allocator y) {return !(x == y);}
135 #if TEST_STD_VER >= 11
139 template <class T, class = std::integral_constant<size_t, 0> > class min_pointer;
140 template <class T, class ID> class min_pointer<const T, ID>;
141 template <class ID> class min_pointer<void, ID>;
142 template <class ID> class min_pointer<const void, ID>;
143 template <class T> class min_allocator;
146 class min_pointer<const void, ID>
150 min_pointer() TEST_NOEXCEPT = default;
151 min_pointer(std::nullptr_t) TEST_NOEXCEPT : ptr_(nullptr) {}
153 min_pointer(min_pointer<T, ID> p) TEST_NOEXCEPT : ptr_(p.ptr_) {}
155 explicit operator bool() const {return ptr_ != nullptr;}
157 friend bool operator==(min_pointer x, min_pointer y) {return x.ptr_ == y.ptr_;}
158 friend bool operator!=(min_pointer x, min_pointer y) {return !(x == y);}
159 template <class U, class XID> friend class min_pointer;
163 class min_pointer<void, ID>
167 min_pointer() TEST_NOEXCEPT = default;
168 min_pointer(std::nullptr_t) TEST_NOEXCEPT : ptr_(nullptr) {}
170 class = typename std::enable_if
172 !std::is_const<T>::value
175 min_pointer(min_pointer<T, ID> p) TEST_NOEXCEPT : ptr_(p.ptr_) {}
177 explicit operator bool() const {return ptr_ != nullptr;}
179 friend bool operator==(min_pointer x, min_pointer y) {return x.ptr_ == y.ptr_;}
180 friend bool operator!=(min_pointer x, min_pointer y) {return !(x == y);}
181 template <class U, class XID> friend class min_pointer;
184 template <class T, class ID>
189 explicit min_pointer(T* p) TEST_NOEXCEPT : ptr_(p) {}
191 min_pointer() TEST_NOEXCEPT = default;
192 min_pointer(std::nullptr_t) TEST_NOEXCEPT : ptr_(nullptr) {}
193 explicit min_pointer(min_pointer<void, ID> p) TEST_NOEXCEPT : ptr_(static_cast<T*>(p.ptr_)) {}
195 explicit operator bool() const {return ptr_ != nullptr;}
197 typedef std::ptrdiff_t difference_type;
198 typedef T& reference;
200 typedef T value_type;
201 typedef std::random_access_iterator_tag iterator_category;
203 reference operator*() const {return *ptr_;}
204 pointer operator->() const {return ptr_;}
206 min_pointer& operator++() {++ptr_; return *this;}
207 min_pointer operator++(int) {min_pointer tmp(*this); ++ptr_; return tmp;}
209 min_pointer& operator--() {--ptr_; return *this;}
210 min_pointer operator--(int) {min_pointer tmp(*this); --ptr_; return tmp;}
212 min_pointer& operator+=(difference_type n) {ptr_ += n; return *this;}
213 min_pointer& operator-=(difference_type n) {ptr_ -= n; return *this;}
215 min_pointer operator+(difference_type n) const
217 min_pointer tmp(*this);
222 friend min_pointer operator+(difference_type n, min_pointer x)
227 min_pointer operator-(difference_type n) const
229 min_pointer tmp(*this);
234 friend difference_type operator-(min_pointer x, min_pointer y)
236 return x.ptr_ - y.ptr_;
239 reference operator[](difference_type n) const {return ptr_[n];}
241 friend bool operator< (min_pointer x, min_pointer y) {return x.ptr_ < y.ptr_;}
242 friend bool operator> (min_pointer x, min_pointer y) {return y < x;}
243 friend bool operator<=(min_pointer x, min_pointer y) {return !(y < x);}
244 friend bool operator>=(min_pointer x, min_pointer y) {return !(x < y);}
246 static min_pointer pointer_to(T& t) {return min_pointer(std::addressof(t));}
248 friend bool operator==(min_pointer x, min_pointer y) {return x.ptr_ == y.ptr_;}
249 friend bool operator!=(min_pointer x, min_pointer y) {return !(x == y);}
250 template <class U, class XID> friend class min_pointer;
251 template <class U> friend class min_allocator;
254 template <class T, class ID>
255 class min_pointer<const T, ID>
259 explicit min_pointer(const T* p) : ptr_(p) {}
261 min_pointer() TEST_NOEXCEPT = default;
262 min_pointer(std::nullptr_t) : ptr_(nullptr) {}
263 min_pointer(min_pointer<T, ID> p) : ptr_(p.ptr_) {}
264 explicit min_pointer(min_pointer<const void, ID> p) : ptr_(static_cast<const T*>(p.ptr_)) {}
266 explicit operator bool() const {return ptr_ != nullptr;}
268 typedef std::ptrdiff_t difference_type;
269 typedef const T& reference;
270 typedef const T* pointer;
271 typedef const T value_type;
272 typedef std::random_access_iterator_tag iterator_category;
274 reference operator*() const {return *ptr_;}
275 pointer operator->() const {return ptr_;}
277 min_pointer& operator++() {++ptr_; return *this;}
278 min_pointer operator++(int) {min_pointer tmp(*this); ++ptr_; return tmp;}
280 min_pointer& operator--() {--ptr_; return *this;}
281 min_pointer operator--(int) {min_pointer tmp(*this); --ptr_; return tmp;}
283 min_pointer& operator+=(difference_type n) {ptr_ += n; return *this;}
284 min_pointer& operator-=(difference_type n) {ptr_ -= n; return *this;}
286 min_pointer operator+(difference_type n) const
288 min_pointer tmp(*this);
293 friend min_pointer operator+(difference_type n, min_pointer x)
298 min_pointer operator-(difference_type n) const
300 min_pointer tmp(*this);
305 friend difference_type operator-(min_pointer x, min_pointer y)
307 return x.ptr_ - y.ptr_;
310 reference operator[](difference_type n) const {return ptr_[n];}
312 friend bool operator< (min_pointer x, min_pointer y) {return x.ptr_ < y.ptr_;}
313 friend bool operator> (min_pointer x, min_pointer y) {return y < x;}
314 friend bool operator<=(min_pointer x, min_pointer y) {return !(y < x);}
315 friend bool operator>=(min_pointer x, min_pointer y) {return !(x < y);}
317 static min_pointer pointer_to(const T& t) {return min_pointer(std::addressof(t));}
319 friend bool operator==(min_pointer x, min_pointer y) {return x.ptr_ == y.ptr_;}
320 friend bool operator!=(min_pointer x, min_pointer y) {return !(x == y);}
321 template <class U, class XID> friend class min_pointer;
324 template <class T, class ID>
327 operator==(min_pointer<T, ID> x, std::nullptr_t)
329 return !static_cast<bool>(x);
332 template <class T, class ID>
335 operator==(std::nullptr_t, min_pointer<T, ID> x)
337 return !static_cast<bool>(x);
340 template <class T, class ID>
343 operator!=(min_pointer<T, ID> x, std::nullptr_t)
345 return static_cast<bool>(x);
348 template <class T, class ID>
351 operator!=(std::nullptr_t, min_pointer<T, ID> x)
353 return static_cast<bool>(x);
360 typedef T value_type;
361 typedef min_pointer<T> pointer;
363 min_allocator() = default;
365 min_allocator(min_allocator<U>) {}
367 pointer allocate(std::ptrdiff_t n)
369 return pointer(static_cast<T*>(::operator new(n*sizeof(T))));
372 void deallocate(pointer p, std::ptrdiff_t)
374 return ::operator delete(p.ptr_);
377 friend bool operator==(min_allocator, min_allocator) {return true;}
378 friend bool operator!=(min_allocator x, min_allocator y) {return !(x == y);}
382 class explicit_allocator
385 typedef T value_type;
387 explicit_allocator() TEST_NOEXCEPT {}
390 explicit explicit_allocator(explicit_allocator<U>) TEST_NOEXCEPT {}
392 T* allocate(std::size_t n)
394 return static_cast<T*>(::operator new(n*sizeof(T)));
397 void deallocate(T* p, std::size_t)
399 return ::operator delete(static_cast<void*>(p));
402 friend bool operator==(explicit_allocator, explicit_allocator) {return true;}
403 friend bool operator!=(explicit_allocator x, explicit_allocator y) {return !(x == y);}
406 #endif // TEST_STD_VER >= 11
408 #endif // MIN_ALLOCATOR_H