ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | |
| 32 | #include "global-handles.h" |
| 33 | #include "snapshot.h" |
| 34 | #include "top.h" |
| 35 | #include "cctest.h" |
| 36 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 37 | using namespace v8::internal; |
| 38 | |
| 39 | static v8::Persistent<v8::Context> env; |
| 40 | |
| 41 | static void InitializeVM() { |
| 42 | if (env.IsEmpty()) env = v8::Context::New(); |
| 43 | v8::HandleScope scope; |
| 44 | env->Enter(); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | TEST(MarkingStack) { |
| 49 | int mem_size = 20 * kPointerSize; |
| 50 | byte* mem = NewArray<byte>(20*kPointerSize); |
| 51 | Address low = reinterpret_cast<Address>(mem); |
| 52 | Address high = low + mem_size; |
| 53 | MarkingStack s; |
| 54 | s.Initialize(low, high); |
| 55 | |
| 56 | Address address = NULL; |
| 57 | while (!s.is_full()) { |
| 58 | s.Push(HeapObject::FromAddress(address)); |
| 59 | address += kPointerSize; |
| 60 | } |
| 61 | |
| 62 | while (!s.is_empty()) { |
| 63 | Address value = s.Pop()->address(); |
| 64 | address -= kPointerSize; |
| 65 | CHECK_EQ(address, value); |
| 66 | } |
| 67 | |
| 68 | CHECK_EQ(NULL, address); |
| 69 | DeleteArray(mem); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | TEST(Promotion) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 74 | // This test requires compaction. If compaction is turned off, we |
| 75 | // skip the entire test. |
| 76 | if (FLAG_never_compact) return; |
| 77 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 78 | // Ensure that we get a compacting collection so that objects are promoted |
| 79 | // from new space. |
| 80 | FLAG_gc_global = true; |
| 81 | FLAG_always_compact = true; |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 82 | Heap::ConfigureHeap(2*256*KB, 4*MB, 4*MB); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 83 | |
| 84 | InitializeVM(); |
| 85 | |
| 86 | v8::HandleScope sc; |
| 87 | |
| 88 | // Allocate a fixed array in the new space. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 89 | int array_size = |
| 90 | (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) / |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 91 | (kPointerSize * 4); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 92 | Object* obj = Heap::AllocateFixedArray(array_size)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 93 | |
| 94 | Handle<FixedArray> array(FixedArray::cast(obj)); |
| 95 | |
| 96 | // Array should be in the new space. |
| 97 | CHECK(Heap::InSpace(*array, NEW_SPACE)); |
| 98 | |
| 99 | // Call the m-c collector, so array becomes an old object. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 100 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 101 | |
| 102 | // Array now sits in the old space |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 103 | CHECK(Heap::InSpace(*array, OLD_POINTER_SPACE)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
| 107 | TEST(NoPromotion) { |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 108 | Heap::ConfigureHeap(2*256*KB, 4*MB, 4*MB); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 109 | |
| 110 | // Test the situation that some objects in new space are promoted to |
| 111 | // the old space |
| 112 | InitializeVM(); |
| 113 | |
| 114 | v8::HandleScope sc; |
| 115 | |
| 116 | // Do a mark compact GC to shrink the heap. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 117 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 118 | |
| 119 | // Allocate a big Fixed array in the new space. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 120 | int size = (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) / |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 121 | kPointerSize; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 122 | Object* obj = Heap::AllocateFixedArray(size)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 123 | |
| 124 | Handle<FixedArray> array(FixedArray::cast(obj)); |
| 125 | |
| 126 | // Array still stays in the new space. |
| 127 | CHECK(Heap::InSpace(*array, NEW_SPACE)); |
| 128 | |
| 129 | // Allocate objects in the old space until out of memory. |
| 130 | FixedArray* host = *array; |
| 131 | while (true) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 132 | Object* obj; |
| 133 | { MaybeObject* maybe_obj = Heap::AllocateFixedArray(100, TENURED); |
| 134 | if (!maybe_obj->ToObject(&obj)) break; |
| 135 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 136 | |
| 137 | host->set(0, obj); |
| 138 | host = FixedArray::cast(obj); |
| 139 | } |
| 140 | |
| 141 | // Call mark compact GC, and it should pass. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 142 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 143 | |
| 144 | // array should not be promoted because the old space is full. |
| 145 | CHECK(Heap::InSpace(*array, NEW_SPACE)); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | TEST(MarkCompactCollector) { |
| 150 | InitializeVM(); |
| 151 | |
| 152 | v8::HandleScope sc; |
| 153 | // call mark-compact when heap is empty |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 154 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 155 | |
| 156 | // keep allocating garbage in new space until it fails |
| 157 | const int ARRAY_SIZE = 100; |
| 158 | Object* array; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 159 | MaybeObject* maybe_array; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 160 | do { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 161 | maybe_array = Heap::AllocateFixedArray(ARRAY_SIZE); |
| 162 | } while (maybe_array->ToObject(&array)); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 163 | Heap::CollectGarbage(NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 164 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 165 | array = Heap::AllocateFixedArray(ARRAY_SIZE)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 166 | |
| 167 | // keep allocating maps until it fails |
| 168 | Object* mapp; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 169 | MaybeObject* maybe_mapp; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 170 | do { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 171 | maybe_mapp = Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 172 | } while (maybe_mapp->ToObject(&mapp)); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 173 | Heap::CollectGarbage(MAP_SPACE); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 174 | mapp = Heap::AllocateMap(JS_OBJECT_TYPE, |
| 175 | JSObject::kHeaderSize)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 176 | |
| 177 | // allocate a garbage |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 178 | String* func_name = |
| 179 | String::cast(Heap::LookupAsciiSymbol("theFunction")->ToObjectChecked()); |
| 180 | SharedFunctionInfo* function_share = SharedFunctionInfo::cast( |
| 181 | Heap::AllocateSharedFunctionInfo(func_name)->ToObjectChecked()); |
| 182 | JSFunction* function = JSFunction::cast( |
| 183 | Heap::AllocateFunction(*Top::function_map(), |
| 184 | function_share, |
| 185 | Heap::undefined_value())->ToObjectChecked()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 186 | Map* initial_map = |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 187 | Map::cast(Heap::AllocateMap(JS_OBJECT_TYPE, |
| 188 | JSObject::kHeaderSize)->ToObjectChecked()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 189 | function->set_initial_map(initial_map); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 190 | Top::context()->global()->SetProperty(func_name, |
| 191 | function, |
| 192 | NONE)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 193 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 194 | JSObject* obj = |
| 195 | JSObject::cast(Heap::AllocateJSObject(function)->ToObjectChecked()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 196 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 197 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 198 | func_name = |
| 199 | String::cast(Heap::LookupAsciiSymbol("theFunction")->ToObjectChecked()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 200 | CHECK(Top::context()->global()->HasLocalProperty(func_name)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 201 | Object* func_value = |
| 202 | Top::context()->global()->GetProperty(func_name)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 203 | CHECK(func_value->IsJSFunction()); |
| 204 | function = JSFunction::cast(func_value); |
| 205 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 206 | obj = JSObject::cast(Heap::AllocateJSObject(function)->ToObjectChecked()); |
| 207 | String* obj_name = |
| 208 | String::cast(Heap::LookupAsciiSymbol("theObject")->ToObjectChecked()); |
| 209 | Top::context()->global()->SetProperty(obj_name, |
| 210 | obj, |
| 211 | NONE)->ToObjectChecked(); |
| 212 | String* prop_name = |
| 213 | String::cast(Heap::LookupAsciiSymbol("theSlot")->ToObjectChecked()); |
| 214 | obj->SetProperty(prop_name, Smi::FromInt(23), NONE)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 215 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 216 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 217 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 218 | obj_name = |
| 219 | String::cast(Heap::LookupAsciiSymbol("theObject")->ToObjectChecked()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 220 | CHECK(Top::context()->global()->HasLocalProperty(obj_name)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 221 | CHECK(Top::context()->global()-> |
| 222 | GetProperty(obj_name)->ToObjectChecked()->IsJSObject()); |
| 223 | obj = JSObject::cast( |
| 224 | Top::context()->global()->GetProperty(obj_name)->ToObjectChecked()); |
| 225 | prop_name = |
| 226 | String::cast(Heap::LookupAsciiSymbol("theSlot")->ToObjectChecked()); |
| 227 | CHECK(obj->GetProperty(prop_name)->ToObjectChecked() == Smi::FromInt(23)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 231 | static Handle<Map> CreateMap() { |
| 232 | return Factory::NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | TEST(MapCompact) { |
| 237 | FLAG_max_map_space_pages = 16; |
| 238 | InitializeVM(); |
| 239 | |
| 240 | { |
| 241 | v8::HandleScope sc; |
| 242 | // keep allocating maps while pointers are still encodable and thus |
| 243 | // mark compact is permitted. |
| 244 | Handle<JSObject> root = Factory::NewJSObjectFromMap(CreateMap()); |
| 245 | do { |
| 246 | Handle<Map> map = CreateMap(); |
| 247 | map->set_prototype(*root); |
| 248 | root = Factory::NewJSObjectFromMap(map); |
| 249 | } while (Heap::map_space()->MapPointersEncodable()); |
| 250 | } |
| 251 | // Now, as we don't have any handles to just allocated maps, we should |
| 252 | // be able to trigger map compaction. |
| 253 | // To give an additional chance to fail, try to force compaction which |
| 254 | // should be impossible right now. |
| 255 | Heap::CollectAllGarbage(true); |
| 256 | // And now map pointers should be encodable again. |
| 257 | CHECK(Heap::map_space()->MapPointersEncodable()); |
| 258 | } |
| 259 | |
| 260 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 261 | static int gc_starts = 0; |
| 262 | static int gc_ends = 0; |
| 263 | |
| 264 | static void GCPrologueCallbackFunc() { |
| 265 | CHECK(gc_starts == gc_ends); |
| 266 | gc_starts++; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | static void GCEpilogueCallbackFunc() { |
| 271 | CHECK(gc_starts == gc_ends + 1); |
| 272 | gc_ends++; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | TEST(GCCallback) { |
| 277 | InitializeVM(); |
| 278 | |
| 279 | Heap::SetGlobalGCPrologueCallback(&GCPrologueCallbackFunc); |
| 280 | Heap::SetGlobalGCEpilogueCallback(&GCEpilogueCallbackFunc); |
| 281 | |
| 282 | // Scavenge does not call GC callback functions. |
| 283 | Heap::PerformScavenge(); |
| 284 | |
| 285 | CHECK_EQ(0, gc_starts); |
| 286 | CHECK_EQ(gc_ends, gc_starts); |
| 287 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 288 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 289 | CHECK_EQ(1, gc_starts); |
| 290 | CHECK_EQ(gc_ends, gc_starts); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | static int NumberOfWeakCalls = 0; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 295 | static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 296 | NumberOfWeakCalls++; |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 297 | handle.Dispose(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | TEST(ObjectGroups) { |
| 301 | InitializeVM(); |
| 302 | |
| 303 | NumberOfWeakCalls = 0; |
| 304 | v8::HandleScope handle_scope; |
| 305 | |
| 306 | Handle<Object> g1s1 = |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 307 | GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 308 | Handle<Object> g1s2 = |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 309 | GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 310 | GlobalHandles::MakeWeak(g1s1.location(), |
| 311 | reinterpret_cast<void*>(1234), |
| 312 | &WeakPointerCallback); |
| 313 | GlobalHandles::MakeWeak(g1s2.location(), |
| 314 | reinterpret_cast<void*>(1234), |
| 315 | &WeakPointerCallback); |
| 316 | |
| 317 | Handle<Object> g2s1 = |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 318 | GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 319 | Handle<Object> g2s2 = |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 320 | GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 321 | GlobalHandles::MakeWeak(g2s1.location(), |
| 322 | reinterpret_cast<void*>(1234), |
| 323 | &WeakPointerCallback); |
| 324 | GlobalHandles::MakeWeak(g2s2.location(), |
| 325 | reinterpret_cast<void*>(1234), |
| 326 | &WeakPointerCallback); |
| 327 | |
| 328 | Handle<Object> root = GlobalHandles::Create(*g1s1); // make a root. |
| 329 | |
| 330 | // Connect group 1 and 2, make a cycle. |
| 331 | Handle<FixedArray>::cast(g1s2)->set(0, *g2s2); |
| 332 | Handle<FixedArray>::cast(g2s1)->set(0, *g1s1); |
| 333 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 334 | { |
| 335 | Object** g1_objects[] = { g1s1.location(), g1s2.location() }; |
| 336 | Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
| 337 | GlobalHandles::AddGroup(g1_objects, 2); |
| 338 | GlobalHandles::AddGroup(g2_objects, 2); |
| 339 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 340 | // Do a full GC |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 341 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 342 | |
| 343 | // All object should be alive. |
| 344 | CHECK_EQ(0, NumberOfWeakCalls); |
| 345 | |
| 346 | // Weaken the root. |
| 347 | GlobalHandles::MakeWeak(root.location(), |
| 348 | reinterpret_cast<void*>(1234), |
| 349 | &WeakPointerCallback); |
| 350 | |
| 351 | // Groups are deleted, rebuild groups. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 352 | { |
| 353 | Object** g1_objects[] = { g1s1.location(), g1s2.location() }; |
| 354 | Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
| 355 | GlobalHandles::AddGroup(g1_objects, 2); |
| 356 | GlobalHandles::AddGroup(g2_objects, 2); |
| 357 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 358 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 359 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 360 | |
| 361 | // All objects should be gone. 5 global handles in total. |
| 362 | CHECK_EQ(5, NumberOfWeakCalls); |
| 363 | } |