VirtualBox

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

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

debugged the heap.

  • Property svn:keywords set to Id
File size: 6.6 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 }, // 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 }, // 5
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 }, // 10
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 }, // 15
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 }, // 20
91 { 16, 0, NULL, 7 },
92 };
93 unsigned i;
94 RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf);
95 size_t cbBefore = RTHeapSimpleGetFreeSize(Heap);
96 static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
97
98 /* allocate */
99 for (i = 0; i < ELEMENTS(aOps); i++)
100 {
101 aOps[i].pvAlloc = RTHeapSimpleAlloc(Heap, aOps[i].cb, aOps[i].uAlignment);
102 if (!aOps[i].pvAlloc)
103 {
104 RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
105 return 1;
106 }
107 memset(aOps[i].pvAlloc, szFill[i], aOps[i].cb);
108 if (ALIGNP(aOps[i].pvAlloc, (aOps[i].uAlignment ? aOps[i].uAlignment : 8)) != aOps[i].pvAlloc)
109 {
110 RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
111 return 1;
112 }
113 }
114
115 /* free and allocate the same node again. */
116 for (i = 0; i < ELEMENTS(aOps); i++)
117 {
118 if (!aOps[i].pvAlloc)
119 continue;
120 //RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, aOps[i].pvAlloc,
121 // aOps[i].cb, aOps[i].uAlignment, RTHeapSimpleSize(Heap, aOps[i].pvAlloc));
122 size_t cbBeforeSub = RTHeapSimpleGetFreeSize(Heap);
123 RTHeapSimpleFree(Heap, aOps[i].pvAlloc);
124 size_t cbAfterSubFree = RTHeapSimpleGetFreeSize(Heap);
125
126 void *pv;
127 pv = RTHeapSimpleAlloc(Heap, aOps[i].cb, aOps[i].uAlignment);
128 if (!pv)
129 {
130 RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
131 return 1;
132 }
133 //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapSimpleSize(Heap, pv),
134 // cbBeforeSub, cbAfterSubFree, RTHeapSimpleGetFreeSize(Heap));
135 if (pv != aOps[i].pvAlloc)
136 RTPrintf("Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, aOps[i].pvAlloc, i);
137 aOps[i].pvAlloc = pv;
138 size_t cbAfterSubAlloc = RTHeapSimpleGetFreeSize(Heap);
139 if (cbBeforeSub != cbAfterSubAlloc)
140 {
141 RTPrintf("Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
142 cbBeforeSub, cbAfterSubFree, cbAfterSubAlloc, i);
143 //return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
144 }
145 }
146
147 /* free it in a specific order. */
148 int cFreed = 0;
149 for (i = 0; i < ELEMENTS(aOps); i++)
150 {
151 unsigned j;
152 for (j = 0; j < ELEMENTS(aOps); j++)
153 {
154 if ( aOps[j].iFreeOrder != i
155 || !aOps[j].pvAlloc)
156 continue;
157 //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(Heap), aOps[j].cb, aOps[j].pvAlloc);
158 RTHeapSimpleFree(Heap, aOps[j].pvAlloc);
159 aOps[j].pvAlloc = NULL;
160 cFreed++;
161 }
162 }
163 Assert(cFreed == RT_ELEMENTS(aOps));
164 RTPrintf("i=done free=%d\n", RTHeapSimpleGetFreeSize(Heap));
165
166 /* check that we're back at the right amount of free memory. */
167 size_t cbAfter = RTHeapSimpleGetFreeSize(Heap);
168 if (cbBefore != cbAfter)
169 {
170 RTPrintf("Warning: Either we've split out an alignment chunk at the start, or we've got\n"
171 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
172 RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf);
173 }
174
175 RTPrintf("tstHeapSimple: Success\n");
176#ifdef LOG_ENABLED
177 RTLogFlush(NULL);
178#endif
179 return 0;
180}
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