2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "misc/michael_allocator.h"
32 #include <cds/os/timer.h>
33 #include <cds/details/allocator.h>
35 #include "cppunit/cppunit_proxy.h"
39 static size_t s_nPassCount = 10;
40 static unsigned long long s_nAllocPerPass = 1024 * 1024 * 1024;
42 static size_t s_nConstructCount = 0;
43 static size_t s_nDestructCount = 0;
45 class Allocator_test : public CppUnitMini::TestCase
47 static const size_t s_nArrSizeSize = 64 * 1024;
48 unsigned int m_arrSize[s_nArrSizeSize];
50 template <typename ALLOC>
55 for ( size_t nPass = 0; nPass < s_nPassCount; ++nPass ) {
56 unsigned long long nTotalAllocated = 0;
58 while ( nTotalAllocated < s_nAllocPerPass ) {
59 size_t nSize = m_arrSize[nCurIdx] + 4;
60 char * p = a.allocate( nSize, nullptr );
61 CPPUNIT_ASSERT( p != nullptr );
62 memset( p, 0x96, nSize );
63 nTotalAllocated += nSize;
65 if ( ++nCurIdx > s_nArrSizeSize )
71 void alloc_free_michael()
73 std::cout << "\n\tMichael allocator" << std::flush;
75 alloc_free<MichaelHeap_NoStat<char> >();
76 double fDur = timer.duration();
77 std::cout << "\tduration=" << fDur << std::endl;
79 //cds::memory::michael_allocator::statistics st;
80 //s_MichaelAlloc.get_statistics( st );
84 std::cout << "\n\tstd::allocator" << std::flush;
86 alloc_free<std::allocator<char> >();
87 double fDur = timer.duration();
88 std::cout << "\tduration=" << fDur << std::endl;
91 template <typename ALLOC>
92 void alloc_all_free_all()
96 for ( size_t nPass = 0; nPass < s_nPassCount; ++nPass ) {
97 unsigned long long nTotalAllocated = 0;
98 char * pHead = a.allocate( sizeof(void *), nullptr );
99 CPPUNIT_ASSERT( pHead != nullptr );
102 while ( nTotalAllocated < s_nAllocPerPass ) {
103 size_t nSize = m_arrSize[nCurIdx] + sizeof(void *);
104 char * p = a.allocate( nSize, nullptr );
105 CPPUNIT_ASSERT( p != nullptr );
106 memset( p, 0x96, nSize );
107 *((char **) pCur) = p;
109 nTotalAllocated += nSize;
110 if ( ++nCurIdx > s_nArrSizeSize )
113 *((char **) pCur) = nullptr;
116 while ( pCur != nullptr ) {
117 char * pNext = *((char **) pCur);
118 a.deallocate( pCur, 0 );
124 void alloc_all_free_all_michael()
126 std::cout << "\n\tMichael allocator" << std::flush;
127 cds::OS::Timer timer;
128 alloc_all_free_all<MichaelHeap_NoStat<char> >();
129 double fDur = timer.duration();
130 std::cout << "\tduration=" << fDur << std::endl;
132 //cds::memory::michael_allocator::statistics st;
133 //s_MichaelAlloc.get_statistics( st );
135 void alloc_all_free_all_std()
137 std::cout << "\n\tstd::allocator" << std::flush;
138 cds::OS::Timer timer;
139 alloc_all_free_all<std::allocator<char> >();
140 double fDur = timer.duration();
141 std::cout << "\tduration=" << fDur << std::endl;
161 size_t const nArraySize = 10;
164 cds::details::Allocator<SimpleStruct> a;
165 pArr = a.NewArray( nArraySize );
166 a.Delete( pArr, nArraySize );
168 CPPUNIT_ASSERT( s_nConstructCount == nArraySize );
169 CPPUNIT_ASSERT( s_nConstructCount == s_nDestructCount );
173 void setUpParams( const CppUnitMini::TestCfg& cfg )
175 s_nPassCount = cfg.getULong( "PassCount", 10 );
176 s_nAllocPerPass = cfg.getULong( "AllocPerPass", 1024 ) * 1024 * 1024;
182 CPPUNIT_ASSERT( s_nArrSizeSize == sizeof(m_arrSize) / sizeof(m_arrSize[0]) );
183 for ( size_t i = 0; i < s_nArrSizeSize; ++i )
184 m_arrSize[i] = rand();
187 CPPUNIT_TEST_SUITE(Allocator_test);
188 CPPUNIT_TEST(test_array)
189 CPPUNIT_TEST(alloc_free_michael)
190 CPPUNIT_TEST(alloc_free_std)
191 CPPUNIT_TEST(alloc_all_free_all_michael)
192 CPPUNIT_TEST(alloc_all_free_all_std)
193 CPPUNIT_TEST_SUITE_END();
195 } // namespace memory
196 CPPUNIT_TEST_SUITE_REGISTRATION( misc::Allocator_test );