VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTHeapSimple.cpp@ 59665

Last change on this file since 59665 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.0 KB
Line 
1/* $Id: tstRTHeapSimple.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Simple Heap.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
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
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/heap.h>
32#include <iprt/initterm.h>
33#include <iprt/err.h>
34#include <iprt/stream.h>
35#include <iprt/string.h>
36#include <iprt/param.h>
37#include <iprt/assert.h>
38#include <iprt/log.h>
39#include <iprt/test.h>
40
41
42int main(int argc, char *argv[])
43{
44 /*
45 * Init runtime.
46 */
47 RTTEST hTest;
48 int rc = RTTestInitAndCreate("tstRTHeapSimple", &hTest);
49 if (rc)
50 return rc;
51 RTTestBanner(hTest);
52
53 /*
54 * Create a heap.
55 */
56 RTTestSub(hTest, "Basics");
57 static uint8_t s_abMem[128*1024];
58 RTHEAPSIMPLE Heap;
59 RTTESTI_CHECK_RC(rc = RTHeapSimpleInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1), VINF_SUCCESS);
60 if (RT_FAILURE(rc))
61 return RTTestSummaryAndDestroy(hTest);
62
63 /*
64 * Try allocate.
65 */
66 static struct TstHeapSimpleOps
67 {
68 size_t cb;
69 unsigned uAlignment;
70 void *pvAlloc;
71 unsigned iFreeOrder;
72 } s_aOps[] =
73 {
74 { 16, 0, NULL, 0 }, // 0
75 { 16, 4, NULL, 1 },
76 { 16, 8, NULL, 2 },
77 { 16, 16, NULL, 5 },
78 { 16, 32, NULL, 4 },
79 { 32, 0, NULL, 3 }, // 5
80 { 31, 0, NULL, 6 },
81 { 1024, 0, NULL, 8 },
82 { 1024, 32, NULL, 10 },
83 { 1024, 32, NULL, 12 },
84 { PAGE_SIZE, PAGE_SIZE, NULL, 13 }, // 10
85 { 1024, 32, NULL, 9 },
86 { PAGE_SIZE, 32, NULL, 11 },
87 { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
88 { 16, 0, NULL, 15 },
89 { 9, 0, NULL, 7 }, // 15
90 { 16, 0, NULL, 7 },
91 { 36, 0, NULL, 7 },
92 { 16, 0, NULL, 7 },
93 { 12344, 0, NULL, 7 },
94 { 50, 0, NULL, 7 }, // 20
95 { 16, 0, NULL, 7 },
96 };
97 unsigned i;
98 RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
99 size_t cbBefore = RTHeapSimpleGetFreeSize(Heap);
100 static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
101
102 /* allocate */
103 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
104 {
105 s_aOps[i].pvAlloc = RTHeapSimpleAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
106 RTTESTI_CHECK_MSG(s_aOps[i].pvAlloc, ("RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
107 if (!s_aOps[i].pvAlloc)
108 return RTTestSummaryAndDestroy(hTest);
109
110 memset(s_aOps[i].pvAlloc, szFill[i], s_aOps[i].cb);
111 RTTESTI_CHECK_MSG(RT_ALIGN_P(s_aOps[i].pvAlloc, (s_aOps[i].uAlignment ? s_aOps[i].uAlignment : 8)) == s_aOps[i].pvAlloc,
112 ("RTHeapSimpleAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
113 if (!s_aOps[i].pvAlloc)
114 return RTTestSummaryAndDestroy(hTest);
115 }
116
117 /* free and allocate the same node again. */
118 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
119 {
120 if (!s_aOps[i].pvAlloc)
121 continue;
122 //RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, s_aOps[i].pvAlloc,
123 // s_aOps[i].cb, s_aOps[i].uAlignment, RTHeapSimpleSize(Heap, s_aOps[i].pvAlloc));
124 size_t cbBeforeSub = RTHeapSimpleGetFreeSize(Heap);
125 RTHeapSimpleFree(Heap, s_aOps[i].pvAlloc);
126 size_t cbAfterSubFree = RTHeapSimpleGetFreeSize(Heap);
127
128 void *pv;
129 pv = RTHeapSimpleAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
130 RTTESTI_CHECK_MSG(pv, ("RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
131 if (!pv)
132 return RTTestSummaryAndDestroy(hTest);
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 != s_aOps[i].pvAlloc)
136 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, s_aOps[i].pvAlloc, i);
137 s_aOps[i].pvAlloc = pv;
138 size_t cbAfterSubAlloc = RTHeapSimpleGetFreeSize(Heap);
139 if (cbBeforeSub != cbAfterSubAlloc)
140 {
141 RTTestIPrintf(RTTESTLVL_ALWAYS, "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 /* make a copy of the heap and the to-be-freed list. */
148 static uint8_t s_abMemCopy[sizeof(s_abMem)];
149 memcpy(s_abMemCopy, s_abMem, sizeof(s_abMem));
150 uintptr_t offDelta = (uintptr_t)&s_abMemCopy[0] - (uintptr_t)&s_abMem[0];
151 RTHEAPSIMPLE hHeapCopy = (RTHEAPSIMPLE)((uintptr_t)Heap + offDelta);
152 static struct TstHeapSimpleOps s_aOpsCopy[RT_ELEMENTS(s_aOps)];
153 memcpy(&s_aOpsCopy[0], &s_aOps[0], sizeof(s_aOps));
154
155 /* free it in a specific order. */
156 int cFreed = 0;
157 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
158 {
159 unsigned j;
160 for (j = 0; j < RT_ELEMENTS(s_aOps); j++)
161 {
162 if ( s_aOps[j].iFreeOrder != i
163 || !s_aOps[j].pvAlloc)
164 continue;
165 //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(Heap), s_aOps[j].cb, s_aOps[j].pvAlloc);
166 RTHeapSimpleFree(Heap, s_aOps[j].pvAlloc);
167 s_aOps[j].pvAlloc = NULL;
168 cFreed++;
169 }
170 }
171 RTTESTI_CHECK(cFreed == RT_ELEMENTS(s_aOps));
172 RTTestIPrintf(RTTESTLVL_ALWAYS, "i=done free=%d\n", RTHeapSimpleGetFreeSize(Heap));
173
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 RTTestIPrintf(RTTESTLVL_ALWAYS,
179 "Warning: Either we've split out an alignment chunk at the start, or we've got\n"
180 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
181 RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf);
182 }
183
184 /* relocate and free the bits in heap2 now. */
185 RTTestSub(hTest, "RTHeapSimpleRelocate");
186 rc = RTHeapSimpleRelocate(hHeapCopy, offDelta);
187 RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
188 if (RT_SUCCESS(rc))
189 {
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, RTHeapSimpleGetFreeSize(hHeapCopy), s_aOpsCopy[j].cb, s_aOpsCopy[j].pvAlloc);
201 RTHeapSimpleFree(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 = RTHeapSimpleGetFreeSize(hHeapCopy);
210 RTTESTI_CHECK_MSG(cbAfterCopy == cbAfter, ("cbAfterCopy=%zu cbAfter=%zu\n", cbAfterCopy, cbAfter));
211 }
212
213
214 return RTTestSummaryAndDestroy(hTest);
215}
216
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