31
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
#include <memory>
|
|
18 |
#include <vector>
|
|
19 |
|
|
20 |
#include <cstdio>
|
|
21 |
|
|
22 |
#include "cppunit/cppunit_proxy.h"
|
|
23 |
|
|
24 |
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
|
|
25 |
using namespace std;
|
|
26 |
#endif
|
|
27 |
|
|
28 |
//
|
|
29 |
// TestCase class
|
|
30 |
//
|
|
31 |
class AllocatorTest : public CPPUNIT_NS::TestCase
|
|
32 |
{
|
|
33 |
CPPUNIT_TEST_SUITE(AllocatorTest);
|
|
34 |
CPPUNIT_TEST(zero_allocation);
|
|
35 |
#if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
|
|
36 |
CPPUNIT_TEST(bad_alloc_test);
|
|
37 |
#endif
|
|
38 |
#if defined (STLPORT) && defined (_STLP_THREADS) && defined (_STLP_USE_PERTHREAD_ALLOC)
|
|
39 |
CPPUNIT_TEST(per_thread_alloc);
|
|
40 |
#endif
|
|
41 |
CPPUNIT_TEST_SUITE_END();
|
|
42 |
|
|
43 |
protected:
|
|
44 |
void zero_allocation();
|
|
45 |
void bad_alloc_test();
|
|
46 |
void per_thread_alloc();
|
|
47 |
};
|
|
48 |
|
|
49 |
CPPUNIT_TEST_SUITE_REGISTRATION(AllocatorTest);
|
|
50 |
|
|
51 |
//
|
|
52 |
// tests implementation
|
|
53 |
//
|
|
54 |
void AllocatorTest::zero_allocation()
|
|
55 |
{
|
|
56 |
typedef allocator<char> CharAllocator;
|
|
57 |
CharAllocator charAllocator;
|
|
58 |
|
|
59 |
char* buf = charAllocator.allocate(0);
|
|
60 |
charAllocator.deallocate(buf, 0);
|
|
61 |
|
|
62 |
charAllocator.deallocate(0, 0);
|
|
63 |
}
|
|
64 |
|
|
65 |
#if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
|
|
66 |
|
|
67 |
struct BigStruct
|
|
68 |
{
|
|
69 |
char _data[4096];
|
|
70 |
};
|
|
71 |
|
|
72 |
void AllocatorTest::bad_alloc_test()
|
|
73 |
{
|
|
74 |
typedef allocator<BigStruct> BigStructAllocType;
|
|
75 |
BigStructAllocType bigStructAlloc;
|
|
76 |
|
|
77 |
try {
|
|
78 |
//Lets try to allocate almost 4096 Go (on most of the platforms) of memory:
|
|
79 |
BigStructAllocType::pointer pbigStruct = bigStructAlloc.allocate(1024 * 1024 * 1024);
|
|
80 |
|
|
81 |
//Allocation failed but no exception thrown
|
|
82 |
CPPUNIT_ASSERT( pbigStruct != 0 );
|
|
83 |
}
|
|
84 |
catch (bad_alloc const&) {
|
|
85 |
}
|
|
86 |
catch (...) {
|
|
87 |
//We shouldn't be there:
|
|
88 |
//Not bad_alloc exception thrown.
|
|
89 |
CPPUNIT_ASSERT( false );
|
|
90 |
}
|
|
91 |
}
|
|
92 |
#endif
|
|
93 |
|
|
94 |
#if defined (STLPORT) && defined (_STLP_THREADS) && defined (_STLP_USE_PERTHREAD_ALLOC)
|
|
95 |
# include <pthread.h>
|
|
96 |
|
|
97 |
class SharedDatas
|
|
98 |
{
|
|
99 |
public:
|
|
100 |
typedef vector<int, per_thread_allocator<int> > thread_vector;
|
|
101 |
|
|
102 |
SharedDatas(size_t nbElems) : threadVectors(nbElems, (thread_vector*)0) {
|
|
103 |
pthread_mutex_init(&mutex, 0);
|
|
104 |
pthread_cond_init(&condition, 0);
|
|
105 |
}
|
|
106 |
|
|
107 |
~SharedDatas() {
|
|
108 |
for (size_t i = 0; i < threadVectors.size(); ++i) {
|
|
109 |
delete threadVectors[i];
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
size_t initThreadVector() {
|
|
114 |
size_t ret;
|
|
115 |
|
|
116 |
pthread_mutex_lock(&mutex);
|
|
117 |
|
|
118 |
for (size_t i = 0; i < threadVectors.size(); ++i) {
|
|
119 |
if (threadVectors[i] == 0) {
|
|
120 |
threadVectors[i] = new thread_vector();
|
|
121 |
ret = i;
|
|
122 |
break;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
if (ret != threadVectors.size() - 1) {
|
|
127 |
//We wait for other thread(s) to call this method too:
|
|
128 |
printf("Thread %d wait\n", ret);
|
|
129 |
pthread_cond_wait(&condition, &mutex);
|
|
130 |
}
|
|
131 |
else {
|
|
132 |
//We are the last thread calling this method, we signal this
|
|
133 |
//to the other thread(s) that might be waiting:
|
|
134 |
printf("Thread %d signal\n", ret);
|
|
135 |
pthread_cond_signal(&condition);
|
|
136 |
}
|
|
137 |
|
|
138 |
pthread_mutex_unlock(&mutex);
|
|
139 |
|
|
140 |
return ret;
|
|
141 |
}
|
|
142 |
|
|
143 |
thread_vector& getThreadVector(size_t index) {
|
|
144 |
//We return other thread thread_vector instance:
|
|
145 |
return *threadVectors[(index + 1 == threadVectors.size()) ? 0 : index + 1];
|
|
146 |
}
|
|
147 |
|
|
148 |
private:
|
|
149 |
pthread_mutex_t mutex;
|
|
150 |
pthread_cond_t condition;
|
|
151 |
vector<thread_vector*> threadVectors;
|
|
152 |
};
|
|
153 |
|
|
154 |
void* f(void* pdatas) {
|
|
155 |
SharedDatas *psharedDatas = (SharedDatas*)pdatas;
|
|
156 |
|
|
157 |
int threadIndex = psharedDatas->initThreadVector();
|
|
158 |
|
|
159 |
for (int i = 0; i < 100; ++i) {
|
|
160 |
psharedDatas->getThreadVector(threadIndex).push_back(i);
|
|
161 |
}
|
|
162 |
|
|
163 |
return 0;
|
|
164 |
}
|
|
165 |
|
|
166 |
void AllocatorTest::per_thread_alloc()
|
|
167 |
{
|
|
168 |
const size_t nth = 2;
|
|
169 |
SharedDatas datas(nth);
|
|
170 |
pthread_t t[nth];
|
|
171 |
|
|
172 |
size_t i;
|
|
173 |
for (i = 0; i < nth; ++i) {
|
|
174 |
pthread_create(&t[i], 0, f, &datas);
|
|
175 |
}
|
|
176 |
|
|
177 |
for (i = 0; i < nth; ++i ) {
|
|
178 |
pthread_join(t[i], 0);
|
|
179 |
}
|
|
180 |
}
|
|
181 |
#endif
|