Advertisement
Guest User

testPushBackVector.cc

a guest
Jul 19th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <boost/interprocess/shared_memory_object.hpp>
  2. #include <boost/interprocess/managed_shared_memory.hpp>
  3. #include <boost/interprocess/offset_ptr.hpp>
  4. #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
  5. #include <boost/interprocess/smart_ptr/weak_ptr.hpp>
  6. #include <boost/interprocess/allocators/allocator.hpp>
  7. #include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
  8. #include <boost/interprocess/containers/map.hpp>
  9. #include <boost/interprocess/containers/vector.hpp>
  10. #include <boost/multi_index_container.hpp>
  11. #include <boost/multi_index/ordered_index.hpp>
  12. #include <boost/multi_index/hashed_index.hpp>
  13. #include <boost/multi_index/member.hpp>
  14. #include <boost/multi_index/mem_fun.hpp>
  15.  
  16. using boost::interprocess::managed_shared_memory;
  17. using boost::interprocess::allocator;
  18. using boost::interprocess::managed_shared_ptr;
  19. using boost::interprocess::managed_weak_ptr;
  20. using boost::interprocess::anonymous_instance;
  21. using boost::interprocess::make_managed_shared_ptr;
  22.  
  23. typedef managed_shared_memory::segment_manager  segment_manager_t;
  24. typedef allocator<void, segment_manager_t> void_allocator;
  25. typedef long long  idType;
  26. typedef unsigned long long  releaseType;
  27.  
  28. // Define a template SharableDataTypes
  29. template<class T, class Key=int> struct SharableDataTypes {
  30.     typedef typename managed_shared_ptr<T, managed_shared_memory>::type         SharedPtrType;
  31.     typedef typename managed_weak_ptr  <T, managed_shared_memory>::type         WeakPtrType;
  32.  
  33.     typedef allocator<WeakPtrType, segment_manager_t>                           WeakPtrAllocatorType;
  34.  
  35.     typedef boost::interprocess::vector<WeakPtrType , WeakPtrAllocatorType>     WeakPtrVectorType;
  36. };
  37.  
  38. class Data{
  39. private:
  40.     idType id;
  41.     releaseType creationRelease;
  42. public:
  43.     Data(idType i, releaseType r){ id=i; creationRelease=r;}
  44.     const idType getObjectId() const {return id;}
  45.     const releaseType getCreationRelease() const {return creationRelease;}
  46.  
  47.     // Define type of container storing this kind of data
  48.     typedef boost::multi_index_container<
  49.             SharableDataTypes<Data>::SharedPtrType,
  50.             boost::multi_index::indexed_by<
  51.             boost::multi_index::hashed_unique<BOOST_MULTI_INDEX_CONST_MEM_FUN(Data, const idType, getObjectId)>
  52.     ,boost::multi_index::ordered_non_unique< boost::multi_index::tag<releaseType>, BOOST_MULTI_INDEX_CONST_MEM_FUN(Data, const releaseType,  getCreationRelease)>
  53.  
  54.     >,
  55.     boost::interprocess::allocator<SharableDataTypes<Data>::SharedPtrType, segment_manager_t>
  56.     > DataContainerType;
  57.  
  58. };
  59.  
  60. class DataVectorContainer{
  61. private:
  62.     SharableDataTypes<Data>::WeakPtrVectorType dataWPVector;
  63. public:
  64.     DataVectorContainer(void_allocator * alloc) : dataWPVector(*alloc){};
  65.  
  66.     void addData (const SharableDataTypes<Data>::SharedPtrType & PData){
  67.  
  68.         unsigned int sizeBefore = dataWPVector.size();
  69.         unsigned int capacityBefore = dataWPVector.capacity();
  70.         dataWPVector.push_back(PData);
  71. //      std::cerr << "adddataWPVectorElem called with PData.id =" << PData->getObjectId() << " from thread " <<  getpid() << ":" << pthread_self() << std::endl;
  72.  
  73.         unsigned int sizeAfter = dataWPVector.size();
  74.         unsigned int capacityAfter = dataWPVector.capacity();
  75.  
  76.         if (dataWPVector.rbegin()->lock()->getObjectId() != PData->getObjectId()){
  77.             std::cerr << "After pushback in dataWPVector vector last elem Id=" <<  dataWPVector.rbegin()->lock()->getObjectId() << " added cable Id =" << PData->getObjectId() << std::endl;
  78.             std::cerr << "--> sizeBefore=" << sizeBefore  << ", capacityBefore=" << capacityBefore << ", sizeAfter=" << sizeAfter<< ", capacityAfter=" << capacityAfter << std::endl;
  79.             printData();
  80.             exit(1);
  81.         }else{
  82.  
  83.             if (capacityBefore != capacityAfter){
  84.                 std::cerr << "After pushback in dataWPVector capacity changed from  " << capacityBefore << " to " << capacityAfter << " without any damage." << std::endl;
  85.                 // printData();
  86.             }
  87.         }
  88.     }
  89.  
  90.     void printData(){
  91.         std::ostringstream oss;
  92.         oss << "dataWPVector[" << std::endl;
  93.         for (SharableDataTypes<Data>::WeakPtrVectorType::iterator cableIt = dataWPVector.begin(); cableIt != dataWPVector.end(); ++cableIt){
  94.             oss << cableIt->lock()->getObjectId() << ", ";
  95.         }
  96.         oss << "]" << std::endl;
  97.         std::cerr << oss.str();
  98.     }
  99. };
  100.  
  101. int main(int argc, char **argv) {
  102.     boost::interprocess::shared_memory_object::remove("testPushBackVector");
  103.     managed_shared_memory * managedSharedMemorySegment = new managed_shared_memory(boost::interprocess::open_or_create, "testPushBackVector", 1024*1024*10);
  104.     void_allocator * managedSharedMemoryAllocator = new void_allocator(managedSharedMemorySegment->get_segment_manager());
  105.  
  106.     Data::DataContainerType * dataContainer = managedSharedMemorySegment->construct<Data::DataContainerType >
  107.     (anonymous_instance)(
  108.             Data::DataContainerType::ctor_args_list(),
  109.             Data::DataContainerType::allocator_type(managedSharedMemorySegment->get_segment_manager())
  110.     );
  111.  
  112.     SharableDataTypes<DataVectorContainer>::SharedPtrType myDataVectorContainer = make_managed_shared_ptr(managedSharedMemorySegment->
  113.             construct<DataVectorContainer>(anonymous_instance)(managedSharedMemoryAllocator), *managedSharedMemorySegment);
  114.  
  115.  
  116.     for (unsigned int i = 1; i <= 10000; ++i){
  117.         SharableDataTypes<Data>::SharedPtrType myData = make_managed_shared_ptr(managedSharedMemorySegment->
  118.                 construct<Data>(anonymous_instance)(2*i, i), *managedSharedMemorySegment);
  119.  
  120.         dataContainer->insert(myData);
  121.         myDataVectorContainer->addData(myData);
  122.     }
  123.  
  124. //  myDataVectorContainer->printData();
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement