VirtualBox

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

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

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

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