data_test.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <functional>
  16. #include <string>
  17. #include "print_container.hpp"
  18. #include "get_process_id_name.hpp"
  19. using namespace boost::interprocess;
  20. int main ()
  21. {
  22. const int memsize = 65536;
  23. std::string process_name;
  24. test::get_process_id_name(process_name);
  25. const char *const shMemName = process_name.c_str();
  26. try{
  27. shared_memory_object::remove(shMemName);
  28. //Create shared memory
  29. managed_shared_memory segment(create_only, shMemName, memsize);
  30. //STL compatible allocator object, uses allocate(), deallocate() functions
  31. typedef allocator<int, managed_shared_memory::segment_manager>
  32. shmem_allocator_int_t;
  33. const shmem_allocator_int_t myallocator (segment.get_segment_manager());
  34. const int max = 100;
  35. void *array[max];
  36. const char *allocName = "testAllocation";
  37. typedef boost::interprocess::vector<int, shmem_allocator_int_t > MyVect;
  38. //---- ALLOC, NAMED_ALLOC, NAMED_NEW TEST ----//
  39. {
  40. int i;
  41. //Let's allocate some memory
  42. for(i = 0; i < max; ++i){
  43. array[i] = segment.allocate(i+1);
  44. }
  45. //Deallocate allocated memory
  46. for(i = 0; i < max; ++i){
  47. segment.deallocate(array[i]);
  48. }
  49. bool res;
  50. MyVect *shmem_vect;
  51. //Construct and find
  52. shmem_vect = segment.construct<MyVect> (allocName) (myallocator);
  53. res = (shmem_vect == segment.find<MyVect>(allocName).first);
  54. if(!res)
  55. return 1;
  56. //Destroy and check it is not present
  57. segment.destroy<MyVect> (allocName);
  58. res = (0 == segment.find<MyVect>(allocName).first);
  59. if(!res)
  60. return 1;
  61. //Construct, dump to a file
  62. shmem_vect = segment.construct<MyVect> (allocName) (myallocator);
  63. if(shmem_vect != segment.find<MyVect>(allocName).first)
  64. return 1;
  65. //Destroy and check it is not present
  66. segment.destroy<MyVect> (allocName);
  67. res = (0 == segment.find<MyVect>(allocName).first);
  68. if(!res)
  69. return 1;
  70. }
  71. }
  72. catch(...){
  73. shared_memory_object::remove(shMemName);
  74. throw;
  75. }
  76. shared_memory_object::remove(shMemName);
  77. return 0;
  78. }
  79. #include <boost/interprocess/detail/config_end.hpp>