1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/* $Id: tstRTMemPool.cpp $ */
/** @file
* IPRT Testcase - MemPool.
*/
/*
* Copyright (C) 2009-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/mempool.h>
#include <iprt/asm.h>
#include <iprt/errcore.h>
#include <iprt/initterm.h>
#include <iprt/string.h>
#include <iprt/test.h>
#include <iprt/thread.h>
#include <iprt/rand.h>
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** The test handle */
static RTTEST g_hTest;
/** Memory pool for tst4. */
static RTMEMPOOL g_hMemPool4;
/**
* Basic API checks.
* We'll return if any of these fails.
*/
static void tst1(RTMEMPOOL hMemPool)
{
void *pv;
/* Normal alloc. */
RTTESTI_CHECK_RETV(pv = RTMemPoolAlloc(hMemPool, 1));
RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
RTTESTI_CHECK_RETV(pv = RTMemPoolAlloc(hMemPool, 0));
RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
/* Zeroed allocation. */
for (uint32_t i = 0; i < 512; i++)
{
RTTESTI_CHECK_RETV(pv = RTMemPoolAllocZ(hMemPool, 1024));
RTTESTI_CHECK(ASMMemFirstMismatchingU32(pv, 1024, 0) == NULL);
memset(pv, 'a', 1024);
RTTESTI_CHECK_RETV(RTMemPoolRefCount(pv) == 1);
RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
}
RTTESTI_CHECK_RETV(pv = RTMemPoolAllocZ(hMemPool, 0));
RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
/* Duped allocation. */
static const char szTest[] = "test string abcdef";
RTTESTI_CHECK_RETV(pv = RTMemPoolDup(hMemPool, szTest, sizeof(szTest)));
RTTESTI_CHECK(memcmp(pv, szTest, sizeof(szTest)) == 0);
RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
for (uint32_t i = 0; i < 512; i++)
{
size_t const cb = 256 - sizeof(szTest);
RTTESTI_CHECK_RETV(pv = RTMemPoolDupEx(hMemPool, szTest, sizeof(szTest), cb));
RTTESTI_CHECK(memcmp(pv, szTest, sizeof(szTest)) == 0);
RTTESTI_CHECK(ASMMemIsZero((uint8_t *)pv + sizeof(szTest), cb));
memset(pv, 'b', sizeof(szTest) + cb);
RTTESTI_CHECK_RETV(RTMemPoolRefCount(pv) == 1);
RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
}
/* Reallocation */
RTTESTI_CHECK_RETV(pv = RTMemPoolRealloc(hMemPool, NULL, 1));
RTTESTI_CHECK_RETV(pv = RTMemPoolRealloc(hMemPool, pv, 2));
RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
RTTESTI_CHECK_RETV(pv = RTMemPoolAlloc(hMemPool, 42));
RTTESTI_CHECK_RETV(pv = RTMemPoolRealloc(hMemPool, pv, 32));
RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, pv) == 0);
RTTESTI_CHECK_RETV(pv = RTMemPoolRealloc(hMemPool, NULL, 128));
RTTESTI_CHECK_RETV(pv = RTMemPoolRealloc(hMemPool, pv, 256));
RTTESTI_CHECK_RETV(RTMemPoolRealloc(hMemPool, pv, 0) == NULL);
/* Free (a bit hard to test) */
RTMemPoolFree(hMemPool, NULL);
RTMemPoolFree(hMemPool, RTMemPoolAlloc(hMemPool, 42));
/* Memory referencing. */
for (uint32_t i = 1; i <= 4096; i *= 3)
{
void *pv2;
RTTESTI_CHECK_RETV(pv = RTMemPoolAlloc(hMemPool, i));
RTTESTI_CHECK(RTMemPoolRefCount(pv) == 1);
memset(pv, 'a', i);
RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x\n", i, pv,(uintptr_t)pv2 - (uintptr_t)pv));
RTTESTI_CHECK(RTMemPoolRetain(pv) == 2);
RTTESTI_CHECK(RTMemPoolRefCount(pv) == 2);
RTTESTI_CHECK(RTMemPoolRetain(pv) == 3);
RTTESTI_CHECK(RTMemPoolRefCount(pv) == 3);
RTTESTI_CHECK(RTMemPoolRetain(pv) == 4);
RTTESTI_CHECK(RTMemPoolRefCount(pv) == 4);
RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv));
RTTESTI_CHECK(RTMemPoolRelease(hMemPool, pv) == 3);
RTTESTI_CHECK(RTMemPoolRefCount(pv) == 3);
RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv));
RTTESTI_CHECK(RTMemPoolRetain(pv) == 4);
RTTESTI_CHECK(RTMemPoolRefCount(pv) == 4);
RTTESTI_CHECK(RTMemPoolRetain(pv) == 5);
RTTESTI_CHECK(RTMemPoolRefCount(pv) == 5);
RTTESTI_CHECK(RTMemPoolRetain(pv) == 6);
RTTESTI_CHECK(RTMemPoolRefCount(pv) == 6);
RTTESTI_CHECK(RTMemPoolRelease(NIL_RTMEMPOOL, pv) == 5);
RTTESTI_CHECK(RTMemPoolRelease(NIL_RTMEMPOOL, pv) == 4);
RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv));
for (uint32_t cRefs = 3;; cRefs--)
{
RTTESTI_CHECK(RTMemPoolRelease(hMemPool, pv) == cRefs);
if (cRefs == 0)
break;
RTTESTI_CHECK(RTMemPoolRefCount(pv) == cRefs);
RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x cRefs=%d\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv, cRefs));
for (uint32_t j = 0; j < 42; j++)
{
RTTESTI_CHECK_RETV(pv2 = RTMemPoolAlloc(hMemPool, i));
RTTESTI_CHECK_RETV(pv2 != pv);
memset(pv2, 'f', i);
RTTESTI_CHECK(RTMemPoolRelease(hMemPool, pv2) == 0);
RTTESTI_CHECK_MSG_RETV((pv2 = ASMMemFirstMismatchingU8(pv, i, 'a')) == NULL, ("i=%#x pv=%p off=%#x cRefs=%d\n", i, pv, (uintptr_t)pv2 - (uintptr_t)pv, cRefs));
}
}
}
}
/**
* Test automatic cleanup upon destruction.
*/
static void tst3(void)
{
RTTestISub("Destroy non-empty pool");
/*
* Nothing freed.
*/
RTMEMPOOL hMemPool;
RTTESTI_CHECK_RC_RETV(RTMemPoolCreate(&hMemPool, "test 3a"), VINF_SUCCESS);
RTTESTI_CHECK_RETV(RTMemPoolAlloc(hMemPool, 10));
RTTESTI_CHECK_RETV(RTMemPoolAlloc(hMemPool, 20));
RTTESTI_CHECK_RETV(RTMemPoolAlloc(hMemPool, 40));
RTTESTI_CHECK_RETV(RTMemPoolAlloc(hMemPool, 80));
RTTESTI_CHECK_RC_RETV(RTMemPoolDestroy(hMemPool), VINF_SUCCESS);
/*
* Pseudo random freeing to test list maintenance.
*/
RTRAND hRand;
RTTESTI_CHECK_RC_OK_RETV(RTRandAdvCreateParkMiller(&hRand));
for (uint32_t i = 0; i < 10; i++)
{
RTTESTI_CHECK_RC_RETV(RTMemPoolCreate(&hMemPool, "test 3b"), VINF_SUCCESS);
void *apvHistory[256];
RT_ZERO(apvHistory);
uint32_t cBlocks = 0;
uint32_t j;
for (j = 0; j < RT_ELEMENTS(apvHistory) - i * 7; j++)
{
RTTESTI_CHECK_RETV(apvHistory[j] = RTMemPoolAlloc(hMemPool, j));
memset(apvHistory[j], 'a', j);
cBlocks++;
if (RTRandAdvU32Ex(hRand, 0, 4) == 4)
{
uint32_t iFree = RTRandAdvU32Ex(hRand, 0, j);
cBlocks -= apvHistory[iFree] != NULL;
RTTESTI_CHECK_RETV(RTMemPoolRelease(hMemPool, apvHistory[iFree]) == 0);
apvHistory[iFree] = NULL;
}
}
RTTESTI_CHECK_RC_RETV(RTMemPoolDestroy(hMemPool), VINF_SUCCESS);
RTTestIPrintf(RTTESTLVL_INFO, "cBlocks=%u j=%u\n", cBlocks, j);
}
RTRandAdvDestroy(hRand);
}
/** Thread function for tst4. */
static DECLCALLBACK(int) tst4Thread(RTTHREAD hSelf, void *pvArg)
{
// uint32_t iThread = (uint32_t)(uintptr_t)pvArg;
RTMEMPOOL hMemPool = g_hMemPool4;
RT_NOREF_PV(pvArg);
/* setup. */
RTTestSetDefault(g_hTest, NULL);
/* wait for the kick-off */
RTThreadUserWait(hSelf, RT_INDEFINITE_WAIT);
/* do the work. */
for (uint32_t i = 0; i < 1024; i++)
{
void *apvHistory[256];
RT_ZERO(apvHistory);
uint32_t j;
for (j = 0; j < RT_ELEMENTS(apvHistory) - (i % 200); j++)
RTTESTI_CHECK_RET(apvHistory[j] = RTMemPoolAlloc(hMemPool, (i & 15) + (j & 63)), VERR_NO_MEMORY);
for (uint32_t k = i & 7; k < j; k += 3)
{
RTTESTI_CHECK_RET(RTMemPoolRelease(hMemPool, apvHistory[k]) == 0, VERR_INTERNAL_ERROR);
apvHistory[k] = NULL;
}
while (j-- > 0)
RTTESTI_CHECK_RET(RTMemPoolRelease(hMemPool, apvHistory[j]) == 0, VERR_INTERNAL_ERROR);
}
return VINF_SUCCESS;
}
/** sub test */
static void tst4Sub(uint32_t cThreads)
{
RTTestISubF("Serialization - %u threads", cThreads);
RTMEMPOOL hMemPool;
RTTESTI_CHECK_RC_RETV(RTMemPoolCreate(&hMemPool, "test 2a"), VINF_SUCCESS);
g_hMemPool4 = hMemPool;
PRTTHREAD pahThreads = (PRTTHREAD)RTMemPoolAlloc(hMemPool, cThreads * sizeof(RTTHREAD));
RTTESTI_CHECK(pahThreads);
if (pahThreads)
{
/* start them. */
for (uint32_t i = 0; i < cThreads; i++)
{
int rc = RTThreadCreateF(&pahThreads[i], tst4Thread, (void *)(uintptr_t)i, 0,
RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tst4-%u/%u", i, cThreads);
RTTESTI_CHECK_RC_OK(rc);
if (RT_FAILURE(rc))
pahThreads[i] = NIL_RTTHREAD;
}
RTThreadYield();
/* kick them off. */
for (uint32_t i = 0; i < cThreads; i++)
if (pahThreads[i] != NIL_RTTHREAD)
RTTESTI_CHECK_RC_OK(RTThreadUserSignal(pahThreads[i]));
/* wait for them. */
for (uint32_t i = 0; i < cThreads; i++)
if (pahThreads[i] != NIL_RTTHREAD)
{
int rc = RTThreadWait(pahThreads[i], 2*60*1000, NULL);
RTTESTI_CHECK_RC_OK(rc);
}
}
RTTESTI_CHECK_RC(RTMemPoolDestroy(hMemPool), VINF_SUCCESS);
}
/**
* Starts a bunch of threads beating on a pool to test serialization.
*/
static void tst4(void)
{
/*
* Test it with a few different thread counts.
*/
tst4Sub(1);
tst4Sub(2);
tst4Sub(3);
tst4Sub(4);
tst4Sub(8);
tst4Sub(16);
}
int main()
{
RTTEST hTest;
int rc = RTTestInitAndCreate("tstRTMemPool", &hTest);
if (rc)
return rc;
RTTestBanner(hTest);
g_hTest = hTest;
/*
* Smoke tests using first the default and then a custom pool.
*/
RTTestSub(hTest, "Smoke test on default pool");
tst1(RTMEMPOOL_DEFAULT);
RTTestSub(hTest, "Smoke test on custom pool");
RTMEMPOOL hMemPool;
RTTESTI_CHECK_RC(rc = RTMemPoolCreate(&hMemPool, "test 2a"), VINF_SUCCESS);
if (RT_SUCCESS(rc))
RTTESTI_CHECK_RC(rc = RTMemPoolDestroy(hMemPool), VINF_SUCCESS);
RTTESTI_CHECK_RC(rc = RTMemPoolDestroy(NIL_RTMEMPOOL), VINF_SUCCESS);
RTTESTI_CHECK_RC(rc = RTMemPoolDestroy(RTMEMPOOL_DEFAULT), VINF_SUCCESS);
RTTESTI_CHECK_RC(rc = RTMemPoolDestroy(RTMEMPOOL_DEFAULT), VINF_SUCCESS);
RTTESTI_CHECK_RC(rc = RTMemPoolCreate(&hMemPool, "test 2b"), VINF_SUCCESS);
if (RT_SUCCESS(rc))
{
tst1(hMemPool);
RTTESTI_CHECK_RC(rc = RTMemPoolDestroy(hMemPool), VINF_SUCCESS);
}
/*
* Further tests.
*/
tst3();
tst4();
/*
* Summary.
*/
return RTTestSummaryAndDestroy(hTest);
}
|