1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // Copyright (C) 2011 Vicente J. Botet Escriba 11 // 12 // Distributed under the Boost Software License, Version 1.0. (See accompanying 13 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 14 15 #ifndef BOOST_THREAD_TEST_ALLOCATOR_HPP 16 #define BOOST_THREAD_TEST_ALLOCATOR_HPP 17 18 #include <cstddef> 19 #include <boost/type_traits.hpp> 20 #include <boost/thread/detail/move.hpp> 21 #include <cstdlib> 22 #include <new> 23 #include <climits> 24 25 class test_alloc_base 26 { 27 public: 28 static int count; 29 public: 30 static int throw_after; 31 }; 32 33 int test_alloc_base::count = 0; 34 int test_alloc_base::throw_after = INT_MAX; 35 36 template <class T> 37 class test_allocator 38 : public test_alloc_base 39 { 40 int data_; 41 42 template <class U> friend class test_allocator; 43 public: 44 45 typedef unsigned size_type; 46 typedef int difference_type; 47 typedef T value_type; 48 typedef value_type* pointer; 49 typedef const value_type* const_pointer; 50 typedef typename boost::add_lvalue_reference<value_type>::type reference; 51 typedef typename boost::add_lvalue_reference<const value_type>::type const_reference; 52 53 template <class U> struct rebind {typedef test_allocator<U> other;}; 54 test_allocator()55 test_allocator() throw() : data_(-1) {} test_allocator(int i)56 explicit test_allocator(int i) throw() : data_(i) {} test_allocator(const test_allocator & a)57 test_allocator(const test_allocator& a) throw() 58 : data_(a.data_) {} test_allocator(const test_allocator<U> & a)59 template <class U> test_allocator(const test_allocator<U>& a) throw() 60 : data_(a.data_) {} ~test_allocator()61 ~test_allocator() throw() {data_ = 0;} address(reference x) const62 pointer address(reference x) const {return &x;} address(const_reference x) const63 const_pointer address(const_reference x) const {return &x;} allocate(size_type n,const void * =0)64 pointer allocate(size_type n, const void* = 0) 65 { 66 if (count >= throw_after) 67 throw std::bad_alloc(); 68 ++count; 69 return (pointer)std::malloc(n * sizeof(T)); 70 } deallocate(pointer p,size_type)71 void deallocate(pointer p, size_type) 72 {--count; std::free(p);} max_size() const73 size_type max_size() const throw() 74 {return UINT_MAX / sizeof(T);} construct(pointer p,const T & val)75 void construct(pointer p, const T& val) 76 {::new(p) T(val);} 77 construct(pointer p,BOOST_THREAD_RV_REF (T)val)78 void construct(pointer p, BOOST_THREAD_RV_REF(T) val) 79 {::new(p) T(boost::move(val));} 80 destroy(pointer p)81 void destroy(pointer p) {p->~T();} 82 operator ==(const test_allocator & x,const test_allocator & y)83 friend bool operator==(const test_allocator& x, const test_allocator& y) 84 {return x.data_ == y.data_;} operator !=(const test_allocator & x,const test_allocator & y)85 friend bool operator!=(const test_allocator& x, const test_allocator& y) 86 {return !(x == y);} 87 }; 88 89 template <> 90 class test_allocator<void> 91 : public test_alloc_base 92 { 93 int data_; 94 95 template <class U> friend class test_allocator; 96 public: 97 98 typedef unsigned size_type; 99 typedef int difference_type; 100 typedef void value_type; 101 typedef value_type* pointer; 102 typedef const value_type* const_pointer; 103 104 template <class U> struct rebind {typedef test_allocator<U> other;}; 105 test_allocator()106 test_allocator() throw() : data_(-1) {} test_allocator(int i)107 explicit test_allocator(int i) throw() : data_(i) {} test_allocator(const test_allocator & a)108 test_allocator(const test_allocator& a) throw() 109 : data_(a.data_) {} test_allocator(const test_allocator<U> & a)110 template <class U> test_allocator(const test_allocator<U>& a) throw() 111 : data_(a.data_) {} ~test_allocator()112 ~test_allocator() throw() {data_ = 0;} 113 operator ==(const test_allocator & x,const test_allocator & y)114 friend bool operator==(const test_allocator& x, const test_allocator& y) 115 {return x.data_ == y.data_;} operator !=(const test_allocator & x,const test_allocator & y)116 friend bool operator!=(const test_allocator& x, const test_allocator& y) 117 {return !(x == y);} 118 }; 119 120 template <class T> 121 class other_allocator 122 { 123 int data_; 124 125 template <class U> friend class other_allocator; 126 127 public: 128 typedef T value_type; 129 other_allocator()130 other_allocator() : data_(-1) {} other_allocator(int i)131 explicit other_allocator(int i) : data_(i) {} other_allocator(const other_allocator<U> & a)132 template <class U> other_allocator(const other_allocator<U>& a) 133 : data_(a.data_) {} allocate(std::size_t n)134 T* allocate(std::size_t n) 135 {return (T*)std::malloc(n * sizeof(T));} deallocate(T * p,std::size_t)136 void deallocate(T* p, std::size_t) 137 {std::free(p);} 138 select_on_container_copy_construction() const139 other_allocator select_on_container_copy_construction() const 140 {return other_allocator(-2);} 141 operator ==(const other_allocator & x,const other_allocator & y)142 friend bool operator==(const other_allocator& x, const other_allocator& y) 143 {return x.data_ == y.data_;} operator !=(const other_allocator & x,const other_allocator & y)144 friend bool operator!=(const other_allocator& x, const other_allocator& y) 145 {return !(x == y);} 146 147 typedef boost::true_type propagate_on_container_copy_assignment; 148 typedef boost::true_type propagate_on_container_move_assignment; 149 typedef boost::true_type propagate_on_container_swap; 150 151 #ifdef BOOST_NO_SFINAE_EXPR max_size() const152 std::size_t max_size() const 153 {return UINT_MAX / sizeof(T);} 154 #endif // BOOST_NO_SFINAE_EXPR 155 156 }; 157 158 #endif // BOOST_THREAD_TEST_ALLOCATOR_HPP 159