1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
12 #pragma warning (disable : 4512)
15 #include <boost/container/detail/dlmalloc.hpp>
17 #define BOOST_INTERPROCESS_VECTOR_ALLOC_STATS
19 #include <iostream> //std::cout, std::endl
20 #include <typeinfo> //typeid
21 #include <cassert> //assert
23 #include <boost/timer/timer.hpp>
24 using boost::timer::cpu_timer
;
25 using boost::timer::cpu_times
;
26 using boost::timer::nanosecond_type
;
28 using namespace boost::container
;
31 void allocation_timing_test(unsigned int num_iterations
, unsigned int num_elements
)
34 unsigned int numalloc
= 0, numexpand
= 0;
37 << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"
38 << " Iterations/Elements: " << num_iterations
<< "/" << num_elements
<< '\n'
39 << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"
43 allocation_type malloc_types
[] = { BOOST_CONTAINER_EXPAND_BWD
, BOOST_CONTAINER_EXPAND_FWD
, BOOST_CONTAINER_ALLOCATE_NEW
};
44 const char * malloc_names
[] = { "Backwards expansion", "Forward expansion", "New allocation" };
45 for(size_t i
= 0; i
< sizeof(malloc_types
)/sizeof(allocation_type
); ++i
){
46 numalloc
= 0; numexpand
= 0;
47 const allocation_type m_mode
= malloc_types
[i
];
48 const char *malloc_name
= malloc_names
[i
];
53 for(unsigned int r
= 0; r
!= num_iterations
; ++r
){
55 if(m_mode
!= BOOST_CONTAINER_EXPAND_FWD
)
56 first_mem
= dlmalloc_malloc(sizeof(POD
)*num_elements
*3/2);
57 void *addr
= dlmalloc_malloc(1*sizeof(POD
));
58 if(m_mode
== BOOST_CONTAINER_EXPAND_FWD
)
59 first_mem
= dlmalloc_malloc(sizeof(POD
)*num_elements
*3/2);
60 capacity
= dlmalloc_size(addr
)/sizeof(POD
);
61 dlmalloc_free(first_mem
);
65 dlmalloc_command_ret_t ret
;
66 for(size_t e
= capacity
+ 1; e
< num_elements
; ++e
){
68 size_t min
= (capacity
+1)*sizeof(POD
);
69 size_t max
= (capacity
*3/2)*sizeof(POD
);
72 ret
= dlmalloc_allocation_command
74 , min
, max
, &received_size
, addr
);
76 std::cout
<< "(!ret.first)!" << std::endl
;
80 assert(m_mode
== BOOST_CONTAINER_ALLOCATE_NEW
);
81 if(m_mode
!= BOOST_CONTAINER_ALLOCATE_NEW
){
82 std::cout
<< "m_mode != BOOST_CONTAINER_ALLOCATE_NEW!" << std::endl
;
90 assert(m_mode
!= BOOST_CONTAINER_ALLOCATE_NEW
);
91 if(m_mode
== BOOST_CONTAINER_ALLOCATE_NEW
){
92 std::cout
<< "m_mode == BOOST_CONTAINER_ALLOCATE_NEW!" << std::endl
;
97 capacity
= received_size
/sizeof(POD
);
109 assert( dlmalloc_allocated_memory() == 0);
110 if(dlmalloc_allocated_memory()!= 0){
111 std::cout
<< "Memory leak!" << std::endl
;
116 nanosecond_type nseconds
= timer
.elapsed().wall
;
118 std::cout
<< " Malloc type: " << malloc_name
120 << " allocation ns: "
121 << float(nseconds
)/(num_iterations
*num_elements
)
123 << " capacity - alloc calls (new/expand): "
124 << (unsigned int)capacity
<< " - "
125 << (float(numalloc
) + float(numexpand
))/num_iterations
126 << "(" << float(numalloc
)/num_iterations
<< "/" << float(numexpand
)/num_iterations
<< ")"
127 << std::endl
<< std::endl
;
139 int allocation_loop()
141 std::cout
<< std::endl
142 << "-------------------------------------------\n"
143 << "-------------------------------------------\n"
144 << " Type(sizeof): " << typeid(POD
).name() << " (" << sizeof(POD
) << ")\n"
145 << "-------------------------------------------\n"
146 << "-------------------------------------------\n"
152 unsigned int numrep
[] = { /*10000, */10000, 100000, 1000000 };
154 unsigned int numrep
[] = { /*10000, */1000, 10000, 100000 };
156 unsigned int numele
[] = { /*10000, */1000, 100, 10 };
159 unsigned int numrep
[] = { 50000 };
161 unsigned int numrep
[] = { 5000 };
163 unsigned int numele
[] = { 100 };
166 for(unsigned int i
= 0; i
< sizeof(numele
)/sizeof(numele
[0]); ++i
){
167 allocation_timing_test
<POD
>(numrep
[i
], numele
[i
]);
175 dlmalloc_mallopt( (-3)//M_MMAP_THRESHOLD
177 //allocation_loop<char_holder<4> >();
178 //allocation_loop<char_holder<6> >();
179 allocation_loop
<char_holder
<8> >();
180 allocation_loop
<char_holder
<12> >();
181 //allocation_loop<char_holder<14> >();
182 allocation_loop
<char_holder
<24> >();