Debian Codegraph @

external/bdwgc/tests/test_cpp.cc

raw
1 /****************************************************************************
2 Copyright (c) 1994 by Xerox Corporation. All rights reserved.
3
4 THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6
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
14 usage: test_cpp number-of-iterations
15
16 This program tries to test the specific C++ functionality provided by
17 gc_c++.h that isn't tested by the more general test routines of the
18 collector.
19
20 A recommended value for number-of-iterations is 10, which will take a
21 few minutes to complete.
22
23 ***************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #undef GC_BUILD
30
31 #include "gc_cpp.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #ifndef DONT_USE_STD_ALLOCATOR
38 # include "gc_allocator.h"
39 #else
40 /* Note: This works only for ancient STL versions. */
41 # include "new_gc_alloc.h"
42 #endif
43
44 # include "private/gcconfig.h"
45
46 # ifndef GC_API_PRIV
47 # define GC_API_PRIV GC_API
48 # endif
49 extern "C" {
50 GC_API_PRIV void GC_printf(const char * format, ...);
51 /* Use GC private output to reach the same log file. */
52 /* Don't include gc_priv.h, since that may include Windows system */
53 /* header files that don't take kindly to this context. */
54 }
55
56 #ifdef MSWIN32
57 # include <windows.h>
58 #endif
59
60 #ifdef GC_NAME_CONFLICT
61 # define USE_GC GC_NS_QUALIFY(UseGC)
62 struct foo * GC;
63 #else
64 # define USE_GC GC_NS_QUALIFY(GC)
65 #endif
66
67 #define my_assert( e ) \
68 if (! (e)) { \
69 GC_printf( "Assertion failure in " __FILE__ ", line %d: " #e "\n", \
70 __LINE__ ); \
71 exit( 1 ); }
72
73 #ifndef GC_ATTR_EXPLICIT
74 # if (__cplusplus >= 201103L) || defined(CPPCHECK)
75 # define GC_ATTR_EXPLICIT explicit
76 # else
77 # define GC_ATTR_EXPLICIT /* empty */
78 # endif
79 #endif
80
81 class A {public:
82 /* An uncollectible class. */
83
84 GC_ATTR_EXPLICIT A( int iArg ): i( iArg ) {}
85 void Test( int iArg ) {
86 my_assert( i == iArg );}
87 int i;};
88
89
90 class B: public GC_NS_QUALIFY(gc), public A { public:
91 /* A collectible class. */
92
93 GC_ATTR_EXPLICIT B( int j ): A( j ) {}
94 virtual ~B() {
95 my_assert( deleting );}
96 static void Deleting( int on ) {
97 deleting = on;}
98 static int deleting;};
99
100 int B::deleting = 0;
101
102
103 class C: public GC_NS_QUALIFY(gc_cleanup), public A { public:
104 /* A collectible class with cleanup and virtual multiple inheritance. */
105
106 // The class uses dynamic memory/resource allocation, so provide both
107 // a copy constructor and an assignment operator to workaround a cppcheck
108 // warning.
109 C(const C& c) : A(c.i), level(c.level) {
110 left = c.left ? new C(*c.left) : 0;
111 right = c.right ? new C(*c.right) : 0;
112 }
113
114 C& operator=(const C& c) {
115 if (this != &c) {
116 delete left;
117 delete right;
118 i = c.i;
119 level = c.level;
120 left = c.left ? new C(*c.left) : 0;
121 right = c.right ? new C(*c.right) : 0;
122 }
123 return *this;
124 }
125
126 GC_ATTR_EXPLICIT C( int levelArg ): A( levelArg ), level( levelArg ) {
127 nAllocated++;
128 if (level > 0) {
129 left = new C( level - 1 );
130 right = new C( level - 1 );}
131 else {
132 left = right = 0;}}
133 ~C() {
134 this->A::Test( level );
135 nFreed++;
136 my_assert( level == 0 ?
137 left == 0 && right == 0 :
138 level == left->level + 1 && level == right->level + 1 );
139 left = right = 0;
140 level = -123456;}
141 static void Test() {
142 my_assert( nFreed <= nAllocated && nFreed >= .8 * nAllocated );}
143
144 static int nFreed;
145 static int nAllocated;
146 int level;
147 C* left;
148 C* right;};
149
150 int C::nFreed = 0;
151 int C::nAllocated = 0;
152
153
154 class D: public GC_NS_QUALIFY(gc) { public:
155 /* A collectible class with a static member function to be used as
156 an explicit clean-up function supplied to ::new. */
157
158 GC_ATTR_EXPLICIT D( int iArg ): i( iArg ) {
159 nAllocated++;}
160 static void CleanUp( void* obj, void* data ) {
161 D* self = static_cast<D*>(obj);
162 nFreed++;
163 my_assert( (GC_word)self->i == (GC_word)data );}
164 static void Test() {
165 my_assert( nFreed >= .8 * nAllocated );}
166
167 int i;
168 static int nFreed;
169 static int nAllocated;};
170
171 int D::nFreed = 0;
172 int D::nAllocated = 0;
173
174
175 class E: public GC_NS_QUALIFY(gc_cleanup) { public:
176 /* A collectible class with clean-up for use by F. */
177
178 E() {
179 nAllocated++;}
180 ~E() {
181 nFreed++;}
182
183 static int nFreed;
184 static int nAllocated;};
185
186 int E::nFreed = 0;
187 int E::nAllocated = 0;
188
189
190 class F: public E {public:
191 /* A collectible class with clean-up, a base with clean-up, and a
192 member with clean-up. */
193
194 F() {
195 nAllocatedF++;
196 }
197
198 ~F() {
199 nFreedF++;
200 }
201
202 static void Test() {
203 my_assert(nFreedF >= .8 * nAllocatedF);
204 my_assert(2 * nFreedF == nFreed);
205 }
206
207 E e;
208 static int nFreedF;
209 static int nAllocatedF;
210 };
211
212 int F::nFreedF = 0;
213 int F::nAllocatedF = 0;
214
215
216 GC_word Disguise( void* p ) {
217 return ~ (GC_word) p;}
218
219 void* Undisguise( GC_word i ) {
220 return (void*) ~ i;}
221
222 #define GC_CHECKED_DELETE(p) \
223 do { \
224 size_t freed_before = GC_get_expl_freed_bytes_since_gc(); \
225 delete p; /* the operator should invoke GC_FREE() */ \
226 size_t freed_after = GC_get_expl_freed_bytes_since_gc(); \
227 my_assert(freed_before != freed_after); \
228 } while (0)
229
230 #if ((defined(MSWIN32) && !defined(__MINGW32__)) || defined(MSWINCE)) \
231 && !defined(NO_WINMAIN_ENTRY)
232 int APIENTRY WinMain( HINSTANCE /* instance */, HINSTANCE /* prev */,
233 LPSTR cmd, int /* cmdShow */)
234 {
235 int argc = 0;
236 char* argv[ 3 ];
237
238 # if defined(CPPCHECK)
239 GC_noop1((GC_word)&WinMain);
240 # endif
241 if (cmd != 0)
242 for (argc = 1; argc < (int)(sizeof(argv) / sizeof(argv[0])); argc++) {
243 // Parse the command-line string. Non-reentrant strtok() is not used
244 // to avoid complains of static analysis tools. (And, strtok_r() is
245 // not available on some platforms.) The code is equivalent to:
246 // if (!(argv[argc] = strtok(argc == 1 ? cmd : 0, " \t"))) break;
247 if (NULL == cmd) {
248 argv[argc] = NULL;
249 break;
250 }
251 argv[argc] = cmd;
252 for (; *cmd != '\0'; cmd++) {
253 if (*cmd != ' ' && *cmd != '\t')
254 break;
255 }
256 if ('\0' == *cmd) {
257 argv[argc] = NULL;
258 break;
259 }
260 argv[argc] = cmd;
261 while (*(++cmd) != '\0') {
262 if (*cmd == ' ' || *cmd == '\t')
263 break;
264 }
265 if (*cmd != '\0') {
266 *(cmd++) = '\0';
267 } else {
268 cmd = NULL;
269 }
270 }
271 #elif defined(MACOS)
272 int main() {
273 char* argv_[] = {"test_cpp", "10"}; // MacOS doesn't have a command line
274 argv = argv_;
275 argc = sizeof(argv_)/sizeof(argv_[0]);
276 #else
277 int main( int argc, char* argv[] ) {
278 #endif
279
280 GC_set_all_interior_pointers(1);
281 /* needed due to C++ multiple inheritance used */
282
283 GC_INIT();
284
285 int i, iters, n;
286 # ifndef DONT_USE_STD_ALLOCATOR
287 int *x = gc_allocator<int>().allocate(1);
288 int *xio;
289 xio = gc_allocator_ignore_off_page<int>().allocate(1);
290 (void)xio;
291 int **xptr = traceable_allocator<int *>().allocate(1);
292 # else
293 int *x = (int *)gc_alloc::allocate(sizeof(int));
294 # endif
295 *x = 29;
296 # ifndef DONT_USE_STD_ALLOCATOR
297 if (!xptr) {
298 fprintf(stderr, "Out of memory!\n");
299 exit(3);
300 }
301 *xptr = x;
302 x = 0;
303 # endif
304 if (argc != 2
305 || (n = (int)COVERT_DATAFLOW(atoi(argv[1]))) <= 0) {
306 GC_printf("usage: test_cpp number-of-iterations\n"
307 "Assuming 10 iters\n");
308 n = 10;
309 }
310
311 for (iters = 1; iters <= n; iters++) {
312 GC_printf( "Starting iteration %d\n", iters );
313
314 /* Allocate some uncollectible As and disguise their pointers.
315 Later we'll check to see if the objects are still there. We're
316 checking to make sure these objects really are uncollectible. */
317 GC_word as[ 1000 ];
318 GC_word bs[ 1000 ];
319 for (i = 0; i < 1000; i++) {
320 as[ i ] = Disguise( new (GC_NS_QUALIFY(NoGC)) A(i) );
321 bs[ i ] = Disguise( new (GC_NS_QUALIFY(NoGC)) B(i) ); }
322
323 /* Allocate a fair number of finalizable Cs, Ds, and Fs.
324 Later we'll check to make sure they've gone away. */
325 for (i = 0; i < 1000; i++) {
326 C* c = new C( 2 );
327 C c1( 2 ); /* stack allocation should work too */
328 D* d;
329 F* f;
330 d = ::new (USE_GC, D::CleanUp, (void*)(GC_word)i) D( i );
331 (void)d;
332 f = new F;
333 F** fa = new F*[1];
334 fa[0] = f;
335 (void)fa;
336 delete[] fa;
337 if (0 == i % 10)
338 GC_CHECKED_DELETE(c);
339 }
340
341 /* Allocate a very large number of collectible As and Bs and
342 drop the references to them immediately, forcing many
343 collections. */
344 for (i = 0; i < 1000000; i++) {
345 A* a;
346 a = new (USE_GC) A( i );
347 (void)a;
348 B* b;
349 b = new B( i );
350 (void)b;
351 b = new (USE_GC) B( i );
352 if (0 == i % 10) {
353 B::Deleting( 1 );
354 GC_CHECKED_DELETE(b);
355 B::Deleting( 0 );}
356 # ifdef FINALIZE_ON_DEMAND
357 GC_invoke_finalizers();
358 # endif
359 }
360
361 /* Make sure the uncollectible As and Bs are still there. */
362 for (i = 0; i < 1000; i++) {
363 A* a = static_cast<A*>(Undisguise(as[i]));
364 B* b = static_cast<B*>(Undisguise(bs[i]));
365 a->Test( i );
366 # if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER)
367 // Workaround for ASan/MSan: the linker uses operator delete
368 // implementation from libclang_rt instead of gc_cpp (thus
369 // causing incompatible alloc/free).
370 GC_FREE(a);
371 # else
372 GC_CHECKED_DELETE(a);
373 # endif
374 b->Test( i );
375 B::Deleting( 1 );
376 GC_CHECKED_DELETE(b);
377 B::Deleting( 0 );
378 # ifdef FINALIZE_ON_DEMAND
379 GC_invoke_finalizers();
380 # endif
381 }
382
383 /* Make sure most of the finalizable Cs, Ds, and Fs have
384 gone away. */
385 C::Test();
386 D::Test();
387 F::Test();}
388
389 # ifndef DONT_USE_STD_ALLOCATOR
390 x = *xptr;
391 # endif
392 my_assert (29 == x[0]);
393 GC_printf( "The test appears to have succeeded.\n" );
394 return( 0 );
395 }