| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 NumberOfWeakCalls++; | 69 NumberOfWeakCalls++; |
| 70 p->first->Reset(); | 70 p->first->Reset(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 | 73 |
| 74 TEST(Weakness) { | 74 TEST(Weakness) { |
| 75 FLAG_incremental_marking = false; | 75 FLAG_incremental_marking = false; |
| 76 LocalContext context; | 76 LocalContext context; |
| 77 Isolate* isolate = GetIsolateFrom(&context); | 77 Isolate* isolate = GetIsolateFrom(&context); |
| 78 Factory* factory = isolate->factory(); | 78 Factory* factory = isolate->factory(); |
| 79 Heap* heap = isolate->heap(); | |
| 80 HandleScope scope(isolate); | 79 HandleScope scope(isolate); |
| 81 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); | 80 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); |
| 82 GlobalHandles* global_handles = isolate->global_handles(); | 81 GlobalHandles* global_handles = isolate->global_handles(); |
| 83 | 82 |
| 84 // Keep global reference to the key. | 83 // Keep global reference to the key. |
| 85 Handle<Object> key; | 84 Handle<Object> key; |
| 86 { | 85 { |
| 87 HandleScope scope(isolate); | 86 HandleScope scope(isolate); |
| 88 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 87 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 89 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 88 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
| 90 key = global_handles->Create(*object); | 89 key = global_handles->Create(*object); |
| 91 } | 90 } |
| 92 CHECK(!global_handles->IsWeak(key.location())); | 91 CHECK(!global_handles->IsWeak(key.location())); |
| 93 | 92 |
| 94 // Put two chained entries into weak map. | 93 // Put two chained entries into weak map. |
| 95 { | 94 { |
| 96 HandleScope scope(isolate); | 95 HandleScope scope(isolate); |
| 97 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 96 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 98 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 97 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
| 99 Handle<Smi> smi(Smi::FromInt(23), isolate); | 98 Handle<Smi> smi(Smi::FromInt(23), isolate); |
| 100 int32_t hash = Object::GetOrCreateHash(isolate, key)->value(); | 99 int32_t hash = Object::GetOrCreateHash(isolate, key)->value(); |
| 101 JSWeakCollection::Set(weakmap, key, object, hash); | 100 JSWeakCollection::Set(weakmap, key, object, hash); |
| 102 int32_t object_hash = Object::GetOrCreateHash(isolate, object)->value(); | 101 int32_t object_hash = Object::GetOrCreateHash(isolate, object)->value(); |
| 103 JSWeakCollection::Set(weakmap, object, smi, object_hash); | 102 JSWeakCollection::Set(weakmap, object, smi, object_hash); |
| 104 } | 103 } |
| 105 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 104 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 106 | 105 |
| 107 // Force a full GC. | 106 // Force a full GC. |
| 108 heap->CollectAllGarbage(false); | 107 CcTest::CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 109 CHECK_EQ(0, NumberOfWeakCalls); | 108 CHECK_EQ(0, NumberOfWeakCalls); |
| 110 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 109 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 111 CHECK_EQ( | 110 CHECK_EQ( |
| 112 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 111 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 113 | 112 |
| 114 // Make the global reference to the key weak. | 113 // Make the global reference to the key weak. |
| 115 { | 114 { |
| 116 HandleScope scope(isolate); | 115 HandleScope scope(isolate); |
| 117 std::pair<Handle<Object>*, int> handle_and_id(&key, 1234); | 116 std::pair<Handle<Object>*, int> handle_and_id(&key, 1234); |
| 118 GlobalHandles::MakeWeak( | 117 GlobalHandles::MakeWeak( |
| 119 key.location(), reinterpret_cast<void*>(&handle_and_id), | 118 key.location(), reinterpret_cast<void*>(&handle_and_id), |
| 120 &WeakPointerCallback, v8::WeakCallbackType::kParameter); | 119 &WeakPointerCallback, v8::WeakCallbackType::kParameter); |
| 121 } | 120 } |
| 122 CHECK(global_handles->IsWeak(key.location())); | 121 CHECK(global_handles->IsWeak(key.location())); |
| 123 | 122 |
| 124 heap->CollectAllGarbage(false); | 123 CcTest::CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 125 CHECK_EQ(1, NumberOfWeakCalls); | 124 CHECK_EQ(1, NumberOfWeakCalls); |
| 126 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 125 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 127 CHECK_EQ(2, | 126 CHECK_EQ(2, |
| 128 ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 127 ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 129 } | 128 } |
| 130 | 129 |
| 131 | 130 |
| 132 TEST(Shrinking) { | 131 TEST(Shrinking) { |
| 133 LocalContext context; | 132 LocalContext context; |
| 134 Isolate* isolate = GetIsolateFrom(&context); | 133 Isolate* isolate = GetIsolateFrom(&context); |
| 135 Factory* factory = isolate->factory(); | 134 Factory* factory = isolate->factory(); |
| 136 Heap* heap = isolate->heap(); | |
| 137 HandleScope scope(isolate); | 135 HandleScope scope(isolate); |
| 138 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); | 136 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); |
| 139 | 137 |
| 140 // Check initial capacity. | 138 // Check initial capacity. |
| 141 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 139 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 142 | 140 |
| 143 // Fill up weak map to trigger capacity change. | 141 // Fill up weak map to trigger capacity change. |
| 144 { | 142 { |
| 145 HandleScope scope(isolate); | 143 HandleScope scope(isolate); |
| 146 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 144 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 147 for (int i = 0; i < 32; i++) { | 145 for (int i = 0; i < 32; i++) { |
| 148 Handle<JSObject> object = factory->NewJSObjectFromMap(map); | 146 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
| 149 Handle<Smi> smi(Smi::FromInt(i), isolate); | 147 Handle<Smi> smi(Smi::FromInt(i), isolate); |
| 150 int32_t object_hash = Object::GetOrCreateHash(isolate, object)->value(); | 148 int32_t object_hash = Object::GetOrCreateHash(isolate, object)->value(); |
| 151 JSWeakCollection::Set(weakmap, object, smi, object_hash); | 149 JSWeakCollection::Set(weakmap, object, smi, object_hash); |
| 152 } | 150 } |
| 153 } | 151 } |
| 154 | 152 |
| 155 // Check increased capacity. | 153 // Check increased capacity. |
| 156 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); | 154 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 157 | 155 |
| 158 // Force a full GC. | 156 // Force a full GC. |
| 159 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 157 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 160 CHECK_EQ( | 158 CHECK_EQ( |
| 161 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 159 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 162 heap->CollectAllGarbage(false); | 160 CcTest::CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 163 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 161 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 164 CHECK_EQ( | 162 CHECK_EQ( |
| 165 32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 163 32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 166 | 164 |
| 167 // Check shrunk capacity. | 165 // Check shrunk capacity. |
| 168 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 166 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 169 } | 167 } |
| 170 | 168 |
| 171 | 169 |
| 172 // Test that weak map values on an evacuation candidate which are not reachable | 170 // Test that weak map values on an evacuation candidate which are not reachable |
| (...skipping 22 matching lines...) Expand all Loading... |
| 195 Handle<JSObject> object = factory->NewJSObject(function, TENURED); | 193 Handle<JSObject> object = factory->NewJSObject(function, TENURED); |
| 196 CHECK(!heap->InNewSpace(*object)); | 194 CHECK(!heap->InNewSpace(*object)); |
| 197 CHECK(!first_page->Contains(object->address())); | 195 CHECK(!first_page->Contains(object->address())); |
| 198 int32_t hash = Object::GetOrCreateHash(isolate, key)->value(); | 196 int32_t hash = Object::GetOrCreateHash(isolate, key)->value(); |
| 199 JSWeakCollection::Set(weakmap, key, object, hash); | 197 JSWeakCollection::Set(weakmap, key, object, hash); |
| 200 } | 198 } |
| 201 } | 199 } |
| 202 | 200 |
| 203 // Force compacting garbage collection. | 201 // Force compacting garbage collection. |
| 204 CHECK(FLAG_always_compact); | 202 CHECK(FLAG_always_compact); |
| 205 heap->CollectAllGarbage(); | 203 CcTest::CollectAllGarbage(i::Heap::kFinalizeIncrementalMarkingMask); |
| 206 } | 204 } |
| 207 | 205 |
| 208 | 206 |
| 209 // Test that weak map keys on an evacuation candidate which are reachable by | 207 // Test that weak map keys on an evacuation candidate which are reachable by |
| 210 // other strong paths are correctly recorded in the slots buffer. | 208 // other strong paths are correctly recorded in the slots buffer. |
| 211 TEST(Regress2060b) { | 209 TEST(Regress2060b) { |
| 212 if (i::FLAG_never_compact) return; | 210 if (i::FLAG_never_compact) return; |
| 213 FLAG_always_compact = true; | 211 FLAG_always_compact = true; |
| 214 #ifdef VERIFY_HEAP | 212 #ifdef VERIFY_HEAP |
| 215 FLAG_verify_heap = true; | 213 FLAG_verify_heap = true; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 237 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); | 235 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); |
| 238 for (int i = 0; i < 32; i++) { | 236 for (int i = 0; i < 32; i++) { |
| 239 Handle<Smi> smi(Smi::FromInt(i), isolate); | 237 Handle<Smi> smi(Smi::FromInt(i), isolate); |
| 240 int32_t hash = Object::GetOrCreateHash(isolate, keys[i])->value(); | 238 int32_t hash = Object::GetOrCreateHash(isolate, keys[i])->value(); |
| 241 JSWeakCollection::Set(weakmap, keys[i], smi, hash); | 239 JSWeakCollection::Set(weakmap, keys[i], smi, hash); |
| 242 } | 240 } |
| 243 | 241 |
| 244 // Force compacting garbage collection. The subsequent collections are used | 242 // Force compacting garbage collection. The subsequent collections are used |
| 245 // to verify that key references were actually updated. | 243 // to verify that key references were actually updated. |
| 246 CHECK(FLAG_always_compact); | 244 CHECK(FLAG_always_compact); |
| 247 heap->CollectAllGarbage(); | 245 CcTest::CollectAllGarbage(i::Heap::kFinalizeIncrementalMarkingMask); |
| 248 heap->CollectAllGarbage(); | 246 CcTest::CollectAllGarbage(i::Heap::kFinalizeIncrementalMarkingMask); |
| 249 heap->CollectAllGarbage(); | 247 CcTest::CollectAllGarbage(i::Heap::kFinalizeIncrementalMarkingMask); |
| 250 } | 248 } |
| 251 | 249 |
| 252 | 250 |
| 253 TEST(Regress399527) { | 251 TEST(Regress399527) { |
| 254 CcTest::InitializeVM(); | 252 CcTest::InitializeVM(); |
| 255 v8::HandleScope scope(CcTest::isolate()); | 253 v8::HandleScope scope(CcTest::isolate()); |
| 256 Isolate* isolate = CcTest::i_isolate(); | 254 Isolate* isolate = CcTest::i_isolate(); |
| 257 Heap* heap = isolate->heap(); | 255 Heap* heap = isolate->heap(); |
| 258 { | 256 { |
| 259 HandleScope scope(isolate); | 257 HandleScope scope(isolate); |
| 260 AllocateJSWeakMap(isolate); | 258 AllocateJSWeakMap(isolate); |
| 261 heap::SimulateIncrementalMarking(heap); | 259 heap::SimulateIncrementalMarking(heap); |
| 262 } | 260 } |
| 263 // The weak map is marked black here but leaving the handle scope will make | 261 // The weak map is marked black here but leaving the handle scope will make |
| 264 // the object unreachable. Aborting incremental marking will clear all the | 262 // the object unreachable. Aborting incremental marking will clear all the |
| 265 // marking bits which makes the weak map garbage. | 263 // marking bits which makes the weak map garbage. |
| 266 heap->CollectAllGarbage(); | 264 CcTest::CollectAllGarbage(i::Heap::kFinalizeIncrementalMarkingMask); |
| 267 } | 265 } |
| OLD | NEW |