VirtualBox

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

Last change on this file was 103005, checked in by vboxsync, 5 months ago

iprt/asm.h,*: Split out the ASMMem* and related stuff into a separate header, asm-mem.h, so that we can get the RT_ASM_PAGE_SIZE stuff out of the way.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use