From 8d7623561493138ee9f33a41fdc7dc3ef233fbd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Wed, 26 Sep 2007 17:46:34 +0000 Subject: [PATCH] Changes introduced by the new intrusive version. [SVN r39552] --- test/check_equal_containers.hpp | 2 +- test/memory_algorithm_test_template.hpp | 517 +++++++++++------------- test/node_pool_test.cpp | 11 - test/shared_ptr_test.cpp | 1 - test/unique_ptr_test.cpp | 13 +- 5 files changed, 244 insertions(+), 300 deletions(-) diff --git a/test/check_equal_containers.hpp b/test/check_equal_containers.hpp index cda3a23..dd68998 100644 --- a/test/check_equal_containers.hpp +++ b/test/check_equal_containers.hpp @@ -32,7 +32,7 @@ bool CheckEqualContainers(MyShmCont *shmcont, MyStdCont *stdcont) typename MyShmCont::iterator itshm(shmcont->begin()), itshmend(shmcont->end()); typename MyStdCont::iterator itstd(stdcont->begin()); - if((typename MyShmCont::size_type)std::distance(itshm, itshmend) != shmcont->size()){ + if((typename MyStdCont::size_type)std::distance(itshm, itshmend) != shmcont->size()){ return false; } for(; itshm != itshmend; ++itshm, ++itstd){ diff --git a/test/memory_algorithm_test_template.hpp b/test/memory_algorithm_test_template.hpp index f517cae..4c1dff2 100644 --- a/test/memory_algorithm_test_template.hpp +++ b/test/memory_algorithm_test_template.hpp @@ -20,77 +20,64 @@ 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 -bool test_allocation_inverse_deallocation(Allocator &a) +bool test_allocation(Allocator &a) { - std::vector buffers; + for( deallocation_type t = DirectDeallocation + ; t != EndDeallocationType + ; t = (deallocation_type)((int)t + 1)){ + std::vector buffers; + std::size_t free_memory = a.get_free_memory(); - for(int i = 0; true; ++i){ - void *ptr = a.allocate(i, std::nothrow); - if(!ptr) + for(int i = 0; true; ++i){ + void *ptr = a.allocate(i, std::nothrow); + if(!ptr) + break; + buffers.push_back(ptr); + } + + switch(t){ + case DirectDeallocation: + { + for(int j = 0, max = (int)buffers.size() + ;j < max + ;++j){ + a.deallocate(buffers[j]); + } + } break; - buffers.push_back(ptr); - } - - for(int j = (int)buffers.size() - ;j-- - ;){ - a.deallocate(buffers[j]); - } - - return a.all_memory_deallocated() && a.check_sanity(); -} - -//This test allocates until there is no more memory -//and after that deallocates all in the same order -template -bool test_allocation_direct_deallocation(Allocator &a) -{ - std::vector buffers; - std::size_t free_memory = a.get_free_memory(); - - for(int i = 0; true; ++i){ - void *ptr = a.allocate(i, std::nothrow); - if(!ptr) + case InverseDeallocation: + { + for(int j = (int)buffers.size() + ;j-- + ;){ + a.deallocate(buffers[j]); + } + } break; - buffers.push_back(ptr); - } - - for(int j = 0, max = (int)buffers.size() - ;j < max - ;++j){ - a.deallocate(buffers[j]); - } - - return free_memory == a.get_free_memory() && - a.all_memory_deallocated() && a.check_sanity(); -} - -//This test allocates until there is no more memory -//and after that deallocates all following a pattern -template -bool test_allocation_mixed_deallocation(Allocator &a) -{ - std::vector buffers; - - for(int i = 0; true; ++i){ - void *ptr = a.allocate(i, std::nothrow); - if(!ptr) + 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; - buffers.push_back(ptr); + default: + break; + } + bool ok = free_memory == a.get_free_memory() && + a.all_memory_deallocated() && a.check_sanity(); + if(!ok) return ok; } - - 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(); + return true; } //This test allocates until there is no more memory @@ -524,98 +511,106 @@ bool test_clear_free_memory(Allocator &a) //This test allocates multiple values until there is no more memory //and after that deallocates all in the inverse order template -bool test_many_equal_allocation_inverse_deallocation(Allocator &a) +bool test_many_equal_allocation(Allocator &a) { typedef typename Allocator::multiallocation_iterator multiallocation_iterator; + for( deallocation_type t = DirectDeallocation + ; t != EndDeallocationType + ; t = (deallocation_type)((int)t + 1)){ + std::size_t free_memory = a.get_free_memory(); - std::vector buffers; - for(int i = 0; true; ++i){ - std::size_t received_size; - multiallocation_iterator it = a.allocate_many(i+1, i+1, (i+1)*2, received_size, std::nothrow); - if(!it) - break; - multiallocation_iterator itend; + std::vector buffers2; - for(; it != itend; ++it){ - buffers.push_back(*it); + //Allocate buffers with extra memory + for(int i = 0; true; ++i){ + void *ptr = a.allocate(i, std::nothrow); + if(!ptr) + break; + buffers2.push_back(ptr); } - } - for(int j = (int)buffers.size() - ;j-- - ;){ - a.deallocate(buffers[j]); - } - - return a.all_memory_deallocated() && a.check_sanity(); -} - -//This test allocates multiple values until there is no more memory -//and after that deallocates all in the same order -template -bool test_many_equal_allocation_direct_deallocation(Allocator &a) -{ - typedef typename Allocator::multiallocation_iterator multiallocation_iterator; - std::vector buffers; - std::size_t free_memory = a.get_free_memory(); - - for(int i = 0; true; ++i){ - std::size_t received_size; - multiallocation_iterator it = a.allocate_many(i+1, i+1, (i+1)*2, received_size, std::nothrow); - if(!it) - break; - multiallocation_iterator itend; - - for(; it != itend; ++it){ - buffers.push_back(*it); + //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; + } } - } - for(int j = 0, max = (int)buffers.size() - ;j < max - ;++j){ - a.deallocate(buffers[j]); - } + std::vector buffers; + for(int i = 0; true; ++i){ + multiallocation_iterator it = a.allocate_many(i+1, (i+1)*2, std::nothrow); + if(!it) + break; + multiallocation_iterator itend; - return free_memory == a.get_free_memory() && - a.all_memory_deallocated() && a.check_sanity(); -} - -//This test allocates multiple values until there is no more memory -//and after that deallocates all following a pattern -template -bool test_many_equal_allocation_mixed_deallocation(Allocator &a) -{ - typedef typename Allocator::multiallocation_iterator multiallocation_iterator; - std::vector buffers; - - for(int i = 0; true; ++i){ - std::size_t received_size; - multiallocation_iterator it = a.allocate_many(i+1, i+1, (i+1)*2, received_size, std::nothrow); - if(!it) - break; - multiallocation_iterator itend; - - for(; it != itend; ++it){ - buffers.push_back(*it); + std::size_t n = 0; + for(; it != itend; ++n){ + buffers.push_back(*it++); + } + if(n != std::size_t((i+1)*2)) + return false; } - } - 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); - } + 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; + } - return a.all_memory_deallocated() && a.check_sanity(); + //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 -bool test_many_different_allocation_inverse_deallocation(Allocator &a) +bool test_many_different_allocation(Allocator &a) { typedef typename Allocator::multiallocation_iterator multiallocation_iterator; const std::size_t ArraySize = 11; @@ -624,178 +619,125 @@ bool test_many_different_allocation_inverse_deallocation(Allocator &a) requested_sizes[i] = 4*i; } - std::vector buffers; - for(int i = 0; true; ++i){ - multiallocation_iterator it = a.allocate_many(requested_sizes, ArraySize, 1, std::nothrow); - if(!it) - break; - multiallocation_iterator itend; + for( deallocation_type t = DirectDeallocation + ; t != EndDeallocationType + ; t = (deallocation_type)((int)t + 1)){ + std::size_t free_memory = a.get_free_memory(); - for(; it != itend; ++it){ - buffers.push_back(*it); + std::vector buffers2; + + //Allocate buffers with extra memory + for(int i = 0; true; ++i){ + void *ptr = a.allocate(i, std::nothrow); + if(!ptr) + break; + buffers2.push_back(ptr); } - } - for(int j = (int)buffers.size() - ;j-- - ;){ - a.deallocate(buffers[j]); - } - - return a.all_memory_deallocated() && a.check_sanity(); -} - -//This test allocates multiple values until there is no more memory -//and after that deallocates all in the same order -template -bool test_many_different_allocation_direct_deallocation(Allocator &a) -{ - std::size_t free_memory = a.get_free_memory(); - typedef typename Allocator::multiallocation_iterator multiallocation_iterator; - const std::size_t ArraySize = 11; - std::size_t requested_sizes[ArraySize]; - for(std::size_t i = 0; i < ArraySize; ++i){ - requested_sizes[i] = 4*i; - } - - std::vector buffers; - for(int i = 0; true; ++i){ - multiallocation_iterator it = a.allocate_many(requested_sizes, ArraySize, 1, std::nothrow); - if(!it) - break; - multiallocation_iterator itend; - - for(; it != itend; ++it){ - buffers.push_back(*it); + //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; + } } - } - for(int j = 0, max = (int)buffers.size() - ;j < max - ;++j){ - a.deallocate(buffers[j]); - } - - return free_memory == a.get_free_memory() && - a.all_memory_deallocated() && a.check_sanity(); -} - -//This test allocates multiple values until there is no more memory -//and after that deallocates all following a pattern -template -bool test_many_different_allocation_mixed_deallocation(Allocator &a) -{ - typedef typename Allocator::multiallocation_iterator multiallocation_iterator; - const std::size_t ArraySize = 11; - std::size_t requested_sizes[ArraySize]; - for(std::size_t i = 0; i < ArraySize; ++i){ - requested_sizes[i] = 4*i; - } - - std::vector buffers; - for(int i = 0; true; ++i){ - multiallocation_iterator it = a.allocate_many(requested_sizes, ArraySize, 1, std::nothrow); - if(!it) - break; - multiallocation_iterator itend; - - for(; it != itend; ++it){ - buffers.push_back(*it); + std::vector buffers; + for(int i = 0; true; ++i){ + multiallocation_iterator it = a.allocate_many(requested_sizes, ArraySize, 1, std::nothrow); + if(!it) + break; + multiallocation_iterator itend; + std::size_t n = 0; + for(; it != itend; ++n){ + buffers.push_back(*it++); + } + if(n != ArraySize) + return false; } - } - 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); - } + 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; + } - return a.all_memory_deallocated() && a.check_sanity(); + //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 function calls all tests template bool test_all_allocation(Allocator &a) { - std::cout << "Starting test_allocation_direct_deallocation. Class: " + std::cout << "Starting test_allocation. Class: " << typeid(a).name() << std::endl; - if(!test_allocation_direct_deallocation(a)){ + if(!test_allocation(a)){ std::cout << "test_allocation_direct_deallocation failed. Class: " << typeid(a).name() << std::endl; return false; } - std::cout << "Starting test_allocation_inverse_deallocation. Class: " + std::cout << "Starting test_many_equal_allocation. Class: " << typeid(a).name() << std::endl; - if(!test_allocation_inverse_deallocation(a)){ - std::cout << "test_allocation_inverse_deallocation failed. Class: " + 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_allocation_mixed_deallocation. Class: " + std::cout << "Starting test_many_different_allocation. Class: " << typeid(a).name() << std::endl; - if(!test_allocation_mixed_deallocation(a)){ - std::cout << "test_allocation_mixed_deallocation failed. Class: " - << typeid(a).name() << std::endl; - return false; - } - - std::cout << "Starting test_many_equal_allocation_direct_deallocation. Class: " - << typeid(a).name() << std::endl; - - if(!test_many_equal_allocation_direct_deallocation(a)){ - std::cout << "test_many_equal_allocation_direct_deallocation failed. Class: " - << typeid(a).name() << std::endl; - return false; - } - - std::cout << "Starting test_many_equal_allocation_inverse_deallocation. Class: " - << typeid(a).name() << std::endl; - - if(!test_many_equal_allocation_inverse_deallocation(a)){ - std::cout << "test_many_equal_allocation_inverse_deallocation failed. Class: " - << typeid(a).name() << std::endl; - return false; - } - - std::cout << "Starting test_many_equal_allocation_mixed_deallocation. Class: " - << typeid(a).name() << std::endl; - - if(!test_many_equal_allocation_mixed_deallocation(a)){ - std::cout << "test_many_equal_allocation_mixed_deallocation failed. Class: " - << typeid(a).name() << std::endl; - return false; - } - - std::cout << "Starting test_many_different_allocation_direct_deallocation. Class: " - << typeid(a).name() << std::endl; - - if(!test_many_different_allocation_direct_deallocation(a)){ - std::cout << "test_many_different_allocation_direct_deallocation failed. Class: " - << typeid(a).name() << std::endl; - return false; - } - - std::cout << "Starting test_many_different_allocation_inverse_deallocation. Class: " - << typeid(a).name() << std::endl; - - if(!test_many_different_allocation_inverse_deallocation(a)){ - std::cout << "test_many_different_allocation_inverse_deallocation failed. Class: " - << typeid(a).name() << std::endl; - return false; - } - - std::cout << "Starting test_many_different_allocation_mixed_deallocation. Class: " - << typeid(a).name() << std::endl; - - if(!test_many_different_allocation_mixed_deallocation(a)){ - std::cout << "test_many_different_allocation_mixed_deallocation failed. Class: " + if(!test_many_different_allocation(a)){ + std::cout << "test_many_different_allocation failed. Class: " << typeid(a).name() << std::endl; return false; } @@ -824,30 +766,45 @@ bool test_all_allocation(Allocator &a) 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; diff --git a/test/node_pool_test.cpp b/test/node_pool_test.cpp index 8d16c97..0d36e0b 100644 --- a/test/node_pool_test.cpp +++ b/test/node_pool_test.cpp @@ -7,7 +7,6 @@ // See http://www.boost.org/libs/interprocess for documentation. // ////////////////////////////////////////////////////////////////////////////// -/* #include #include #include @@ -165,13 +164,3 @@ int main () } #include -*/ - -#include - -int main() -{ - void *addr = malloc(100); - free(addr); - return 0; -} \ No newline at end of file diff --git a/test/shared_ptr_test.cpp b/test/shared_ptr_test.cpp index 7a03ef6..7371f89 100644 --- a/test/shared_ptr_test.cpp +++ b/test/shared_ptr_test.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include diff --git a/test/unique_ptr_test.cpp b/test/unique_ptr_test.cpp index 325273b..19830fb 100644 --- a/test/unique_ptr_test.cpp +++ b/test/unique_ptr_test.cpp @@ -31,8 +31,7 @@ class MyClass {} }; -typedef deleter my_deleter_type; -typedef unique_ptr my_unique_ptr_class; +typedef managed_unique_ptr::type my_unique_ptr_class; typedef set ,allocator (anonymous_instance)() - ,my_deleter); + ,segment.get_deleter()); my_unique_ptr_class my_ptr2(segment.construct(anonymous_instance)() - ,my_deleter); + ,segment.get_deleter()); //Backup relative pointers to future tests offset_ptr ptr1 = my_ptr.get(); offset_ptr ptr2 = my_ptr2.get(); //Test some copy constructors - my_unique_ptr_class my_ptr3(0, my_deleter); + my_unique_ptr_class my_ptr3(0, segment.get_deleter()); my_unique_ptr_class my_ptr4(move(my_ptr3)); //Construct a list and fill @@ -141,7 +140,7 @@ int main() assert(vector.begin()->get() == ptr1); assert(vector.rbegin()->get() == ptr2); - my_unique_ptr_class a(0, my_deleter), b(0, my_deleter); + my_unique_ptr_class a(0, segment.get_deleter()), b(0, segment.get_deleter()); a = move(b); } shared_memory_object::remove(process_name.c_str());