VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTHeapOffset.cpp@ 25791

Last change on this file since 25791 was 25064, checked in by vboxsync, 15 years ago

tstRTHeapOffset.cpp: extended the testcase.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.1 KB
Line 
1/* $Id: tstRTHeapOffset.cpp 25064 2009-11-28 02:28:56Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Offset Based Heap.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/heap.h>
35
36#include <iprt/assert.h>
37#include <iprt/err.h>
38#include <iprt/initterm.h>
39#include <iprt/log.h>
40#include <iprt/rand.h>
41#include <iprt/stream.h>
42#include <iprt/string.h>
43#include <iprt/param.h>
44#include <iprt/test.h>
45#include <iprt/time.h>
46
47
48int main(int argc, char *argv[])
49{
50 /*
51 * Init runtime.
52 */
53 RTTEST hTest;
54 int rc = RTTestInitAndCreate("tstRTHeapOffset", &hTest);
55 if (rc)
56 return rc;
57 RTTestBanner(hTest);
58
59 /*
60 * Create a heap.
61 */
62 RTTestSub(hTest, "Basics");
63 static uint8_t s_abMem[128*1024];
64 RTHEAPOFFSET Heap;
65 RTTESTI_CHECK_RC(rc = RTHeapOffsetInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1), VINF_SUCCESS);
66 if (RT_FAILURE(rc))
67 return RTTestSummaryAndDestroy(hTest);
68
69 /*
70 * Try allocate.
71 */
72 static struct TstHeapOffsetOps
73 {
74 size_t cb;
75 unsigned uAlignment;
76 void *pvAlloc;
77 unsigned iFreeOrder;
78 } s_aOps[] =
79 {
80 { 16, 0, NULL, 0 }, // 0
81 { 16, 4, NULL, 1 },
82 { 16, 8, NULL, 2 },
83 { 16, 16, NULL, 5 },
84 { 16, 32, NULL, 4 },
85 { 32, 0, NULL, 3 }, // 5
86 { 31, 0, NULL, 6 },
87 { 1024, 0, NULL, 8 },
88 { 1024, 32, NULL, 10 },
89 { 1024, 32, NULL, 12 },
90 { PAGE_SIZE, PAGE_SIZE, NULL, 13 }, // 10
91 { 1024, 32, NULL, 9 },
92 { PAGE_SIZE, 32, NULL, 11 },
93 { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
94 { 16, 0, NULL, 15 },
95 { 9, 0, NULL, 7 }, // 15
96 { 16, 0, NULL, 7 },
97 { 36, 0, NULL, 7 },
98 { 16, 0, NULL, 7 },
99 { 12344, 0, NULL, 7 },
100 { 50, 0, NULL, 7 }, // 20
101 { 16, 0, NULL, 7 },
102 };
103 unsigned i;
104 RTHeapOffsetDump(Heap, (PFNRTHEAPOFFSETPRINTF)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
105 size_t cbBefore = RTHeapOffsetGetFreeSize(Heap);
106 static char const s_szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
107
108 /* allocate */
109 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
110 {
111 s_aOps[i].pvAlloc = RTHeapOffsetAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
112 RTTESTI_CHECK_MSG(s_aOps[i].pvAlloc, ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
113 if (!s_aOps[i].pvAlloc)
114 return RTTestSummaryAndDestroy(hTest);
115
116 memset(s_aOps[i].pvAlloc, s_szFill[i], s_aOps[i].cb);
117 RTTESTI_CHECK_MSG(RT_ALIGN_P(s_aOps[i].pvAlloc, (s_aOps[i].uAlignment ? s_aOps[i].uAlignment : 8)) == s_aOps[i].pvAlloc,
118 ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
119 if (!s_aOps[i].pvAlloc)
120 return RTTestSummaryAndDestroy(hTest);
121 }
122
123 /* free and allocate the same node again. */
124 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
125 {
126 if (!s_aOps[i].pvAlloc)
127 continue;
128 //RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, s_aOps[i].pvAlloc,
129 // s_aOps[i].cb, s_aOps[i].uAlignment, RTHeapOffsetSize(Heap, s_aOps[i].pvAlloc));
130 size_t cbBeforeSub = RTHeapOffsetGetFreeSize(Heap);
131 RTHeapOffsetFree(Heap, s_aOps[i].pvAlloc);
132 size_t cbAfterSubFree = RTHeapOffsetGetFreeSize(Heap);
133
134 void *pv;
135 pv = RTHeapOffsetAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
136 RTTESTI_CHECK_MSG(pv, ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
137 if (!pv)
138 return RTTestSummaryAndDestroy(hTest);
139 //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapOffsetSize(Heap, pv),
140 // cbBeforeSub, cbAfterSubFree, RTHeapOffsetGetFreeSize(Heap));
141
142 if (pv != s_aOps[i].pvAlloc)
143 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, s_aOps[i].pvAlloc, i);
144 s_aOps[i].pvAlloc = pv;
145 size_t cbAfterSubAlloc = RTHeapOffsetGetFreeSize(Heap);
146 if (cbBeforeSub != cbAfterSubAlloc)
147 {
148 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
149 cbBeforeSub, cbAfterSubFree, cbAfterSubAlloc, i);
150 //return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
151 }
152 }
153
154 /* make a copy of the heap and the to-be-freed list. */
155 static uint8_t s_abMemCopy[sizeof(s_abMem)];
156 memcpy(s_abMemCopy, s_abMem, sizeof(s_abMem));
157 uintptr_t offDelta = (uintptr_t)&s_abMemCopy[0] - (uintptr_t)&s_abMem[0];
158 RTHEAPOFFSET hHeapCopy = (RTHEAPOFFSET)((uintptr_t)Heap + offDelta);
159 static struct TstHeapOffsetOps s_aOpsCopy[RT_ELEMENTS(s_aOps)];
160 memcpy(&s_aOpsCopy[0], &s_aOps[0], sizeof(s_aOps));
161
162 /* free it in a specific order. */
163 int cFreed = 0;
164 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
165 {
166 unsigned j;
167 for (j = 0; j < RT_ELEMENTS(s_aOps); j++)
168 {
169 if ( s_aOps[j].iFreeOrder != i
170 || !s_aOps[j].pvAlloc)
171 continue;
172 //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapOffsetGetFreeSize(Heap), s_aOps[j].cb, s_aOps[j].pvAlloc);
173 RTHeapOffsetFree(Heap, s_aOps[j].pvAlloc);
174 s_aOps[j].pvAlloc = NULL;
175 cFreed++;
176 }
177 }
178 RTTESTI_CHECK(cFreed == RT_ELEMENTS(s_aOps));
179 RTTestIPrintf(RTTESTLVL_ALWAYS, "i=done free=%d\n", RTHeapOffsetGetFreeSize(Heap));
180
181 /* check that we're back at the right amount of free memory. */
182 size_t cbAfter = RTHeapOffsetGetFreeSize(Heap);
183 if (cbBefore != cbAfter)
184 {
185 RTTestIPrintf(RTTESTLVL_ALWAYS,
186 "Warning: Either we've split out an alignment chunk at the start, or we've got\n"
187 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
188 RTHeapOffsetDump(Heap, (PFNRTHEAPOFFSETPRINTF)RTPrintf);
189 }
190
191 /* relocate and free the bits in heap2 now. */
192 RTTestSub(hTest, "Relocated Heap");
193 /* free it in a specific order. */
194 int cFreed2 = 0;
195 for (i = 0; i < RT_ELEMENTS(s_aOpsCopy); i++)
196 {
197 unsigned j;
198 for (j = 0; j < RT_ELEMENTS(s_aOpsCopy); j++)
199 {
200 if ( s_aOpsCopy[j].iFreeOrder != i
201 || !s_aOpsCopy[j].pvAlloc)
202 continue;
203 //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapOffsetGetFreeSize(hHeapCopy), s_aOpsCopy[j].cb, s_aOpsCopy[j].pvAlloc);
204 RTHeapOffsetFree(hHeapCopy, (uint8_t *)s_aOpsCopy[j].pvAlloc + offDelta);
205 s_aOpsCopy[j].pvAlloc = NULL;
206 cFreed2++;
207 }
208 }
209 RTTESTI_CHECK(cFreed2 == RT_ELEMENTS(s_aOpsCopy));
210
211 /* check that we're back at the right amount of free memory. */
212 size_t cbAfterCopy = RTHeapOffsetGetFreeSize(hHeapCopy);
213 RTTESTI_CHECK_MSG(cbAfterCopy == cbAfter, ("cbAfterCopy=%zu cbAfter=%zu\n", cbAfterCopy, cbAfter));
214
215 /*
216 * Use random allocation pattern
217 */
218 RTTestSub(hTest, "Random Test");
219 RTTESTI_CHECK_RC(rc = RTHeapOffsetInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1), VINF_SUCCESS);
220 if (RT_FAILURE(rc))
221 return RTTestSummaryAndDestroy(hTest);
222
223 RTRAND hRand;
224 RTTESTI_CHECK_RC(rc = RTRandAdvCreateParkMiller(&hRand), VINF_SUCCESS);
225 if (RT_FAILURE(rc))
226 return RTTestSummaryAndDestroy(hTest);
227#if 0
228 RTRandAdvSeed(hRand, 42);
229#else
230 RTRandAdvSeed(hRand, RTTimeNanoTS());
231#endif
232
233 static struct
234 {
235 size_t cb;
236 void *pv;
237 } s_aHistory[1536];
238 RT_ZERO(s_aHistory);
239
240 for (unsigned iTest = 0; iTest < 131072; iTest++)
241 {
242 uint32_t i = RTRandAdvU32Ex(hRand, 0, RT_ELEMENTS(s_aHistory) - 1);
243 if (!s_aHistory[i].pv)
244 {
245 uint32_t uAlignment = 1 << RTRandAdvU32Ex(hRand, 0, 7);
246 s_aHistory[i].cb = RTRandAdvU32Ex(hRand, 9, 1024);
247 s_aHistory[i].pv = RTHeapOffsetAlloc(Heap, s_aHistory[i].cb, uAlignment);
248 if (!s_aHistory[i].pv)
249 {
250 s_aHistory[i].cb = 9;
251 s_aHistory[i].pv = RTHeapOffsetAlloc(Heap, s_aHistory[i].cb, 0);
252 }
253 if (s_aHistory[i].pv)
254 memset(s_aHistory[i].pv, 0xbb, s_aHistory[i].cb);
255 }
256 else
257 {
258 RTHeapOffsetFree(Heap, s_aHistory[i].pv);
259 s_aHistory[i].pv = NULL;
260 }
261
262 if ((iTest % 7777) == 7776)
263 {
264 /* exhaust the heap */
265 for (unsigned i = 0; i < RT_ELEMENTS(s_aHistory) && RTHeapOffsetGetFreeSize(Heap) >= 256; i++)
266 if (!s_aHistory[i].pv)
267 {
268 s_aHistory[i].cb = RTRandAdvU32Ex(hRand, 256, 16384);
269 s_aHistory[i].pv = RTHeapOffsetAlloc(Heap, s_aHistory[i].cb, 0);
270 }
271 for (unsigned i = 0; i < RT_ELEMENTS(s_aHistory) && RTHeapOffsetGetFreeSize(Heap); i++)
272 {
273 if (!s_aHistory[i].pv)
274 {
275 s_aHistory[i].cb = 1;
276 s_aHistory[i].pv = RTHeapOffsetAlloc(Heap, s_aHistory[i].cb, 1);
277 }
278 if (s_aHistory[i].pv)
279 memset(s_aHistory[i].pv, 0x55, s_aHistory[i].cb);
280 }
281 RTTESTI_CHECK_MSG(RTHeapOffsetGetFreeSize(Heap) == 0, ("%zu\n", RTHeapOffsetGetFreeSize(Heap)));
282 }
283 else if ((iTest % 7777) == 1111)
284 {
285 /* free all */
286 for (unsigned i = 0; i < RT_ELEMENTS(s_aHistory); i++)
287 {
288 RTHeapOffsetFree(Heap, s_aHistory[i].pv);
289 s_aHistory[i].pv = NULL;
290 }
291 size_t cbAfterRand = RTHeapOffsetGetFreeSize(Heap);
292 RTTESTI_CHECK_MSG(cbAfterRand == cbAfter, ("cbAfterRand=%zu cbAfter=%zu\n", cbAfterRand, cbAfter));
293 }
294 }
295
296 /* free the rest. */
297 for (unsigned i = 0; i < RT_ELEMENTS(s_aHistory); i++)
298 {
299 RTHeapOffsetFree(Heap, s_aHistory[i].pv);
300 s_aHistory[i].pv = NULL;
301 }
302
303 /* check that we're back at the right amount of free memory. */
304 size_t cbAfterRand = RTHeapOffsetGetFreeSize(Heap);
305 RTTESTI_CHECK_MSG(cbAfterRand == cbAfter, ("cbAfterRand=%zu cbAfter=%zu\n", cbAfterRand, cbAfter));
306
307 return RTTestSummaryAndDestroy(hTest);
308}
309
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