Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 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" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "cctest.h" |
| 35 | |
| 36 | using namespace v8::internal; |
| 37 | |
| 38 | static v8::Persistent<v8::Context> env; |
| 39 | |
| 40 | static void InitializeVM() { |
| 41 | if (env.IsEmpty()) env = v8::Context::New(); |
| 42 | v8::HandleScope scope; |
| 43 | env->Enter(); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | TEST(MarkingStack) { |
| 48 | int mem_size = 20 * kPointerSize; |
| 49 | byte* mem = NewArray<byte>(20*kPointerSize); |
| 50 | Address low = reinterpret_cast<Address>(mem); |
| 51 | Address high = low + mem_size; |
| 52 | MarkingStack s; |
| 53 | s.Initialize(low, high); |
| 54 | |
| 55 | Address address = NULL; |
| 56 | while (!s.is_full()) { |
| 57 | s.Push(HeapObject::FromAddress(address)); |
| 58 | address += kPointerSize; |
| 59 | } |
| 60 | |
| 61 | while (!s.is_empty()) { |
| 62 | Address value = s.Pop()->address(); |
| 63 | address -= kPointerSize; |
| 64 | CHECK_EQ(address, value); |
| 65 | } |
| 66 | |
| 67 | CHECK_EQ(NULL, address); |
| 68 | DeleteArray(mem); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | TEST(Promotion) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 73 | // This test requires compaction. If compaction is turned off, we |
| 74 | // skip the entire test. |
| 75 | if (FLAG_never_compact) return; |
| 76 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | // Ensure that we get a compacting collection so that objects are promoted |
| 78 | // from new space. |
| 79 | FLAG_gc_global = true; |
| 80 | FLAG_always_compact = true; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 81 | HEAP->ConfigureHeap(2*256*KB, 4*MB, 4*MB); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 82 | |
| 83 | InitializeVM(); |
| 84 | |
| 85 | v8::HandleScope sc; |
| 86 | |
| 87 | // Allocate a fixed array in the new space. |
| 88 | int array_size = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 89 | (HEAP->MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) / |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 90 | (kPointerSize * 4); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 91 | Object* obj = HEAP->AllocateFixedArray(array_size)->ToObjectChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | |
| 93 | Handle<FixedArray> array(FixedArray::cast(obj)); |
| 94 | |
| 95 | // Array should be in the new space. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 96 | CHECK(HEAP->InSpace(*array, NEW_SPACE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | |
| 98 | // Call the m-c collector, so array becomes an old object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 99 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | |
| 101 | // Array now sits in the old space |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 102 | CHECK(HEAP->InSpace(*array, OLD_POINTER_SPACE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
| 106 | TEST(NoPromotion) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 107 | HEAP->ConfigureHeap(2*256*KB, 4*MB, 4*MB); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | |
| 109 | // Test the situation that some objects in new space are promoted to |
| 110 | // the old space |
| 111 | InitializeVM(); |
| 112 | |
| 113 | v8::HandleScope sc; |
| 114 | |
| 115 | // Do a mark compact GC to shrink the heap. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 116 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | |
| 118 | // Allocate a big Fixed array in the new space. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 119 | int size = (HEAP->MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) / |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 120 | kPointerSize; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 121 | Object* obj = HEAP->AllocateFixedArray(size)->ToObjectChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | |
| 123 | Handle<FixedArray> array(FixedArray::cast(obj)); |
| 124 | |
| 125 | // Array still stays in the new space. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 126 | CHECK(HEAP->InSpace(*array, NEW_SPACE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 127 | |
| 128 | // Allocate objects in the old space until out of memory. |
| 129 | FixedArray* host = *array; |
| 130 | while (true) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 131 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 132 | { MaybeObject* maybe_obj = HEAP->AllocateFixedArray(100, TENURED); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 133 | if (!maybe_obj->ToObject(&obj)) break; |
| 134 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | |
| 136 | host->set(0, obj); |
| 137 | host = FixedArray::cast(obj); |
| 138 | } |
| 139 | |
| 140 | // Call mark compact GC, and it should pass. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 141 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 142 | |
| 143 | // array should not be promoted because the old space is full. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 144 | CHECK(HEAP->InSpace(*array, NEW_SPACE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
| 148 | TEST(MarkCompactCollector) { |
| 149 | InitializeVM(); |
| 150 | |
| 151 | v8::HandleScope sc; |
| 152 | // call mark-compact when heap is empty |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 153 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | |
| 155 | // keep allocating garbage in new space until it fails |
| 156 | const int ARRAY_SIZE = 100; |
| 157 | Object* array; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 158 | MaybeObject* maybe_array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 159 | do { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 160 | maybe_array = HEAP->AllocateFixedArray(ARRAY_SIZE); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 161 | } while (maybe_array->ToObject(&array)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 162 | HEAP->CollectGarbage(NEW_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 164 | array = HEAP->AllocateFixedArray(ARRAY_SIZE)->ToObjectChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | |
| 166 | // keep allocating maps until it fails |
| 167 | Object* mapp; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 168 | MaybeObject* maybe_mapp; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | do { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 170 | maybe_mapp = HEAP->AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 171 | } while (maybe_mapp->ToObject(&mapp)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 172 | HEAP->CollectGarbage(MAP_SPACE); |
| 173 | mapp = HEAP->AllocateMap(JS_OBJECT_TYPE, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 174 | JSObject::kHeaderSize)->ToObjectChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | |
| 176 | // allocate a garbage |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 177 | String* func_name = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 178 | String::cast(HEAP->LookupAsciiSymbol("theFunction")->ToObjectChecked()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 179 | SharedFunctionInfo* function_share = SharedFunctionInfo::cast( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 180 | HEAP->AllocateSharedFunctionInfo(func_name)->ToObjectChecked()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 181 | JSFunction* function = JSFunction::cast( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 182 | HEAP->AllocateFunction(*Isolate::Current()->function_map(), |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 183 | function_share, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 184 | HEAP->undefined_value())->ToObjectChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | Map* initial_map = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 186 | Map::cast(HEAP->AllocateMap(JS_OBJECT_TYPE, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 187 | JSObject::kHeaderSize)->ToObjectChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 188 | function->set_initial_map(initial_map); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 189 | Isolate::Current()->context()->global()->SetProperty( |
| 190 | func_name, function, NONE, kNonStrictMode)->ToObjectChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 191 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 192 | JSObject* obj = JSObject::cast( |
| 193 | HEAP->AllocateJSObject(function)->ToObjectChecked()); |
| 194 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 196 | func_name = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 197 | String::cast(HEAP->LookupAsciiSymbol("theFunction")->ToObjectChecked()); |
| 198 | CHECK(Isolate::Current()->context()->global()->HasLocalProperty(func_name)); |
| 199 | Object* func_value = Isolate::Current()->context()->global()-> |
| 200 | GetProperty(func_name)->ToObjectChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | CHECK(func_value->IsJSFunction()); |
| 202 | function = JSFunction::cast(func_value); |
| 203 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 204 | obj = JSObject::cast(HEAP->AllocateJSObject(function)->ToObjectChecked()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 205 | String* obj_name = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 206 | String::cast(HEAP->LookupAsciiSymbol("theObject")->ToObjectChecked()); |
| 207 | Isolate::Current()->context()->global()->SetProperty( |
| 208 | obj_name, obj, NONE, kNonStrictMode)->ToObjectChecked(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 209 | String* prop_name = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 210 | String::cast(HEAP->LookupAsciiSymbol("theSlot")->ToObjectChecked()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 211 | obj->SetProperty(prop_name, |
| 212 | Smi::FromInt(23), |
| 213 | NONE, |
| 214 | kNonStrictMode)->ToObjectChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 215 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 216 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 218 | obj_name = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 219 | String::cast(HEAP->LookupAsciiSymbol("theObject")->ToObjectChecked()); |
| 220 | CHECK(Isolate::Current()->context()->global()->HasLocalProperty(obj_name)); |
| 221 | CHECK(Isolate::Current()->context()->global()-> |
| 222 | GetProperty(obj_name)->ToObjectChecked()->IsJSObject()); |
| 223 | obj = JSObject::cast(Isolate::Current()->context()->global()-> |
| 224 | GetProperty(obj_name)->ToObjectChecked()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 225 | prop_name = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 226 | String::cast(HEAP->LookupAsciiSymbol("theSlot")->ToObjectChecked()); |
| 227 | CHECK(obj->GetProperty(prop_name) == Smi::FromInt(23)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 231 | static Handle<Map> CreateMap() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 232 | return FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 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. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 244 | Handle<JSObject> root = FACTORY->NewJSObjectFromMap(CreateMap()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 245 | do { |
| 246 | Handle<Map> map = CreateMap(); |
| 247 | map->set_prototype(*root); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 248 | root = FACTORY->NewJSObjectFromMap(map); |
| 249 | } while (HEAP->map_space()->MapPointersEncodable()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 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. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 255 | HEAP->CollectAllGarbage(true); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 256 | // And now map pointers should be encodable again. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 257 | CHECK(HEAP->map_space()->MapPointersEncodable()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 279 | HEAP->SetGlobalGCPrologueCallback(&GCPrologueCallbackFunc); |
| 280 | HEAP->SetGlobalGCEpilogueCallback(&GCEpilogueCallbackFunc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 281 | |
| 282 | // Scavenge does not call GC callback functions. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 283 | HEAP->PerformScavenge(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 284 | |
| 285 | CHECK_EQ(0, gc_starts); |
| 286 | CHECK_EQ(gc_ends, gc_starts); |
| 287 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 288 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 289 | CHECK_EQ(1, gc_starts); |
| 290 | CHECK_EQ(gc_ends, gc_starts); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | static int NumberOfWeakCalls = 0; |
| 295 | static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 296 | ASSERT(id == reinterpret_cast<void*>(1234)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 297 | NumberOfWeakCalls++; |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 298 | handle.Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | TEST(ObjectGroups) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 302 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 303 | InitializeVM(); |
| 304 | |
| 305 | NumberOfWeakCalls = 0; |
| 306 | v8::HandleScope handle_scope; |
| 307 | |
| 308 | Handle<Object> g1s1 = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 309 | global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | Handle<Object> g1s2 = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 311 | global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 312 | Handle<Object> g1c1 = |
| 313 | global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 314 | global_handles->MakeWeak(g1s1.location(), |
| 315 | reinterpret_cast<void*>(1234), |
| 316 | &WeakPointerCallback); |
| 317 | global_handles->MakeWeak(g1s2.location(), |
| 318 | reinterpret_cast<void*>(1234), |
| 319 | &WeakPointerCallback); |
| 320 | global_handles->MakeWeak(g1c1.location(), |
| 321 | reinterpret_cast<void*>(1234), |
| 322 | &WeakPointerCallback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | |
| 324 | Handle<Object> g2s1 = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 325 | global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 326 | Handle<Object> g2s2 = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 327 | global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 328 | Handle<Object> g2c1 = |
| 329 | global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 330 | global_handles->MakeWeak(g2s1.location(), |
| 331 | reinterpret_cast<void*>(1234), |
| 332 | &WeakPointerCallback); |
| 333 | global_handles->MakeWeak(g2s2.location(), |
| 334 | reinterpret_cast<void*>(1234), |
| 335 | &WeakPointerCallback); |
| 336 | global_handles->MakeWeak(g2c1.location(), |
| 337 | reinterpret_cast<void*>(1234), |
| 338 | &WeakPointerCallback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 339 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 340 | Handle<Object> root = global_handles->Create(*g1s1); // make a root. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 341 | |
| 342 | // Connect group 1 and 2, make a cycle. |
| 343 | Handle<FixedArray>::cast(g1s2)->set(0, *g2s2); |
| 344 | Handle<FixedArray>::cast(g2s1)->set(0, *g1s1); |
| 345 | |
| 346 | { |
| 347 | Object** g1_objects[] = { g1s1.location(), g1s2.location() }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 348 | Object** g1_children[] = { g1c1.location() }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 349 | Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 350 | Object** g2_children[] = { g2c1.location() }; |
| 351 | global_handles->AddObjectGroup(g1_objects, 2, NULL); |
| 352 | global_handles->AddImplicitReferences(HeapObject::cast(*g1s1), |
| 353 | g1_children, 1); |
| 354 | global_handles->AddObjectGroup(g2_objects, 2, NULL); |
| 355 | global_handles->AddImplicitReferences(HeapObject::cast(*g2s2), |
| 356 | g2_children, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 357 | } |
| 358 | // Do a full GC |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 359 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | |
| 361 | // All object should be alive. |
| 362 | CHECK_EQ(0, NumberOfWeakCalls); |
| 363 | |
| 364 | // Weaken the root. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 365 | global_handles->MakeWeak(root.location(), |
| 366 | reinterpret_cast<void*>(1234), |
| 367 | &WeakPointerCallback); |
| 368 | // But make children strong roots---all the objects (except for children) |
| 369 | // should be collectable now. |
| 370 | global_handles->ClearWeakness(g1c1.location()); |
| 371 | global_handles->ClearWeakness(g2c1.location()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | |
| 373 | // Groups are deleted, rebuild groups. |
| 374 | { |
| 375 | Object** g1_objects[] = { g1s1.location(), g1s2.location() }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 376 | Object** g1_children[] = { g1c1.location() }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 377 | Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 378 | Object** g2_children[] = { g2c1.location() }; |
| 379 | global_handles->AddObjectGroup(g1_objects, 2, NULL); |
| 380 | global_handles->AddImplicitReferences(HeapObject::cast(*g1s1), |
| 381 | g1_children, 1); |
| 382 | global_handles->AddObjectGroup(g2_objects, 2, NULL); |
| 383 | global_handles->AddImplicitReferences(HeapObject::cast(*g2s2), |
| 384 | g2_children, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 387 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 388 | |
| 389 | // All objects should be gone. 5 global handles in total. |
| 390 | CHECK_EQ(5, NumberOfWeakCalls); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame^] | 391 | |
| 392 | // And now make children weak again and collect them. |
| 393 | global_handles->MakeWeak(g1c1.location(), |
| 394 | reinterpret_cast<void*>(1234), |
| 395 | &WeakPointerCallback); |
| 396 | global_handles->MakeWeak(g2c1.location(), |
| 397 | reinterpret_cast<void*>(1234), |
| 398 | &WeakPointerCallback); |
| 399 | |
| 400 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
| 401 | CHECK_EQ(7, NumberOfWeakCalls); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 402 | } |