Add Polymorphic Memory Resource utilities and rework the separately compiled library:

- Dlmalloc's based C function are boost_cont_xxx no longer exported, but wrapped into C++ linkage dlmalloc_xxx functions to effectively reuse Boost's dynamic library and autolink machinery instead of rewriting machinery to compile the C source file.
- Refactored scoped_allocator_adaptor's construct logic as it was shared with polymorphic allocator's one. Moved common logic to detail/dispatch_uses_allocator.hpp. Refactored also scoped_allocator_adaptor test utilities to be reused with polymorphic_allocator tests.
This commit is contained in:
Ion Gaztañaga
2015-09-07 19:16:46 +02:00
parent 77100c0bfc
commit 33d2f0f7af
110 changed files with 10143 additions and 1160 deletions

View File

@@ -17,7 +17,7 @@
#include <iostream>
#include <cstring>
#include <algorithm> //std::remove
#include <boost/container/detail/alloc_lib_auto_link.hpp>
#include <boost/container/detail/dlmalloc.hpp>
namespace boost { namespace container { namespace test {
@@ -30,9 +30,9 @@ enum deallocation_type { DirectDeallocation, InverseDeallocation, MixedDeallocat
bool test_allocation()
{
if(!boost_cont_all_deallocated())
if(!dlmalloc_all_deallocated())
return false;
boost_cont_malloc_check();
dlmalloc_malloc_check();
for( deallocation_type t = DirectDeallocation
; t != EndDeallocationType
; t = (deallocation_type)((int)t + 1)){
@@ -40,7 +40,7 @@ bool test_allocation()
//std::size_t free_memory = a.get_free_memory();
for(int i = 0; i != NumIt; ++i){
void *ptr = boost_cont_malloc(i);
void *ptr = dlmalloc_malloc(i);
if(!ptr)
break;
buffers.push_back(ptr);
@@ -52,7 +52,7 @@ bool test_allocation()
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
boost_cont_free(buffers[j]);
dlmalloc_free(buffers[j]);
}
}
break;
@@ -61,7 +61,7 @@ bool test_allocation()
for(int j = (int)buffers.size()
;j--
;){
boost_cont_free(buffers[j]);
dlmalloc_free(buffers[j]);
}
}
break;
@@ -71,7 +71,7 @@ bool test_allocation()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
boost_cont_free(buffers[pos]);
dlmalloc_free(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
}
@@ -79,14 +79,14 @@ bool test_allocation()
default:
break;
}
if(!boost_cont_all_deallocated())
if(!dlmalloc_all_deallocated())
return false;
//bool ok = free_memory == a.get_free_memory() &&
//a.all_memory_deallocated() && a.check_sanity();
//if(!ok) return ok;
}
boost_cont_malloc_check();
return 0 != boost_cont_all_deallocated();
dlmalloc_malloc_check();
return 0 != dlmalloc_all_deallocated();
}
//This test allocates until there is no more memory
@@ -95,12 +95,12 @@ bool test_allocation()
bool test_allocation_shrink()
{
boost_cont_malloc_check();
dlmalloc_malloc_check();
std::vector<void*> buffers;
//Allocate buffers with extra memory
for(int i = 0; i != NumIt; ++i){
void *ptr = boost_cont_malloc(i*2);
void *ptr = dlmalloc_malloc(i*2);
if(!ptr)
break;
buffers.push_back(ptr);
@@ -111,12 +111,12 @@ bool test_allocation_shrink()
;i < max
; ++i){
std::size_t try_received_size = 0;
void* try_result = boost_cont_allocation_command
void* try_result = dlmalloc_allocation_command
( BOOST_CONTAINER_TRY_SHRINK_IN_PLACE, 1, i*2
, i, &try_received_size, (char*)buffers[i]).first;
std::size_t received_size = 0;
void* result = boost_cont_allocation_command
void* result = dlmalloc_allocation_command
( BOOST_CONTAINER_SHRINK_IN_PLACE, 1, i*2
, i, &received_size, (char*)buffers[i]).first;
@@ -141,11 +141,11 @@ bool test_allocation_shrink()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
boost_cont_free(buffers[pos]);
dlmalloc_free(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
boost_cont_malloc_check();
return 0 != boost_cont_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
dlmalloc_malloc_check();
return 0 != dlmalloc_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates until there is no more memory
@@ -154,12 +154,12 @@ bool test_allocation_shrink()
bool test_allocation_expand()
{
boost_cont_malloc_check();
dlmalloc_malloc_check();
std::vector<void*> buffers;
//Allocate buffers with extra memory
for(int i = 0; i != NumIt; ++i){
void *ptr = boost_cont_malloc(i);
void *ptr = dlmalloc_malloc(i);
if(!ptr)
break;
buffers.push_back(ptr);
@@ -173,7 +173,7 @@ bool test_allocation_expand()
std::size_t min_size = i+1;
std::size_t preferred_size = i*2;
preferred_size = min_size > preferred_size ? min_size : preferred_size;
while(boost_cont_allocation_command
while(dlmalloc_allocation_command
( BOOST_CONTAINER_EXPAND_FWD, 1, min_size
, preferred_size, &received_size, (char*)buffers[i]).first){
//Check received size is bigger than minimum
@@ -191,11 +191,11 @@ bool test_allocation_expand()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
boost_cont_free(buffers[pos]);
dlmalloc_free(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
boost_cont_malloc_check();
return 0 != boost_cont_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
dlmalloc_malloc_check();
return 0 != dlmalloc_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates until there is no more memory
@@ -210,10 +210,10 @@ bool test_allocation_shrink_and_expand()
//Allocate buffers wand store received sizes
for(int i = 0; i != NumIt; ++i){
std::size_t received_size = 0;
void *ptr = boost_cont_allocation_command
void *ptr = dlmalloc_allocation_command
(BOOST_CONTAINER_ALLOCATE_NEW, 1, i, i*2, &received_size, 0).first;
if(!ptr){
ptr = boost_cont_allocation_command
ptr = dlmalloc_allocation_command
( BOOST_CONTAINER_ALLOCATE_NEW, 1, 1, i*2, &received_size, 0).first;
if(!ptr)
break;
@@ -229,7 +229,7 @@ bool test_allocation_shrink_and_expand()
std::size_t received_size = 0;
bool size_reduced_flag;
if(true == (size_reduced_flag = !!
boost_cont_allocation_command
dlmalloc_allocation_command
( BOOST_CONTAINER_SHRINK_IN_PLACE, 1, received_sizes[i]
, i, &received_size, (char*)buffers[i]).first)){
if(received_size > std::size_t(received_sizes[i])){
@@ -249,7 +249,7 @@ bool test_allocation_shrink_and_expand()
if(!size_reduced[i]) continue;
std::size_t received_size = 0;
std::size_t request_size = received_sizes[i];
if(boost_cont_allocation_command
if(dlmalloc_allocation_command
( BOOST_CONTAINER_EXPAND_FWD, 1, request_size
, request_size, &received_size, (char*)buffers[i]).first){
if(received_size != request_size){
@@ -266,11 +266,11 @@ bool test_allocation_shrink_and_expand()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
boost_cont_free(buffers[pos]);
dlmalloc_free(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
return 0 != boost_cont_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
return 0 != dlmalloc_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates until there is no more memory
@@ -280,12 +280,12 @@ bool test_allocation_shrink_and_expand()
bool test_allocation_deallocation_expand()
{
boost_cont_malloc_check();
dlmalloc_malloc_check();
std::vector<void*> buffers;
//Allocate buffers with extra memory
for(int i = 0; i != NumIt; ++i){
void *ptr = boost_cont_malloc(i);
void *ptr = dlmalloc_malloc(i);
if(!ptr)
break;
buffers.push_back(ptr);
@@ -297,7 +297,7 @@ bool test_allocation_deallocation_expand()
;i < max
;++i){
if(i%2){
boost_cont_free(buffers[i]);
dlmalloc_free(buffers[i]);
buffers[i] = 0;
}
}
@@ -313,7 +313,7 @@ bool test_allocation_deallocation_expand()
std::size_t preferred_size = i*2;
preferred_size = min_size > preferred_size ? min_size : preferred_size;
while(boost_cont_allocation_command
while(dlmalloc_allocation_command
( BOOST_CONTAINER_EXPAND_FWD, 1, min_size
, preferred_size, &received_size, (char*)buffers[i]).first){
//Check received size is bigger than minimum
@@ -336,11 +336,11 @@ bool test_allocation_deallocation_expand()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
boost_cont_free(buffers[pos]);
dlmalloc_free(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
boost_cont_malloc_check();
return 0 != boost_cont_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
dlmalloc_malloc_check();
return 0 != dlmalloc_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates until there is no more memory
@@ -352,14 +352,14 @@ bool test_allocation_deallocation_expand()
bool test_allocation_with_reuse()
{
boost_cont_malloc_check();
dlmalloc_malloc_check();
//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; i != NumIt; ++i){
void *ptr = boost_cont_malloc(i*sizeof_object);
void *ptr = dlmalloc_malloc(i*sizeof_object);
if(!ptr)
break;
buffers.push_back(ptr);
@@ -370,7 +370,7 @@ bool test_allocation_with_reuse()
for(int i = 0, max = (int)buffers.size() - 1
;i < max
;++i){
boost_cont_free(buffers[i]);
dlmalloc_free(buffers[i]);
}
//Save the unique buffer and clear vector
@@ -382,7 +382,7 @@ bool test_allocation_with_reuse()
for(int i = 0; i != NumIt; ++i){
std::size_t min_size = (received_size/sizeof_object + 1)*sizeof_object;
std::size_t prf_size = (received_size/sizeof_object + (i+1)*2)*sizeof_object;
boost_cont_command_ret_t ret = boost_cont_allocation_command
dlmalloc_command_ret_t ret = dlmalloc_allocation_command
( BOOST_CONTAINER_EXPAND_BWD, sizeof_object, min_size
, prf_size, &received_size, (char*)ptr);
//If we have memory, this must be a buffer reuse
@@ -396,9 +396,9 @@ bool test_allocation_with_reuse()
ptr = ret.first;
}
//There should be only a single block so deallocate it
boost_cont_free(ptr);
boost_cont_malloc_check();
if(!boost_cont_all_deallocated())
dlmalloc_free(ptr);
dlmalloc_malloc_check();
if(!dlmalloc_all_deallocated())
return false;
}
return true;
@@ -410,26 +410,26 @@ bool test_allocation_with_reuse()
bool test_aligned_allocation()
{
boost_cont_malloc_check();
dlmalloc_malloc_check();
//Allocate aligned buffers in a loop
//and then deallocate it
for(unsigned int i = 1; i != (1 << (sizeof(int)/2)); i <<= 1){
for(unsigned int j = 1; j != 512; j <<= 1){
void *ptr = boost_cont_memalign(i-1, j);
void *ptr = dlmalloc_memalign(i-1, j);
if(!ptr){
return false;
}
if(((std::size_t)ptr & (j - 1)) != 0)
return false;
boost_cont_free(ptr);
dlmalloc_free(ptr);
//if(!a.all_memory_deallocated() || !a.check_sanity()){
// return false;
//}
}
}
boost_cont_malloc_check();
return 0 != boost_cont_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
dlmalloc_malloc_check();
return 0 != dlmalloc_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
}
//This test allocates memory with different alignments
@@ -437,7 +437,7 @@ bool test_aligned_allocation()
bool test_continuous_aligned_allocation()
{
boost_cont_malloc_check();
dlmalloc_malloc_check();
std::vector<void*> buffers;
//Allocate aligned buffers in a loop
//and then deallocate it
@@ -447,7 +447,7 @@ bool test_continuous_aligned_allocation()
for(unsigned i = 1; i < MaxSize; i <<= 1){
for(unsigned int j = 1; j < MaxAlign; j <<= 1){
for(int k = 0; k != NumIt; ++k){
void *ptr = boost_cont_memalign(i-1, j);
void *ptr = dlmalloc_memalign(i-1, j);
buffers.push_back(ptr);
if(!ptr){
continue_loop = false;
@@ -459,7 +459,7 @@ bool test_continuous_aligned_allocation()
}
//Deallocate all
for(int k = (int)buffers.size(); k--;){
boost_cont_free(buffers[k]);
dlmalloc_free(buffers[k]);
}
buffers.clear();
//if(!a.all_memory_deallocated() && a.check_sanity())
@@ -468,15 +468,15 @@ bool test_continuous_aligned_allocation()
break;
}
}
boost_cont_malloc_check();
return 0 != boost_cont_all_deallocated();//a.all_memory_deallocated() && a.check_sanity();
dlmalloc_malloc_check();
return 0 != dlmalloc_all_deallocated();//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 inverse order
bool test_many_equal_allocation()
{
boost_cont_malloc_check();
dlmalloc_malloc_check();
for( deallocation_type t = DirectDeallocation
; t != EndDeallocationType
; t = (deallocation_type)((int)t + 1)){
@@ -486,7 +486,7 @@ bool test_many_equal_allocation()
//Allocate buffers with extra memory
for(int i = 0; i != NumIt; ++i){
void *ptr = boost_cont_malloc(i);
void *ptr = dlmalloc_malloc(i);
if(!ptr)
break;
//if(!a.check_sanity())
@@ -500,7 +500,7 @@ bool test_many_equal_allocation()
;i < max
;++i){
if(i%2){
boost_cont_free(buffers2[i]);
dlmalloc_free(buffers2[i]);
buffers2[i] = 0;
}
}
@@ -510,10 +510,10 @@ bool test_many_equal_allocation()
std::vector<void*> buffers;
for(int i = 0; i != NumIt/10; ++i){
boost_cont_memchain chain;
dlmalloc_memchain chain;
BOOST_CONTAINER_MEMCHAIN_INIT(&chain);
boost_cont_multialloc_nodes((i+1)*2, i+1, DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &chain);
boost_cont_memchain_it it = BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(&chain);
dlmalloc_multialloc_nodes((i+1)*2, i+1, DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &chain);
dlmalloc_memchain_it it = BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(&chain);
if(BOOST_CONTAINER_MEMCHAIN_IS_END_IT(chain, it))
break;
@@ -535,7 +535,7 @@ bool test_many_equal_allocation()
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
boost_cont_free(buffers[j]);
dlmalloc_free(buffers[j]);
}
}
break;
@@ -544,7 +544,7 @@ bool test_many_equal_allocation()
for(int j = (int)buffers.size()
;j--
;){
boost_cont_free(buffers[j]);
dlmalloc_free(buffers[j]);
}
}
break;
@@ -554,7 +554,7 @@ bool test_many_equal_allocation()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
boost_cont_free(buffers[pos]);
dlmalloc_free(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
}
@@ -570,7 +570,7 @@ bool test_many_equal_allocation()
;j < max
;++j){
int pos = (j%4)*((int)buffers2.size())/4;
boost_cont_free(buffers2[pos]);
dlmalloc_free(buffers2[pos]);
buffers2.erase(buffers2.begin()+pos);
}
@@ -578,8 +578,8 @@ bool test_many_equal_allocation()
//a.all_memory_deallocated() && a.check_sanity();
//if(!ok) return ok;
}
boost_cont_malloc_check();
return 0 != boost_cont_all_deallocated();
dlmalloc_malloc_check();
return 0 != dlmalloc_all_deallocated();
}
//This test allocates multiple values until there is no more memory
@@ -587,7 +587,7 @@ bool test_many_equal_allocation()
bool test_many_different_allocation()
{
boost_cont_malloc_check();
dlmalloc_malloc_check();
const std::size_t ArraySize = 11;
std::size_t requested_sizes[ArraySize];
for(std::size_t i = 0; i < ArraySize; ++i){
@@ -603,7 +603,7 @@ bool test_many_different_allocation()
//Allocate buffers with extra memory
for(int i = 0; i != NumIt; ++i){
void *ptr = boost_cont_malloc(i);
void *ptr = dlmalloc_malloc(i);
if(!ptr)
break;
buffers2.push_back(ptr);
@@ -615,17 +615,17 @@ bool test_many_different_allocation()
;i < max
;++i){
if(i%2){
boost_cont_free(buffers2[i]);
dlmalloc_free(buffers2[i]);
buffers2[i] = 0;
}
}
std::vector<void*> buffers;
for(int i = 0; i != NumIt; ++i){
boost_cont_memchain chain;
dlmalloc_memchain chain;
BOOST_CONTAINER_MEMCHAIN_INIT(&chain);
boost_cont_multialloc_arrays(ArraySize, requested_sizes, 1, DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &chain);
boost_cont_memchain_it it = BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(&chain);
dlmalloc_multialloc_arrays(ArraySize, requested_sizes, 1, DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &chain);
dlmalloc_memchain_it it = BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(&chain);
if(BOOST_CONTAINER_MEMCHAIN_IS_END_IT(chain, it))
break;
std::size_t n = 0;
@@ -643,7 +643,7 @@ bool test_many_different_allocation()
for(int j = 0, max = (int)buffers.size()
;j < max
;++j){
boost_cont_free(buffers[j]);
dlmalloc_free(buffers[j]);
}
}
break;
@@ -652,7 +652,7 @@ bool test_many_different_allocation()
for(int j = (int)buffers.size()
;j--
;){
boost_cont_free(buffers[j]);
dlmalloc_free(buffers[j]);
}
}
break;
@@ -662,7 +662,7 @@ bool test_many_different_allocation()
;j < max
;++j){
int pos = (j%4)*((int)buffers.size())/4;
boost_cont_free(buffers[pos]);
dlmalloc_free(buffers[pos]);
buffers.erase(buffers.begin()+pos);
}
}
@@ -678,7 +678,7 @@ bool test_many_different_allocation()
;j < max
;++j){
int pos = (j%4)*((int)buffers2.size())/4;
boost_cont_free(buffers2[pos]);
dlmalloc_free(buffers2[pos]);
buffers2.erase(buffers2.begin()+pos);
}
@@ -686,53 +686,53 @@ bool test_many_different_allocation()
//a.all_memory_deallocated() && a.check_sanity();
//if(!ok) return ok;
}
boost_cont_malloc_check();
return 0 != boost_cont_all_deallocated();
dlmalloc_malloc_check();
return 0 != dlmalloc_all_deallocated();
}
bool test_many_deallocation()
{
const std::size_t ArraySize = 11;
std::vector<boost_cont_memchain> buffers;
std::vector<dlmalloc_memchain> buffers;
std::size_t requested_sizes[ArraySize];
for(std::size_t i = 0; i < ArraySize; ++i){
requested_sizes[i] = 4*i;
}
for(int i = 0; i != NumIt; ++i){
boost_cont_memchain chain;
dlmalloc_memchain chain;
BOOST_CONTAINER_MEMCHAIN_INIT(&chain);
boost_cont_multialloc_arrays(ArraySize, requested_sizes, 1, DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &chain);
boost_cont_memchain_it it = BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(&chain);
dlmalloc_multialloc_arrays(ArraySize, requested_sizes, 1, DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &chain);
dlmalloc_memchain_it it = BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(&chain);
if(BOOST_CONTAINER_MEMCHAIN_IS_END_IT(chain, it))
return false;
buffers.push_back(chain);
}
for(int i = 0; i != NumIt; ++i){
boost_cont_multidealloc(&buffers[i]);
dlmalloc_multidealloc(&buffers[i]);
}
buffers.clear();
boost_cont_malloc_check();
if(!boost_cont_all_deallocated())
dlmalloc_malloc_check();
if(!dlmalloc_all_deallocated())
return false;
for(int i = 0; i != NumIt; ++i){
boost_cont_memchain chain;
dlmalloc_memchain chain;
BOOST_CONTAINER_MEMCHAIN_INIT(&chain);
boost_cont_multialloc_nodes(ArraySize, i*4+1, DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &chain);
boost_cont_memchain_it it = BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(&chain);
dlmalloc_multialloc_nodes(ArraySize, i*4+1, DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &chain);
dlmalloc_memchain_it it = BOOST_CONTAINER_MEMCHAIN_BEGIN_IT(&chain);
if(BOOST_CONTAINER_MEMCHAIN_IS_END_IT(chain, it))
return false;
buffers.push_back(chain);
}
for(int i = 0; i != NumIt; ++i){
boost_cont_multidealloc(&buffers[i]);
dlmalloc_multidealloc(&buffers[i]);
}
buffers.clear();
boost_cont_malloc_check();
if(!boost_cont_all_deallocated())
dlmalloc_malloc_check();
if(!dlmalloc_all_deallocated())
return false;
return true;
@@ -835,7 +835,7 @@ bool test_all_allocation()
return false;
}
return 0 != boost_cont_all_deallocated();
return 0 != dlmalloc_all_deallocated();
}
}}} //namespace boost { namespace container { namespace test {