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 TEST_ALLOCATOR_H
11 #define TEST_ALLOCATOR_H
14 #include <type_traits>
24 static int throw_after;
27 int test_alloc_base::count = 0;
28 int test_alloc_base::throw_after = INT_MAX;
32 : public test_alloc_base
36 template <class U> friend class test_allocator;
39 typedef unsigned size_type;
40 typedef int difference_type;
42 typedef value_type* pointer;
43 typedef const value_type* const_pointer;
44 typedef typename std::add_lvalue_reference<value_type>::type reference;
45 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
47 template <class U> struct rebind {typedef test_allocator<U> other;};
49 test_allocator() throw() : data_(-1) {}
50 explicit test_allocator(int i) throw() : data_(i) {}
51 test_allocator(const test_allocator& a) throw()
53 template <class U> test_allocator(const test_allocator<U>& a) throw()
55 ~test_allocator() throw() {data_ = 0;}
56 pointer address(reference x) const {return &x;}
57 const_pointer address(const_reference x) const {return &x;}
58 pointer allocate(size_type n, const void* = 0)
60 if (count >= throw_after) {
61 #ifndef _LIBCPP_NO_EXCEPTIONS
62 throw std::bad_alloc();
68 return (pointer)std::malloc(n * sizeof(T));
70 void deallocate(pointer p, size_type n)
71 {--count; std::free(p);}
72 size_type max_size() const throw()
73 {return UINT_MAX / sizeof(T);}
74 void construct(pointer p, const T& val)
76 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
77 void construct(pointer p, T&& val)
78 {::new(p) T(std::move(val));}
79 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
80 void destroy(pointer p) {p->~T();}
82 friend bool operator==(const test_allocator& x, const test_allocator& y)
83 {return x.data_ == y.data_;}
84 friend bool operator!=(const test_allocator& x, const test_allocator& y)
89 class test_allocator<void>
90 : public test_alloc_base
94 template <class U> friend class test_allocator;
97 typedef unsigned size_type;
98 typedef int difference_type;
99 typedef void value_type;
100 typedef value_type* pointer;
101 typedef const value_type* const_pointer;
103 template <class U> struct rebind {typedef test_allocator<U> other;};
105 test_allocator() throw() : data_(-1) {}
106 explicit test_allocator(int i) throw() : data_(i) {}
107 test_allocator(const test_allocator& a) throw()
109 template <class U> test_allocator(const test_allocator<U>& a) throw()
111 ~test_allocator() throw() {data_ = 0;}
113 friend bool operator==(const test_allocator& x, const test_allocator& y)
114 {return x.data_ == y.data_;}
115 friend bool operator!=(const test_allocator& x, const test_allocator& y)
120 class other_allocator
124 template <class U> friend class other_allocator;
127 typedef T value_type;
129 other_allocator() : data_(-1) {}
130 explicit other_allocator(int i) : data_(i) {}
131 template <class U> other_allocator(const other_allocator<U>& a)
133 T* allocate(std::size_t n)
134 {return (T*)std::malloc(n * sizeof(T));}
135 void deallocate(T* p, std::size_t n)
138 other_allocator select_on_container_copy_construction() const
139 {return other_allocator(-2);}
141 friend bool operator==(const other_allocator& x, const other_allocator& y)
142 {return x.data_ == y.data_;}
143 friend bool operator!=(const other_allocator& x, const other_allocator& y)
146 typedef std::true_type propagate_on_container_copy_assignment;
147 typedef std::true_type propagate_on_container_move_assignment;
148 typedef std::true_type propagate_on_container_swap;
150 #ifdef _LIBCPP_HAS_NO_ADVANCED_SFINAE
151 std::size_t max_size() const
152 {return UINT_MAX / sizeof(T);}
153 #endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
157 #endif // TEST_ALLOCATOR_H