VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstMMHyperHeap.cpp@ 400

Last change on this file since 400 was 23, checked in by vboxsync, 18 years ago

string.h & stdio.h + header cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/* $Id: tstMMHyperHeap.cpp 23 2007-01-15 14:08:28Z vboxsync $ */
2/** @file
3 * MM Hypervisor Heap testcase.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung 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 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include <VBox/mm.h>
26#include <VBox/stam.h>
27#include <VBox/vm.h>
28#include <VBox/sup.h>
29#include <VBox/param.h>
30#include <VBox/err.h>
31
32#include <VBox/log.h>
33#include <iprt/runtime.h>
34#include <iprt/assert.h>
35#include <iprt/stream.h>
36#include <iprt/string.h>
37
38
39int main(int argc, char *argv[])
40{
41
42 /*
43 * Init runtime.
44 */
45 RTR3Init();
46
47 /*
48 * Create empty VM structure and call MMR3Init().
49 */
50 PVM pVM;
51 int rc = SUPInit(NULL);
52 if (VBOX_SUCCESS(rc))
53 rc = SUPPageAlloc((sizeof(*pVM) + PAGE_SIZE - 1) >> PAGE_SHIFT, (void **)&pVM);
54 if (VBOX_FAILURE(rc))
55 {
56 RTPrintf("Fatal error: SUP Failure! rc=%Vrc\n", rc);
57 return 1;
58 }
59 rc = STAMR3Init(pVM);
60 if (VBOX_FAILURE(rc))
61 {
62 RTPrintf("Fatal error: STAMR3Init failed! rc=%Vrc\n", rc);
63 return 1;
64 }
65 rc = MMR3Init(pVM);
66 if (VBOX_FAILURE(rc))
67 {
68 RTPrintf("Fatal error: MMR3Init failed! rc=%Vrc\n", rc);
69 return 1;
70 }
71
72 /*
73 * Try allocate.
74 */
75 static struct
76 {
77 size_t cb;
78 unsigned uAlignment;
79 void *pvAlloc;
80 unsigned iFreeOrder;
81 } aOps[] =
82 {
83 { 16, 0, NULL, 0 },
84 { 16, 4, NULL, 1 },
85 { 16, 8, NULL, 2 },
86 { 16, 16, NULL, 5 },
87 { 16, 32, NULL, 4 },
88 { 32, 0, NULL, 3 },
89 { 31, 0, NULL, 6 },
90 { 1024, 0, NULL, 8 },
91 { 1024, 32, NULL, 10 },
92 { 1024, 32, NULL, 12 },
93 { PAGE_SIZE, PAGE_SIZE, NULL, 13 },
94 { 1024, 32, NULL, 9 },
95 { PAGE_SIZE, 32, NULL, 11 },
96 { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
97 { 16, 0, NULL, 15 },
98 { 9, 0, NULL, 7 },
99 { 16, 0, NULL, 7 },
100 { 36, 0, NULL, 7 },
101 { 16, 0, NULL, 7 },
102 { 12344, 0, NULL, 7 },
103 { 50, 0, NULL, 7 },
104 { 16, 0, NULL, 7 },
105 };
106 unsigned i;
107#ifdef DEBUG
108 MMHyperHeapDump(pVM);
109#endif
110 size_t cbBefore = MMHyperHeapGetFreeSize(pVM);
111 static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
112
113 /* allocate */
114 for (i = 0; i < ELEMENTS(aOps); i++)
115 {
116 rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM, &aOps[i].pvAlloc);
117 if (VBOX_FAILURE(rc))
118 {
119 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
120 return 1;
121 }
122 memset(aOps[i].pvAlloc, szFill[i], aOps[i].cb);
123 if (ALIGNP(aOps[i].pvAlloc, (aOps[i].uAlignment ? aOps[i].uAlignment : 8)) != aOps[i].pvAlloc)
124 {
125 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %p, invalid alignment!\n", aOps[i].cb, aOps[i].uAlignment, aOps[i].pvAlloc);
126 return 1;
127 }
128 }
129
130 /* free and allocate the same node again. */
131 for (i = 0; i < ELEMENTS(aOps); i++)
132 {
133 if ( !aOps[i].pvAlloc
134 || aOps[i].uAlignment == PAGE_SIZE)
135 continue;
136 //size_t cbBeforeSub = MMHyperHeapGetFreeSize(pVM);
137 rc = MMHyperFree(pVM, aOps[i].pvAlloc);
138 if (VBOX_FAILURE(rc))
139 {
140 RTPrintf("Failure: MMHyperFree(, %p,) -> %d i=%d\n", aOps[i].pvAlloc, rc, i);
141 return 1;
142 }
143 //RTPrintf("debug: i=%d cbBeforeSub=%d now=%d\n", i, cbBeforeSub, MMHyperHeapGetFreeSize(pVM));
144 void *pv;
145 rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM_REQ, &pv);
146 if (VBOX_FAILURE(rc))
147 {
148 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
149 return 1;
150 }
151 if (pv != aOps[i].pvAlloc)
152 {
153 RTPrintf("Failure: Free+Alloc returned different address. new=%p old=%p i=%d (doesn't work with delayed free)\n", pv, aOps[i].pvAlloc, i);
154 //return 1;
155 }
156 aOps[i].pvAlloc = pv;
157 #if 0 /* won't work :/ */
158 size_t cbAfterSub = MMHyperHeapGetFreeSize(pVM);
159 if (cbBeforeSub != cbAfterSub)
160 {
161 RTPrintf("Failure: cbBeforeSub=%d cbAfterSub=%d. i=%d\n", cbBeforeSub, cbAfterSub, i);
162 return 1;
163 }
164 #endif
165 }
166
167 /* free it in a specific order. */
168 int cFreed = 0;
169 for (i = 0; i < ELEMENTS(aOps); i++)
170 {
171 unsigned j;
172 for (j = 0; j < ELEMENTS(aOps); j++)
173 {
174 if ( aOps[j].iFreeOrder != i
175 || !aOps[j].pvAlloc)
176 continue;
177 RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, MMHyperHeapGetFreeSize(pVM), aOps[j].cb, aOps[j].pvAlloc);
178 if (aOps[j].uAlignment == PAGE_SIZE)
179 cbBefore -= aOps[j].cb;
180 else
181 {
182 rc = MMHyperFree(pVM, aOps[j].pvAlloc);
183 if (VBOX_FAILURE(rc))
184 {
185 RTPrintf("Failure: MMHyperFree(, %p,) -> %d j=%d i=%d\n", aOps[j].pvAlloc, rc, i, j);
186 return 1;
187 }
188 }
189 aOps[j].pvAlloc = NULL;
190 cFreed++;
191 }
192 }
193 Assert(cFreed == ELEMENTS(aOps));
194 RTPrintf("i=done free=%d\n", MMHyperHeapGetFreeSize(pVM));
195
196 /* check that we're back at the right amount of free memory. */
197 size_t cbAfter = MMHyperHeapGetFreeSize(pVM);
198 if (cbBefore != cbAfter)
199 {
200 RTPrintf("Warning: Either we've split out an alignment chunk at the start, or we've got\n"
201 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
202#ifdef DEBUG
203 MMHyperHeapDump(pVM);
204#endif
205 }
206
207 RTPrintf("tstMMHyperHeap: Success\n");
208#ifdef LOG_ENABLED
209 RTLogFlush(NULL);
210#endif
211 return 0;
212}
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