1 | /* $Id: tstRTHeapSimple.cpp 25059 2009-11-27 18:17:44Z vboxsync $ */
|
---|
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 | #include <iprt/test.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | int main(int argc, char *argv[])
|
---|
46 | {
|
---|
47 | /*
|
---|
48 | * Init runtime.
|
---|
49 | */
|
---|
50 | RTTEST hTest;
|
---|
51 | int rc = RTTestInitAndCreate("tstRTHeapSimple", &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 | RTHEAPSIMPLE Heap;
|
---|
62 | RTTESTI_CHECK_RC(rc = RTHeapSimpleInit(&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 TstHeapSimpleOps
|
---|
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 | RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
|
---|
102 | size_t cbBefore = RTHeapSimpleGetFreeSize(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 = RTHeapSimpleAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
|
---|
109 | 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));
|
---|
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 | ("RTHeapSimpleAlloc(%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, RTHeapSimpleSize(Heap, s_aOps[i].pvAlloc));
|
---|
127 | size_t cbBeforeSub = RTHeapSimpleGetFreeSize(Heap);
|
---|
128 | RTHeapSimpleFree(Heap, s_aOps[i].pvAlloc);
|
---|
129 | size_t cbAfterSubFree = RTHeapSimpleGetFreeSize(Heap);
|
---|
130 |
|
---|
131 | void *pv;
|
---|
132 | pv = RTHeapSimpleAlloc(Heap, s_aOps[i].cb, s_aOps[i].uAlignment);
|
---|
133 | RTTESTI_CHECK_MSG(pv, ("RTHeapSimpleAlloc(%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, RTHeapSimpleSize(Heap, pv),
|
---|
137 | // cbBeforeSub, cbAfterSubFree, RTHeapSimpleGetFreeSize(Heap));
|
---|
138 | if (pv != s_aOps[i].pvAlloc)
|
---|
139 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, s_aOps[i].pvAlloc, i);
|
---|
140 | s_aOps[i].pvAlloc = pv;
|
---|
141 | size_t cbAfterSubAlloc = RTHeapSimpleGetFreeSize(Heap);
|
---|
142 | if (cbBeforeSub != cbAfterSubAlloc)
|
---|
143 | {
|
---|
144 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
|
---|
145 | cbBeforeSub, cbAfterSubFree, cbAfterSubAlloc, i);
|
---|
146 | //return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | /* make a copy of the heap and the to-be-freed list. */
|
---|
151 | static uint8_t s_abMemCopy[sizeof(s_abMem)];
|
---|
152 | memcpy(s_abMemCopy, s_abMem, sizeof(s_abMem));
|
---|
153 | uintptr_t offDelta = (uintptr_t)&s_abMemCopy[0] - (uintptr_t)&s_abMem[0];
|
---|
154 | RTHEAPSIMPLE hHeapCopy = (RTHEAPSIMPLE)((uintptr_t)Heap + offDelta);
|
---|
155 | static struct TstHeapSimpleOps s_aOpsCopy[RT_ELEMENTS(s_aOps)];
|
---|
156 | memcpy(&s_aOpsCopy[0], &s_aOps[0], sizeof(s_aOps));
|
---|
157 |
|
---|
158 | /* free it in a specific order. */
|
---|
159 | int cFreed = 0;
|
---|
160 | for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
|
---|
161 | {
|
---|
162 | unsigned j;
|
---|
163 | for (j = 0; j < RT_ELEMENTS(s_aOps); j++)
|
---|
164 | {
|
---|
165 | if ( s_aOps[j].iFreeOrder != i
|
---|
166 | || !s_aOps[j].pvAlloc)
|
---|
167 | continue;
|
---|
168 | //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(Heap), s_aOps[j].cb, s_aOps[j].pvAlloc);
|
---|
169 | RTHeapSimpleFree(Heap, s_aOps[j].pvAlloc);
|
---|
170 | s_aOps[j].pvAlloc = NULL;
|
---|
171 | cFreed++;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | RTTESTI_CHECK(cFreed == RT_ELEMENTS(s_aOps));
|
---|
175 | RTTestIPrintf(RTTESTLVL_ALWAYS, "i=done free=%d\n", RTHeapSimpleGetFreeSize(Heap));
|
---|
176 |
|
---|
177 | /* check that we're back at the right amount of free memory. */
|
---|
178 | size_t cbAfter = RTHeapSimpleGetFreeSize(Heap);
|
---|
179 | if (cbBefore != cbAfter)
|
---|
180 | {
|
---|
181 | RTTestIPrintf(RTTESTLVL_ALWAYS,
|
---|
182 | "Warning: Either we've split out an alignment chunk at the start, or we've got\n"
|
---|
183 | " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
|
---|
184 | RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /* relocate and free the bits in heap2 now. */
|
---|
188 | RTTestSub(hTest, "RTHeapSimpleRelocate");
|
---|
189 | rc = RTHeapSimpleRelocate(hHeapCopy, offDelta);
|
---|
190 | RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
|
---|
191 | if (RT_SUCCESS(rc))
|
---|
192 | {
|
---|
193 | /* free it in a specific order. */
|
---|
194 | int cFreed2 = 0;
|
---|
195 | for (i = 0; i < RT_ELEMENTS(s_aOpsCopy); i++)
|
---|
196 | {
|
---|
197 | unsigned j;
|
---|
198 | for (j = 0; j < RT_ELEMENTS(s_aOpsCopy); j++)
|
---|
199 | {
|
---|
200 | if ( s_aOpsCopy[j].iFreeOrder != i
|
---|
201 | || !s_aOpsCopy[j].pvAlloc)
|
---|
202 | continue;
|
---|
203 | //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(hHeapCopy), s_aOpsCopy[j].cb, s_aOpsCopy[j].pvAlloc);
|
---|
204 | RTHeapSimpleFree(hHeapCopy, (uint8_t *)s_aOpsCopy[j].pvAlloc + offDelta);
|
---|
205 | s_aOpsCopy[j].pvAlloc = NULL;
|
---|
206 | cFreed2++;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | RTTESTI_CHECK(cFreed2 == RT_ELEMENTS(s_aOpsCopy));
|
---|
210 |
|
---|
211 | /* check that we're back at the right amount of free memory. */
|
---|
212 | size_t cbAfterCopy = RTHeapSimpleGetFreeSize(hHeapCopy);
|
---|
213 | RTTESTI_CHECK_MSG(cbAfterCopy == cbAfter, ("cbAfterCopy=%zu cbAfter=%zu\n", cbAfterCopy, cbAfter));
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | return RTTestSummaryAndDestroy(hTest);
|
---|
218 | }
|
---|
219 |
|
---|