anonymous_shared_memory_test.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 <iostream>
  12. #include <boost/interprocess/mapped_region.hpp>
  13. #include <boost/interprocess/anonymous_shared_memory.hpp>
  14. #include <cstddef>
  15. #include <exception>
  16. using namespace boost::interprocess;
  17. int main ()
  18. {
  19. try{
  20. const std::size_t MemSize = 99999*2;
  21. {
  22. //Now check anonymous mapping
  23. mapped_region region(anonymous_shared_memory(MemSize));
  24. //Write pattern
  25. unsigned char *pattern = static_cast<unsigned char*>(region.get_address());
  26. for(std::size_t i = 0
  27. ;i < MemSize
  28. ;++i, ++pattern){
  29. *pattern = static_cast<unsigned char>(i);
  30. }
  31. //Check pattern
  32. pattern = static_cast<unsigned char*>(region.get_address());
  33. for(std::size_t i = 0
  34. ;i < MemSize
  35. ;++i, ++pattern){
  36. if(*pattern != static_cast<unsigned char>(i)){
  37. return 1;
  38. }
  39. }
  40. }
  41. }
  42. catch(std::exception &exc){
  43. std::cout << "Unhandled exception: " << exc.what() << std::endl;
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. #include <boost/interprocess/detail/config_end.hpp>