3
* IPRT Testcase - Simple Heap.
7
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
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.
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.
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.
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.
31
/*******************************************************************************
33
*******************************************************************************/
34
#include <iprt/heap.h>
35
#include <iprt/initterm.h>
37
#include <iprt/stream.h>
38
#include <iprt/string.h>
39
#include <iprt/param.h>
40
#include <iprt/assert.h>
44
int main(int argc, char *argv[])
52
RTPrintf("RTR3Init failed: %Rrc\n", rc);
55
RTPrintf("tstHeapSimple: TESTING...\n");
60
static uint8_t s_abMem[128*1024];
62
rc = RTHeapSimpleInit(&Heap, &s_abMem[1], sizeof(s_abMem) - 1);
65
RTPrintf("RTHeapSimpleInit failed: %Rrc\n", rc);
80
{ 16, 0, NULL, 0 }, // 0
85
{ 32, 0, NULL, 3 }, // 5
88
{ 1024, 32, NULL, 10 },
89
{ 1024, 32, NULL, 12 },
90
{ PAGE_SIZE, PAGE_SIZE, NULL, 13 }, // 10
91
{ 1024, 32, NULL, 9 },
92
{ PAGE_SIZE, 32, NULL, 11 },
93
{ PAGE_SIZE, PAGE_SIZE, NULL, 14 },
95
{ 9, 0, NULL, 7 }, // 15
99
{ 12344, 0, NULL, 7 },
100
{ 50, 0, NULL, 7 }, // 20
104
RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf);
105
size_t cbBefore = RTHeapSimpleGetFreeSize(Heap);
106
static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
109
for (i = 0; i < RT_ELEMENTS(aOps); i++)
111
aOps[i].pvAlloc = RTHeapSimpleAlloc(Heap, aOps[i].cb, aOps[i].uAlignment);
112
if (!aOps[i].pvAlloc)
114
RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
117
memset(aOps[i].pvAlloc, szFill[i], aOps[i].cb);
118
if (RT_ALIGN_P(aOps[i].pvAlloc, (aOps[i].uAlignment ? aOps[i].uAlignment : 8)) != aOps[i].pvAlloc)
120
RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> %p\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
125
/* free and allocate the same node again. */
126
for (i = 0; i < RT_ELEMENTS(aOps); i++)
128
if (!aOps[i].pvAlloc)
130
//RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, aOps[i].pvAlloc,
131
// aOps[i].cb, aOps[i].uAlignment, RTHeapSimpleSize(Heap, aOps[i].pvAlloc));
132
size_t cbBeforeSub = RTHeapSimpleGetFreeSize(Heap);
133
RTHeapSimpleFree(Heap, aOps[i].pvAlloc);
134
size_t cbAfterSubFree = RTHeapSimpleGetFreeSize(Heap);
137
pv = RTHeapSimpleAlloc(Heap, aOps[i].cb, aOps[i].uAlignment);
140
RTPrintf("Failure: RTHeapSimpleAlloc(%p, %#x, %#x,) -> NULL i=%d\n", (void *)Heap, aOps[i].cb, aOps[i].uAlignment, i);
143
//RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapSimpleSize(Heap, pv),
144
// cbBeforeSub, cbAfterSubFree, RTHeapSimpleGetFreeSize(Heap));
145
if (pv != aOps[i].pvAlloc)
146
RTPrintf("Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, aOps[i].pvAlloc, i);
147
aOps[i].pvAlloc = pv;
148
size_t cbAfterSubAlloc = RTHeapSimpleGetFreeSize(Heap);
149
if (cbBeforeSub != cbAfterSubAlloc)
151
RTPrintf("Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
152
cbBeforeSub, cbAfterSubFree, cbAfterSubAlloc, i);
153
//return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
157
/* free it in a specific order. */
159
for (i = 0; i < RT_ELEMENTS(aOps); i++)
162
for (j = 0; j < RT_ELEMENTS(aOps); j++)
164
if ( aOps[j].iFreeOrder != i
167
//RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, RTHeapSimpleGetFreeSize(Heap), aOps[j].cb, aOps[j].pvAlloc);
168
RTHeapSimpleFree(Heap, aOps[j].pvAlloc);
169
aOps[j].pvAlloc = NULL;
173
Assert(cFreed == RT_ELEMENTS(aOps));
174
RTPrintf("i=done free=%d\n", RTHeapSimpleGetFreeSize(Heap));
176
/* check that we're back at the right amount of free memory. */
177
size_t cbAfter = RTHeapSimpleGetFreeSize(Heap);
178
if (cbBefore != cbAfter)
180
RTPrintf("Warning: Either we've split out an alignment chunk at the start, or we've got\n"
181
" an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
182
RTHeapSimpleDump(Heap, (PFNRTHEAPSIMPLEPRINTF)RTPrintf);
185
RTPrintf("tstHeapSimple: Success\n");