VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTMemPool.cpp@ 33540

Last change on this file since 33540 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette