| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 Heap::ConfigureHeap(2*256*KB, 4*MB); | 78 Heap::ConfigureHeap(2*256*KB, 4*MB); |
| 79 | 79 |
| 80 InitializeVM(); | 80 InitializeVM(); |
| 81 | 81 |
| 82 v8::HandleScope sc; | 82 v8::HandleScope sc; |
| 83 | 83 |
| 84 // Allocate a fixed array in the new space. | 84 // Allocate a fixed array in the new space. |
| 85 int array_size = | 85 int array_size = |
| 86 (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) / | 86 (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) / |
| 87 (kPointerSize * 4); | 87 (kPointerSize * 4); |
| 88 Object* obj = Heap::AllocateFixedArray(array_size); | 88 Object* obj = Heap::AllocateFixedArray(array_size)->ToObjectChecked(); |
| 89 CHECK(!obj->IsFailure()); | |
| 90 | 89 |
| 91 Handle<FixedArray> array(FixedArray::cast(obj)); | 90 Handle<FixedArray> array(FixedArray::cast(obj)); |
| 92 | 91 |
| 93 // Array should be in the new space. | 92 // Array should be in the new space. |
| 94 CHECK(Heap::InSpace(*array, NEW_SPACE)); | 93 CHECK(Heap::InSpace(*array, NEW_SPACE)); |
| 95 | 94 |
| 96 // Call the m-c collector, so array becomes an old object. | 95 // Call the m-c collector, so array becomes an old object. |
| 97 Heap::CollectGarbage(OLD_POINTER_SPACE); | 96 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 98 | 97 |
| 99 // Array now sits in the old space | 98 // Array now sits in the old space |
| 100 CHECK(Heap::InSpace(*array, OLD_POINTER_SPACE)); | 99 CHECK(Heap::InSpace(*array, OLD_POINTER_SPACE)); |
| 101 } | 100 } |
| 102 | 101 |
| 103 | 102 |
| 104 TEST(NoPromotion) { | 103 TEST(NoPromotion) { |
| 105 Heap::ConfigureHeap(2*256*KB, 4*MB); | 104 Heap::ConfigureHeap(2*256*KB, 4*MB); |
| 106 | 105 |
| 107 // Test the situation that some objects in new space are promoted to | 106 // Test the situation that some objects in new space are promoted to |
| 108 // the old space | 107 // the old space |
| 109 InitializeVM(); | 108 InitializeVM(); |
| 110 | 109 |
| 111 v8::HandleScope sc; | 110 v8::HandleScope sc; |
| 112 | 111 |
| 113 // Do a mark compact GC to shrink the heap. | 112 // Do a mark compact GC to shrink the heap. |
| 114 Heap::CollectGarbage(OLD_POINTER_SPACE); | 113 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 115 | 114 |
| 116 // Allocate a big Fixed array in the new space. | 115 // Allocate a big Fixed array in the new space. |
| 117 int size = (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) / | 116 int size = (Heap::MaxObjectSizeInPagedSpace() - FixedArray::kHeaderSize) / |
| 118 kPointerSize; | 117 kPointerSize; |
| 119 Object* obj = Heap::AllocateFixedArray(size); | 118 Object* obj = Heap::AllocateFixedArray(size)->ToObjectChecked(); |
| 120 | 119 |
| 121 Handle<FixedArray> array(FixedArray::cast(obj)); | 120 Handle<FixedArray> array(FixedArray::cast(obj)); |
| 122 | 121 |
| 123 // Array still stays in the new space. | 122 // Array still stays in the new space. |
| 124 CHECK(Heap::InSpace(*array, NEW_SPACE)); | 123 CHECK(Heap::InSpace(*array, NEW_SPACE)); |
| 125 | 124 |
| 126 // Allocate objects in the old space until out of memory. | 125 // Allocate objects in the old space until out of memory. |
| 127 FixedArray* host = *array; | 126 FixedArray* host = *array; |
| 128 while (true) { | 127 while (true) { |
| 129 Object* obj = Heap::AllocateFixedArray(100, TENURED); | 128 Object* obj; |
| 130 if (obj->IsFailure()) break; | 129 { MaybeObject* maybe_obj = Heap::AllocateFixedArray(100, TENURED); |
| 130 if (!maybe_obj->ToObject(&obj)) break; |
| 131 } |
| 131 | 132 |
| 132 host->set(0, obj); | 133 host->set(0, obj); |
| 133 host = FixedArray::cast(obj); | 134 host = FixedArray::cast(obj); |
| 134 } | 135 } |
| 135 | 136 |
| 136 // Call mark compact GC, and it should pass. | 137 // Call mark compact GC, and it should pass. |
| 137 Heap::CollectGarbage(OLD_POINTER_SPACE); | 138 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 138 | 139 |
| 139 // array should not be promoted because the old space is full. | 140 // array should not be promoted because the old space is full. |
| 140 CHECK(Heap::InSpace(*array, NEW_SPACE)); | 141 CHECK(Heap::InSpace(*array, NEW_SPACE)); |
| 141 } | 142 } |
| 142 | 143 |
| 143 | 144 |
| 144 TEST(MarkCompactCollector) { | 145 TEST(MarkCompactCollector) { |
| 145 InitializeVM(); | 146 InitializeVM(); |
| 146 | 147 |
| 147 v8::HandleScope sc; | 148 v8::HandleScope sc; |
| 148 // call mark-compact when heap is empty | 149 // call mark-compact when heap is empty |
| 149 Heap::CollectGarbage(OLD_POINTER_SPACE); | 150 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 150 | 151 |
| 151 // keep allocating garbage in new space until it fails | 152 // keep allocating garbage in new space until it fails |
| 152 const int ARRAY_SIZE = 100; | 153 const int ARRAY_SIZE = 100; |
| 153 Object* array; | 154 Object* array; |
| 155 MaybeObject* maybe_array; |
| 154 do { | 156 do { |
| 155 array = Heap::AllocateFixedArray(ARRAY_SIZE); | 157 maybe_array = Heap::AllocateFixedArray(ARRAY_SIZE); |
| 156 } while (!array->IsFailure()); | 158 } while (maybe_array->ToObject(&array)); |
| 157 Heap::CollectGarbage(NEW_SPACE); | 159 Heap::CollectGarbage(NEW_SPACE); |
| 158 | 160 |
| 159 array = Heap::AllocateFixedArray(ARRAY_SIZE); | 161 array = Heap::AllocateFixedArray(ARRAY_SIZE)->ToObjectChecked(); |
| 160 CHECK(!array->IsFailure()); | |
| 161 | 162 |
| 162 // keep allocating maps until it fails | 163 // keep allocating maps until it fails |
| 163 Object* mapp; | 164 Object* mapp; |
| 165 MaybeObject* maybe_mapp; |
| 164 do { | 166 do { |
| 165 mapp = Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 167 maybe_mapp = Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 166 } while (!mapp->IsFailure()); | 168 } while (maybe_mapp->ToObject(&mapp)); |
| 167 Heap::CollectGarbage(MAP_SPACE); | 169 Heap::CollectGarbage(MAP_SPACE); |
| 168 mapp = Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 170 mapp = Heap::AllocateMap(JS_OBJECT_TYPE, |
| 169 CHECK(!mapp->IsFailure()); | 171 JSObject::kHeaderSize)->ToObjectChecked(); |
| 170 | 172 |
| 171 // allocate a garbage | 173 // allocate a garbage |
| 172 String* func_name = String::cast(Heap::LookupAsciiSymbol("theFunction")); | 174 String* func_name = |
| 173 SharedFunctionInfo* function_share = | 175 String::cast(Heap::LookupAsciiSymbol("theFunction")->ToObjectChecked()); |
| 174 SharedFunctionInfo::cast(Heap::AllocateSharedFunctionInfo(func_name)); | 176 SharedFunctionInfo* function_share = SharedFunctionInfo::cast( |
| 175 JSFunction* function = | 177 Heap::AllocateSharedFunctionInfo(func_name)->ToObjectChecked()); |
| 176 JSFunction::cast(Heap::AllocateFunction(*Top::function_map(), | 178 JSFunction* function = JSFunction::cast( |
| 177 function_share, | 179 Heap::AllocateFunction(*Top::function_map(), |
| 178 Heap::undefined_value())); | 180 function_share, |
| 181 Heap::undefined_value())->ToObjectChecked()); |
| 179 Map* initial_map = | 182 Map* initial_map = |
| 180 Map::cast(Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize)); | 183 Map::cast(Heap::AllocateMap(JS_OBJECT_TYPE, |
| 184 JSObject::kHeaderSize)->ToObjectChecked()); |
| 181 function->set_initial_map(initial_map); | 185 function->set_initial_map(initial_map); |
| 182 Top::context()->global()->SetProperty(func_name, function, NONE); | 186 Top::context()->global()->SetProperty(func_name, |
| 187 function, |
| 188 NONE)->ToObjectChecked(); |
| 183 | 189 |
| 184 JSObject* obj = JSObject::cast(Heap::AllocateJSObject(function)); | 190 JSObject* obj = |
| 191 JSObject::cast(Heap::AllocateJSObject(function)->ToObjectChecked()); |
| 185 Heap::CollectGarbage(OLD_POINTER_SPACE); | 192 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 186 | 193 |
| 187 func_name = String::cast(Heap::LookupAsciiSymbol("theFunction")); | 194 func_name = |
| 195 String::cast(Heap::LookupAsciiSymbol("theFunction")->ToObjectChecked()); |
| 188 CHECK(Top::context()->global()->HasLocalProperty(func_name)); | 196 CHECK(Top::context()->global()->HasLocalProperty(func_name)); |
| 189 Object* func_value = Top::context()->global()->GetProperty(func_name); | 197 Object* func_value = |
| 198 Top::context()->global()->GetProperty(func_name)->ToObjectChecked(); |
| 190 CHECK(func_value->IsJSFunction()); | 199 CHECK(func_value->IsJSFunction()); |
| 191 function = JSFunction::cast(func_value); | 200 function = JSFunction::cast(func_value); |
| 192 | 201 |
| 193 obj = JSObject::cast(Heap::AllocateJSObject(function)); | 202 obj = JSObject::cast(Heap::AllocateJSObject(function)->ToObjectChecked()); |
| 194 String* obj_name = String::cast(Heap::LookupAsciiSymbol("theObject")); | 203 String* obj_name = |
| 195 Top::context()->global()->SetProperty(obj_name, obj, NONE); | 204 String::cast(Heap::LookupAsciiSymbol("theObject")->ToObjectChecked()); |
| 196 String* prop_name = String::cast(Heap::LookupAsciiSymbol("theSlot")); | 205 Top::context()->global()->SetProperty(obj_name, |
| 197 obj->SetProperty(prop_name, Smi::FromInt(23), NONE); | 206 obj, |
| 207 NONE)->ToObjectChecked(); |
| 208 String* prop_name = |
| 209 String::cast(Heap::LookupAsciiSymbol("theSlot")->ToObjectChecked()); |
| 210 obj->SetProperty(prop_name, Smi::FromInt(23), NONE)->ToObjectChecked(); |
| 198 | 211 |
| 199 Heap::CollectGarbage(OLD_POINTER_SPACE); | 212 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 200 | 213 |
| 201 obj_name = String::cast(Heap::LookupAsciiSymbol("theObject")); | 214 obj_name = |
| 215 String::cast(Heap::LookupAsciiSymbol("theObject")->ToObjectChecked()); |
| 202 CHECK(Top::context()->global()->HasLocalProperty(obj_name)); | 216 CHECK(Top::context()->global()->HasLocalProperty(obj_name)); |
| 203 CHECK(Top::context()->global()->GetProperty(obj_name)->IsJSObject()); | 217 CHECK(Top::context()->global()-> |
| 204 obj = JSObject::cast(Top::context()->global()->GetProperty(obj_name)); | 218 GetProperty(obj_name)->ToObjectChecked()->IsJSObject()); |
| 205 prop_name = String::cast(Heap::LookupAsciiSymbol("theSlot")); | 219 obj = JSObject::cast( |
| 206 CHECK(obj->GetProperty(prop_name) == Smi::FromInt(23)); | 220 Top::context()->global()->GetProperty(obj_name)->ToObjectChecked()); |
| 221 prop_name = |
| 222 String::cast(Heap::LookupAsciiSymbol("theSlot")->ToObjectChecked()); |
| 223 CHECK(obj->GetProperty(prop_name)->ToObjectChecked() == Smi::FromInt(23)); |
| 207 } | 224 } |
| 208 | 225 |
| 209 | 226 |
| 210 static Handle<Map> CreateMap() { | 227 static Handle<Map> CreateMap() { |
| 211 return Factory::NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 228 return Factory::NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 212 } | 229 } |
| 213 | 230 |
| 214 | 231 |
| 215 TEST(MapCompact) { | 232 TEST(MapCompact) { |
| 216 FLAG_max_map_space_pages = 16; | 233 FLAG_max_map_space_pages = 16; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 handle.Dispose(); | 293 handle.Dispose(); |
| 277 } | 294 } |
| 278 | 295 |
| 279 TEST(ObjectGroups) { | 296 TEST(ObjectGroups) { |
| 280 InitializeVM(); | 297 InitializeVM(); |
| 281 | 298 |
| 282 NumberOfWeakCalls = 0; | 299 NumberOfWeakCalls = 0; |
| 283 v8::HandleScope handle_scope; | 300 v8::HandleScope handle_scope; |
| 284 | 301 |
| 285 Handle<Object> g1s1 = | 302 Handle<Object> g1s1 = |
| 286 GlobalHandles::Create(Heap::AllocateFixedArray(1)); | 303 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
| 287 Handle<Object> g1s2 = | 304 Handle<Object> g1s2 = |
| 288 GlobalHandles::Create(Heap::AllocateFixedArray(1)); | 305 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
| 289 GlobalHandles::MakeWeak(g1s1.location(), | 306 GlobalHandles::MakeWeak(g1s1.location(), |
| 290 reinterpret_cast<void*>(1234), | 307 reinterpret_cast<void*>(1234), |
| 291 &WeakPointerCallback); | 308 &WeakPointerCallback); |
| 292 GlobalHandles::MakeWeak(g1s2.location(), | 309 GlobalHandles::MakeWeak(g1s2.location(), |
| 293 reinterpret_cast<void*>(1234), | 310 reinterpret_cast<void*>(1234), |
| 294 &WeakPointerCallback); | 311 &WeakPointerCallback); |
| 295 | 312 |
| 296 Handle<Object> g2s1 = | 313 Handle<Object> g2s1 = |
| 297 GlobalHandles::Create(Heap::AllocateFixedArray(1)); | 314 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
| 298 Handle<Object> g2s2 = | 315 Handle<Object> g2s2 = |
| 299 GlobalHandles::Create(Heap::AllocateFixedArray(1)); | 316 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
| 300 GlobalHandles::MakeWeak(g2s1.location(), | 317 GlobalHandles::MakeWeak(g2s1.location(), |
| 301 reinterpret_cast<void*>(1234), | 318 reinterpret_cast<void*>(1234), |
| 302 &WeakPointerCallback); | 319 &WeakPointerCallback); |
| 303 GlobalHandles::MakeWeak(g2s2.location(), | 320 GlobalHandles::MakeWeak(g2s2.location(), |
| 304 reinterpret_cast<void*>(1234), | 321 reinterpret_cast<void*>(1234), |
| 305 &WeakPointerCallback); | 322 &WeakPointerCallback); |
| 306 | 323 |
| 307 Handle<Object> root = GlobalHandles::Create(*g1s1); // make a root. | 324 Handle<Object> root = GlobalHandles::Create(*g1s1); // make a root. |
| 308 | 325 |
| 309 // Connect group 1 and 2, make a cycle. | 326 // Connect group 1 and 2, make a cycle. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 333 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; | 350 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
| 334 GlobalHandles::AddGroup(g1_objects, 2); | 351 GlobalHandles::AddGroup(g1_objects, 2); |
| 335 GlobalHandles::AddGroup(g2_objects, 2); | 352 GlobalHandles::AddGroup(g2_objects, 2); |
| 336 } | 353 } |
| 337 | 354 |
| 338 Heap::CollectGarbage(OLD_POINTER_SPACE); | 355 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 339 | 356 |
| 340 // All objects should be gone. 5 global handles in total. | 357 // All objects should be gone. 5 global handles in total. |
| 341 CHECK_EQ(5, NumberOfWeakCalls); | 358 CHECK_EQ(5, NumberOfWeakCalls); |
| 342 } | 359 } |
| OLD | NEW |