VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstHeapSimple.cpp@ 4953

Last change on this file since 4953 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1/* $Id $ */
2/** @file
3 * innotek Portable Runtime Testcase - Simple Heap.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#include <iprt/heap.h>
22#include <iprt/initterm.h>
23#include <iprt/err.h>
24#include <iprt/stream.h>
25#include <iprt/string.h>
26#include <iprt/param.h>
27#include <iprt/assert.h>
28
29
30int main(int argc, char *argv[])
31{
32 /*
33 * Init runtime.
34 */
35 int rc = RTR3Init();
36 if (RT_FAILURE(rc))
37 {
38 RTPrintf("RTR3Init failed: %Vrc\n", rc);
39 return 1;
40 }
41 RTPrintf("tstHeapSimple: TESTING...\n");
42
43 /*
44 * Create a heap.
45 */
46 static uint8_t s_abMem[128*1024];
47 RTHEAPSIMPLE Heap;
48 rc = RTHeapSimpleInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1);
49 if (RT_FAILURE(rc))
50 {
51 RTPrintf("RTHeapSimpleInit failed: %Vrc\n", rc);
52 return 1;
53 }
54
55 /*
56 * Try allocate.
57 */
58 static struct
59 {
60 size_t cb;
61 unsigned uAlignment;
62 void *pvAlloc;
63 unsigned iFreeOrder;
64 } aOps[] =
65 {
66 { 16, 0, NULL, 0 }, // 0
67 { 16, 4, NULL, 1 },
68 { 16, 8, NULL, 2 },
69 { 16, 16, NULL, 5 },
70 { 16, 32, NULL, 4 },
71 { 32, 0, NULL, 3 }, // 5
72 { 31, 0, NULL, 6 },
73 { 1024, 0, NULL, 8 },
74 { 1024, 32, NULL, 10 },
75 { 1024, 32, NULL, 12 },
76 { PAGE_SIZE, PAGE_SIZE, NULL, 13 }, // 10
77 { 1024, 32, NULL, 9 },
78 { PAGE_SIZE, 32, NULL, 11 },
79 { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
80 { 16, 0, NULL, 15 },
81 { 9, 0, NULL, 7 }, // 15
82 { 16, 0, NULL, 7 },
83 { 36, 0, NULL, 7 },
84 { 16, 0, NULL, 7 },
85 { 12344, 0, NULL, 7 },
86 { 50, 0, NULL, 7 }, // 20
87 { 16, 0, NULL, 7 },
88 };
89 unsigned i;
90 RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf);
91 size_t cbBefore = RTHeapSimpleGetFreeSize(Heap);
92 static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
93
94 /* allocate */
95 for (i = 0; i < ELEMENTS(aOps); i++)
96 {
97 aOps[i].pvAlloc = RTHeapSimpleAlloc(Heap, aOps[i].cb, aOps[i].uAlignment);
98 if (!aOps[i].pvAlloc)
99 {
100 RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
101 return 1;
102 }
103 memset(aOps[i].pvAlloc, szFill[i], aOps[i].cb);
104 if (ALIGNP(aOps[i].pvAlloc, (aOps[i].uAlignment ? aOps[i].uAlignment : 8)) != aOps[i].pvAlloc)
105 {
106 RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
107 return 1;
108 }
109 }
110
111 /* free and allocate the same node again. */
112 for (i = 0; i < ELEMENTS(aOps); i++)
113 {
114 if (!aOps[i].pvAlloc)
115 continue;
116 //RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, aOps[i].pvAlloc,
117 // aOps[i].cb, aOps[i].uAlignment, RTHeapSimpleSize(Heap, aOps[i].pvAlloc));
118 size_t cbBeforeSub = RTHeapSimpleGetFreeSize(Heap);
119 RTHeapSimpleFree(Heap, aOps[i].pvAlloc);
120 size_t cbAfterSubFree = RTHeapSimpleGetFreeSize(Heap);
121
122 void *pv;
123 pv = RTHeapSimpleAlloc(Heap, aOps[i].cb, aOps[i].uAlignment);
124 if (!pv)
125 {
126 RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
127 return 1;
128 }
129 //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapSimpleSize(Heap, pv),
130 // cbBeforeSub, cbAfterSubFree, RTHeapSimpleGetFreeSize(Heap));
131 if (pv != aOps[i].pvAlloc)
132 RTPrintf("Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, aOps[i].pvAlloc, i);
133 aOps[i].pvAlloc = pv;
134 size_t cbAfterSubAlloc = RTHeapSimpleGetFreeSize(Heap);
135 if (cbBeforeSub != cbAfterSubAlloc)
136 {
137 RTPrintf("Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
138 cbBeforeSub, cbAfterSubFree, cbAfterSubAlloc, i);
139 //return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
140 }
141 }
142
143 /* free it in a specific order. */
144 int cFreed = 0;
145 for (i = 0; i < ELEMENTS(aOps); i++)
146 {
147 unsigned j;
148 for (j = 0; j < ELEMENTS(aOps); j++)
149 {
150 if ( aOps[j].iFreeOrder != i
151 || !aOps[j].pvAlloc)
152 continue;
153 //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(Heap), aOps[j].cb, aOps[j].pvAlloc);
154 RTHeapSimpleFree(Heap, aOps[j].pvAlloc);
155 aOps[j].pvAlloc = NULL;
156 cFreed++;
157 }
158 }
159 Assert(cFreed == RT_ELEMENTS(aOps));
160 RTPrintf("i=done free=%d\n", RTHeapSimpleGetFreeSize(Heap));
161
162 /* check that we're back at the right amount of free memory. */
163 size_t cbAfter = RTHeapSimpleGetFreeSize(Heap);
164 if (cbBefore != cbAfter)
165 {
166 RTPrintf("Warning: Either we've split out an alignment chunk at the start, or we've got\n"
167 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
168 RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf);
169 }
170
171 RTPrintf("tstHeapSimple: Success\n");
172#ifdef LOG_ENABLED
173 RTLogFlush(NULL);
174#endif
175 return 0;
176}
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