1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Peter Dimov 2002-2005, 2007.
// (C) Copyright Ion Gaztanaga 2006-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/offset_ptr.hpp>
#include <boost/interprocess/smart_ptr/shared_ptr.hpp>
#include <boost/interprocess/smart_ptr/weak_ptr.hpp>
#include <boost/interprocess/smart_ptr/enable_shared_from_this.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/smart_ptr/deleter.hpp>
#include <boost/interprocess/smart_ptr/scoped_ptr.hpp>
#include <boost/core/lightweight_test.hpp>
#include <string>
#include "get_process_id_name.hpp"
#include <boost/interprocess/sync/upgradable_lock.hpp>
#include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
using namespace boost::interprocess;
class base_class
{
public:
virtual ~base_class()
{}
};
class derived_class
: public base_class
{
public:
virtual ~derived_class() BOOST_OVERRIDE
{}
};
int simple_test()
{
typedef managed_shared_memory::segment_manager segment_mngr_t;
typedef allocator<base_class, segment_mngr_t> base_class_allocator;
typedef deleter<base_class, segment_mngr_t> base_deleter_t;
typedef shared_ptr<base_class, base_class_allocator, base_deleter_t> base_shared_ptr;
std::string process_name;
test::get_process_id_name(process_name);
shared_memory_object::remove(process_name.c_str());
{
managed_shared_memory shmem(create_only, process_name.c_str(), 10000);
{
base_shared_ptr s_ptr(base_shared_ptr::pointer(0),
base_class_allocator(shmem.get_segment_manager()),
base_deleter_t(shmem.get_segment_manager()));
base_shared_ptr s_ptr2(shmem.construct<base_class>("base_class")(),
base_class_allocator(shmem.get_segment_manager()),
base_deleter_t(shmem.get_segment_manager()));
base_shared_ptr s_ptr3(offset_ptr<derived_class>(shmem.construct<derived_class>("derived_class")()),
base_class_allocator(shmem.get_segment_manager()),
base_deleter_t(shmem.get_segment_manager()));
if(s_ptr3.get_deleter() == 0){
return 1;
}
//if(s_ptr3.get_allocator() == 0){
//return 1;
//}
base_shared_ptr s_ptr_empty;
if(s_ptr_empty.get_deleter() != 0){
return 1;
}
//if(s_ptr_empty.get_allocator() != 0){
//return 1;
//}
}
}
shared_memory_object::remove(process_name.c_str());
return 0;
}
int string_shared_ptr_vector_insertion_test()
{
typedef managed_shared_memory::segment_manager segment_mngr_t;
//Allocator of chars
typedef allocator<char, segment_mngr_t> char_allocator_t;
//A shared memory string class
typedef basic_string<char, std::char_traits<char>, char_allocator_t> string_t;
//A shared memory string allocator
typedef allocator<string_t, segment_mngr_t> string_allocator_t;
//A deleter for shared_ptr<> that erases a shared memory string
typedef deleter<string_t, segment_mngr_t> string_deleter_t;
//A shared pointer that points to a shared memory string and its instantiation
typedef shared_ptr<string_t, string_allocator_t, string_deleter_t> string_shared_ptr_t;
//An allocator for shared pointers to a string in shared memory
typedef allocator<string_shared_ptr_t, segment_mngr_t> string_shared_ptr_allocator_t;
//A weak pointer that points to a shared memory string and its instantiation
typedef weak_ptr<string_t, string_allocator_t, string_deleter_t> string_weak_ptr_t;
//An allocator for weak pointers to a string in shared memory
typedef allocator<string_weak_ptr_t, segment_mngr_t > string_weak_ptr_allocator_t;
//A vector of shared pointers to strings (all in shared memory) and its instantiation
typedef vector<string_shared_ptr_t, string_shared_ptr_allocator_t>
string_shared_ptr_vector_t;
//A vector of weak pointers to strings (all in shared memory) and its instantiation
typedef vector<string_weak_ptr_t, string_weak_ptr_allocator_t>
string_weak_ptr_vector_t;
std::string process_name;
test::get_process_id_name(process_name);
//A shared memory managed memory classes
shared_memory_object::remove(process_name.c_str());
{
managed_shared_memory shmem(create_only, process_name.c_str(), 20000);
{
const int NumElements = 100;
//Construct the allocator of strings
string_allocator_t string_allocator(shmem.get_segment_manager());
//Construct the allocator of a shared_ptr to string
string_shared_ptr_allocator_t string_shared_ptr_allocator(shmem.get_segment_manager());
//Construct the allocator of a shared_ptr to string
string_weak_ptr_allocator_t string_weak_ptr_allocator(shmem.get_segment_manager());
//This is a string deleter using destroy_ptr() function of the managed_shared_memory
string_deleter_t deleter(shmem.get_segment_manager());
//Create a string in shared memory, to avoid leaks with exceptions use
//scoped ptr until we store this pointer in the shared ptr
scoped_ptr<string_t, string_deleter_t> scoped_string
(shmem.construct<string_t>(anonymous_instance)(string_allocator), deleter);
//Now construct a shared pointer to a string
string_shared_ptr_t string_shared_ptr (scoped_string.get(),
string_shared_ptr_allocator,
deleter);
//Check use count is just one
if(!string_shared_ptr.unique()){
return 1;
}
//We don't need the scoped_ptr anonymous since the raw pointer is in the shared ptr
scoped_string.release();
//Now fill a shared memory vector of shared_ptrs to a string
string_shared_ptr_vector_t my_sharedptr_vector(string_shared_ptr_allocator);
my_sharedptr_vector.insert(my_sharedptr_vector.begin(), NumElements, string_shared_ptr);
//Insert in the middle to test movability
my_sharedptr_vector.insert(my_sharedptr_vector.nth(my_sharedptr_vector.size()/2u), NumElements, string_shared_ptr);
//Now check the shared count is the objects contained in the
//vector plus string_shared_ptr
if(string_shared_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){
return 1;
}
//Now create a weak ptr from the shared_ptr
string_weak_ptr_t string_weak_ptr (string_shared_ptr);
//Use count should remain the same
if(string_weak_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){
return 1;
}
//Now reset the local shared_ptr and check use count
string_shared_ptr.reset();
if(string_weak_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size())){
return 1;
}
//Now reset the local shared_ptr's use count should be zero
if(string_shared_ptr.use_count() != 0){
return 1;
}
//Now recreate the shared ptr from the weak ptr
//and recheck use count
string_shared_ptr = string_shared_ptr_t(string_weak_ptr);
if(string_shared_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){
return 1;
}
//Now fill a vector of weak_ptr-s
string_weak_ptr_vector_t my_weakptr_vector(string_weak_ptr_allocator);
my_weakptr_vector.insert(my_weakptr_vector.begin(), NumElements, string_weak_ptr);
//The shared count should remain the same
if(string_shared_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){
return 1;
}
//So weak pointers should be fine
string_weak_ptr_vector_t::iterator beg = my_weakptr_vector.begin(),
end = my_weakptr_vector.end();
for(;beg != end; ++beg){
if(beg->expired()){
return 1;
}
//The shared pointer constructed from weak ptr should
//be the same as the original, since all weak pointer
//point the the same object
if(string_shared_ptr_t(*beg) != string_shared_ptr){
return 1;
}
}
//Now destroy all the shared ptr-s of the shared ptr vector
my_sharedptr_vector.clear();
//The only alive shared ptr should be the local one
if(string_shared_ptr.use_count() != 1){
return 1;
}
//Now we invalidate the last alive shared_ptr
string_shared_ptr.reset();
//Now all weak pointers should have expired
beg = my_weakptr_vector.begin();
end = my_weakptr_vector.end();
for(;beg != end; ++beg){
if(!beg->expired()){
return 1;
}
bool success = false;
//Now this should throw
BOOST_TRY{
string_shared_ptr_t dummy(*beg);
//We should never reach here
return 1;
}
BOOST_CATCH(const boost::interprocess::bad_weak_ptr &){
success = true;
} BOOST_CATCH_END
if(!success){
return 1;
}
}
//Clear weak ptr vector
my_weakptr_vector.clear();
//Now lock returned shared ptr should return null
if(string_weak_ptr.lock().get()){
return 1;
}
//Reset weak_ptr
string_weak_ptr.reset();
}
}
shared_memory_object::remove(process_name.c_str());
return 0;
}
//
// This part is taken from shared_ptr_basic_test.cpp
//
// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
// Copyright (c) 2006 Ion Gaztanaga
//
// 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)
//
static int cnt = 0;
struct X
{
X(){ ++cnt; }
// virtual destructor deliberately omitted
virtual ~X(){ --cnt; }
virtual int id() const
{ return 1; }
private:
X(X const &);
X & operator= (X const &);
};
struct Y: public X
{
Y(){ ++cnt; }
virtual ~Y() BOOST_OVERRIDE{ --cnt; }
virtual int id() const BOOST_OVERRIDE
{ return 2; }
private:
Y(Y const &);
Y & operator= (Y const &);
};
int * get_object()
{ ++cnt; return &cnt; }
void release_object(int * p)
{ BOOST_TEST(p == &cnt); --cnt; }
template<class T, class A, class D>
void test_is_X(shared_ptr<T, A, D> const & p)
{
BOOST_TEST(p->id() == 1);
BOOST_TEST((*p).id() == 1);
}
template<class T, class A, class D>
void test_is_X(weak_ptr<T, A, D> const & p)
{
BOOST_TEST(p.get() != 0);
BOOST_TEST(p.get()->id() == 1);
}
template<class T, class A, class D>
void test_is_Y(shared_ptr<T, A, D> const & p)
{
BOOST_TEST(p->id() == 2);
BOOST_TEST((*p).id() == 2);
}
template<class T, class A, class D>
void test_is_Y(weak_ptr<T, A, D> const & p)
{
shared_ptr<T, A, D> q = p.lock();
BOOST_TEST(q.get() != 0);
BOOST_TEST(q->id() == 2);
}
template<class T, class T2>
void test_eq(T const & a, T2 const & b)
{
BOOST_TEST(a == b);
BOOST_TEST(!(a != b));
BOOST_TEST(!(a < b));
BOOST_TEST(!(b < a));
}
template<class T, class T2>
void test_ne(T const & a, T2 const & b)
{
BOOST_TEST(!(a == b));
BOOST_TEST(a != b);
BOOST_TEST(a < b || b < a);
BOOST_TEST(!(a < b && b < a));
}
template<class T, class U, class A, class D, class D2>
void test_shared(weak_ptr<T, A, D> const & a, weak_ptr<U, A, D2> const & b)
{
BOOST_TEST(!(a < b));
BOOST_TEST(!(b < a));
}
template<class T, class U, class A, class D, class D2>
void test_nonshared(weak_ptr<T, A, D> const & a, weak_ptr<U, A, D2> const & b)
{
BOOST_TEST(a < b || b < a);
BOOST_TEST(!(a < b && b < a));
}
template<class T, class U>
void test_eq2(T const & a, U const & b)
{
BOOST_TEST(a == b);
BOOST_TEST(!(a != b));
}
template<class T, class U>
void test_ne2(T const & a, U const & b)
{
BOOST_TEST(!(a == b));
BOOST_TEST(a != b);
}
template<class T, class A, class D>
void test_is_zero(shared_ptr<T, A, D> const & p)
{
BOOST_TEST(!p);
BOOST_TEST(p.get() == 0);
}
template<class T, class A, class D>
void test_is_nonzero(shared_ptr<T, A, D> const & p)
{
// p? true: false is used to test p in a boolean context.
// BOOST_TEST(p) is not guaranteed to test the conversion,
// as the macro might test !!p instead.
BOOST_TEST(p? true: false);
BOOST_TEST(p.get() != 0);
}
int basic_shared_ptr_test()
{
typedef managed_shared_memory::segment_manager segment_mngr_t;
typedef allocator<void, segment_mngr_t> v_allocator_t;
typedef deleter<X, segment_mngr_t> x_deleter_t;
typedef deleter<Y, segment_mngr_t> y_deleter_t;
typedef shared_ptr<X, v_allocator_t, x_deleter_t> x_shared_ptr;
typedef shared_ptr<Y, v_allocator_t, y_deleter_t> y_shared_ptr;
typedef weak_ptr<X, v_allocator_t, x_deleter_t> x_weak_ptr;
typedef weak_ptr<Y, v_allocator_t, y_deleter_t> y_weak_ptr;
std::string process_name;
test::get_process_id_name(process_name);
shared_memory_object::remove(process_name.c_str());
{
managed_shared_memory shmem(create_only, process_name.c_str(), 10000);
{
v_allocator_t v_allocator (shmem.get_segment_manager());
x_deleter_t x_deleter (shmem.get_segment_manager());
y_deleter_t y_deleter (shmem.get_segment_manager());
y_shared_ptr p (shmem.construct<Y>(anonymous_instance)(), v_allocator, y_deleter);
x_shared_ptr p2(shmem.construct<X>(anonymous_instance)(), v_allocator, x_deleter);
test_is_nonzero(p);
test_is_nonzero(p2);
test_is_Y(p);
test_is_X(p2);
test_ne(p, p2);
{
shared_ptr<X, v_allocator_t, y_deleter_t> q(p);
test_eq(p, q);
}
y_shared_ptr p3 (dynamic_pointer_cast<Y>(p));
shared_ptr<Y, v_allocator_t, x_deleter_t> p4 (dynamic_pointer_cast<Y>(p2));
test_is_nonzero(p3);
test_is_zero(p4);
BOOST_TEST(p.use_count() == 2);
BOOST_TEST(p2.use_count() == 1);
BOOST_TEST(p3.use_count() == 2);
test_is_Y(p3);
test_eq2(p, p3);
test_ne2(p2, p4);
shared_ptr<void, v_allocator_t, y_deleter_t> p5(p);
test_is_nonzero(p5);
test_eq2(p, p5);
BOOST_TEST(p5.use_count() == 3);
x_weak_ptr wp1(p2);
BOOST_TEST(!wp1.expired());
BOOST_TEST(wp1.use_count() != 0);
p.reset();
p2.reset();
p3.reset();
p4.reset();
test_is_zero(p);
test_is_zero(p2);
test_is_zero(p3);
test_is_zero(p4);
BOOST_TEST(p5.use_count() == 1);
BOOST_TEST(wp1.expired());
BOOST_TEST(wp1.use_count() == 0);
BOOST_TRY{
x_shared_ptr sp1(wp1);
BOOST_ERROR("shared_ptr<X, A, D> sp1(wp1) failed to throw");
}
BOOST_CATCH(boost::interprocess::bad_weak_ptr const &)
{} BOOST_CATCH_END
test_is_zero(wp1.lock());
weak_ptr<X, v_allocator_t, y_deleter_t> wp2 = static_pointer_cast<X>(p5);
BOOST_TEST(wp2.use_count() == 1);
test_is_Y(wp2);
test_nonshared(wp1, wp2);
// Scoped to not affect the subsequent use_count() tests.
{
shared_ptr<X, v_allocator_t, y_deleter_t> sp2(wp2);
test_is_nonzero(wp2.lock());
}
y_weak_ptr wp3 = dynamic_pointer_cast<Y>(wp2.lock());
BOOST_TEST(wp3.use_count() == 1);
test_shared(wp2, wp3);
weak_ptr<X, v_allocator_t, y_deleter_t> wp4(wp3);
BOOST_TEST(wp4.use_count() == 1);
test_shared(wp2, wp4);
wp1 = p2;
test_is_zero(wp1.lock());
wp1 = p4;
x_weak_ptr wp5;
bool b1 = wp1 < wp5;
bool b2 = wp5 < wp1;
y_shared_ptr p6 = static_pointer_cast<Y>(p5);
p5.reset();
p6.reset();
BOOST_TEST(wp1.use_count() == 0);
BOOST_TEST(wp2.use_count() == 0);
BOOST_TEST(wp3.use_count() == 0);
// Test operator< stability for std::set< weak_ptr<> >
// Thanks to Joe Gottman for pointing this out
BOOST_TEST(b1 == (wp1 < wp5));
BOOST_TEST(b2 == (wp5 < wp1));
}
BOOST_TEST(cnt == 0);
}
shared_memory_object::remove(process_name.c_str());
return boost::report_errors();
}
struct alias_tester
{
int v_;
explicit alias_tester( int v ): v_( v )
{
}
~alias_tester()
{
v_ = 0;
}
};
void test_alias()
{
typedef managed_shared_memory::segment_manager segment_mngr_t;
typedef allocator<void, segment_mngr_t> v_allocator_t;
typedef deleter<int, segment_mngr_t> int_deleter_t;
typedef shared_ptr<int, v_allocator_t, int_deleter_t> int_shared_ptr;
typedef shared_ptr<const int, v_allocator_t, int_deleter_t> const_int_shared_ptr;
std::string process_name;
test::get_process_id_name(process_name);
shared_memory_object::remove(process_name.c_str());
{
managed_shared_memory shmem(create_only, process_name.c_str(), 10000);
{
int m = 0;
int_shared_ptr p;
int_shared_ptr p2( p, &m );
BOOST_TEST( ipcdetail::to_raw_pointer(p2.get()) == &m );
BOOST_TEST( p2? true: false );
BOOST_TEST( !!p2 );
BOOST_TEST( p2.use_count() == p.use_count() );
BOOST_TEST( !( p < p2 ) && !( p2 < p ) );
p2.reset( p, static_cast<int*>(0) );
BOOST_TEST( p2.get() == 0 );
BOOST_TEST( p2? false: true );
BOOST_TEST( !p2 );
BOOST_TEST( p2.use_count() == p.use_count() );
BOOST_TEST( !( p < p2 ) && !( p2 < p ) );
}
{
int m = 0;
int_shared_ptr p(make_managed_shared_ptr
(shmem.construct<int>(anonymous_instance)(), shmem));
const_int_shared_ptr p2( p, &m );
BOOST_TEST( ipcdetail::to_raw_pointer(p2.get()) == &m );
BOOST_TEST( p2? true: false );
BOOST_TEST( !!p2 );
BOOST_TEST( p2.use_count() == p.use_count() );
BOOST_TEST( !( p < p2 ) && !( p2 < p ) );
int_shared_ptr p_nothrow(make_managed_shared_ptr
(shmem.construct<int>(anonymous_instance)(), shmem, std::nothrow));
}
}
shared_memory_object::remove(process_name.c_str());
}
struct std_deleter
{
typedef const void* pointer;
void operator()(const void* p) const;
};
struct shared_from_this_tester: enable_shared_from_this<
const shared_from_this_tester, std::allocator<void>, std_deleter
> {};
void std_deleter::operator()(const void* p) const
{
delete static_cast<const shared_from_this_tester*>(p);
}
void test_const_shared_from_this()
{
shared_ptr<const shared_from_this_tester, std::allocator<void>, std_deleter> cptr(
new shared_from_this_tester()
);
BOOST_TEST( cptr->shared_from_this().get() == cptr.get() );
}
int main()
{
if(0 != simple_test())
return 1;
if(0 != string_shared_ptr_vector_insertion_test())
return 1;
if(0 != basic_shared_ptr_test())
return 1;
test_alias();
test_const_shared_from_this();
}
|