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