1 /****************************************************************************
2 Copyright (c) 1994 by Xerox Corporation. All rights reserved.
4 THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 Permission is hereby granted to use or copy this program for any
8 purpose, provided the above notices are retained on all copies.
9 Permission to modify the code and to distribute modified code is
10 granted, provided the above notices are retained, and a notice that
11 the code was modified is included with the above copyright notice.
12 ****************************************************************************
13 Last modified on Mon Jul 10 21:06:03 PDT 1995 by ellis
14 modified on December 20, 1994 7:27 pm PST by boehm
16 usage: test_cpp number-of-iterations
18 This program tries to test the specific C++ functionality provided by
19 gc_c++.h that isn't tested by the more general test routines of the
22 A recommended value for number-of-iterations is 10, which will take a
23 few minutes to complete.
25 ***************************************************************************/
28 # include "private/config.h"
39 #define USE_STD_ALLOCATOR
41 #ifdef USE_STD_ALLOCATOR
42 # include "gc_allocator.h"
44 # include "new_gc_alloc.h"
48 # include "private/gcconfig.h"
51 # define GC_API_PRIV GC_API
53 GC_API_PRIV void GC_printf(const char * format, ...);
54 /* Use GC private output to reach the same log file. */
55 /* Don't include gc_priv.h, since that may include Windows system */
56 /* header files that don't take kindly to this context. */
63 #ifdef GC_NAME_CONFLICT
70 #define my_assert( e ) \
72 GC_printf( "Assertion failure in " __FILE__ ", line %d: " #e "\n", \
78 /* An uncollectable class. */
80 A( int iArg ): i( iArg ) {}
81 void Test( int iArg ) {
82 my_assert( i == iArg );}
86 class B: public gc, public A {public:
87 /* A collectable class. */
91 my_assert( deleting );}
92 static void Deleting( int on ) {
94 static int deleting;};
99 class C: public gc_cleanup, public A {public:
100 /* A collectable class with cleanup and virtual multiple inheritance. */
102 C( int levelArg ): A( levelArg ), level( levelArg ) {
105 left = new C( level - 1 );
106 right = new C( level - 1 );}
110 this->A::Test( level );
112 my_assert( level == 0 ?
113 left == 0 && right == 0 :
114 level == left->level + 1 && level == right->level + 1 );
118 my_assert( nFreed <= nAllocated && nFreed >= .8 * nAllocated );}
121 static int nAllocated;
127 int C::nAllocated = 0;
130 class D: public gc {public:
131 /* A collectable class with a static member function to be used as
132 an explicit clean-up function supplied to ::new. */
134 D( int iArg ): i( iArg ) {
136 static void CleanUp( void* obj, void* data ) {
139 my_assert( self->i == (int) (GC_word) data );}
141 my_assert( nFreed >= .8 * nAllocated );}
145 static int nAllocated;};
148 int D::nAllocated = 0;
151 class E: public gc_cleanup {public:
152 /* A collectable class with clean-up for use by F. */
160 static int nAllocated;};
163 int E::nAllocated = 0;
166 class F: public E {public:
167 /* A collectable class with clean-up, a base with clean-up, and a
168 member with clean-up. */
175 my_assert( nFreed >= .8 * nAllocated );
176 my_assert( 2 * nFreed == E::nFreed );}
180 static int nAllocated;};
183 int F::nAllocated = 0;
186 GC_word Disguise( void* p ) {
187 return ~ (GC_word) p;}
189 void* Undisguise( GC_word i ) {
193 int APIENTRY WinMain(
194 HINSTANCE instance, HINSTANCE prev, LPSTR cmd, int cmdShow )
200 for (argc = 1; argc < (int)(sizeof(argv) / sizeof(argv[0])); argc++) {
201 argv[ argc ] = strtok( argc == 1 ? cmd : 0, " \t" );
202 if (0 == argv[ argc ]) break;}
205 char* argv_[] = {"test_cpp", "10"}; // MacOS doesn't have a commandline
207 argc = sizeof(argv_)/sizeof(argv_[0]);
209 int main( int argc, char* argv[] ) {
212 GC_set_all_interior_pointers(1);
213 /* needed due to C++ multiple inheritance used */
218 # ifdef USE_STD_ALLOCATOR
219 int *x = gc_allocator<int>().allocate(1);
221 xio = gc_allocator_ignore_off_page<int>().allocate(1);
222 int **xptr = traceable_allocator<int *>().allocate(1);
224 int *x = (int *)gc_alloc::allocate(sizeof(int));
227 # ifdef USE_STD_ALLOCATOR
231 if (argc != 2 || (0 >= (n = atoi( argv[ 1 ] )))) {
232 GC_printf( "usage: test_cpp number-of-iterations\nAssuming 10 iters\n" );
235 for (iters = 1; iters <= n; iters++) {
236 GC_printf( "Starting iteration %d\n", iters );
238 /* Allocate some uncollectable As and disguise their pointers.
239 Later we'll check to see if the objects are still there. We're
240 checking to make sure these objects really are uncollectable. */
243 for (i = 0; i < 1000; i++) {
244 as[ i ] = Disguise( new (NoGC ) A( i ) );
245 bs[ i ] = Disguise( new (NoGC) B( i ) );}
247 /* Allocate a fair number of finalizable Cs, Ds, and Fs.
248 Later we'll check to make sure they've gone away. */
249 for (i = 0; i < 1000; i++) {
251 C c1( 2 ); /* stack allocation should work too */
254 d = ::new (USE_GC, D::CleanUp, (void*)(GC_word)i) D( i );
256 if (0 == i % 10) delete c;}
258 /* Allocate a very large number of collectable As and Bs and
259 drop the references to them immediately, forcing many
261 for (i = 0; i < 1000000; i++) {
263 a = new (USE_GC) A( i );
265 b = new (USE_GC) B( i );
270 # ifdef FINALIZE_ON_DEMAND
271 GC_invoke_finalizers();
275 /* Make sure the uncollectable As and Bs are still there. */
276 for (i = 0; i < 1000; i++) {
277 A* a = (A*) Undisguise( as[ i ] );
278 B* b = (B*) Undisguise( bs[ i ] );
285 # ifdef FINALIZE_ON_DEMAND
286 GC_invoke_finalizers();
290 /* Make sure most of the finalizable Cs, Ds, and Fs have
296 # ifdef USE_STD_ALLOCATOR
299 my_assert (29 == x[0]);
300 GC_printf( "The test appears to have succeeded.\n" );