6 #include "cppunit/cppunit_proxy.h"
8 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
15 class AllocatorTest
: public CPPUNIT_NS::TestCase
17 CPPUNIT_TEST_SUITE(AllocatorTest
);
18 CPPUNIT_TEST(zero_allocation
);
19 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
20 CPPUNIT_TEST(bad_alloc_test
);
22 #if defined (STLPORT) && defined (_STLP_THREADS) && defined (_STLP_USE_PERTHREAD_ALLOC)
23 CPPUNIT_TEST(per_thread_alloc
);
25 CPPUNIT_TEST_SUITE_END();
28 void zero_allocation();
29 void bad_alloc_test();
30 void per_thread_alloc();
33 CPPUNIT_TEST_SUITE_REGISTRATION(AllocatorTest
);
36 // tests implementation
38 void AllocatorTest::zero_allocation()
40 typedef allocator
<char> CharAllocator
;
41 CharAllocator charAllocator
;
43 char* buf
= charAllocator
.allocate(0);
44 charAllocator
.deallocate(buf
, 0);
46 charAllocator
.deallocate(0, 0);
49 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
56 void AllocatorTest::bad_alloc_test()
58 typedef allocator
<BigStruct
> BigStructAllocType
;
59 BigStructAllocType bigStructAlloc
;
62 //Lets try to allocate almost 4096 Go (on most of the platforms) of memory:
63 BigStructAllocType::pointer pbigStruct
= bigStructAlloc
.allocate(1024 * 1024 * 1024);
65 //Allocation failed but no exception thrown
66 CPPUNIT_ASSERT( pbigStruct
!= 0 );
68 // Just it case it succeeds:
69 bigStructAlloc
.deallocate(pbigStruct
, 1024 * 1024 * 1024);
71 catch (bad_alloc
const&) {
74 //We shouldn't be there:
75 //Not bad_alloc exception thrown.
81 #if defined (STLPORT) && defined (_STLP_THREADS) && defined (_STLP_USE_PERTHREAD_ALLOC)
87 typedef vector
<int, per_thread_allocator
<int> > thread_vector
;
89 SharedDatas(size_t nbElems
) : threadVectors(nbElems
, (thread_vector
*)0) {
90 pthread_mutex_init(&mutex
, 0);
91 pthread_cond_init(&condition
, 0);
95 for (size_t i
= 0; i
< threadVectors
.size(); ++i
) {
96 delete threadVectors
[i
];
100 size_t initThreadVector() {
103 pthread_mutex_lock(&mutex
);
105 for (size_t i
= 0; i
< threadVectors
.size(); ++i
) {
106 if (threadVectors
[i
] == 0) {
107 threadVectors
[i
] = new thread_vector();
113 if (ret
!= threadVectors
.size() - 1) {
114 //We wait for other thread(s) to call this method too:
115 printf("Thread %d wait\n", ret
);
116 pthread_cond_wait(&condition
, &mutex
);
119 //We are the last thread calling this method, we signal this
120 //to the other thread(s) that might be waiting:
121 printf("Thread %d signal\n", ret
);
122 pthread_cond_signal(&condition
);
125 pthread_mutex_unlock(&mutex
);
130 thread_vector
& getThreadVector(size_t index
) {
131 //We return other thread thread_vector instance:
132 return *threadVectors
[(index
+ 1 == threadVectors
.size()) ? 0 : index
+ 1];
136 pthread_mutex_t mutex
;
137 pthread_cond_t condition
;
138 vector
<thread_vector
*> threadVectors
;
141 void* f(void* pdatas
) {
142 SharedDatas
*psharedDatas
= (SharedDatas
*)pdatas
;
144 int threadIndex
= psharedDatas
->initThreadVector();
146 for (int i
= 0; i
< 100; ++i
) {
147 psharedDatas
->getThreadVector(threadIndex
).push_back(i
);
153 void AllocatorTest::per_thread_alloc()
155 const size_t nth
= 2;
156 SharedDatas
datas(nth
);
160 for (i
= 0; i
< nth
; ++i
) {
161 pthread_create(&t
[i
], 0, f
, &datas
);
164 for (i
= 0; i
< nth
; ++i
) {
165 pthread_join(t
[i
], 0);