1 #ifndef TEST_ALLOCATOR_H
2 #define TEST_ALLOCATOR_H
15 static int throw_after;
18 int test_alloc_base::count = 0;
19 int test_alloc_base::throw_after = INT_MAX;
23 : public test_alloc_base
27 template <class U> friend class test_allocator;
30 typedef unsigned size_type;
31 typedef int difference_type;
33 typedef value_type* pointer;
34 typedef const value_type* const_pointer;
35 typedef typename std::add_lvalue_reference<value_type>::type reference;
36 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
38 template <class U> struct rebind {typedef test_allocator<U> other;};
40 test_allocator() throw() : data_(-1) {}
41 explicit test_allocator(int i) throw() : data_(i) {}
42 test_allocator(const test_allocator& a) throw()
44 template <class U> test_allocator(const test_allocator<U>& a) throw()
46 ~test_allocator() throw() {data_ = 0;}
47 pointer address(reference x) const {return &x;}
48 const_pointer address(const_reference x) const {return &x;}
49 pointer allocate(size_type n, const void* = 0)
51 if (count >= throw_after) {
52 #ifndef _LIBCPP_NO_EXCEPTIONS
53 throw std::bad_alloc();
59 return (pointer)std::malloc(n * sizeof(T));
61 void deallocate(pointer p, size_type n)
62 {--count; std::free(p);}
63 size_type max_size() const throw()
64 {return UINT_MAX / sizeof(T);}
65 void construct(pointer p, const T& val)
67 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
68 void construct(pointer p, T&& val)
69 {::new(p) T(std::move(val));}
70 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
71 void destroy(pointer p) {p->~T();}
73 friend bool operator==(const test_allocator& x, const test_allocator& y)
74 {return x.data_ == y.data_;}
75 friend bool operator!=(const test_allocator& x, const test_allocator& y)
80 class test_allocator<void>
81 : public test_alloc_base
85 template <class U> friend class test_allocator;
88 typedef unsigned size_type;
89 typedef int difference_type;
90 typedef void value_type;
91 typedef value_type* pointer;
92 typedef const value_type* const_pointer;
94 template <class U> struct rebind {typedef test_allocator<U> other;};
96 test_allocator() throw() : data_(-1) {}
97 explicit test_allocator(int i) throw() : data_(i) {}
98 test_allocator(const test_allocator& a) throw()
100 template <class U> test_allocator(const test_allocator<U>& a) throw()
102 ~test_allocator() throw() {data_ = 0;}
104 friend bool operator==(const test_allocator& x, const test_allocator& y)
105 {return x.data_ == y.data_;}
106 friend bool operator!=(const test_allocator& x, const test_allocator& y)
111 class other_allocator
115 template <class U> friend class other_allocator;
118 typedef T value_type;
120 other_allocator() : data_(-1) {}
121 explicit other_allocator(int i) : data_(i) {}
122 template <class U> other_allocator(const other_allocator<U>& a)
124 T* allocate(std::size_t n)
125 {return (T*)std::malloc(n * sizeof(T));}
126 void deallocate(T* p, std::size_t n)
129 other_allocator select_on_container_copy_construction() const
130 {return other_allocator(-2);}
132 friend bool operator==(const other_allocator& x, const other_allocator& y)
133 {return x.data_ == y.data_;}
134 friend bool operator!=(const other_allocator& x, const other_allocator& y)
137 typedef std::true_type propagate_on_container_copy_assignment;
138 typedef std::true_type propagate_on_container_move_assignment;
139 typedef std::true_type propagate_on_container_swap;
141 #ifdef _LIBCPP_HAS_NO_ADVANCED_SFINAE
142 std::size_t max_size() const
143 {return UINT_MAX / sizeof(T);}
144 #endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
148 #endif // TEST_ALLOCATOR_H