test_allocator.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // Copyright (C) 2011 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_THREAD_TEST_ALLOCATOR_HPP
  14. #define BOOST_THREAD_TEST_ALLOCATOR_HPP
  15. #include <cstddef>
  16. #include <boost/type_traits.hpp>
  17. #include <boost/thread/detail/move.hpp>
  18. #include <cstdlib>
  19. #include <new>
  20. #include <climits>
  21. class test_alloc_base
  22. {
  23. public:
  24. static int count;
  25. public:
  26. static int throw_after;
  27. };
  28. int test_alloc_base::count = 0;
  29. int test_alloc_base::throw_after = INT_MAX;
  30. template <class T>
  31. class test_allocator
  32. : public test_alloc_base
  33. {
  34. int data_;
  35. template <class U> friend class test_allocator;
  36. public:
  37. typedef unsigned size_type;
  38. typedef int difference_type;
  39. typedef T value_type;
  40. typedef value_type* pointer;
  41. typedef const value_type* const_pointer;
  42. typedef typename boost::add_lvalue_reference<value_type>::type reference;
  43. typedef typename boost::add_lvalue_reference<const value_type>::type const_reference;
  44. template <class U> struct rebind {typedef test_allocator<U> other;};
  45. test_allocator() throw() : data_(-1) {}
  46. explicit test_allocator(int i) throw() : data_(i) {}
  47. test_allocator(const test_allocator& a) throw()
  48. : data_(a.data_) {}
  49. template <class U> test_allocator(const test_allocator<U>& a) throw()
  50. : data_(a.data_) {}
  51. ~test_allocator() throw() {data_ = 0;}
  52. pointer address(reference x) const {return &x;}
  53. const_pointer address(const_reference x) const {return &x;}
  54. pointer allocate(size_type n, const void* = 0)
  55. {
  56. if (count >= throw_after)
  57. throw std::bad_alloc();
  58. ++count;
  59. return (pointer)std::malloc(n * sizeof(T));
  60. }
  61. void deallocate(pointer p, size_type)
  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)
  66. {::new(p) T(val);}
  67. void construct(pointer p, BOOST_THREAD_RV_REF(T) val)
  68. {::new(p) T(boost::move(val));}
  69. void destroy(pointer p) {p->~T();}
  70. friend bool operator==(const test_allocator& x, const test_allocator& y)
  71. {return x.data_ == y.data_;}
  72. friend bool operator!=(const test_allocator& x, const test_allocator& y)
  73. {return !(x == y);}
  74. };
  75. template <>
  76. class test_allocator<void>
  77. : public test_alloc_base
  78. {
  79. int data_;
  80. template <class U> friend class test_allocator;
  81. public:
  82. typedef unsigned size_type;
  83. typedef int difference_type;
  84. typedef void value_type;
  85. typedef value_type* pointer;
  86. typedef const value_type* const_pointer;
  87. template <class U> struct rebind {typedef test_allocator<U> other;};
  88. test_allocator() throw() : data_(-1) {}
  89. explicit test_allocator(int i) throw() : data_(i) {}
  90. test_allocator(const test_allocator& a) throw()
  91. : data_(a.data_) {}
  92. template <class U> test_allocator(const test_allocator<U>& a) throw()
  93. : data_(a.data_) {}
  94. ~test_allocator() throw() {data_ = 0;}
  95. friend bool operator==(const test_allocator& x, const test_allocator& y)
  96. {return x.data_ == y.data_;}
  97. friend bool operator!=(const test_allocator& x, const test_allocator& y)
  98. {return !(x == y);}
  99. };
  100. template <class T>
  101. class other_allocator
  102. {
  103. int data_;
  104. template <class U> friend class other_allocator;
  105. public:
  106. typedef T value_type;
  107. other_allocator() : data_(-1) {}
  108. explicit other_allocator(int i) : data_(i) {}
  109. template <class U> other_allocator(const other_allocator<U>& a)
  110. : data_(a.data_) {}
  111. T* allocate(std::size_t n)
  112. {return (T*)std::malloc(n * sizeof(T));}
  113. void deallocate(T* p, std::size_t)
  114. {std::free(p);}
  115. other_allocator select_on_container_copy_construction() const
  116. {return other_allocator(-2);}
  117. friend bool operator==(const other_allocator& x, const other_allocator& y)
  118. {return x.data_ == y.data_;}
  119. friend bool operator!=(const other_allocator& x, const other_allocator& y)
  120. {return !(x == y);}
  121. typedef boost::true_type propagate_on_container_copy_assignment;
  122. typedef boost::true_type propagate_on_container_move_assignment;
  123. typedef boost::true_type propagate_on_container_swap;
  124. #ifdef BOOST_NO_SFINAE_EXPR
  125. std::size_t max_size() const
  126. {return UINT_MAX / sizeof(T);}
  127. #endif // BOOST_NO_SFINAE_EXPR
  128. };
  129. #endif // BOOST_THREAD_TEST_ALLOCATOR_HPP