1 | /* $Id: tstMemAutoPtr.cpp 11021 2008-07-30 23:03:57Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Testcase the RTMemAutoPtr template.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 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/mem.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/rand.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Structures and Typedefs *
|
---|
43 | *******************************************************************************/
|
---|
44 | typedef struct TSTMEMAUTOPTRSTRUCT
|
---|
45 | {
|
---|
46 | uint32_t a;
|
---|
47 | uint32_t b;
|
---|
48 | uint32_t c;
|
---|
49 | } TSTMEMAUTOPTRSTRUCT;
|
---|
50 |
|
---|
51 |
|
---|
52 | /*******************************************************************************
|
---|
53 | * Global Variables *
|
---|
54 | *******************************************************************************/
|
---|
55 | static unsigned g_cErrors = 0;
|
---|
56 | static unsigned g_cFrees;
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | template <class T>
|
---|
61 | void tstMemAutoPtrDestructorCounter(T *aMem)
|
---|
62 | {
|
---|
63 | if (aMem == NULL)
|
---|
64 | {
|
---|
65 | RTPrintf("tstMemAutoPtr(%d): Destructor called with NILL handle!\n");
|
---|
66 | g_cErrors++;
|
---|
67 | }
|
---|
68 | else if (!VALID_PTR(aMem))
|
---|
69 | {
|
---|
70 | RTPrintf("tstMemAutoPtr(%d): Destructor called with a bad handle %p\n", aMem);
|
---|
71 | g_cErrors++;
|
---|
72 | }
|
---|
73 | RTMemEfFree(aMem);
|
---|
74 | g_cFrees++;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | void *tstMemAutoPtrAllocatorNoZero(void *pvOld, size_t cbNew)
|
---|
79 | {
|
---|
80 | void *pvNew = RTMemRealloc(pvOld, cbNew);
|
---|
81 | if (pvNew)
|
---|
82 | memset(pvNew, 0xfe, cbNew);
|
---|
83 | return pvNew;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Feel free to inspect with gdb / objdump / whatever / g++ -fverbose-asm in
|
---|
89 | * a release build and compare with tstMemAutoPtrDisas1PureC.
|
---|
90 | */
|
---|
91 | extern "C" int tstMemAutoPtrDisas1(void **ppv)
|
---|
92 | {
|
---|
93 | RTMemAutoPtr<TSTMEMAUTOPTRSTRUCT> Handle(1);
|
---|
94 | if (!Handle)
|
---|
95 | {
|
---|
96 | Handle->a = RTRandU32();
|
---|
97 | if (Handle->a < UINT32_MAX / 2)
|
---|
98 | {
|
---|
99 | *ppv = Handle.release();
|
---|
100 | return VINF_SUCCESS;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | return VERR_TRY_AGAIN;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * For comparing to tstMemAutoPtrDisas1.
|
---|
108 | */
|
---|
109 | extern "C" int tstMemAutoPtrDisas1PureC(void **ppv)
|
---|
110 | {
|
---|
111 | TSTMEMAUTOPTRSTRUCT *pHandle = (TSTMEMAUTOPTRSTRUCT *)RTMemRealloc(NULL, sizeof(*pHandle));
|
---|
112 | if (pHandle)
|
---|
113 | {
|
---|
114 | pHandle->a = RTRandU32();
|
---|
115 | if (pHandle->a < UINT32_MAX / 2)
|
---|
116 | {
|
---|
117 | *ppv = pHandle;
|
---|
118 | return VINF_SUCCESS;
|
---|
119 | }
|
---|
120 | RTMemFree(pHandle);
|
---|
121 | }
|
---|
122 | return VERR_TRY_AGAIN;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | int main()
|
---|
127 | {
|
---|
128 | RTR3Init(false);
|
---|
129 | RTPrintf("tstMemAutoPtr: TESTING...\n");
|
---|
130 |
|
---|
131 | #define CHECK_EXPR(expr) \
|
---|
132 | do { bool const f = !!(expr); if (!f) { RTPrintf("tstMemAutoPtr(%d): %s!\n", __LINE__, #expr); g_cErrors++; } } while (0)
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Some simple stuff.
|
---|
136 | */
|
---|
137 | {
|
---|
138 | RTMemAutoPtr<char> NilObj;
|
---|
139 | CHECK_EXPR(!NilObj);
|
---|
140 | CHECK_EXPR(NilObj.get() == NULL);
|
---|
141 | CHECK_EXPR(NilObj.release() == NULL);
|
---|
142 | NilObj.reset();
|
---|
143 | }
|
---|
144 |
|
---|
145 | {
|
---|
146 | RTMemAutoPtr<char> Alloc(10);
|
---|
147 | CHECK_EXPR(Alloc.get() != NULL);
|
---|
148 | char *pch = Alloc.release();
|
---|
149 | CHECK_EXPR(pch != NULL);
|
---|
150 | CHECK_EXPR(Alloc.get() == NULL);
|
---|
151 |
|
---|
152 | RTMemAutoPtr<char> Manage(pch);
|
---|
153 | CHECK_EXPR(Manage.get() == pch);
|
---|
154 | CHECK_EXPR(&Manage[0] == pch);
|
---|
155 | CHECK_EXPR(&Manage[1] == &pch[1]);
|
---|
156 | CHECK_EXPR(&Manage[9] == &pch[9]);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Use the electric fence memory API to check alternative template
|
---|
161 | * arguments and also check some subscript / reference limit thing.
|
---|
162 | */
|
---|
163 | {
|
---|
164 | RTMemAutoPtr<char, RTMemEfAutoFree<char>, RTMemEfRealloc> Electric(10);
|
---|
165 | CHECK_EXPR(Electric.get() != NULL);
|
---|
166 | Electric[0] = '0';
|
---|
167 | CHECK_EXPR(Electric[0] == '0');
|
---|
168 | CHECK_EXPR(*Electric == '0');
|
---|
169 | //CHECK_EXPR(Electric == '0');
|
---|
170 | Electric[9] = '1';
|
---|
171 | CHECK_EXPR(Electric[9] == '1');
|
---|
172 | /* Electric[10] = '2'; - this will crash (of course) */
|
---|
173 | }
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * Check that memory is actually free when it should be and isn't when it shouldn't.
|
---|
177 | * Use the electric heap to get some extra checks.
|
---|
178 | */
|
---|
179 | g_cFrees = 0;
|
---|
180 | {
|
---|
181 | RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt(128);
|
---|
182 | FreeIt[127] = '0';
|
---|
183 | }
|
---|
184 | CHECK_EXPR(g_cFrees == 1);
|
---|
185 |
|
---|
186 | g_cFrees = 0;
|
---|
187 | {
|
---|
188 | RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt2(128);
|
---|
189 | FreeIt2[127] = '1';
|
---|
190 | FreeIt2.reset();
|
---|
191 | FreeIt2.alloc(128);
|
---|
192 | FreeIt2[127] = '2';
|
---|
193 | FreeIt2.reset(FreeIt2.get()); /* this one is weird, but it's how things works... */
|
---|
194 | }
|
---|
195 | CHECK_EXPR(g_cFrees == 2);
|
---|
196 |
|
---|
197 | g_cFrees = 0;
|
---|
198 | {
|
---|
199 | RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> DontFreeIt(256);
|
---|
200 | DontFreeIt[255] = '0';
|
---|
201 | RTMemEfFree(DontFreeIt.release());
|
---|
202 | }
|
---|
203 | CHECK_EXPR(g_cFrees == 0);
|
---|
204 |
|
---|
205 | g_cFrees = 0;
|
---|
206 | {
|
---|
207 | RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt3(128);
|
---|
208 | FreeIt3[127] = '0';
|
---|
209 | CHECK_EXPR(FreeIt3.realloc(128));
|
---|
210 | FreeIt3[127] = '0';
|
---|
211 | CHECK_EXPR(FreeIt3.realloc(256));
|
---|
212 | FreeIt3[255] = '0';
|
---|
213 | CHECK_EXPR(FreeIt3.realloc(64));
|
---|
214 | FreeIt3[63] = '0';
|
---|
215 | CHECK_EXPR(FreeIt3.realloc(32));
|
---|
216 | FreeIt3[31] = '0';
|
---|
217 | }
|
---|
218 | CHECK_EXPR(g_cFrees == 1);
|
---|
219 |
|
---|
220 | g_cFrees = 0;
|
---|
221 | {
|
---|
222 | RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt4;
|
---|
223 | CHECK_EXPR(FreeIt4.alloc(123));
|
---|
224 | CHECK_EXPR(FreeIt4.realloc(543));
|
---|
225 | FreeIt4 = (char *)NULL;
|
---|
226 | CHECK_EXPR(FreeIt4.get() == NULL);
|
---|
227 | }
|
---|
228 | CHECK_EXPR(g_cFrees == 1);
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Check the ->, [] and * (unary) operators with some useful struct.
|
---|
232 | */
|
---|
233 | {
|
---|
234 | RTMemAutoPtr<TSTMEMAUTOPTRSTRUCT> Struct1(1);
|
---|
235 | Struct1->a = 0x11223344;
|
---|
236 | Struct1->b = 0x55667788;
|
---|
237 | Struct1->c = 0x99aabbcc;
|
---|
238 | CHECK_EXPR(Struct1->a == 0x11223344);
|
---|
239 | CHECK_EXPR(Struct1->b == 0x55667788);
|
---|
240 | CHECK_EXPR(Struct1->c == 0x99aabbcc);
|
---|
241 |
|
---|
242 | Struct1[0].a = 0x11223344;
|
---|
243 | Struct1[0].b = 0x55667788;
|
---|
244 | Struct1[0].c = 0x99aabbcc;
|
---|
245 | CHECK_EXPR(Struct1[0].a == 0x11223344);
|
---|
246 | CHECK_EXPR(Struct1[0].b == 0x55667788);
|
---|
247 | CHECK_EXPR(Struct1[0].c == 0x99aabbcc);
|
---|
248 |
|
---|
249 | (*Struct1).a = 0x11223344;
|
---|
250 | (*Struct1).b = 0x55667788;
|
---|
251 | (*Struct1).c = 0x99aabbcc;
|
---|
252 | CHECK_EXPR((*Struct1).a == 0x11223344);
|
---|
253 | CHECK_EXPR((*Struct1).b == 0x55667788);
|
---|
254 | CHECK_EXPR((*Struct1).c == 0x99aabbcc);
|
---|
255 |
|
---|
256 | /* since at it... */
|
---|
257 | Struct1.get()->a = 0x11223344;
|
---|
258 | Struct1.get()->b = 0x55667788;
|
---|
259 | Struct1.get()->c = 0x99aabbcc;
|
---|
260 | CHECK_EXPR(Struct1.get()->a == 0x11223344);
|
---|
261 | CHECK_EXPR(Struct1.get()->b == 0x55667788);
|
---|
262 | CHECK_EXPR(Struct1.get()->c == 0x99aabbcc);
|
---|
263 | }
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * Check the zeroing of memory.
|
---|
267 | */
|
---|
268 | {
|
---|
269 | RTMemAutoPtr<uint64_t, RTMemAutoDestructor<uint64_t>, tstMemAutoPtrAllocatorNoZero> Zeroed1(1, true);
|
---|
270 | CHECK_EXPR(*Zeroed1 == 0);
|
---|
271 | }
|
---|
272 |
|
---|
273 | {
|
---|
274 | RTMemAutoPtr<uint64_t, RTMemAutoDestructor<uint64_t>, tstMemAutoPtrAllocatorNoZero> Zeroed2;
|
---|
275 | Zeroed2.alloc(5, true);
|
---|
276 | CHECK_EXPR(Zeroed2[0] == 0);
|
---|
277 | CHECK_EXPR(Zeroed2[1] == 0);
|
---|
278 | CHECK_EXPR(Zeroed2[2] == 0);
|
---|
279 | CHECK_EXPR(Zeroed2[3] == 0);
|
---|
280 | CHECK_EXPR(Zeroed2[4] == 0);
|
---|
281 | }
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * Summary.
|
---|
285 | */
|
---|
286 | if (!g_cErrors)
|
---|
287 | RTPrintf("tstMemAutoPtr: SUCCESS\n");
|
---|
288 | else
|
---|
289 | RTPrintf("tstMemAutoPtr: FAILED - %d errors\n", g_cErrors);
|
---|
290 | return !!g_cErrors;
|
---|
291 | }
|
---|