SHOW:
|
|
- or go back to the newest paste.
| 1 | - | /** |
| 1 | + | |
| 2 | - | ******************************************************************************** |
| 2 | + | |
| 3 | - | * @file testPushBackVector.cc |
| 3 | + | |
| 4 | - | * @brief A brief description of the content of the file. |
| 4 | + | |
| 5 | - | ******************************************************************************** |
| 5 | + | |
| 6 | - | * |
| 6 | + | |
| 7 | - | * A potentially lengthy description of the content of the file. |
| 7 | + | |
| 8 | - | * You can use list : |
| 8 | + | |
| 9 | - | * \li first elem, |
| 9 | + | |
| 10 | - | * \li second elen, |
| 10 | + | |
| 11 | - | * \li last elem.\n |
| 11 | + | |
| 12 | - | * |
| 12 | + | |
| 13 | - | * You can use HTML tags <b>bold</b>, <i>italic</i> |
| 13 | + | |
| 14 | - | * And doxygen tags ex : |
| 14 | + | |
| 15 | - | * \code int resultI = anIntI + anotherIntI \encode |
| 15 | + | |
| 16 | - | * |
| 16 | + | |
| 17 | - | * @date Jul 18, 2012 |
| 17 | + | |
| 18 | - | * @author gg01347 |
| 18 | + | |
| 19 | - | * |
| 19 | + | |
| 20 | - | ******************************************************************************** |
| 20 | + | |
| 21 | - | **/ |
| 21 | + | |
| 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 | } |