VirtualBox

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

Last change on this file since 108869 was 108653, checked in by vboxsync, 6 weeks ago

Runtime/testcase/tstRTHeapSimple.cpp: Make it work for hosts where we don't know the host page size when building, bugref:10391

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.4 KB
Line 
1/* $Id: tstRTHeapSimple.cpp 108653 2025-03-20 14:51:56Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Simple Heap.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/heap.h>
42#include <iprt/initterm.h>
43#include <iprt/errcore.h>
44#include <iprt/stream.h>
45#include <iprt/string.h>
46#include <iprt/param.h>
47#include <iprt/assert.h>
48#include <iprt/log.h>
49#include <iprt/test.h>
50#include <iprt/system.h>
51
52
53int main(int argc, char **argv)
54{
55 RT_NOREF_PV(argc); RT_NOREF_PV(argv);
56
57 /*
58 * Init runtime.
59 */
60 RTTEST hTest;
61 int rc = RTTestInitAndCreate("tstRTHeapSimple", &hTest);
62 if (rc)
63 return rc;
64 RTTestBanner(hTest);
65
66 /*
67 * Create a heap.
68 */
69 RTTestSub(hTest, "Basics");
70 static uint8_t s_abMem[128*1024];
71 RTHEAPSIMPLE Heap;
72 RTTESTI_CHECK_RC(rc = RTHeapSimpleInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1), VINF_SUCCESS);
73 if (RT_FAILURE(rc))
74 return RTTestSummaryAndDestroy(hTest);
75
76 /*
77 * Try allocate.
78 */
79 uint32_t const cbPageSize = (uint32_t)RTSystemGetPageSize();
80 static struct TstHeapSimpleOps
81 {
82 size_t cb;
83 unsigned uAlignment;
84 void *pvAlloc;
85 unsigned iFreeOrder;
86 } s_aOps[] =
87 {
88 { 16, 0, NULL, 0 }, // 0
89 { 16, 4, NULL, 1 },
90 { 16, 8, NULL, 2 },
91 { 16, 16, NULL, 5 },
92 { 16, 32, NULL, 4 },
93 { 32, 0, NULL, 3 }, // 5
94 { 31, 0, NULL, 6 },
95 { 1024, 0, NULL, 8 },
96 { 1024, 32, NULL, 10 },
97 { 1024, 32, NULL, 12 },
98 { cbPageSize, cbPageSize, NULL, 13 }, // 10
99 { 1024, 32, NULL, 9 },
100 { cbPageSize, 32, NULL, 11 },
101 { cbPageSize, cbPageSize, NULL, 14 },
102 { 16, 0, NULL, 15 },
103 { 9, 0, NULL, 7 }, // 15
104 { 16, 0, NULL, 7 },
105 { 36, 0, NULL, 7 },
106 { 16, 0, NULL, 7 },
107 { 12344, 0, NULL, 7 },
108 { 50, 0, NULL, 7 }, // 20
109 { 16, 0, NULL, 7 },
110 };
111 unsigned i;
112 RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)(uintptr_t)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
113 size_t cbBefore = RTHeapSimpleGetFreeSize(Heap);
114 static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
115
116 /* allocate */
117 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
118 {
119 s_aOps[i].pvAlloc = RTHeapSimpleAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
120 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));
121 if (!s_aOps[i].pvAlloc)
122 return RTTestSummaryAndDestroy(hTest);
123
124 memset(s_aOps[i].pvAlloc, szFill[i], s_aOps[i].cb);
125 RTTESTI_CHECK_MSG(RT_ALIGN_P(s_aOps[i].pvAlloc, (s_aOps[i].uAlignment ? s_aOps[i].uAlignment : 8)) == s_aOps[i].pvAlloc,
126 ("RTHeapSimpleAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
127 if (!s_aOps[i].pvAlloc)
128 return RTTestSummaryAndDestroy(hTest);
129 }
130
131 /* free and allocate the same node again. */
132 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
133 {
134 if (!s_aOps[i].pvAlloc)
135 continue;
136 //RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, s_aOps[i].pvAlloc,
137 // s_aOps[i].cb, s_aOps[i].uAlignment, RTHeapSimpleSize(Heap, s_aOps[i].pvAlloc));
138 size_t cbBeforeSub = RTHeapSimpleGetFreeSize(Heap);
139 RTHeapSimpleFree(Heap, s_aOps[i].pvAlloc);
140 size_t cbAfterSubFree = RTHeapSimpleGetFreeSize(Heap);
141
142 void *pv;
143 pv = RTHeapSimpleAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
144 RTTESTI_CHECK_MSG(pv, ("RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, s_aOps[i].cb, s_aOps[i].uAlignment, i));
145 if (!pv)
146 return RTTestSummaryAndDestroy(hTest);
147 //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapSimpleSize(Heap, pv),
148 // cbBeforeSub, cbAfterSubFree, RTHeapSimpleGetFreeSize(Heap));
149 if (pv != s_aOps[i].pvAlloc)
150 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, s_aOps[i].pvAlloc, i);
151 s_aOps[i].pvAlloc = pv;
152 size_t cbAfterSubAlloc = RTHeapSimpleGetFreeSize(Heap);
153 if (cbBeforeSub != cbAfterSubAlloc)
154 {
155 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
156 cbBeforeSub, cbAfterSubFree, cbAfterSubAlloc, i);
157 //return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
158 }
159 }
160
161 /* make a copy of the heap and the to-be-freed list. */
162 static uint8_t s_abMemCopy[sizeof(s_abMem)];
163 memcpy(s_abMemCopy, s_abMem, sizeof(s_abMem));
164 uintptr_t offDelta = (uintptr_t)&s_abMemCopy[0] - (uintptr_t)&s_abMem[0];
165 RTHEAPSIMPLE hHeapCopy = (RTHEAPSIMPLE)((uintptr_t)Heap + offDelta);
166 static struct TstHeapSimpleOps s_aOpsCopy[RT_ELEMENTS(s_aOps)];
167 memcpy(&s_aOpsCopy[0], &s_aOps[0], sizeof(s_aOps));
168
169 /* free it in a specific order. */
170 int cFreed = 0;
171 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
172 {
173 unsigned j;
174 for (j = 0; j < RT_ELEMENTS(s_aOps); j++)
175 {
176 if ( s_aOps[j].iFreeOrder != i
177 || !s_aOps[j].pvAlloc)
178 continue;
179 //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(Heap), s_aOps[j].cb, s_aOps[j].pvAlloc);
180 RTHeapSimpleFree(Heap, s_aOps[j].pvAlloc);
181 s_aOps[j].pvAlloc = NULL;
182 cFreed++;
183 }
184 }
185 RTTESTI_CHECK(cFreed == RT_ELEMENTS(s_aOps));
186 RTTestIPrintf(RTTESTLVL_ALWAYS, "i=done free=%d\n", RTHeapSimpleGetFreeSize(Heap));
187
188 /* check that we're back at the right amount of free memory. */
189 size_t cbAfter = RTHeapSimpleGetFreeSize(Heap);
190 if (cbBefore != cbAfter)
191 {
192 RTTestIPrintf(RTTESTLVL_ALWAYS,
193 "Warning: Either we've split out an alignment chunk at the start, or we've got\n"
194 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
195 RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)(uintptr_t)RTPrintf);
196 }
197
198 /* relocate and free the bits in heap2 now. */
199 RTTestSub(hTest, "RTHeapSimpleRelocate");
200 rc = RTHeapSimpleRelocate(hHeapCopy, offDelta);
201 RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
202 if (RT_SUCCESS(rc))
203 {
204 /* free it in a specific order. */
205 int cFreed2 = 0;
206 for (i = 0; i < RT_ELEMENTS(s_aOpsCopy); i++)
207 {
208 unsigned j;
209 for (j = 0; j < RT_ELEMENTS(s_aOpsCopy); j++)
210 {
211 if ( s_aOpsCopy[j].iFreeOrder != i
212 || !s_aOpsCopy[j].pvAlloc)
213 continue;
214 //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(hHeapCopy), s_aOpsCopy[j].cb, s_aOpsCopy[j].pvAlloc);
215 RTHeapSimpleFree(hHeapCopy, (uint8_t *)s_aOpsCopy[j].pvAlloc + offDelta);
216 s_aOpsCopy[j].pvAlloc = NULL;
217 cFreed2++;
218 }
219 }
220 RTTESTI_CHECK(cFreed2 == RT_ELEMENTS(s_aOpsCopy));
221
222 /* check that we're back at the right amount of free memory. */
223 size_t cbAfterCopy = RTHeapSimpleGetFreeSize(hHeapCopy);
224 RTTESTI_CHECK_MSG(cbAfterCopy == cbAfter, ("cbAfterCopy=%zu cbAfter=%zu\n", cbAfterCopy, cbAfter));
225 }
226
227
228 return RTTestSummaryAndDestroy(hTest);
229}
230
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette