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