Files
interprocess/test/allocexcept_test.cpp
Ion Gaztañaga 25206e6332 Deprecate boost/interprocess/containers/*.hpp header:
- Change all examples and test to include <boost/container/*>
- Update documentation, including current std library implementation portability regarding offset_ptr
2024-08-08 01:21:14 +02:00

103 lines
3.3 KiB
C++

//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/container/vector.hpp>
#include <boost/container/list.hpp>
#include <iostream>
#include <functional>
#include "print_container.hpp"
#include <string>
#include "get_process_id_name.hpp"
struct InstanceCounter
{
InstanceCounter(){++counter;}
InstanceCounter(const InstanceCounter&){++counter;}
InstanceCounter & operator=(const InstanceCounter&){ return *this; }
~InstanceCounter(){--counter;}
static std::size_t counter;
};
std::size_t InstanceCounter::counter = 0;
using namespace boost::interprocess;
int main ()
{
const int memsize = 16384;
const char *const shMemName = test::get_process_id_name();
BOOST_INTERPROCESS_TRY{
shared_memory_object::remove(shMemName);
//Named allocate capable shared mem allocator
managed_shared_memory segment(create_only, shMemName, memsize);
//STL compatible allocator object, uses allocate(), deallocate() functions
typedef allocator<InstanceCounter,
managed_shared_memory::segment_manager>
inst_allocator_t;
const inst_allocator_t myallocator (segment.get_segment_manager());
typedef boost::container::vector<InstanceCounter, inst_allocator_t> MyVect;
//We'll provoke an exception, let's see if exception handling works
BOOST_INTERPROCESS_TRY{
//Fill vector until there is no more memory
MyVect myvec(myallocator);
while(true){
InstanceCounter a;
myvec.push_back(a);
}
}
BOOST_INTERPROCESS_CATCH(boost::interprocess::bad_alloc &){
if (InstanceCounter::counter != 0) {
std::cout << "Error: InstanceCounter::counter: " << InstanceCounter::counter;
return 1;
}
} BOOST_INTERPROCESS_CATCH_END
//We'll provoke an exception, let's see if exception handling works
BOOST_INTERPROCESS_TRY{
//Fill vector at the beginning until there is no more memory
MyVect myvec(myallocator);
std::size_t i;
InstanceCounter ic;
for(i = 0; true; ++i){
myvec.insert(myvec.begin(), i, ic);
}
}
BOOST_INTERPROCESS_CATCH(boost::interprocess::bad_alloc &){
if(InstanceCounter::counter != 0){
std::cout << "Error: InstanceCounter::counter: " << InstanceCounter::counter;
return 1;
}
}
BOOST_INTERPROCESS_CATCH(std::length_error &){
if(InstanceCounter::counter != 0){
std::cout << "Error: InstanceCounter::counter: " << InstanceCounter::counter;
return 1;
}
} BOOST_INTERPROCESS_CATCH_END
}
BOOST_INTERPROCESS_CATCH(...){
shared_memory_object::remove(shMemName);
BOOST_INTERPROCESS_RETHROW
}
BOOST_INTERPROCESS_CATCH_END
shared_memory_object::remove(shMemName);
return 0;
}