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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
|
//////////////////////////////////////////////////////////////////////////////
//
// (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.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_INTERPROCESS_TEST_MEMORY_ALGORITHM_TEST_TEMPLATE_HEADER
#define BOOST_INTERPROCESS_TEST_MEMORY_ALGORITHM_TEST_TEMPLATE_HEADER
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <vector>
#include <iostream>
#include <new> //std::nothrow
#include <cstring> //std::memset
namespace boost { namespace interprocess { namespace test {
enum deallocation_type { DirectDeallocation, InverseDeallocation, MixedDeallocation, EndDeallocationType };
//This test allocates until there is no more memory
//and after that deallocates all in the inverse order
template<class Allocator>
bool test_allocation(Allocator &a)
{
for( deallocation_type t = DirectDeallocation
; t != EndDeallocationType
; t = (deallocation_type)((int)t + 1)){
std::vector<void*> buffers;
typename Allocator::size_type free_memory = a.get_free_memory();
for(int i = 0; true; ++i){
void *ptr = a.allocate(i, std::nothrow);
if(!ptr)
break;
std::size_t size = a.size(ptr);
std::memset(ptr, 0, size);
buffers.push_back(ptr);
}
switch(t){
case DirectDeallocation:
{
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
a.deallocate(buffers[j]);
}
}
break;
case InverseDeallocation:
{
for(int j = (int)buffers.size()
;j--
;){
a.deallocate(buffers[j]);
}
}
break;
case MixedDeallocation:
{
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
a.deallocate(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
}
break;
default:
break;
}
bool ok = free_memory == a.get_free_memory() &&
a.all_memory_deallocated() && a.check_sanity();
if(!ok) return ok;
}
return true;
}
//This test allocates until there is no more memory
//and after that tries to shrink all the buffers to the
//half of the original size
template<class Allocator>
bool test_allocation_shrink(Allocator &a)
{
std::vector<void*> buffers;
//Allocate buffers with extra memory
for(int i = 0; true; ++i){
void *ptr = a.allocate(i*2, std::nothrow);
if(!ptr)
break;
std::size_t size = a.size(ptr);
std::memset(ptr, 0, size);
buffers.push_back(ptr);
}
//Now shrink to half
for(int i = 0, max = (int)buffers.size()
;i < max
; ++i){
typename Allocator::size_type received_size;
char *reuse = static_cast<char*>(buffers[i]);
if(a.template allocation_command<char>
( boost::interprocess::shrink_in_place | boost::interprocess::nothrow_allocation, i*2
, received_size = i, reuse)){
if(received_size > std::size_t(i*2)){
return false;
}
if(received_size < std::size_t(i)){
return false;
}
std::memset(buffers[i], 0, a.size(buffers[i]));
}
}
//Deallocate it in non sequential order
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
a.deallocate(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
return a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates until there is no more memory
//and after that tries to expand all the buffers to
//avoid the wasted internal fragmentation
template<class Allocator>
bool test_allocation_expand(Allocator &a)
{
std::vector<void*> buffers;
//Allocate buffers with extra memory
for(int i = 0; true; ++i){
void *ptr = a.allocate(i, std::nothrow);
if(!ptr)
break;
std::size_t size = a.size(ptr);
std::memset(ptr, 0, size);
buffers.push_back(ptr);
}
//Now try to expand to the double of the size
for(int i = 0, max = (int)buffers.size()
;i < max
;++i){
typename Allocator::size_type received_size;
std::size_t min_size = i+1;
std::size_t preferred_size = i*2;
preferred_size = min_size > preferred_size ? min_size : preferred_size;
char *reuse = static_cast<char*>(buffers[i]);
while(a.template allocation_command<char>
( boost::interprocess::expand_fwd | boost::interprocess::nothrow_allocation, min_size
, received_size = preferred_size, reuse)){
//Check received size is bigger than minimum
if(received_size < min_size){
return false;
}
//Now, try to expand further
min_size = received_size+1;
preferred_size = min_size*2;
}
}
//Deallocate it in non sequential order
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
a.deallocate(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
return a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates until there is no more memory
//and after that tries to expand all the buffers to
//avoid the wasted internal fragmentation
template<class Allocator>
bool test_allocation_shrink_and_expand(Allocator &a)
{
std::vector<void*> buffers;
std::vector<typename Allocator::size_type> received_sizes;
std::vector<bool> size_reduced;
//Allocate buffers wand store received sizes
for(int i = 0; true; ++i){
typename Allocator::size_type received_size;
char *reuse = 0;
void *ptr = a.template allocation_command<char>
( boost::interprocess::allocate_new | boost::interprocess::nothrow_allocation, i, received_size = i*2, reuse);
if(!ptr){
ptr = a.template allocation_command<char>
( boost::interprocess::allocate_new | boost::interprocess::nothrow_allocation, 1, received_size = i*2, reuse);
if(!ptr)
break;
}
buffers.push_back(ptr);
received_sizes.push_back(received_size);
}
//Now shrink to half
for(int i = 0, max = (int)buffers.size()
; i < max
; ++i){
typename Allocator::size_type received_size;
char *reuse = static_cast<char*>(buffers[i]);
if(a.template allocation_command<char>
( boost::interprocess::shrink_in_place | boost::interprocess::nothrow_allocation, received_sizes[i]
, received_size = i, reuse)){
if(received_size > std::size_t(received_sizes[i])){
return false;
}
if(received_size < std::size_t(i)){
return false;
}
size_reduced.push_back(received_size != received_sizes[i]);
}
}
//Now try to expand to the original size
for(int i = 0, max = (int)buffers.size()
;i < max
;++i){
typename Allocator::size_type received_size;
std::size_t request_size = received_sizes[i];
char *reuse = static_cast<char*>(buffers[i]);
if(a.template allocation_command<char>
( boost::interprocess::expand_fwd | boost::interprocess::nothrow_allocation, request_size
, received_size = request_size, reuse)){
if(received_size != received_sizes[i]){
return false;
}
}
else{
return false;
}
}
//Deallocate it in non sequential order
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
a.deallocate(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
return a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates until there is no more memory
//and after that deallocates the odd buffers to
//make room for expansions. The expansion will probably
//success since the deallocation left room for that.
template<class Allocator>
bool test_allocation_deallocation_expand(Allocator &a)
{
std::vector<void*> buffers;
//Allocate buffers with extra memory
for(int i = 0; true; ++i){
void *ptr = a.allocate(i, std::nothrow);
if(!ptr)
break;
std::size_t size = a.size(ptr);
std::memset(ptr, 0, size);
buffers.push_back(ptr);
}
//Now deallocate the half of the blocks
//so expand maybe can merge new free blocks
for(int i = 0, max = (int)buffers.size()
;i < max
;++i){
if(i%2){
a.deallocate(buffers[i]);
buffers[i] = 0;
}
}
//Now try to expand to the double of the size
for(int i = 0, max = (int)buffers.size()
;i < max
;++i){
//
if(buffers[i]){
typename Allocator::size_type received_size;
std::size_t min_size = i+1;
std::size_t preferred_size = i*2;
preferred_size = min_size > preferred_size ? min_size : preferred_size;
char *reuse = static_cast<char*>(buffers[i]);
while(a.template allocation_command<char>
( boost::interprocess::expand_fwd | boost::interprocess::nothrow_allocation, min_size
, received_size = preferred_size, reuse)){
//Check received size is bigger than minimum
if(received_size < min_size){
return false;
}
//Now, try to expand further
min_size = received_size+1;
preferred_size = min_size*2;
}
}
}
//Now erase null values from the vector
buffers.erase( std::remove(buffers.begin(), buffers.end(), static_cast<void*>(0))
, buffers.end());
//Deallocate it in non sequential order
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
a.deallocate(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
return a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates until there is no more memory
//and after that deallocates all except the last.
//If the allocation algorithm is a bottom-up algorithm
//the last buffer will be in the end of the segment.
//Then the test will start expanding backwards, until
//the buffer fills all the memory
template<class Allocator>
bool test_allocation_with_reuse(Allocator &a)
{
//We will repeat this test for different sized elements
for(int sizeof_object = 1; sizeof_object < 20; ++sizeof_object){
std::vector<void*> buffers;
//Allocate buffers with extra memory
for(int i = 0; true; ++i){
void *ptr = a.allocate(i*sizeof_object, std::nothrow);
if(!ptr)
break;
std::size_t size = a.size(ptr);
std::memset(ptr, 0, size);
buffers.push_back(ptr);
}
//Now deallocate all except the latest
//Now try to expand to the double of the sizeof_object
for(int i = 0, max = (int)buffers.size() - 1
;i < max
;++i){
a.deallocate(buffers[i]);
}
//Save the unique buffer and clear vector
void *ptr = buffers.back();
buffers.clear();
//Now allocate with reuse
typename Allocator::size_type received_size = 0;
for(int i = 0; true; ++i){
std::size_t min_size = (received_size + 1);
std::size_t prf_size = (received_size + (i+1)*2);
void *reuse = ptr;
void *ret = a.raw_allocation_command
( boost::interprocess::expand_bwd | boost::interprocess::nothrow_allocation, min_size
, received_size = prf_size, reuse, sizeof_object);
if(!ret)
break;
//If we have memory, this must be a buffer reuse
if(!reuse)
return 1;
if(received_size < min_size)
return 1;
ptr = ret;
}
//There is only a single block so deallocate it
a.deallocate(ptr);
if(!a.all_memory_deallocated() || !a.check_sanity())
return false;
}
return true;
}
//This test allocates memory with different alignments
//and checks returned memory is aligned.
template<class Allocator>
bool test_aligned_allocation(Allocator &a)
{
//Allocate aligned buffers in a loop
//and then deallocate it
bool continue_loop = true;
for(unsigned int i = 1; continue_loop; i <<= 1){
for(unsigned int j = 1; true; j <<= 1){
void *ptr = a.allocate_aligned(i-1, j, std::nothrow);
if(!ptr){
if(j == 1)
continue_loop = false;
break;
}
if(((std::size_t)ptr & (j - 1)) != 0)
return false;
a.deallocate(ptr);
if(!a.all_memory_deallocated() || !a.check_sanity()){
return false;
}
}
}
return a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates memory with different alignments
//and checks returned memory is aligned.
template<class Allocator>
bool test_continuous_aligned_allocation(Allocator &a)
{
std::vector<void*> buffers;
//Allocate aligned buffers in a loop
//and then deallocate it
bool continue_loop = true;
for(unsigned i = 1; continue_loop && i; i <<= 1){
for(unsigned int j = 1; j; j <<= 1){
for(bool any_allocated = false; 1;){
void *ptr = a.allocate_aligned(i-1, j, std::nothrow);
buffers.push_back(ptr);
if(!ptr){
if(j == 1 && !any_allocated){
continue_loop = false;
}
break;
}
else{
any_allocated = true;
}
if(((std::size_t)ptr & (j - 1)) != 0)
return false;
}
//Deallocate all
for(unsigned int k = (int)buffers.size(); k--;){
a.deallocate(buffers[k]);
}
buffers.clear();
if(!a.all_memory_deallocated() && a.check_sanity())
return false;
if(!continue_loop)
break;
}
}
return a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates memory, writes it with a non-zero value and
//tests zero_free_memory initializes to zero for the next allocation
template<class Allocator>
bool test_clear_free_memory(Allocator &a)
{
std::vector<void*> buffers;
//Allocate memory
for(int i = 0; true; ++i){
void *ptr = a.allocate(i, std::nothrow);
if(!ptr)
break;
std::size_t size = a.size(ptr);
std::memset(ptr, 1, size);
buffers.push_back(ptr);
}
//Mark it
for(int i = 0, max = buffers.size(); i < max; ++i){
std::memset(buffers[i], 1, i);
}
//Deallocate all
for(int j = (int)buffers.size()
;j--
;){
a.deallocate(buffers[j]);
}
buffers.clear();
if(!a.all_memory_deallocated() && a.check_sanity())
return false;
//Now clear all free memory
a.zero_free_memory();
if(!a.all_memory_deallocated() && a.check_sanity())
return false;
//Now test all allocated memory is zero
//Allocate memory
const char *first_addr = 0;
for(int i = 0; true; ++i){
void *ptr = a.allocate(i, std::nothrow);
if(!ptr)
break;
if(i == 0){
first_addr = (char*)ptr;
}
std::size_t memsize = a.size(ptr);
buffers.push_back(ptr);
for(int j = 0; j < (int)memsize; ++j){
if(static_cast<char*>((char*)ptr)[j]){
std::cout << "Zero memory test failed. in buffer " << i
<< " byte " << j << " first address " << (void*) first_addr << " offset " << ((char*)ptr+j) - (char*)first_addr << " memsize: " << memsize << std::endl;
return false;
}
}
}
//Deallocate all
for(int j = (int)buffers.size()
;j--
;){
a.deallocate(buffers[j]);
}
if(!a.all_memory_deallocated() && a.check_sanity())
return false;
return true;
}
//This test uses tests grow and shrink_to_fit functions
template<class Allocator>
bool test_grow_shrink_to_fit(Allocator &a)
{
std::vector<void*> buffers;
typename Allocator::size_type original_size = a.get_size();
typename Allocator::size_type original_free = a.get_free_memory();
a.shrink_to_fit();
if(!a.all_memory_deallocated() && a.check_sanity())
return false;
typename Allocator::size_type shrunk_size = a.get_size();
typename Allocator::size_type shrunk_free_memory = a.get_free_memory();
if(shrunk_size != a.get_min_size())
return 1;
a.grow(original_size - shrunk_size);
if(!a.all_memory_deallocated() && a.check_sanity())
return false;
if(original_size != a.get_size())
return false;
if(original_free != a.get_free_memory())
return false;
//Allocate memory
for(int i = 0; true; ++i){
void *ptr = a.allocate(i, std::nothrow);
if(!ptr)
break;
std::size_t size = a.size(ptr);
std::memset(ptr, 0, size);
buffers.push_back(ptr);
}
//Now deallocate the half of the blocks
//so expand maybe can merge new free blocks
for(int i = 0, max = (int)buffers.size()
;i < max
;++i){
if(i%2){
a.deallocate(buffers[i]);
buffers[i] = 0;
}
}
//Deallocate the rest of the blocks
//Deallocate it in non sequential order
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
int pos = (j%5)*((int)buffers.size())/4;
if(pos == int(buffers.size()))
--pos;
a.deallocate(buffers[pos]);
buffers.erase(buffers.begin()+pos);
typename Allocator::size_type old_free = a.get_free_memory();
a.shrink_to_fit();
if(!a.check_sanity()) return false;
if(original_size < a.get_size()) return false;
if(old_free < a.get_free_memory()) return false;
a.grow(original_size - a.get_size());
if(!a.check_sanity()) return false;
if(original_size != a.get_size()) return false;
if(old_free != a.get_free_memory()) return false;
}
//Now shrink it to the maximum
a.shrink_to_fit();
if(a.get_size() != a.get_min_size())
return 1;
if(shrunk_free_memory != a.get_free_memory())
return 1;
if(!a.all_memory_deallocated() && a.check_sanity())
return false;
a.grow(original_size - shrunk_size);
if(original_size != a.get_size())
return false;
if(original_free != a.get_free_memory())
return false;
if(!a.all_memory_deallocated() && a.check_sanity())
return false;
return true;
}
//This test allocates multiple values until there is no more memory
//and after that deallocates all in the inverse order
template<class Allocator>
bool test_many_equal_allocation(Allocator &a)
{
for( deallocation_type t = DirectDeallocation
; t != EndDeallocationType
; t = (deallocation_type)((int)t + 1)){
typename Allocator::size_type free_memory = a.get_free_memory();
std::vector<void*> buffers2;
//Allocate buffers with extra memory
for(int i = 0; true; ++i){
void *ptr = a.allocate(i, std::nothrow);
if(!ptr)
break;
std::size_t size = a.size(ptr);
std::memset(ptr, 0, size);
if(!a.check_sanity())
return false;
buffers2.push_back(ptr);
}
//Now deallocate the half of the blocks
//so expand maybe can merge new free blocks
for(int i = 0, max = (int)buffers2.size()
;i < max
;++i){
if(i%2){
a.deallocate(buffers2[i]);
buffers2[i] = 0;
}
}
if(!a.check_sanity())
return false;
typedef typename Allocator::multiallocation_chain multiallocation_chain;
std::vector<void*> buffers;
for(int i = 0; true; ++i){
multiallocation_chain chain;
a.allocate_many(std::nothrow, i+1, (i+1)*2, chain);
if(chain.empty())
break;
typename multiallocation_chain::size_type n = chain.size();
while(!chain.empty()){
buffers.push_back(ipcdetail::to_raw_pointer(chain.pop_front()));
}
if(n != std::size_t((i+1)*2))
return false;
}
if(!a.check_sanity())
return false;
switch(t){
case DirectDeallocation:
{
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
a.deallocate(buffers[j]);
}
}
break;
case InverseDeallocation:
{
for(int j = (int)buffers.size()
;j--
;){
a.deallocate(buffers[j]);
}
}
break;
case MixedDeallocation:
{
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
a.deallocate(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
}
break;
default:
break;
}
//Deallocate the rest of the blocks
//Deallocate it in non sequential order
for(int j = 0, max = (int)buffers2.size()
;j < max
;++j){
int pos = (j%4)*((int)buffers2.size())/4;
a.deallocate(buffers2[pos]);
buffers2.erase(buffers2.begin()+pos);
}
bool ok = free_memory == a.get_free_memory() &&
a.all_memory_deallocated() && a.check_sanity();
if(!ok) return ok;
}
return true;
}
//This test allocates multiple values until there is no more memory
//and after that deallocates all in the inverse order
template<class Allocator>
bool test_many_different_allocation(Allocator &a)
{
typedef typename Allocator::multiallocation_chain multiallocation_chain;
const std::size_t ArraySize = 11;
typename Allocator::size_type requested_sizes[ArraySize];
for(std::size_t i = 0; i < ArraySize; ++i){
requested_sizes[i] = 4*i;
}
for( deallocation_type t = DirectDeallocation
; t != EndDeallocationType
; t = (deallocation_type)((int)t + 1)){
typename Allocator::size_type free_memory = a.get_free_memory();
std::vector<void*> buffers2;
//Allocate buffers with extra memory
for(int i = 0; true; ++i){
void *ptr = a.allocate(i, std::nothrow);
if(!ptr)
break;
std::size_t size = a.size(ptr);
std::memset(ptr, 0, size);
buffers2.push_back(ptr);
}
//Now deallocate the half of the blocks
//so expand maybe can merge new free blocks
for(int i = 0, max = (int)buffers2.size()
;i < max
;++i){
if(i%2){
a.deallocate(buffers2[i]);
buffers2[i] = 0;
}
}
std::vector<void*> buffers;
for(int i = 0; true; ++i){
multiallocation_chain chain;
a.allocate_many(std::nothrow, requested_sizes, ArraySize, 1, chain);
if(chain.empty())
break;
typename multiallocation_chain::size_type n = chain.size();
while(!chain.empty()){
buffers.push_back(ipcdetail::to_raw_pointer(chain.pop_front()));
}
if(n != ArraySize)
return false;
}
switch(t){
case DirectDeallocation:
{
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
a.deallocate(buffers[j]);
}
}
break;
case InverseDeallocation:
{
for(int j = (int)buffers.size()
;j--
;){
a.deallocate(buffers[j]);
}
}
break;
case MixedDeallocation:
{
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
a.deallocate(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
}
break;
default:
break;
}
//Deallocate the rest of the blocks
//Deallocate it in non sequential order
for(int j = 0, max = (int)buffers2.size()
;j < max
;++j){
int pos = (j%4)*((int)buffers2.size())/4;
a.deallocate(buffers2[pos]);
buffers2.erase(buffers2.begin()+pos);
}
bool ok = free_memory == a.get_free_memory() &&
a.all_memory_deallocated() && a.check_sanity();
if(!ok) return ok;
}
return true;
}
//This test allocates multiple values until there is no more memory
//and after that deallocates all in the inverse order
template<class Allocator>
bool test_many_deallocation(Allocator &a)
{
typedef typename Allocator::multiallocation_chain multiallocation_chain;
typedef typename Allocator::multiallocation_chain multiallocation_chain;
const std::size_t ArraySize = 11;
vector<multiallocation_chain> buffers;
typename Allocator::size_type requested_sizes[ArraySize];
for(std::size_t i = 0; i < ArraySize; ++i){
requested_sizes[i] = 4*i;
}
typename Allocator::size_type free_memory = a.get_free_memory();
{
for(int i = 0; true; ++i){
multiallocation_chain chain;
a.allocate_many(std::nothrow, requested_sizes, ArraySize, 1, chain);
if(chain.empty())
break;
buffers.push_back(boost::move(chain));
}
for(int i = 0, max = (int)buffers.size(); i != max; ++i){
a.deallocate_many(buffers[i]);
}
buffers.clear();
bool ok = free_memory == a.get_free_memory() &&
a.all_memory_deallocated() && a.check_sanity();
if(!ok) return ok;
}
{
for(int i = 0; true; ++i){
multiallocation_chain chain;
a.allocate_many(std::nothrow, i*4, ArraySize, chain);
if(chain.empty())
break;
buffers.push_back(boost::move(chain));
}
for(int i = 0, max = (int)buffers.size(); i != max; ++i){
a.deallocate_many(buffers[i]);
}
buffers.clear();
bool ok = free_memory == a.get_free_memory() &&
a.all_memory_deallocated() && a.check_sanity();
if(!ok) return ok;
}
return true;
}
//This function calls all tests
template<class Allocator>
bool test_all_allocation(Allocator &a)
{
std::cout << "Starting test_allocation. Class: "
<< typeid(a).name() << std::endl;
if(!test_allocation(a)){
std::cout << "test_allocation_direct_deallocation failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
std::cout << "Starting test_many_equal_allocation. Class: "
<< typeid(a).name() << std::endl;
if(!test_many_equal_allocation(a)){
std::cout << "test_many_equal_allocation failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
std::cout << "Starting test_many_different_allocation. Class: "
<< typeid(a).name() << std::endl;
if(!test_many_different_allocation(a)){
std::cout << "test_many_different_allocation failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
if(!test_many_deallocation(a)){
std::cout << "test_many_deallocation failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
std::cout << "Starting test_allocation_shrink. Class: "
<< typeid(a).name() << std::endl;
if(!test_allocation_shrink(a)){
std::cout << "test_allocation_shrink failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
if(!test_allocation_shrink_and_expand(a)){
std::cout << "test_allocation_shrink_and_expand failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
std::cout << "Starting test_allocation_expand. Class: "
<< typeid(a).name() << std::endl;
if(!test_allocation_expand(a)){
std::cout << "test_allocation_expand failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
std::cout << "Starting test_allocation_deallocation_expand. Class: "
<< typeid(a).name() << std::endl;
if(!test_allocation_deallocation_expand(a)){
std::cout << "test_allocation_deallocation_expand failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
std::cout << "Starting test_allocation_with_reuse. Class: "
<< typeid(a).name() << std::endl;
if(!test_allocation_with_reuse(a)){
std::cout << "test_allocation_with_reuse failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
std::cout << "Starting test_aligned_allocation. Class: "
<< typeid(a).name() << std::endl;
if(!test_aligned_allocation(a)){
std::cout << "test_aligned_allocation failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
std::cout << "Starting test_continuous_aligned_allocation. Class: "
<< typeid(a).name() << std::endl;
if(!test_continuous_aligned_allocation(a)){
std::cout << "test_continuous_aligned_allocation failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
std::cout << "Starting test_clear_free_memory. Class: "
<< typeid(a).name() << std::endl;
if(!test_clear_free_memory(a)){
std::cout << "test_clear_free_memory failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
std::cout << "Starting test_grow_shrink_to_fit. Class: "
<< typeid(a).name() << std::endl;
if(!test_grow_shrink_to_fit(a)){
std::cout << "test_grow_shrink_to_fit failed. Class: "
<< typeid(a).name() << std::endl;
return false;
}
return true;
}
}}} //namespace boost { namespace interprocess { namespace test {
#include <boost/interprocess/detail/config_end.hpp>
#endif //BOOST_INTERPROCESS_TEST_MEMORY_ALGORITHM_TEST_TEMPLATE_HEADER
|