Files

315 lines
8.5 KiB
C++
Raw Permalink Normal View History

2011-07-26 14:51:28 +04:00
/****************************************************************************
Copyright (c) 1994 by Xerox Corporation. All rights reserved.
2009-09-16 14:43:37 +00:00
2011-07-26 14:51:28 +04:00
THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
2009-09-16 14:43:37 +00:00
2011-07-26 14:51:28 +04:00
Permission is hereby granted to use or copy this program for any
purpose, provided the above notices are retained on all copies.
Permission to modify the code and to distribute modified code is
granted, provided the above notices are retained, and a notice that
the code was modified is included with the above copyright notice.
****************************************************************************
usage: test_cpp number-of-iterations
This program tries to test the specific C++ functionality provided by
gc_c++.h that isn't tested by the more general test routines of the
collector.
A recommended value for number-of-iterations is 10, which will take a
few minutes to complete.
***************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2011-04-20 21:25:18 +00:00
#undef GC_BUILD
2011-04-20 21:25:18 +00:00
2011-07-26 14:51:28 +04:00
#include "gc_cpp.h"
2011-04-20 21:25:18 +00:00
2011-07-26 14:51:28 +04:00
#include <stdio.h>
#include <stdlib.h>
2011-07-26 15:16:41 +04:00
#include <string.h>
2011-04-20 21:25:18 +00:00
#ifndef DONT_USE_STD_ALLOCATOR
# include "gc_allocator.h"
2011-07-26 16:56:55 +04:00
#else
/* Note: This works only for ancient STL versions. */
# include "new_gc_alloc.h"
2011-07-26 15:09:22 +04:00
#endif
2011-04-20 21:25:18 +00:00
2011-07-26 14:51:28 +04:00
extern "C" {
# include "private/gcconfig.h"
# ifndef GC_API_PRIV
# define GC_API_PRIV GC_API
# endif
GC_API_PRIV void GC_printf(const char * format, ...);
/* Use GC private output to reach the same log file. */
2009-09-16 14:43:37 +00:00
/* Don't include gc_priv.h, since that may include Windows system */
/* header files that don't take kindly to this context. */
2011-07-26 14:51:28 +04:00
}
2011-04-20 21:25:18 +00:00
2011-07-26 15:20:24 +04:00
#ifdef MSWIN32
2011-04-20 21:25:18 +00:00
# include <windows.h>
2011-07-26 15:36:22 +04:00
#endif
2011-07-26 14:51:28 +04:00
2011-04-20 21:25:18 +00:00
#ifdef GC_NAME_CONFLICT
# define USE_GC GC_NS_QUALIFY(UseGC)
2011-04-20 21:25:18 +00:00
struct foo * GC;
#else
# define USE_GC GC_NS_QUALIFY(GC)
2011-04-20 21:25:18 +00:00
#endif
2011-07-26 14:51:28 +04:00
#define my_assert( e ) \
if (! (e)) { \
2011-07-26 20:30:36 +04:00
GC_printf( "Assertion failure in " __FILE__ ", line %d: " #e "\n", \
2011-07-26 14:51:28 +04:00
__LINE__ ); \
exit( 1 ); }
2012-06-15 18:24:18 +04:00
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
# define ATTR_UNUSED __attribute__((__unused__))
#else
# define ATTR_UNUSED /* empty */
#endif
2011-07-26 14:51:28 +04:00
class A {public:
2013-07-11 11:57:32 +04:00
/* An uncollectible class. */
2011-07-26 14:51:28 +04:00
A( int iArg ): i( iArg ) {}
void Test( int iArg ) {
2009-09-16 14:43:37 +00:00
my_assert( i == iArg );}
2011-07-26 14:51:28 +04:00
int i;};
class B: public GC_NS_QUALIFY(gc), public A { public:
2013-07-11 11:57:32 +04:00
/* A collectible class. */
2011-07-26 14:51:28 +04:00
B( int j ): A( j ) {}
~B() {
my_assert( deleting );}
static void Deleting( int on ) {
deleting = on;}
static int deleting;};
int B::deleting = 0;
class C: public GC_NS_QUALIFY(gc_cleanup), public A { public:
2013-07-11 11:57:32 +04:00
/* A collectible class with cleanup and virtual multiple inheritance. */
2011-07-26 14:51:28 +04:00
C( int levelArg ): A( levelArg ), level( levelArg ) {
nAllocated++;
if (level > 0) {
left = new C( level - 1 );
right = new C( level - 1 );}
else {
left = right = 0;}}
~C() {
this->A::Test( level );
nFreed++;
2009-09-16 14:43:37 +00:00
my_assert( level == 0 ?
2011-07-26 14:51:28 +04:00
left == 0 && right == 0 :
level == left->level + 1 && level == right->level + 1 );
left = right = 0;
level = -123456;}
static void Test() {
my_assert( nFreed <= nAllocated && nFreed >= .8 * nAllocated );}
static int nFreed;
static int nAllocated;
int level;
C* left;
C* right;};
int C::nFreed = 0;
int C::nAllocated = 0;
class D: public GC_NS_QUALIFY(gc) { public:
2013-07-11 11:57:32 +04:00
/* A collectible class with a static member function to be used as
2011-07-26 14:51:28 +04:00
an explicit clean-up function supplied to ::new. */
D( int iArg ): i( iArg ) {
nAllocated++;}
static void CleanUp( void* obj, void* data ) {
D* self = (D*) obj;
nFreed++;
2007-06-07 02:53:32 +00:00
my_assert( self->i == (int) (GC_word) data );}
2011-07-26 14:51:28 +04:00
static void Test() {
my_assert( nFreed >= .8 * nAllocated );}
2009-09-16 14:43:37 +00:00
2011-07-26 14:51:28 +04:00
int i;
static int nFreed;
static int nAllocated;};
int D::nFreed = 0;
int D::nAllocated = 0;
class E: public GC_NS_QUALIFY(gc_cleanup) { public:
2013-07-11 11:57:32 +04:00
/* A collectible class with clean-up for use by F. */
2011-07-26 14:51:28 +04:00
E() {
nAllocated++;}
~E() {
nFreed++;}
static int nFreed;
static int nAllocated;};
2009-09-16 14:43:37 +00:00
2011-07-26 14:51:28 +04:00
int E::nFreed = 0;
int E::nAllocated = 0;
2009-09-16 14:43:37 +00:00
2011-07-26 14:51:28 +04:00
class F: public E {public:
2013-07-11 11:57:32 +04:00
/* A collectible class with clean-up, a base with clean-up, and a
2011-07-26 14:51:28 +04:00
member with clean-up. */
F() {
nAllocated++;}
~F() {
nFreed++;}
static void Test() {
my_assert( nFreed >= .8 * nAllocated );
my_assert( 2 * nFreed == E::nFreed );}
2009-09-16 14:43:37 +00:00
2011-07-26 14:51:28 +04:00
E e;
static int nFreed;
static int nAllocated;};
2009-09-16 14:43:37 +00:00
2011-07-26 14:51:28 +04:00
int F::nFreed = 0;
int F::nAllocated = 0;
2009-09-16 14:43:37 +00:00
2011-07-26 14:51:28 +04:00
2007-06-07 02:53:32 +00:00
GC_word Disguise( void* p ) {
return ~ (GC_word) p;}
2011-07-26 14:51:28 +04:00
2007-06-07 02:53:32 +00:00
void* Undisguise( GC_word i ) {
2011-07-26 14:51:28 +04:00
return (void*) ~ i;}
#ifdef MSWIN32
2012-06-15 18:24:18 +04:00
int APIENTRY WinMain( HINSTANCE instance ATTR_UNUSED,
HINSTANCE prev ATTR_UNUSED, LPSTR cmd, int cmdShow ATTR_UNUSED )
2011-07-26 14:51:28 +04:00
{
int argc = 0;
2011-07-26 14:51:28 +04:00
char* argv[ 3 ];
if (cmd != 0)
for (argc = 1; argc < (int)(sizeof(argv) / sizeof(argv[0])); argc++) {
2011-07-26 14:51:28 +04:00
argv[ argc ] = strtok( argc == 1 ? cmd : 0, " \t" );
if (0 == argv[ argc ]) break;}
2011-04-20 21:25:18 +00:00
#elif defined(MACOS)
int main() {
char* argv_[] = {"test_cpp", "10"}; // MacOS doesn't have a commandline
argv = argv_;
argc = sizeof(argv_)/sizeof(argv_[0]);
2011-07-26 14:51:28 +04:00
#else
2011-04-20 21:25:18 +00:00
int main( int argc, char* argv[] ) {
2011-07-26 14:51:28 +04:00
#endif
GC_set_all_interior_pointers(1);
/* needed due to C++ multiple inheritance used */
2011-04-20 21:25:18 +00:00
GC_INIT();
2011-07-26 17:28:12 +04:00
2011-07-26 14:51:28 +04:00
int i, iters, n;
# ifndef DONT_USE_STD_ALLOCATOR
2011-07-26 17:07:21 +04:00
int *x = gc_allocator<int>().allocate(1);
2011-04-20 21:25:18 +00:00
int *xio;
xio = gc_allocator_ignore_off_page<int>().allocate(1);
2012-06-15 18:24:18 +04:00
(void)xio;
2011-07-26 17:07:21 +04:00
int **xptr = traceable_allocator<int *>().allocate(1);
2009-09-16 14:43:37 +00:00
# else
int *x = (int *)gc_alloc::allocate(sizeof(int));
2011-07-26 17:07:21 +04:00
# endif
*x = 29;
# ifndef DONT_USE_STD_ALLOCATOR
if (!xptr) {
fprintf(stderr, "Out of memory!\n");
exit(3);
}
2011-07-26 17:07:21 +04:00
*xptr = x;
x = 0;
2011-07-26 15:09:22 +04:00
# endif
2011-07-26 14:51:28 +04:00
if (argc != 2 || (0 >= (n = atoi( argv[ 1 ] )))) {
2011-07-26 20:30:36 +04:00
GC_printf( "usage: test_cpp number-of-iterations\nAssuming 10 iters\n" );
2011-07-26 17:28:12 +04:00
n = 10;}
2009-09-16 14:43:37 +00:00
2011-07-26 14:51:28 +04:00
for (iters = 1; iters <= n; iters++) {
2011-07-26 20:30:36 +04:00
GC_printf( "Starting iteration %d\n", iters );
2011-07-26 14:51:28 +04:00
2013-07-11 11:57:32 +04:00
/* Allocate some uncollectible As and disguise their pointers.
2011-07-26 14:51:28 +04:00
Later we'll check to see if the objects are still there. We're
2013-07-11 11:57:32 +04:00
checking to make sure these objects really are uncollectible. */
2007-06-07 02:53:32 +00:00
GC_word as[ 1000 ];
GC_word bs[ 1000 ];
2011-07-26 14:51:28 +04:00
for (i = 0; i < 1000; i++) {
as[ i ] = Disguise( new (GC_NS_QUALIFY(NoGC)) A(i) );
bs[ i ] = Disguise( new (GC_NS_QUALIFY(NoGC)) B(i) ); }
2011-07-26 14:51:28 +04:00
/* Allocate a fair number of finalizable Cs, Ds, and Fs.
Later we'll check to make sure they've gone away. */
for (i = 0; i < 1000; i++) {
C* c = new C( 2 );
C c1( 2 ); /* stack allocation should work too */
2011-04-20 21:25:18 +00:00
D* d;
F* f;
d = ::new (USE_GC, D::CleanUp, (void*)(GC_word)i) D( i );
2012-06-15 18:24:18 +04:00
(void)d;
2011-04-20 21:25:18 +00:00
f = new F;
2012-06-15 18:24:18 +04:00
(void)f;
2011-07-26 14:51:28 +04:00
if (0 == i % 10) delete c;}
2013-07-11 11:57:32 +04:00
/* Allocate a very large number of collectible As and Bs and
2011-07-26 14:51:28 +04:00
drop the references to them immediately, forcing many
collections. */
for (i = 0; i < 1000000; i++) {
2011-04-20 21:25:18 +00:00
A* a;
a = new (USE_GC) A( i );
2012-06-15 18:24:18 +04:00
(void)a;
B* b;
b = new B( i );
(void)b;
2011-07-26 15:36:22 +04:00
b = new (USE_GC) B( i );
2011-07-26 14:51:28 +04:00
if (0 == i % 10) {
B::Deleting( 1 );
delete b;
2011-07-26 15:20:24 +04:00
B::Deleting( 0 );}
2009-09-16 14:43:37 +00:00
# ifdef FINALIZE_ON_DEMAND
GC_invoke_finalizers();
# endif
}
2011-07-26 14:51:28 +04:00
2013-07-11 11:57:32 +04:00
/* Make sure the uncollectible As and Bs are still there. */
2011-07-26 14:51:28 +04:00
for (i = 0; i < 1000; i++) {
A* a = (A*) Undisguise( as[ i ] );
B* b = (B*) Undisguise( bs[ i ] );
a->Test( i );
delete a;
b->Test( i );
B::Deleting( 1 );
delete b;
2011-07-26 15:20:24 +04:00
B::Deleting( 0 );
2009-09-16 14:43:37 +00:00
# ifdef FINALIZE_ON_DEMAND
GC_invoke_finalizers();
# endif
}
2011-07-26 14:51:28 +04:00
/* Make sure most of the finalizable Cs, Ds, and Fs have
gone away. */
C::Test();
D::Test();
F::Test();}
# ifndef DONT_USE_STD_ALLOCATOR
2011-07-26 17:07:21 +04:00
x = *xptr;
2011-07-26 15:09:22 +04:00
# endif
2011-07-26 17:07:21 +04:00
my_assert (29 == x[0]);
2011-07-26 20:30:36 +04:00
GC_printf( "The test appears to have succeeded.\n" );
2011-04-20 21:25:18 +00:00
return( 0 );
}