alloc_basic_test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/container/detail/dlmalloc.hpp>
  11. #include <boost/container/allocator.hpp>
  12. #include <boost/container/vector.hpp>
  13. #include <boost/container/list.hpp>
  14. using namespace boost::container;
  15. bool basic_test()
  16. {
  17. size_t received = 0;
  18. if(!dlmalloc_all_deallocated())
  19. return false;
  20. void *ptr = dlmalloc_alloc(50, 98, &received);
  21. if(dlmalloc_size(ptr) != received)
  22. return false;
  23. if(dlmalloc_allocated_memory() != dlmalloc_chunksize(ptr))
  24. return false;
  25. if(dlmalloc_all_deallocated())
  26. return false;
  27. dlmalloc_grow(ptr, received + 20, received + 30, &received);
  28. if(dlmalloc_allocated_memory() != dlmalloc_chunksize(ptr))
  29. return false;
  30. if(dlmalloc_size(ptr) != received)
  31. return false;
  32. if(!dlmalloc_shrink(ptr, 100, 140, &received, 1))
  33. return false;
  34. if(dlmalloc_allocated_memory() != dlmalloc_chunksize(ptr))
  35. return false;
  36. if(!dlmalloc_shrink(ptr, 0, 140, &received, 1))
  37. return false;
  38. if(dlmalloc_allocated_memory() != dlmalloc_chunksize(ptr))
  39. return false;
  40. if(dlmalloc_shrink(ptr, 0, received/2, &received, 1))
  41. return false;
  42. if(dlmalloc_allocated_memory() != dlmalloc_chunksize(ptr))
  43. return false;
  44. if(dlmalloc_size(ptr) != received)
  45. return false;
  46. dlmalloc_free(ptr);
  47. dlmalloc_malloc_check();
  48. if(!dlmalloc_all_deallocated())
  49. return false;
  50. return true;
  51. }
  52. bool vector_test()
  53. {
  54. typedef boost::container::vector<int, allocator<int> > Vector;
  55. if(!dlmalloc_all_deallocated())
  56. return false;
  57. {
  58. const int NumElem = 1000;
  59. Vector v;
  60. v.resize(NumElem);
  61. int *orig_buf = &v[0];
  62. int *new_buf = &v[0];
  63. while(orig_buf == new_buf){
  64. Vector::size_type cl = v.capacity() - v.size();
  65. while(cl--){
  66. v.push_back(0);
  67. }
  68. v.push_back(0);
  69. new_buf = &v[0];
  70. }
  71. }
  72. if(!dlmalloc_all_deallocated())
  73. return false;
  74. return true;
  75. }
  76. bool list_test()
  77. {
  78. typedef boost::container::list<int, allocator<int> > List;
  79. if(!dlmalloc_all_deallocated())
  80. return false;
  81. {
  82. const int NumElem = 1000;
  83. List l;
  84. int values[NumElem];
  85. l.insert(l.end(), &values[0], &values[NumElem]);
  86. }
  87. if(!dlmalloc_all_deallocated())
  88. return false;
  89. return true;
  90. }
  91. int main()
  92. {
  93. if(!basic_test())
  94. return 1;
  95. if(!vector_test())
  96. return 1;
  97. if(!list_test())
  98. return 1;
  99. return 0;
  100. }