allocator_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Boost.Function library
  2. // Copyright Douglas Gregor 2001-2003. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org
  7. #include <boost/function.hpp>
  8. #include <boost/core/lightweight_test.hpp>
  9. #include <cassert>
  10. #include <functional>
  11. using namespace std;
  12. using namespace boost;
  13. static int alloc_count = 0;
  14. static int dealloc_count = 0;
  15. template<typename T>
  16. struct counting_allocator : public std::allocator<T>
  17. {
  18. template<typename U>
  19. struct rebind
  20. {
  21. typedef counting_allocator<U> other;
  22. };
  23. counting_allocator()
  24. {
  25. }
  26. template<typename U>
  27. counting_allocator( counting_allocator<U> )
  28. {
  29. }
  30. T* allocate(std::size_t n)
  31. {
  32. alloc_count++;
  33. return std::allocator<T>::allocate(n);
  34. }
  35. void deallocate(T* p, std::size_t n)
  36. {
  37. dealloc_count++;
  38. std::allocator<T>::deallocate(p, n);
  39. }
  40. };
  41. struct enable_small_object_optimization
  42. {
  43. };
  44. struct disable_small_object_optimization
  45. {
  46. int unused_state_data[32];
  47. };
  48. template <typename base>
  49. struct plus_int: base
  50. {
  51. int operator()(int x, int y) const { return x + y; }
  52. };
  53. static int do_minus(int x, int y) { return x-y; }
  54. template <typename base>
  55. struct DoNothing: base
  56. {
  57. void operator()() const {}
  58. };
  59. static void do_nothing() {}
  60. int main()
  61. {
  62. function2<int, int, int> f;
  63. f.assign( plus_int<disable_small_object_optimization>(), counting_allocator<int>() );
  64. f.clear();
  65. BOOST_TEST_EQ( alloc_count, 1 );
  66. BOOST_TEST_EQ( dealloc_count, 1 );
  67. alloc_count = 0;
  68. dealloc_count = 0;
  69. f.assign( plus_int<enable_small_object_optimization>(), counting_allocator<int>() );
  70. f.clear();
  71. BOOST_TEST_EQ( alloc_count, 0 );
  72. BOOST_TEST_EQ( dealloc_count, 0 );
  73. f.assign( plus_int<disable_small_object_optimization>(), std::allocator<int>() );
  74. f.clear();
  75. f.assign( plus_int<enable_small_object_optimization>(), std::allocator<int>() );
  76. f.clear();
  77. alloc_count = 0;
  78. dealloc_count = 0;
  79. f.assign( &do_minus, counting_allocator<int>() );
  80. f.clear();
  81. BOOST_TEST_EQ( alloc_count, 0 );
  82. BOOST_TEST_EQ( dealloc_count, 0 );
  83. f.assign( &do_minus, std::allocator<int>() );
  84. f.clear();
  85. function0<void> fv;
  86. alloc_count = 0;
  87. dealloc_count = 0;
  88. fv.assign( DoNothing<disable_small_object_optimization>(), counting_allocator<int>() );
  89. fv.clear();
  90. BOOST_TEST_EQ( alloc_count, 1 );
  91. BOOST_TEST_EQ( dealloc_count, 1 );
  92. alloc_count = 0;
  93. dealloc_count = 0;
  94. fv.assign( DoNothing<enable_small_object_optimization>(), counting_allocator<int>() );
  95. fv.clear();
  96. BOOST_TEST_EQ( alloc_count, 0 );
  97. BOOST_TEST_EQ( dealloc_count, 0 );
  98. fv.assign( DoNothing<disable_small_object_optimization>(), std::allocator<int>() );
  99. fv.clear();
  100. fv.assign( DoNothing<enable_small_object_optimization>(), std::allocator<int>() );
  101. fv.clear();
  102. alloc_count = 0;
  103. dealloc_count = 0;
  104. fv.assign( &do_nothing, counting_allocator<int>() );
  105. fv.clear();
  106. BOOST_TEST_EQ( alloc_count, 0 );
  107. BOOST_TEST_EQ( dealloc_count, 0 );
  108. fv.assign( &do_nothing, std::allocator<int>() );
  109. fv.clear();
  110. function0<void> fv2;
  111. fv.assign(&do_nothing, std::allocator<int>() );
  112. fv2.assign(fv, std::allocator<int>() );
  113. return boost::report_errors();
  114. }