VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTHeapOffset.cpp@ 25059

Last change on this file since 25059 was 25059, checked in by vboxsync, 15 years ago

RTHeapOffset: Initial conversion of RTHeapSimple.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1/* $Id: tstRTHeapOffset.cpp 25059 2009-11-27 18:17:44Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Offset Based Heap.
4 */
5
6/*
7 * Copyright (C) 2006-2009 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#include <iprt/test.h>
43
44
45int main(int argc, char *argv[])
46{
47 /*
48 * Init runtime.
49 */
50 RTTEST hTest;
51 int rc = RTTestInitAndCreate("tstRTHeapOffset", &hTest);
52 if (rc)
53 return rc;
54 RTTestBanner(hTest);
55
56 /*
57 * Create a heap.
58 */
59 RTTestSub(hTest, "Basics");
60 static uint8_t s_abMem[128*1024];
61 RTHEAPOFFSET Heap;
62 RTTESTI_CHECK_RC(rc = RTHeapOffsetInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1), VINF_SUCCESS);
63 if (RT_FAILURE(rc))
64 return RTTestSummaryAndDestroy(hTest);
65
66 /*
67 * Try allocate.
68 */
69 static struct TstHeapOffsetOps
70 {
71 size_t cb;
72 unsigned uAlignment;
73 void *pvAlloc;
74 unsigned iFreeOrder;
75 } s_aOps[] =
76 {
77 { 16, 0, NULL, 0 }, // 0
78 { 16, 4, NULL, 1 },
79 { 16, 8, NULL, 2 },
80 { 16, 16, NULL, 5 },
81 { 16, 32, NULL, 4 },
82 { 32, 0, NULL, 3 }, // 5
83 { 31, 0, NULL, 6 },
84 { 1024, 0, NULL, 8 },
85 { 1024, 32, NULL, 10 },
86 { 1024, 32, NULL, 12 },
87 { PAGE_SIZE, PAGE_SIZE, NULL, 13 }, // 10
88 { 1024, 32, NULL, 9 },
89 { PAGE_SIZE, 32, NULL, 11 },
90 { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
91 { 16, 0, NULL, 15 },
92 { 9, 0, NULL, 7 }, // 15
93 { 16, 0, NULL, 7 },
94 { 36, 0, NULL, 7 },
95 { 16, 0, NULL, 7 },
96 { 12344, 0, NULL, 7 },
97 { 50, 0, NULL, 7 }, // 20
98 { 16, 0, NULL, 7 },
99 };
100 unsigned i;
101 RTHeapOffsetDump(Heap, (PFNRTHEAPOFFSETPRINTF)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
102 size_t cbBefore = RTHeapOffsetGetFreeSize(Heap);
103 static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
104
105 /* allocate */
106 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
107 {
108 s_aOps[i].pvAlloc = RTHeapOffsetAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
109 RTTESTI_CHECK_MSG(s_aOps[i].pvAlloc, ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
110 if (!s_aOps[i].pvAlloc)
111 return RTTestSummaryAndDestroy(hTest);
112
113 memset(s_aOps[i].pvAlloc, szFill[i], s_aOps[i].cb);
114 RTTESTI_CHECK_MSG(RT_ALIGN_P(s_aOps[i].pvAlloc, (s_aOps[i].uAlignment ? s_aOps[i].uAlignment : 8)) == s_aOps[i].pvAlloc,
115 ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
116 if (!s_aOps[i].pvAlloc)
117 return RTTestSummaryAndDestroy(hTest);
118 }
119
120 /* free and allocate the same node again. */
121 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
122 {
123 if (!s_aOps[i].pvAlloc)
124 continue;
125 //RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, s_aOps[i].pvAlloc,
126 // s_aOps[i].cb, s_aOps[i].uAlignment, RTHeapOffsetSize(Heap, s_aOps[i].pvAlloc));
127 size_t cbBeforeSub = RTHeapOffsetGetFreeSize(Heap);
128 RTHeapOffsetFree(Heap, s_aOps[i].pvAlloc);
129 size_t cbAfterSubFree = RTHeapOffsetGetFreeSize(Heap);
130
131 void *pv;
132 pv = RTHeapOffsetAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
133 RTTESTI_CHECK_MSG(pv, ("RTHeapOffsetAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
134 if (!pv)
135 return RTTestSummaryAndDestroy(hTest);
136 //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapOffsetSize(Heap, pv),
137 // cbBeforeSub, cbAfterSubFree, RTHeapOffsetGetFreeSize(Heap));
138
139 if (pv != s_aOps[i].pvAlloc)
140 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, s_aOps[i].pvAlloc, i);
141 s_aOps[i].pvAlloc = pv;
142 size_t cbAfterSubAlloc = RTHeapOffsetGetFreeSize(Heap);
143 if (cbBeforeSub != cbAfterSubAlloc)
144 {
145 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
146 cbBeforeSub, cbAfterSubFree, cbAfterSubAlloc, i);
147 //return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
148 }
149 }
150
151 /* make a copy of the heap and the to-be-freed list. */
152 static uint8_t s_abMemCopy[sizeof(s_abMem)];
153 memcpy(s_abMemCopy, s_abMem, sizeof(s_abMem));
154 uintptr_t offDelta = (uintptr_t)&s_abMemCopy[0] - (uintptr_t)&s_abMem[0];
155 RTHEAPOFFSET hHeapCopy = (RTHEAPOFFSET)((uintptr_t)Heap + offDelta);
156 static struct TstHeapOffsetOps s_aOpsCopy[RT_ELEMENTS(s_aOps)];
157 memcpy(&s_aOpsCopy[0], &s_aOps[0], sizeof(s_aOps));
158
159 /* free it in a specific order. */
160 int cFreed = 0;
161 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
162 {
163 unsigned j;
164 for (j = 0; j < RT_ELEMENTS(s_aOps); j++)
165 {
166 if ( s_aOps[j].iFreeOrder != i
167 || !s_aOps[j].pvAlloc)
168 continue;
169 //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapOffsetGetFreeSize(Heap), s_aOps[j].cb, s_aOps[j].pvAlloc);
170 RTHeapOffsetFree(Heap, s_aOps[j].pvAlloc);
171 s_aOps[j].pvAlloc = NULL;
172 cFreed++;
173 }
174 }
175 RTTESTI_CHECK(cFreed == RT_ELEMENTS(s_aOps));
176 RTTestIPrintf(RTTESTLVL_ALWAYS, "i=done free=%d\n", RTHeapOffsetGetFreeSize(Heap));
177
178 /* check that we're back at the right amount of free memory. */
179 size_t cbAfter = RTHeapOffsetGetFreeSize(Heap);
180 if (cbBefore != cbAfter)
181 {
182 RTTestIPrintf(RTTESTLVL_ALWAYS,
183 "Warning: Either we've split out an alignment chunk at the start, or we've got\n"
184 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
185 RTHeapOffsetDump(Heap, (PFNRTHEAPOFFSETPRINTF)RTPrintf);
186 }
187
188 /* relocate and free the bits in heap2 now. */
189 RTTestSub(hTest, "Relocated Heap");
190 /* free it in a specific order. */
191 int cFreed2 = 0;
192 for (i = 0; i < RT_ELEMENTS(s_aOpsCopy); i++)
193 {
194 unsigned j;
195 for (j = 0; j < RT_ELEMENTS(s_aOpsCopy); j++)
196 {
197 if ( s_aOpsCopy[j].iFreeOrder != i
198 || !s_aOpsCopy[j].pvAlloc)
199 continue;
200 //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapOffsetGetFreeSize(hHeapCopy), s_aOpsCopy[j].cb, s_aOpsCopy[j].pvAlloc);
201 RTHeapOffsetFree(hHeapCopy, (uint8_t *)s_aOpsCopy[j].pvAlloc + offDelta);
202 s_aOpsCopy[j].pvAlloc = NULL;
203 cFreed2++;
204 }
205 }
206 RTTESTI_CHECK(cFreed2 == RT_ELEMENTS(s_aOpsCopy));
207
208 /* check that we're back at the right amount of free memory. */
209 size_t cbAfterCopy = RTHeapOffsetGetFreeSize(hHeapCopy);
210 RTTESTI_CHECK_MSG(cbAfterCopy == cbAfter, ("cbAfterCopy=%zu cbAfter=%zu\n", cbAfterCopy, cbAfter));
211
212
213 return RTTestSummaryAndDestroy(hTest);
214}
215
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