tstMemAutoPtr.cpp revision 23214ec638d069bd560e9d80520453b931e5d6b8
77b1a2d8b5dbe2c0b5200794914239fee3c8ee5dvboxsync * IPRT - Testcase the RTMemAutoPtr template.
bbede9c189def47a9880f0ffb03c0c230c774185vboxsync * Copyright (C) 2008 Sun Microsystems, Inc.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * available from http://www.virtualbox.org. This file is free software;
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * The contents of this file may alternatively be used under the terms
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * of the Common Development and Distribution License Version 1.0
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * VirtualBox OSE distribution, in which case the provisions of the
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * CDDL are applicable instead of those of the GPL.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * You may elect to license modified versions of this file under the
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * terms and conditions of either the GPL or the CDDL or both.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
662d52947eeb3fc8fca3b23991a5eee47077f896vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
da957c069c2a3c582fe265ff88170ce4c42b499dvboxsync * additional information or have any questions.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync/*******************************************************************************
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync* Header Files *
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync*******************************************************************************/
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync/*******************************************************************************
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync* Structures and Typedefs *
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync*******************************************************************************/
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync/*******************************************************************************
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync* Global Variables *
efff36b306e370346025647a158689021df2e1d1vboxsync*******************************************************************************/
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsyncstatic unsigned g_cErrors = 0;
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsyncstatic unsigned g_cFrees;
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * Feel free to inspect with gdb / objdump / whatever / g++ -fverbose-asm in
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * a release build and compare with tstMemAutoPtrDisas1PureC.
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * For comparing to tstMemAutoPtrDisas1.
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync TSTMEMAUTOPTRSTRUCT *pHandle = (TSTMEMAUTOPTRSTRUCT *)RTMemRealloc(NULL, sizeof(*pHandle));
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsynctemplate <class T>
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync RTPrintf("tstMemAutoPtr(%d): Destructor called with NILL handle!\n");
bbede9c189def47a9880f0ffb03c0c230c774185vboxsync RTPrintf("tstMemAutoPtr(%d): Destructor called with a bad handle %p\n", aMem);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsyncvoid *tstMemAutoPtrAllocatorNoZero(void *pvOld, size_t cbNew)
21029597fc4b76d0db0c9542daee201447281781vboxsync do { bool const f = !!(expr); if (!f) { RTPrintf("tstMemAutoPtr(%d): %s!\n", __LINE__, #expr); g_cErrors++; } } while (0)
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * Some simple stuff.
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * Use the electric fence memory API to check alternative template
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * arguments and also check some subscript / reference limit thing.
16a9adc14900ca18e6909679a579f6833425e030vboxsync RTMemAutoPtr<char, RTMemEfAutoFree<char>, RTMemEfRealloc> Electric(10);
16a9adc14900ca18e6909679a579f6833425e030vboxsync //CHECK_EXPR(Electric == '0');
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync /* Electric[10] = '2'; - this will crash (of course) */
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * Check that memory is actually free when it should be and isn't when it shouldn't.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * Use the electric heap to get some extra checks.
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt(128);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt2(128);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync FreeIt2.reset(FreeIt2.get()); /* this one is weird, but it's how things works... */
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> DontFreeIt(256);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt3(128);
16a9adc14900ca18e6909679a579f6833425e030vboxsync RTMemAutoPtr<char, tstMemAutoPtrDestructorCounter, RTMemEfRealloc> FreeIt4;
16a9adc14900ca18e6909679a579f6833425e030vboxsync * Check the ->, [] and * (unary) operators with some useful struct.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync /* since at it... */
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * Check the zeroing of memory.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync RTMemAutoPtr<uint64_t, RTMemAutoDestructor<uint64_t>, tstMemAutoPtrAllocatorNoZero> Zeroed1(1, true);
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync RTMemAutoPtr<uint64_t, RTMemAutoDestructor<uint64_t>, tstMemAutoPtrAllocatorNoZero> Zeroed2;
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync RTPrintf("tstMemAutoPtr: FAILED - %d errors\n", g_cErrors);
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync#endif /* TST_MEM_AUTO_PTR_ONLY_DISAS */