allocexcept_test.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2004-2012. 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/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/interprocess/detail/config_begin.hpp>
  11. #include <boost/interprocess/allocators/allocator.hpp>
  12. #include <boost/interprocess/managed_shared_memory.hpp>
  13. #include <boost/interprocess/containers/vector.hpp>
  14. #include <boost/interprocess/containers/list.hpp>
  15. #include <iostream>
  16. #include <functional>
  17. #include "print_container.hpp"
  18. #include <string>
  19. #include "get_process_id_name.hpp"
  20. struct InstanceCounter
  21. {
  22. InstanceCounter(){++counter;}
  23. InstanceCounter(const InstanceCounter&){++counter;}
  24. ~InstanceCounter(){--counter;}
  25. static std::size_t counter;
  26. };
  27. std::size_t InstanceCounter::counter = 0;
  28. using namespace boost::interprocess;
  29. int main ()
  30. {
  31. const int memsize = 16384;
  32. const char *const shMemName = test::get_process_id_name();
  33. try{
  34. shared_memory_object::remove(shMemName);
  35. //Named allocate capable shared mem allocator
  36. managed_shared_memory segment(create_only, shMemName, memsize);
  37. //STL compatible allocator object, uses allocate(), deallocate() functions
  38. typedef allocator<InstanceCounter,
  39. managed_shared_memory::segment_manager>
  40. inst_allocator_t;
  41. const inst_allocator_t myallocator (segment.get_segment_manager());
  42. typedef vector<InstanceCounter, inst_allocator_t> MyVect;
  43. //We'll provoke an exception, let's see if exception handling works
  44. try{
  45. //Fill vector until there is no more memory
  46. MyVect myvec(myallocator);
  47. int i;
  48. for(i = 0; true; ++i){
  49. myvec.push_back(InstanceCounter());
  50. }
  51. }
  52. catch(boost::interprocess::bad_alloc &){
  53. if(InstanceCounter::counter != 0)
  54. return 1;
  55. }
  56. //We'll provoke an exception, let's see if exception handling works
  57. try{
  58. //Fill vector at the beginning until there is no more memory
  59. MyVect myvec(myallocator);
  60. int i;
  61. InstanceCounter ic;
  62. for(i = 0; true; ++i){
  63. myvec.insert(myvec.begin(), i, ic);
  64. }
  65. }
  66. catch(boost::interprocess::bad_alloc &){
  67. if(InstanceCounter::counter != 0)
  68. return 1;
  69. }
  70. catch(std::length_error &){
  71. if(InstanceCounter::counter != 0)
  72. return 1;
  73. }
  74. }
  75. catch(...){
  76. shared_memory_object::remove(shMemName);
  77. throw;
  78. }
  79. shared_memory_object::remove(shMemName);
  80. return 0;
  81. }
  82. #include <boost/interprocess/detail/config_end.hpp>