Added copyright and license
[libcds.git] / tests / test-hdr / misc / allocator_test.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.     
29 */
30
31 #include "misc/michael_allocator.h"
32 #include <cds/os/timer.h>
33 #include <cds/details/allocator.h>
34
35 #include "cppunit/cppunit_proxy.h"
36
37 namespace misc {
38
39     static size_t s_nPassCount = 10;
40     static unsigned long long s_nAllocPerPass = 1024 * 1024 * 1024;
41
42     static size_t s_nConstructCount = 0;
43     static size_t s_nDestructCount = 0;
44
45     class Allocator_test : public CppUnitMini::TestCase
46     {
47         static const size_t s_nArrSizeSize = 64 * 1024;
48         unsigned int    m_arrSize[s_nArrSizeSize];
49
50         template <typename ALLOC>
51         void alloc_free()
52         {
53             ALLOC a;
54
55             for ( size_t nPass = 0; nPass < s_nPassCount; ++nPass ) {
56                 unsigned long long nTotalAllocated = 0;
57                 size_t nCurIdx = 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;
64                     a.deallocate( p, 1 );
65                     if ( ++nCurIdx > s_nArrSizeSize )
66                         nCurIdx = 0;
67                 }
68             }
69         }
70
71         void alloc_free_michael()
72         {
73             std::cout << "\n\tMichael allocator" << std::flush;
74             cds::OS::Timer    timer;
75             alloc_free<MichaelHeap_NoStat<char> >();
76             double fDur = timer.duration();
77             std::cout << "\tduration=" << fDur << std::endl;
78
79             //cds::memory::michael_allocator::statistics st;
80             //s_MichaelAlloc.get_statistics( st );
81         }
82         void alloc_free_std()
83         {
84             std::cout << "\n\tstd::allocator" << std::flush;
85             cds::OS::Timer    timer;
86             alloc_free<std::allocator<char> >();
87             double fDur = timer.duration();
88             std::cout << "\tduration=" << fDur << std::endl;
89         }
90
91         template <typename ALLOC>
92         void alloc_all_free_all()
93         {
94             ALLOC a;
95
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 );
100                 char * pCur = pHead;
101                 size_t nCurIdx = 0;
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;
108                     pCur = p;
109                     nTotalAllocated += nSize;
110                     if ( ++nCurIdx > s_nArrSizeSize )
111                         nCurIdx = 0;
112                 }
113                 *((char **) pCur) = nullptr;
114
115                 pCur = pHead;
116                 while ( pCur != nullptr ) {
117                     char * pNext = *((char **) pCur);
118                     a.deallocate( pCur, 0 );
119                     pCur = pNext;
120                 }
121             }
122         }
123
124         void alloc_all_free_all_michael()
125         {
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;
131
132             //cds::memory::michael_allocator::statistics st;
133             //s_MichaelAlloc.get_statistics( st );
134         }
135         void alloc_all_free_all_std()
136         {
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;
142         }
143
144         struct SimpleStruct
145         {
146             int     n;
147
148             SimpleStruct()
149             {
150                 ++s_nConstructCount;
151             }
152
153             ~SimpleStruct()
154             {
155                 ++s_nDestructCount;
156             }
157         };
158
159         void test_array()
160         {
161             size_t const nArraySize = 10;
162
163             SimpleStruct * pArr;
164             cds::details::Allocator<SimpleStruct>  a;
165             pArr = a.NewArray( nArraySize );
166             a.Delete( pArr, nArraySize );
167
168             CPPUNIT_ASSERT( s_nConstructCount == nArraySize );
169             CPPUNIT_ASSERT( s_nConstructCount == s_nDestructCount );
170         }
171
172
173         void setUpParams( const CppUnitMini::TestCfg& cfg )
174         {
175             s_nPassCount = cfg.getULong( "PassCount", 10 );
176             s_nAllocPerPass = cfg.getULong( "AllocPerPass", 1024 ) * 1024 * 1024;
177         }
178
179     public:
180         Allocator_test()
181         {
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();
185         }
186
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();
194     };
195 }   // namespace memory
196 CPPUNIT_TEST_SUITE_REGISTRATION( misc::Allocator_test );