Changeset 25064 in vbox
- Timestamp:
- Nov 28, 2009 2:28:56 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/testcase/tstRTHeapOffset.cpp
r25059 r25064 33 33 *******************************************************************************/ 34 34 #include <iprt/heap.h> 35 36 #include <iprt/assert.h> 37 #include <iprt/err.h> 35 38 #include <iprt/initterm.h> 36 #include <iprt/err.h> 39 #include <iprt/log.h> 40 #include <iprt/rand.h> 37 41 #include <iprt/stream.h> 38 42 #include <iprt/string.h> 39 43 #include <iprt/param.h> 40 #include <iprt/assert.h>41 #include <iprt/log.h>42 44 #include <iprt/test.h> 45 #include <iprt/time.h> 43 46 44 47 … … 101 104 RTHeapOffsetDump(Heap, (PFNRTHEAPOFFSETPRINTF)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */ 102 105 size_t cbBefore = RTHeapOffsetGetFreeSize(Heap); 103 static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";106 static char const s_szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 104 107 105 108 /* allocate */ … … 111 114 return RTTestSummaryAndDestroy(hTest); 112 115 113 memset(s_aOps[i].pvAlloc, s zFill[i], s_aOps[i].cb);116 memset(s_aOps[i].pvAlloc, s_szFill[i], s_aOps[i].cb); 114 117 RTTESTI_CHECK_MSG(RT_ALIGN_P(s_aOps[i].pvAlloc, (s_aOps[i].uAlignment ? s_aOps[i].uAlignment : 8)) == s_aOps[i].pvAlloc, 115 118 ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i)); … … 210 213 RTTESTI_CHECK_MSG(cbAfterCopy == cbAfter, ("cbAfterCopy=%zu cbAfter=%zu\n", cbAfterCopy, cbAfter)); 211 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)); 212 306 213 307 return RTTestSummaryAndDestroy(hTest);
Note:
See TracChangeset
for help on using the changeset viewer.