VirtualBox

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

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

more simple heap stuff.

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