set executable bit on promote-build.sh (r=cpeyer)
[tamarin-stm.git] / MMgc / GCTests.cpp
blob8c58cafbafc2ff6d9b94af02a94aa66f75e75cd3
1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2004-2006
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 #include "MMgc.h"
43 #ifdef _MSC_VER
44 // "behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized"
45 #pragma warning(disable:4345) // b/c GCObject doesn't have a ctor
46 #ifdef _DEBUG
47 // we turn on exceptions in DEBUG builds
48 #pragma warning(disable:4291) // no matching operator delete found; memory will not be freed if initialization throws an exception
49 #endif
50 #endif
52 #ifdef _DEBUG
54 namespace MMgc
56 GC *gc;
58 void collect()
60 gc->Collect();
63 GCWeakRef* createWeakRef(int extra=0)
65 // Bogusly use up some extra stack.
67 // Certain compilers/platforms require this (at least gcc/MacIntel).
68 // A pointer to the temporary GCObject can be left on the stack, so
69 // far down that CleanStack doesn't dare touch it. Leaving the
70 // pointer there causes the temporary to be marked, and not collected,
71 // which causes tests to fail with assertions.
72 ///
73 // The extra 64 bytes here causes the temporary to end up higher on
74 // the stack (numerically lesser address, on Intel at least) where
75 // CleanStack will clobber it.
77 char buf[64];
78 VMPI_sprintf(buf, "%d", extra); // don't optimize away buf
80 return (new (gc, extra) GCObject())->GetWeakRef();
83 void weakRefSweepSmall()
85 GCWeakRef *ref = createWeakRef();
86 collect();
87 gc->CleanStack(true);
88 collect();
89 (void)ref;
90 GCAssert(ref->isNull());
93 void weakRefSweepLarge()
95 GCWeakRef *ref = createWeakRef(5000);
96 collect();
97 gc->CleanStack(true);
98 collect();
99 (void)ref;
100 GCAssert(ref->isNull());
103 void weakRefFreeSmall()
105 GCWeakRef *ref = createWeakRef();
106 delete ref->get();
107 GCAssert(ref->isNull());
110 void weakRefFreeLarge()
112 GCWeakRef *ref = createWeakRef(5000);
113 delete ref->get();
114 GCAssert(ref->isNull());
117 class RCObjectAddRefInDtor : public RCObject
119 public:
120 RCObjectAddRefInDtor(RCObject ** _stackPinners, int _length) : rcs(_stackPinners), length(_length) {}
121 ~RCObjectAddRefInDtor()
123 // whack these, used create freelist
124 for(int i=0, n=length; i<n;i++)
126 r1 = rcs[i];
129 // add/remove myself (this was the apollo bug)
130 r1 = this;
131 r1 = NULL;
132 rcs = NULL;
133 length = 0;
135 DRCWB(RCObject*) r1;
137 // naked pointer so I can kick these pinners out out of the ZCT during reap
138 RCObject **rcs;
139 int length;
142 GCWeakRef* createProblem(RCObject **stackPinners)
144 // now create one that causes some removes from the dtor
145 return (new (gc) RCObjectAddRefInDtor(stackPinners, 3))->GetWeakRef();
148 /* see bug 182420 */
149 void drcApolloTest()
151 // prime ZCT with some pinners
152 RCObject *stackPinners[3];
153 VMPI_memset(stackPinners, 0, sizeof(stackPinners));
155 GCWeakRef *wr = createProblem(stackPinners);
157 stackPinners[0] = new (gc) RCObject();
158 stackPinners[1] = new (gc) RCObject();
159 stackPinners[2] = new (gc) RCObject();
161 // force ZCT
162 for(int i=0, n=1000;i<n; i++)
164 new (gc) RCObject();
167 // it may still be alive if we had a dirty stack
168 if(!wr->isNull())
169 delete wr->get();
172 // See comments in header file regarding what might happen if this is called.
173 void RunGCTests(GC *g)
175 gc = g;
176 weakRefSweepSmall();
177 weakRefSweepLarge();
178 weakRefFreeSmall();
179 weakRefFreeLarge();
180 drcApolloTest();
184 #endif // _DEBUG