int list_test (bool copied_allocators_equal = true)
{
   typedef std::list<int> MyStdList;
   typedef typename MyShmList::value_type IntType;
   const int memsize = 65536;
   const char *const shMemName = test::get_process_id_name();
   const int max = 100;
   typedef push_data_function<DoublyLinked> push_data_t;

   try{
      //Named new capable shared mem allocator
      //Create shared memory
      shared_memory_object::remove(shMemName);
      ManagedSharedMemory segment(create_only, shMemName, memsize);

      segment.reserve_named_objects(100);

      //Shared memory allocator must be always be initialized
      //since it has no default constructor
      MyShmList *shmlist = segment.template construct<MyShmList>("MyList")
                              (segment.get_segment_manager());


      MyStdList *stdlist = new MyStdList;

      if(push_data_t::execute(max, shmlist, stdlist)){
         return 1;
      }

      shmlist->erase(shmlist->begin()++);
      stdlist->erase(stdlist->begin()++);
      if(!CheckEqualContainers(shmlist, stdlist)) return 1;

      if(pop_back_function<DoublyLinked>::execute(shmlist, stdlist)){
         return 1;
      }

      shmlist->pop_front();
      stdlist->pop_front();
      if(!CheckEqualContainers(shmlist, stdlist)) return 1;

      {
         IntType aux_vect[50];
         for(int i = 0; i < 50; ++i){
            IntType move_me(-1);
            aux_vect[i] = boost::interprocess::move(move_me);
         }
         int aux_vect2[50];
         for(int i = 0; i < 50; ++i){
            aux_vect2[i] = -1;
         }
         shmlist->assign(boost::interprocess::make_move_iterator(&aux_vect[0])
                        ,boost::interprocess::make_move_iterator(&aux_vect[50]));
         stdlist->assign(&aux_vect2[0], &aux_vect2[50]);
         if(!CheckEqualContainers(shmlist, stdlist)) return 1;
      }

      if(copied_allocators_equal){
         shmlist->sort();
         stdlist->sort();
         if(!CheckEqualContainers(shmlist, stdlist)) return 1;
      }

      shmlist->reverse();
      stdlist->reverse();
      if(!CheckEqualContainers(shmlist, stdlist)) return 1;

      shmlist->reverse();
      stdlist->reverse();
      if(!CheckEqualContainers(shmlist, stdlist)) return 1;

      {
         IntType aux_vect[50];
         for(int i = 0; i < 50; ++i){
            IntType move_me(-1);
            aux_vect[i] = boost::interprocess::move(move_me);
         }
         int aux_vect2[50];
         for(int i = 0; i < 50; ++i){
            aux_vect2[i] = -1;
         }
         shmlist->insert(shmlist->begin()
                        ,boost::interprocess::make_move_iterator(&aux_vect[0])
                        ,boost::interprocess::make_move_iterator(&aux_vect[50]));
         stdlist->insert(stdlist->begin(), &aux_vect2[0], &aux_vect2[50]);
      }

      shmlist->unique();
      stdlist->unique();
      if(!CheckEqualContainers(shmlist, stdlist))
         return 1;

      if(copied_allocators_equal){
         shmlist->sort(std::greater<IntType>());
         stdlist->sort(std::greater<int>());
         if(!CheckEqualContainers(shmlist, stdlist))
            return 1;
      }

      shmlist->resize(25);
      stdlist->resize(25);
      shmlist->resize(50);
      stdlist->resize(50);
      shmlist->resize(0);
      stdlist->resize(0);
      if(!CheckEqualContainers(shmlist, stdlist))
         return 1;

      if(push_data_t::execute(max, shmlist, stdlist)){
         return 1;
      }
      {
         MyShmList othershmlist(shmlist->get_allocator());
         MyStdList otherstdlist;

         int listsize = (int)shmlist->size();

         if(push_data_t::execute(listsize, shmlist, stdlist)){
            return 1;
         }

         if(copied_allocators_equal){
            shmlist->splice(shmlist->begin(), othershmlist);
            stdlist->splice(stdlist->begin(), otherstdlist);
            if(!CheckEqualContainers(shmlist, stdlist))
               return 1;   
         }

         listsize = (int)shmlist->size();

         if(push_data_t::execute(listsize, shmlist, stdlist)){
            return 1;
         }

         if(push_data_t::execute(listsize, &othershmlist, &otherstdlist)){
            return 1;
         }

         if(copied_allocators_equal){
            shmlist->sort(std::greater<IntType>());
            stdlist->sort(std::greater<int>());
            if(!CheckEqualContainers(shmlist, stdlist))
               return 1;

            othershmlist.sort(std::greater<IntType>());
            otherstdlist.sort(std::greater<int>());
            if(!CheckEqualContainers(&othershmlist, &otherstdlist))
               return 1;

            shmlist->merge(othershmlist, std::greater<IntType>());
            stdlist->merge(otherstdlist, std::greater<int>());
            if(!CheckEqualContainers(shmlist, stdlist))
               return 1;
         }
      }

      segment.template destroy<MyShmList>("MyList");
      delete stdlist;
      segment.shrink_to_fit_indexes();

      if(!segment.all_memory_deallocated())
         return 1;
   }
   catch(...){
      shared_memory_object::remove(shMemName);
      throw;
   }
   shared_memory_object::remove(shMemName);
   return 0;
}
Esempio n. 2
0
int list_test (bool copied_allocators_equal = true)
{
   typedef std::list<int> MyStdList;
   typedef typename MyBoostList::value_type IntType;
   const int max = 100;
   typedef list_push_data_function<DoublyLinked> push_data_t;

   BOOST_TRY{
      MyBoostList *boostlist = new MyBoostList;
      MyStdList *stdlist = new MyStdList;

      if(push_data_t::execute(max, boostlist, stdlist)){
         return 1;
      }

      boostlist->erase(boostlist->begin()++);
      stdlist->erase(stdlist->begin()++);
      if(!CheckEqualContainers(boostlist, stdlist)) return 1;

      if(list_pop_back_function<DoublyLinked>::execute(boostlist, stdlist)){
         return 1;
      }

      boostlist->pop_front();
      stdlist->pop_front();
      if(!CheckEqualContainers(boostlist, stdlist)) return 1;

      {
         IntType aux_vect[50];
         for(int i = 0; i < 50; ++i){
            IntType move_me(-1);
            aux_vect[i] = boost::move(move_me);
         }
         int aux_vect2[50];
         for(int i = 0; i < 50; ++i){
            aux_vect2[i] = -1;
         }
         boostlist->assign(boost::make_move_iterator(&aux_vect[0])
                          ,boost::make_move_iterator(&aux_vect[50]));
         stdlist->assign(&aux_vect2[0], &aux_vect2[50]);
         if(!CheckEqualContainers(boostlist, stdlist)) return 1;

         for(int i = 0; i < 50; ++i){
            IntType move_me(-1);
            aux_vect[i] = boost::move(move_me);
         }

         for(int i = 0; i < 50; ++i){
            aux_vect2[i] = -1;
         }
         boostlist->assign(boost::make_move_iterator(make_input_from_forward_iterator(&aux_vect[0]))
                          ,boost::make_move_iterator(make_input_from_forward_iterator(&aux_vect[50])));
         stdlist->assign(&aux_vect2[0], &aux_vect2[50]);
         if(!CheckEqualContainers(boostlist, stdlist)) return 1;
      }

      if(copied_allocators_equal){
         boostlist->sort();
         stdlist->sort();
         if(!CheckEqualContainers(boostlist, stdlist)) return 1;
      }

      boostlist->reverse();
      stdlist->reverse();
      if(!CheckEqualContainers(boostlist, stdlist)) return 1;

      boostlist->reverse();
      stdlist->reverse();
      if(!CheckEqualContainers(boostlist, stdlist)) return 1;

      {
         IntType aux_vect[50];
         for(int i = 0; i < 50; ++i){
            IntType move_me(-1);
            aux_vect[i] = boost::move(move_me);
         }
         int aux_vect2[50];
         for(int i = 0; i < 50; ++i){
            aux_vect2[i] = -1;
         }
         typename MyBoostList::iterator old_begin = boostlist->begin();
         typename MyBoostList::iterator it_insert =
            boostlist->insert(boostlist->begin()
                        ,boost::make_move_iterator(&aux_vect[0])
                        ,boost::make_move_iterator(&aux_vect[50]));
         if(it_insert != boostlist->begin() || std::distance(it_insert, old_begin) != 50)
            return 1;

         stdlist->insert(stdlist->begin(), &aux_vect2[0], &aux_vect2[50]);
         if(!CheckEqualContainers(boostlist, stdlist))
            return 1;

         for(int i = 0; i < 50; ++i){
            IntType move_me(-1);
            aux_vect[i] = boost::move(move_me);
         }

         for(int i = 0; i < 50; ++i){
            aux_vect2[i] = -1;
         }

         old_begin = boostlist->begin();
         it_insert = boostlist->insert(boostlist->end()
                        ,boost::make_move_iterator(make_input_from_forward_iterator(&aux_vect[0]))
                        ,boost::make_move_iterator(make_input_from_forward_iterator(&aux_vect[50])));
         if(std::distance(it_insert, boostlist->end()) != 50)
            return 1;
         stdlist->insert(stdlist->end(), &aux_vect2[0], &aux_vect2[50]);
         if(!CheckEqualContainers(boostlist, stdlist))
            return 1;
      }

      boostlist->unique();
      stdlist->unique();
      if(!CheckEqualContainers(boostlist, stdlist))
         return 1;

      if(copied_allocators_equal){
         boostlist->sort(std::greater<IntType>());
         stdlist->sort(std::greater<int>());
         if(!CheckEqualContainers(boostlist, stdlist))
            return 1;
      }

      for(int i = 0; i < max; ++i){
         IntType new_int(i);
         boostlist->insert(boostlist->end(), boost::move(new_int));
         stdlist->insert(stdlist->end(), i);
         if(!test::CheckEqualContainers(boostlist, stdlist)) return 1;
      }
      if(!test::CheckEqualContainers(boostlist, stdlist)) return 1;

      boostlist->resize(25);
      stdlist->resize(25);
      boostlist->resize(50);
      stdlist->resize(50);
      boostlist->resize(0);
      stdlist->resize(0);
      if(!CheckEqualContainers(boostlist, stdlist))
         return 1;

      if(push_data_t::execute(max, boostlist, stdlist)){
         return 1;
      }
      {
         MyBoostList otherboostlist(boostlist->get_allocator());
         MyStdList otherstdlist;

         int listsize = (int)boostlist->size();

         if(push_data_t::execute(listsize, boostlist, stdlist)){
            return 1;
         }

         if(copied_allocators_equal){
            boostlist->splice(boostlist->begin(), otherboostlist);
            stdlist->splice(stdlist->begin(), otherstdlist);
            if(!CheckEqualContainers(boostlist, stdlist))
               return 1;  
         }

         listsize = (int)boostlist->size();

         if(push_data_t::execute(listsize, boostlist, stdlist)){
            return 1;
         }

         if(push_data_t::execute(listsize, &otherboostlist, &otherstdlist)){
            return 1;
         }

         if(copied_allocators_equal){
            boostlist->sort(std::greater<IntType>());
            stdlist->sort(std::greater<int>());
            if(!CheckEqualContainers(boostlist, stdlist))
               return 1;

            otherboostlist.sort(std::greater<IntType>());
            otherstdlist.sort(std::greater<int>());
            if(!CheckEqualContainers(&otherboostlist, &otherstdlist))
               return 1;

            boostlist->merge(otherboostlist, std::greater<IntType>());
            stdlist->merge(otherstdlist, std::greater<int>());
            if(!CheckEqualContainers(boostlist, stdlist))
               return 1;
         }

         if(!list_copyable_only(boostlist, stdlist
                        ,container_detail::bool_<boost::container::test::is_copyable<IntType>::value>())){
            return 1;
         }
      }

      delete boostlist;
      delete stdlist;
   }
   BOOST_CATCH(...){
      BOOST_RETHROW;
   }
   BOOST_CATCH_END
   return 0;
}
Esempio n. 3
0
int main ()
{
   //Create the user memory who will store all objects
   const int size_aligner  = sizeof(::boost::detail::max_align);
   const int memsize       = 65536/size_aligner*size_aligner;
   static ::boost::detail::max_align static_buffer[memsize/size_aligner];

   {
      //Now test move semantics
      managed_heap_memory original(memsize);
      managed_heap_memory move_ctor(boost::move(original));
      managed_heap_memory move_assign;
      move_assign = boost::move(move_ctor);
      original.swap(move_assign);
   }
   {
      //Now test move semantics
      managed_external_buffer original(create_only, static_buffer, memsize);
      managed_external_buffer move_ctor(boost::move(original));
      managed_external_buffer move_assign;
      move_assign = boost::move(move_ctor);
      original.swap(move_assign);
   }

   //Named new capable user mem allocator
   wmanaged_external_buffer user_buffer(create_only, static_buffer, memsize);

   //Named new capable heap mem allocator
   wmanaged_heap_memory heap_buffer(memsize);

   //Test move semantics
   {
      wmanaged_external_buffer user_default;
      wmanaged_external_buffer temp_external(boost::move(user_buffer));
      user_default = boost::move(temp_external);
      user_buffer  = boost::move(user_default);
      wmanaged_heap_memory heap_default;
      wmanaged_heap_memory temp_heap(boost::move(heap_buffer));
      heap_default = boost::move(temp_heap);
      heap_buffer  = boost::move(heap_default);
   }

   //Initialize memory
   user_buffer.reserve_named_objects(100);
   heap_buffer.reserve_named_objects(100);

   //User memory allocator must be always be initialized
   //since it has no default constructor
   MyUserList *userlist = user_buffer.construct<MyUserList>(L"MyUserList")
                           (user_buffer.get_segment_manager());

   MyHeapList *heaplist = heap_buffer.construct<MyHeapList>(L"MyHeapList")
                           (heap_buffer.get_segment_manager());

   //Alias heap list
   typedef std::list<int>   MyStdList;
   MyStdList *stdlist = new MyStdList;

   int i;
   const int max = 100;
   for(i = 0; i < max; ++i){
      userlist->push_back(i);
      heaplist->push_back(i);
      stdlist->push_back(i);
   }
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   userlist->erase(userlist->begin()++);
   heaplist->erase(heaplist->begin()++);
   stdlist->erase(stdlist->begin()++);
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   userlist->pop_back();
   heaplist->pop_back();
   stdlist->pop_back();
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   userlist->pop_front();
   heaplist->pop_front();
   stdlist->pop_front();
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   std::vector<int> aux_vect;
   #if !BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
   aux_vect.assign(50, -1);
   userlist->assign(aux_vect.begin(), aux_vect.end());
   heaplist->assign(aux_vect.begin(), aux_vect.end());
   stdlist->assign(aux_vect.begin(), aux_vect.end());
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;
   #endif

   userlist->sort();
   heaplist->sort();
   stdlist->sort();
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   #if !BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
   aux_vect.assign(50, 0);
   #endif
   userlist->insert(userlist->begin(), aux_vect.begin(), aux_vect.end());
   heaplist->insert(heaplist->begin(), aux_vect.begin(), aux_vect.end());
   stdlist->insert(stdlist->begin(), aux_vect.begin(), aux_vect.end());

   userlist->unique();
   heaplist->unique();
   stdlist->unique();
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   userlist->sort(std::greater<int>());
   heaplist->sort(std::greater<int>());
   stdlist->sort(std::greater<int>());
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   userlist->resize(userlist->size()/2);
   heaplist->resize(heaplist->size()/2);
   stdlist->resize(stdlist->size()/2);
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   userlist->remove(*userlist->begin());
   heaplist->remove(*heaplist->begin());
   stdlist->remove(*stdlist->begin());
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   for(i = 0; i < max; ++i){
      userlist->push_back(i);
      heaplist->push_back(i);
      stdlist->push_back(i);
   }

   MyUserList otheruserlist(*userlist);
   MyHeapList otherheaplist(*heaplist);
   MyStdList otherstdlist(*stdlist);
   userlist->splice(userlist->begin(), otheruserlist);
   heaplist->splice(heaplist->begin(), otherheaplist);
   stdlist->splice(stdlist->begin(), otherstdlist);
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   otheruserlist = *userlist;
   otherheaplist = *heaplist;
   otherstdlist = *stdlist;

   userlist->sort(std::greater<int>());
   heaplist->sort(std::greater<int>());
   stdlist->sort(std::greater<int>());
   otheruserlist.sort(std::greater<int>());
   otherheaplist.sort(std::greater<int>());
   otherstdlist.sort(std::greater<int>());
   userlist->merge(otheruserlist, std::greater<int>());
   heaplist->merge(otherheaplist, std::greater<int>());
   stdlist->merge(otherstdlist, std::greater<int>());
   if(!CheckEqual(userlist, stdlist, heaplist)) return 1;

   user_buffer.destroy<MyUserList>(L"MyUserList");
   delete stdlist;

   //Fill heap buffer until is full
   try{
      while(1){
         heaplist->insert(heaplist->end(), 0);
      }
   }
   catch(boost::interprocess::bad_alloc &){}

   MyHeapList::size_type heap_list_size = heaplist->size();

   //Copy heap buffer to another
   const char *insert_beg = static_cast<char*>(heap_buffer.get_address());
   const char *insert_end = insert_beg + heap_buffer.get_size();
   std::vector<char> grow_copy (insert_beg, insert_end);

   //Destroy old list
   heap_buffer.destroy<MyHeapList>(L"MyHeapList");

   //Resize copy buffer
   grow_copy.resize(memsize*2);

   //Open Interprocess machinery in the new managed external buffer
   wmanaged_external_buffer user_buffer2(open_only, &grow_copy[0], memsize);

   //Expand old Interprocess machinery to the new size
   user_buffer2.grow(memsize);

   //Get a pointer to the full list
   userlist = user_buffer2.find<MyUserList>(L"MyHeapList").first;
   if(!userlist){
      return 1;
   }

   //Fill user buffer until is full
   try{
      while(1){
         userlist->insert(userlist->end(), 0);
      }
   }
   catch(boost::interprocess::bad_alloc &){}

   MyUserList::size_type user_list_size = userlist->size();

   if(user_list_size <= heap_list_size){
      return 1;
   }

   user_buffer2.destroy_ptr(userlist);

   return 0;
}
Esempio n. 4
0
int list_test (bool copied_allocators_equal = true)
{
   typedef std::list<int> MyStdList;
   typedef typename MyShmList::value_type IntType;
   const int max = 100;
   typedef list_push_data_function<DoublyLinked> push_data_t;

   try{
      MyShmList *shmlist = new MyShmList;
      MyStdList *stdlist = new MyStdList;

      if(push_data_t::execute(max, shmlist, stdlist)){
         return 1;
      }

      shmlist->erase(shmlist->begin()++);
      stdlist->erase(stdlist->begin()++);
      if(!CheckEqualContainers(shmlist, stdlist)) return 1;

      if(list_pop_back_function<DoublyLinked>::execute(shmlist, stdlist)){
         return 1;
      }

      shmlist->pop_front();
      stdlist->pop_front();
      if(!CheckEqualContainers(shmlist, stdlist)) return 1;

      {
         IntType aux_vect[50];
         for(int i = 0; i < 50; ++i){
            IntType move_me(-1);
            aux_vect[i] = boost::move(move_me);
         }
         int aux_vect2[50];
         for(int i = 0; i < 50; ++i){
            aux_vect2[i] = -1;
         }
         shmlist->assign(boost::make_move_iterator(&aux_vect[0])
                        ,boost::make_move_iterator(&aux_vect[50]));
         stdlist->assign(&aux_vect2[0], &aux_vect2[50]);
         if(!CheckEqualContainers(shmlist, stdlist)) return 1;
      }

      if(copied_allocators_equal){
         shmlist->sort();
         stdlist->sort();
         if(!CheckEqualContainers(shmlist, stdlist)) return 1;
      }

      shmlist->reverse();
      stdlist->reverse();
      if(!CheckEqualContainers(shmlist, stdlist)) return 1;

      shmlist->reverse();
      stdlist->reverse();
      if(!CheckEqualContainers(shmlist, stdlist)) return 1;

      {
         IntType aux_vect[50];
         for(int i = 0; i < 50; ++i){
            IntType move_me(-1);
            aux_vect[i] = boost::move(move_me);
         }
         int aux_vect2[50];
         for(int i = 0; i < 50; ++i){
            aux_vect2[i] = -1;
         }
         shmlist->insert(shmlist->begin()
                        ,boost::make_move_iterator(&aux_vect[0])
                        ,boost::make_move_iterator(&aux_vect[50]));
         stdlist->insert(stdlist->begin(), &aux_vect2[0], &aux_vect2[50]);
      }

      shmlist->unique();
      stdlist->unique();
      if(!CheckEqualContainers(shmlist, stdlist))
         return 1;

      if(copied_allocators_equal){
         shmlist->sort(std::greater<IntType>());
         stdlist->sort(std::greater<int>());
         if(!CheckEqualContainers(shmlist, stdlist))
            return 1;
      }

      for(int i = 0; i < max; ++i){
         IntType new_int(i);
         shmlist->insert(shmlist->end(), boost::move(new_int));
         stdlist->insert(stdlist->end(), i);
         if(!test::CheckEqualContainers(shmlist, stdlist)) return 1;
      }
      if(!test::CheckEqualContainers(shmlist, stdlist)) return 1;

      shmlist->resize(25);
      stdlist->resize(25);
      shmlist->resize(50);
      stdlist->resize(50);
      shmlist->resize(0);
      stdlist->resize(0);
      if(!CheckEqualContainers(shmlist, stdlist))
         return 1;

      if(push_data_t::execute(max, shmlist, stdlist)){
         return 1;
      }
      {
         MyShmList othershmlist(shmlist->get_allocator());
         MyStdList otherstdlist;

         int listsize = (int)shmlist->size();

         if(push_data_t::execute(listsize, shmlist, stdlist)){
            return 1;
         }

         if(copied_allocators_equal){
            shmlist->splice(shmlist->begin(), othershmlist);
            stdlist->splice(stdlist->begin(), otherstdlist);
            if(!CheckEqualContainers(shmlist, stdlist))
               return 1;   
         }

         listsize = (int)shmlist->size();

         if(push_data_t::execute(listsize, shmlist, stdlist)){
            return 1;
         }

         if(push_data_t::execute(listsize, &othershmlist, &otherstdlist)){
            return 1;
         }

         if(copied_allocators_equal){
            shmlist->sort(std::greater<IntType>());
            stdlist->sort(std::greater<int>());
            if(!CheckEqualContainers(shmlist, stdlist))
               return 1;

            othershmlist.sort(std::greater<IntType>());
            otherstdlist.sort(std::greater<int>());
            if(!CheckEqualContainers(&othershmlist, &otherstdlist))
               return 1;

            shmlist->merge(othershmlist, std::greater<IntType>());
            stdlist->merge(otherstdlist, std::greater<int>());
            if(!CheckEqualContainers(shmlist, stdlist))
               return 1;
         }

         if(!list_copyable_only(shmlist, stdlist
                        ,containers_detail::bool_<!is_movable<IntType>::value>())){
            return 1;
         }
      }

      delete shmlist;
      delete stdlist;
   }
   catch(...){
      throw;
   }
   return 0;
}