1 | /* $Id $ */
|
---|
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 |
|
---|
42 |
|
---|
43 | int main(int argc, char *argv[])
|
---|
44 | {
|
---|
45 | /*
|
---|
46 | * Init runtime.
|
---|
47 | */
|
---|
48 | int rc = RTR3Init();
|
---|
49 | if (RT_FAILURE(rc))
|
---|
50 | {
|
---|
51 | RTPrintf("RTR3Init failed: %Vrc\n", rc);
|
---|
52 | return 1;
|
---|
53 | }
|
---|
54 | RTPrintf("tstHeapSimple: TESTING...\n");
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Create a heap.
|
---|
58 | */
|
---|
59 | static uint8_t s_abMem[128*1024];
|
---|
60 | RTHEAPSIMPLE Heap;
|
---|
61 | rc = RTHeapSimpleInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1);
|
---|
62 | if (RT_FAILURE(rc))
|
---|
63 | {
|
---|
64 | RTPrintf("RTHeapSimpleInit failed: %Vrc\n", rc);
|
---|
65 | return 1;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Try allocate.
|
---|
70 | */
|
---|
71 | static struct
|
---|
72 | {
|
---|
73 | size_t cb;
|
---|
74 | unsigned uAlignment;
|
---|
75 | void *pvAlloc;
|
---|
76 | unsigned iFreeOrder;
|
---|
77 | } aOps[] =
|
---|
78 | {
|
---|
79 | { 16, 0, NULL, 0 }, // 0
|
---|
80 | { 16, 4, NULL, 1 },
|
---|
81 | { 16, 8, NULL, 2 },
|
---|
82 | { 16, 16, NULL, 5 },
|
---|
83 | { 16, 32, NULL, 4 },
|
---|
84 | { 32, 0, NULL, 3 }, // 5
|
---|
85 | { 31, 0, NULL, 6 },
|
---|
86 | { 1024, 0, NULL, 8 },
|
---|
87 | { 1024, 32, NULL, 10 },
|
---|
88 | { 1024, 32, NULL, 12 },
|
---|
89 | { PAGE_SIZE, PAGE_SIZE, NULL, 13 }, // 10
|
---|
90 | { 1024, 32, NULL, 9 },
|
---|
91 | { PAGE_SIZE, 32, NULL, 11 },
|
---|
92 | { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
|
---|
93 | { 16, 0, NULL, 15 },
|
---|
94 | { 9, 0, NULL, 7 }, // 15
|
---|
95 | { 16, 0, NULL, 7 },
|
---|
96 | { 36, 0, NULL, 7 },
|
---|
97 | { 16, 0, NULL, 7 },
|
---|
98 | { 12344, 0, NULL, 7 },
|
---|
99 | { 50, 0, NULL, 7 }, // 20
|
---|
100 | { 16, 0, NULL, 7 },
|
---|
101 | };
|
---|
102 | unsigned i;
|
---|
103 | RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf);
|
---|
104 | size_t cbBefore = RTHeapSimpleGetFreeSize(Heap);
|
---|
105 | static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
106 |
|
---|
107 | /* allocate */
|
---|
108 | for (i = 0; i < ELEMENTS(aOps); i++)
|
---|
109 | {
|
---|
110 | aOps[i].pvAlloc = RTHeapSimpleAlloc(Heap, aOps[i].cb, aOps[i].uAlignment);
|
---|
111 | if (!aOps[i].pvAlloc)
|
---|
112 | {
|
---|
113 | RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
|
---|
114 | return 1;
|
---|
115 | }
|
---|
116 | memset(aOps[i].pvAlloc, szFill[i], aOps[i].cb);
|
---|
117 | if (RT_ALIGN_P(aOps[i].pvAlloc, (aOps[i].uAlignment ? aOps[i].uAlignment : 8)) != aOps[i].pvAlloc)
|
---|
118 | {
|
---|
119 | RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
|
---|
120 | return 1;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* free and allocate the same node again. */
|
---|
125 | for (i = 0; i < ELEMENTS(aOps); i++)
|
---|
126 | {
|
---|
127 | if (!aOps[i].pvAlloc)
|
---|
128 | continue;
|
---|
129 | //RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, aOps[i].pvAlloc,
|
---|
130 | // aOps[i].cb, aOps[i].uAlignment, RTHeapSimpleSize(Heap, aOps[i].pvAlloc));
|
---|
131 | size_t cbBeforeSub = RTHeapSimpleGetFreeSize(Heap);
|
---|
132 | RTHeapSimpleFree(Heap, aOps[i].pvAlloc);
|
---|
133 | size_t cbAfterSubFree = RTHeapSimpleGetFreeSize(Heap);
|
---|
134 |
|
---|
135 | void *pv;
|
---|
136 | pv = RTHeapSimpleAlloc(Heap, aOps[i].cb, aOps[i].uAlignment);
|
---|
137 | if (!pv)
|
---|
138 | {
|
---|
139 | RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
|
---|
140 | return 1;
|
---|
141 | }
|
---|
142 | //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapSimpleSize(Heap, pv),
|
---|
143 | // cbBeforeSub, cbAfterSubFree, RTHeapSimpleGetFreeSize(Heap));
|
---|
144 | if (pv != aOps[i].pvAlloc)
|
---|
145 | RTPrintf("Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, aOps[i].pvAlloc, i);
|
---|
146 | aOps[i].pvAlloc = pv;
|
---|
147 | size_t cbAfterSubAlloc = RTHeapSimpleGetFreeSize(Heap);
|
---|
148 | if (cbBeforeSub != cbAfterSubAlloc)
|
---|
149 | {
|
---|
150 | RTPrintf("Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
|
---|
151 | cbBeforeSub, cbAfterSubFree, cbAfterSubAlloc, i);
|
---|
152 | //return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* free it in a specific order. */
|
---|
157 | int cFreed = 0;
|
---|
158 | for (i = 0; i < ELEMENTS(aOps); i++)
|
---|
159 | {
|
---|
160 | unsigned j;
|
---|
161 | for (j = 0; j < ELEMENTS(aOps); j++)
|
---|
162 | {
|
---|
163 | if ( aOps[j].iFreeOrder != i
|
---|
164 | || !aOps[j].pvAlloc)
|
---|
165 | continue;
|
---|
166 | //RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(Heap), aOps[j].cb, aOps[j].pvAlloc);
|
---|
167 | RTHeapSimpleFree(Heap, aOps[j].pvAlloc);
|
---|
168 | aOps[j].pvAlloc = NULL;
|
---|
169 | cFreed++;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | Assert(cFreed == RT_ELEMENTS(aOps));
|
---|
173 | RTPrintf("i=done free=%d\n", RTHeapSimpleGetFreeSize(Heap));
|
---|
174 |
|
---|
175 | /* check that we're back at the right amount of free memory. */
|
---|
176 | size_t cbAfter = RTHeapSimpleGetFreeSize(Heap);
|
---|
177 | if (cbBefore != cbAfter)
|
---|
178 | {
|
---|
179 | RTPrintf("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 | RTPrintf("tstHeapSimple: Success\n");
|
---|
185 | #ifdef LOG_ENABLED
|
---|
186 | RTLogFlush(NULL);
|
---|
187 | #endif
|
---|
188 | return 0;
|
---|
189 | }
|
---|