| 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 30 matching lines...) Expand all Loading... |
| 41 // Do not use handles for the hash table, it would make entries strong. | 41 // Do not use handles for the hash table, it would make entries strong. |
| 42 Object* table_obj = ObjectHashTable::Allocate(1)->ToObjectChecked(); | 42 Object* table_obj = ObjectHashTable::Allocate(1)->ToObjectChecked(); |
| 43 ObjectHashTable* table = ObjectHashTable::cast(table_obj); | 43 ObjectHashTable* table = ObjectHashTable::cast(table_obj); |
| 44 weakmap->set_table(table); | 44 weakmap->set_table(table); |
| 45 weakmap->set_next(Smi::FromInt(0)); | 45 weakmap->set_next(Smi::FromInt(0)); |
| 46 return weakmap; | 46 return weakmap; |
| 47 } | 47 } |
| 48 | 48 |
| 49 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap, | 49 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap, |
| 50 Handle<JSObject> key, | 50 Handle<JSObject> key, |
| 51 int value) { | 51 Handle<Object> value) { |
| 52 Handle<ObjectHashTable> table = PutIntoObjectHashTable( | 52 Handle<ObjectHashTable> table = PutIntoObjectHashTable( |
| 53 Handle<ObjectHashTable>(ObjectHashTable::cast(weakmap->table())), | 53 Handle<ObjectHashTable>(ObjectHashTable::cast(weakmap->table())), |
| 54 Handle<JSObject>(JSObject::cast(*key)), | 54 Handle<JSObject>(JSObject::cast(*key)), |
| 55 Handle<Smi>(Smi::FromInt(value))); | 55 value); |
| 56 weakmap->set_table(*table); | 56 weakmap->set_table(*table); |
| 57 } | 57 } |
| 58 | 58 |
| 59 static int NumberOfWeakCalls = 0; | 59 static int NumberOfWeakCalls = 0; |
| 60 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { | 60 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { |
| 61 ASSERT(id == reinterpret_cast<void*>(1234)); | 61 ASSERT(id == reinterpret_cast<void*>(1234)); |
| 62 NumberOfWeakCalls++; | 62 NumberOfWeakCalls++; |
| 63 handle.Dispose(); | 63 handle.Dispose(); |
| 64 } | 64 } |
| 65 | 65 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 76 v8::HandleScope scope; | 76 v8::HandleScope scope; |
| 77 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 77 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 78 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); | 78 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); |
| 79 key = global_handles->Create(*object); | 79 key = global_handles->Create(*object); |
| 80 } | 80 } |
| 81 CHECK(!global_handles->IsWeak(key.location())); | 81 CHECK(!global_handles->IsWeak(key.location())); |
| 82 | 82 |
| 83 // Put entry into weak map. | 83 // Put entry into weak map. |
| 84 { | 84 { |
| 85 v8::HandleScope scope; | 85 v8::HandleScope scope; |
| 86 PutIntoWeakMap(weakmap, Handle<JSObject>(JSObject::cast(*key)), 23); | 86 PutIntoWeakMap(weakmap, |
| 87 Handle<JSObject>(JSObject::cast(*key)), |
| 88 Handle<Smi>(Smi::FromInt(23))); |
| 87 } | 89 } |
| 88 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 90 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 89 | 91 |
| 90 // Force a full GC. | 92 // Force a full GC. |
| 91 HEAP->CollectAllGarbage(false); | 93 HEAP->CollectAllGarbage(false); |
| 92 CHECK_EQ(0, NumberOfWeakCalls); | 94 CHECK_EQ(0, NumberOfWeakCalls); |
| 93 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 95 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 94 CHECK_EQ( | 96 CHECK_EQ( |
| 95 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 97 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 96 | 98 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 126 | 128 |
| 127 // Check initial capacity. | 129 // Check initial capacity. |
| 128 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 130 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 129 | 131 |
| 130 // Fill up weak map to trigger capacity change. | 132 // Fill up weak map to trigger capacity change. |
| 131 { | 133 { |
| 132 v8::HandleScope scope; | 134 v8::HandleScope scope; |
| 133 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 135 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 134 for (int i = 0; i < 32; i++) { | 136 for (int i = 0; i < 32; i++) { |
| 135 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); | 137 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); |
| 136 PutIntoWeakMap(weakmap, object, i); | 138 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(i))); |
| 137 } | 139 } |
| 138 } | 140 } |
| 139 | 141 |
| 140 // Check increased capacity. | 142 // Check increased capacity. |
| 141 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); | 143 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 142 | 144 |
| 143 // Force a full GC. | 145 // Force a full GC. |
| 144 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 146 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 145 CHECK_EQ( | 147 CHECK_EQ( |
| 146 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 148 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 147 HEAP->CollectAllGarbage(false); | 149 HEAP->CollectAllGarbage(false); |
| 148 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 150 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 149 CHECK_EQ( | 151 CHECK_EQ( |
| 150 32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 152 32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 151 | 153 |
| 152 // Check shrunk capacity. | 154 // Check shrunk capacity. |
| 153 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 155 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 154 } | 156 } |
| 157 |
| 158 |
| 159 // Test that weak map values on an evacuation candidate which are not reachable |
| 160 // by other paths are correctly recorded in in the slots buffer. |
| 161 TEST(Regress2060) { |
| 162 FLAG_always_compact = true; |
| 163 LocalContext context; |
| 164 v8::HandleScope scope; |
| 165 Handle<JSFunction> function = |
| 166 FACTORY->NewFunction(FACTORY->function_symbol(), FACTORY->null_value()); |
| 167 Handle<JSObject> key = FACTORY->NewJSObject(function); |
| 168 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); |
| 169 |
| 170 // Start second old-space page so that values land on evacuation candidate. |
| 171 Page* first_page = HEAP->old_pointer_space()->anchor()->next_page(); |
| 172 FACTORY->NewFixedArray(900 * KB / kPointerSize, TENURED); |
| 173 |
| 174 // Fill up weak map with values on an evacuation candidate. |
| 175 { |
| 176 v8::HandleScope scope; |
| 177 for (int i = 0; i < 32; i++) { |
| 178 Handle<JSObject> object = FACTORY->NewJSObject(function, TENURED); |
| 179 CHECK(!HEAP->InNewSpace(object->address())); |
| 180 CHECK(!first_page->Contains(object->address())); |
| 181 PutIntoWeakMap(weakmap, key, object); |
| 182 printf("o%d = %p (!= %p)\n", i, (void*)*object, (void*)first_page); |
| 183 } |
| 184 } |
| 185 |
| 186 // Force compacting garbage collection. |
| 187 CHECK(FLAG_always_compact); |
| 188 HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 189 } |
| OLD | NEW |