VirtualBox

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

Last change on this file since 20139 was 16977, checked in by vboxsync, 16 years ago

LOG_ENABLED fix

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