Changes introduced by the new intrusive version.

[SVN r39552]
This commit is contained in:
Ion Gaztañaga
2007-09-26 17:46:34 +00:00
parent db1dba818e
commit 8d76235614
5 changed files with 244 additions and 300 deletions

View File

@@ -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){

View File

@@ -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<class Allocator>
bool test_allocation_inverse_deallocation(Allocator &a)
bool test_allocation(Allocator &a)
{
std::vector<void*> buffers;
for( deallocation_type t = DirectDeallocation
; t != EndDeallocationType
; t = (deallocation_type)((int)t + 1)){
std::vector<void*> 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<class Allocator>
bool test_allocation_direct_deallocation(Allocator &a)
{
std::vector<void*> 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<class Allocator>
bool test_allocation_mixed_deallocation(Allocator &a)
{
std::vector<void*> 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<class Allocator>
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<void*> 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<void*> 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<class Allocator>
bool test_many_equal_allocation_direct_deallocation(Allocator &a)
{
typedef typename Allocator::multiallocation_iterator multiallocation_iterator;
std::vector<void*> 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<void*> 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<class Allocator>
bool test_many_equal_allocation_mixed_deallocation(Allocator &a)
{
typedef typename Allocator::multiallocation_iterator multiallocation_iterator;
std::vector<void*> 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<class Allocator>
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<void*> 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<void*> 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<class Allocator>
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<void*> 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<class Allocator>
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<void*> 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<void*> 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<class Allocator>
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;

View File

@@ -7,7 +7,6 @@
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
/*
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/detail/node_pool.hpp>
@@ -165,13 +164,3 @@ int main ()
}
#include <boost/interprocess/detail/config_end.hpp>
*/
#include<stdlib.h>
int main()
{
void *addr = malloc(100);
free(addr);
return 0;
}

View File

@@ -19,7 +19,6 @@
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/detail/utilities.hpp>
#include <boost/interprocess/smart_ptr/deleter.hpp>
#include <boost/interprocess/smart_ptr/scoped_ptr.hpp>
#include <boost/detail/lightweight_test.hpp>

View File

@@ -31,8 +31,7 @@ class MyClass
{}
};
typedef deleter<MyClass, managed_shared_memory::segment_manager> my_deleter_type;
typedef unique_ptr<MyClass, my_deleter_type> my_unique_ptr_class;
typedef managed_unique_ptr<MyClass, managed_shared_memory>::type my_unique_ptr_class;
typedef set <my_unique_ptr_class
,std::less<my_unique_ptr_class>
,allocator <my_unique_ptr_class
@@ -58,19 +57,19 @@ int main()
shared_memory_object::remove(process_name.c_str());
{
managed_shared_memory segment(create_only, process_name.c_str(), 10000);
my_deleter_type my_deleter(segment.get_segment_manager());
//Create unique_ptr using dynamic allocation
my_unique_ptr_class my_ptr (segment.construct<MyClass>(anonymous_instance)()
,my_deleter);
,segment.get_deleter<MyClass>());
my_unique_ptr_class my_ptr2(segment.construct<MyClass>(anonymous_instance)()
,my_deleter);
,segment.get_deleter<MyClass>());
//Backup relative pointers to future tests
offset_ptr<MyClass> ptr1 = my_ptr.get();
offset_ptr<MyClass> 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<MyClass>());
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<MyClass>()), b(0, segment.get_deleter<MyClass>());
a = move(b);
}
shared_memory_object::remove(process_name.c_str());