Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/heap/PersistentNode.h" | 5 #include "platform/heap/PersistentNode.h" |
| 6 | 6 |
| 7 #include "platform/heap/Handle.h" | 7 #include "platform/heap/Handle.h" |
| 8 | 8 |
| 9 namespace blink { | 9 namespace blink { |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 ASSERT(node->isUnused()); | 42 ASSERT(node->isUnused()); |
| 43 } | 43 } |
| 44 slots->m_next = m_slots; | 44 slots->m_next = m_slots; |
| 45 m_slots = slots; | 45 m_slots = slots; |
| 46 } | 46 } |
| 47 | 47 |
| 48 // This function traces all PersistentNodes. If we encounter | 48 // This function traces all PersistentNodes. If we encounter |
| 49 // a PersistentNodeSlot that contains only freed PersistentNodes, | 49 // a PersistentNodeSlot that contains only freed PersistentNodes, |
| 50 // we delete the PersistentNodeSlot. This function rebuilds the free | 50 // we delete the PersistentNodeSlot. This function rebuilds the free |
| 51 // list of PersistentNodes. | 51 // list of PersistentNodes. |
| 52 void PersistentRegion::tracePersistentNodes(Visitor* visitor) | 52 void PersistentRegion::tracePersistentNodes(Visitor* visitor, ShouldTraceCallbac k shouldTrace) |
| 53 { | 53 { |
| 54 m_freeListHead = nullptr; | 54 m_freeListHead = nullptr; |
| 55 int persistentCount = 0; | 55 int persistentCount = 0; |
| 56 PersistentNodeSlots** prevNext = &m_slots; | 56 PersistentNodeSlots** prevNext = &m_slots; |
| 57 PersistentNodeSlots* slots = m_slots; | 57 PersistentNodeSlots* slots = m_slots; |
| 58 while (slots) { | 58 while (slots) { |
| 59 PersistentNode* freeListNext = nullptr; | 59 PersistentNode* freeListNext = nullptr; |
| 60 PersistentNode* freeListLast = nullptr; | 60 PersistentNode* freeListLast = nullptr; |
| 61 int freeCount = 0; | 61 int freeCount = 0; |
| 62 for (int i = 0; i < PersistentNodeSlots::slotCount; ++i) { | 62 for (int i = 0; i < PersistentNodeSlots::slotCount; ++i) { |
| 63 PersistentNode* node = &slots->m_slot[i]; | 63 PersistentNode* node = &slots->m_slot[i]; |
| 64 if (node->isUnused()) { | 64 if (node->isUnused()) { |
| 65 if (!freeListNext) | 65 if (!freeListNext) |
| 66 freeListLast = node; | 66 freeListLast = node; |
| 67 node->setFreeListNext(freeListNext); | 67 node->setFreeListNext(freeListNext); |
| 68 freeListNext = node; | 68 freeListNext = node; |
| 69 ++freeCount; | 69 ++freeCount; |
| 70 } else { | 70 } else { |
| 71 if (shouldTrace && !shouldTrace(visitor, node)) | |
|
haraken
2016/02/29 11:17:45
Nit: Or you can implement PersistentRegion::should
keishi
2016/03/02 06:01:03
Done.
| |
| 72 continue; | |
| 71 node->tracePersistentNode(visitor); | 73 node->tracePersistentNode(visitor); |
| 72 ++persistentCount; | 74 ++persistentCount; |
| 73 } | 75 } |
| 74 } | 76 } |
| 75 if (freeCount == PersistentNodeSlots::slotCount) { | 77 if (freeCount == PersistentNodeSlots::slotCount) { |
| 76 PersistentNodeSlots* deadSlots = slots; | 78 PersistentNodeSlots* deadSlots = slots; |
| 77 *prevNext = slots->m_next; | 79 *prevNext = slots->m_next; |
| 78 slots = slots->m_next; | 80 slots = slots->m_next; |
| 79 delete deadSlots; | 81 delete deadSlots; |
| 80 } else { | 82 } else { |
| 81 if (freeListLast) { | 83 if (freeListLast) { |
| 82 ASSERT(freeListNext); | 84 ASSERT(freeListNext); |
| 83 ASSERT(!freeListLast->freeListNext()); | 85 ASSERT(!freeListLast->freeListNext()); |
| 84 freeListLast->setFreeListNext(m_freeListHead); | 86 freeListLast->setFreeListNext(m_freeListHead); |
| 85 m_freeListHead = freeListNext; | 87 m_freeListHead = freeListNext; |
| 86 } | 88 } |
| 87 prevNext = &slots->m_next; | 89 prevNext = &slots->m_next; |
| 88 slots = slots->m_next; | 90 slots = slots->m_next; |
| 89 } | 91 } |
| 90 } | 92 } |
| 91 ASSERT(persistentCount == m_persistentCount); | |
|
haraken
2016/02/29 11:17:45
Can we keep this assert by incrementing persistent
keishi
2016/03/02 06:01:03
Done.
| |
| 92 } | 93 } |
| 93 | 94 |
| 94 namespace { | 95 namespace { |
| 95 class GCObject final : public GarbageCollected<GCObject> { | 96 class GCObject final : public GarbageCollected<GCObject> { |
| 96 public: | 97 public: |
| 97 DEFINE_INLINE_TRACE() { } | 98 DEFINE_INLINE_TRACE() { } |
| 98 }; | 99 }; |
| 99 } | 100 } |
| 100 | 101 |
| 102 bool CrossThreadPersistentRegion::shouldTracePersistentNode(Visitor* visitor, Pe rsistentNode* node) | |
| 103 { | |
| 104 CrossThreadPersistent<GCObject>* persistent = reinterpret_cast<CrossThreadPe rsistent<GCObject>*>(node->self()); | |
| 105 ASSERT(persistent); | |
| 106 Address rawObject = reinterpret_cast<Address>(persistent->get()); | |
| 107 if (!rawObject) | |
| 108 return false; | |
| 109 return visitor->visitorScope()->heap().threads().contains(ThreadState::fromO bject(rawObject)); | |
| 110 } | |
| 111 | |
| 101 void CrossThreadPersistentRegion::prepareForThreadStateTermination(ThreadState* threadState) | 112 void CrossThreadPersistentRegion::prepareForThreadStateTermination(ThreadState* threadState) |
| 102 { | 113 { |
| 103 // For heaps belonging to a thread that's detaching, any cross-thread persis tents | 114 // For heaps belonging to a thread that's detaching, any cross-thread persis tents |
| 104 // pointing into them needs to be disabled. Do that by clearing out the unde rlying | 115 // pointing into them needs to be disabled. Do that by clearing out the unde rlying |
| 105 // heap reference. | 116 // heap reference. |
| 106 MutexLocker lock(m_mutex); | 117 MutexLocker lock(m_mutex); |
| 107 | 118 |
| 108 // TODO(sof): consider ways of reducing overhead. (e.g., tracking number of active | 119 // TODO(sof): consider ways of reducing overhead. (e.g., tracking number of active |
| 109 // CrossThreadPersistent<>s pointing into the heaps of each ThreadState and use that | 120 // CrossThreadPersistent<>s pointing into the heaps of each ThreadState and use that |
| 110 // count to bail out early.) | 121 // count to bail out early.) |
| 111 PersistentNodeSlots* slots = m_persistentRegion->m_slots; | 122 PersistentNodeSlots* slots = m_persistentRegion->m_slots; |
| 112 while (slots) { | 123 while (slots) { |
| 113 for (int i = 0; i < PersistentNodeSlots::slotCount; ++i) { | 124 for (int i = 0; i < PersistentNodeSlots::slotCount; ++i) { |
| 114 if (slots->m_slot[i].isUnused()) | 125 if (slots->m_slot[i].isUnused()) |
| 115 continue; | 126 continue; |
| 116 | 127 |
| 117 // 'self' is in use, containing the cross-thread persistent wrapper object. | 128 // 'self' is in use, containing the cross-thread persistent wrapper object. |
| 118 CrossThreadPersistent<GCObject>* persistent = reinterpret_cast<Cross ThreadPersistent<GCObject>*>(slots->m_slot[i].self()); | 129 CrossThreadPersistent<GCObject>* persistent = reinterpret_cast<Cross ThreadPersistent<GCObject>*>(slots->m_slot[i].self()); |
| 119 ASSERT(persistent); | 130 ASSERT(persistent); |
| 120 void* rawObject = persistent->atomicGet(); | 131 void* rawObject = persistent->atomicGet(); |
| 121 if (!rawObject) | 132 if (!rawObject) |
| 122 continue; | 133 continue; |
| 123 BasePage* page = pageFromObject(rawObject); | 134 BasePage* page = pageFromObject(rawObject); |
| 124 ASSERT(page); | 135 ASSERT(page); |
| 125 // The main thread will upon detach just mark its heap pages as orph aned, | 136 // The main thread will upon detach just mark its heap pages as orph aned, |
| 126 // but not invalidate its CrossThreadPersistent<>s. | 137 // but not invalidate its CrossThreadPersistent<>s. |
| 127 if (page->orphaned()) | 138 if (page->orphaned()) |
| 128 continue; | 139 continue; |
| 129 if (page->heap()->threadState() == threadState) | 140 if (page->arena()->threadState() == threadState) |
| 130 persistent->clear(); | 141 persistent->clear(); |
| 131 } | 142 } |
| 132 slots = slots->m_next; | 143 slots = slots->m_next; |
| 133 } | 144 } |
| 134 } | 145 } |
| 135 | 146 |
| 136 } // namespace blink | 147 } // namespace blink |
| OLD | NEW |