OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/heap/slots-buffer.h" |
| 6 |
| 7 #include "src/assembler.h" |
| 8 #include "src/heap/heap.h" |
| 9 #include "src/objects-inl.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 |
| 14 bool SlotsBuffer::IsTypedSlot(ObjectSlot slot) { |
| 15 return reinterpret_cast<uintptr_t>(slot) < NUMBER_OF_SLOT_TYPES; |
| 16 } |
| 17 |
| 18 |
| 19 bool SlotsBuffer::AddToSynchronized(SlotsBufferAllocator* allocator, |
| 20 SlotsBuffer** buffer_address, |
| 21 base::Mutex* buffer_mutex, SlotType type, |
| 22 Address addr, AdditionMode mode) { |
| 23 base::LockGuard<base::Mutex> lock_guard(buffer_mutex); |
| 24 return AddTo(allocator, buffer_address, type, addr, mode); |
| 25 } |
| 26 |
| 27 |
| 28 bool SlotsBuffer::AddTo(SlotsBufferAllocator* allocator, |
| 29 SlotsBuffer** buffer_address, SlotType type, |
| 30 Address addr, AdditionMode mode) { |
| 31 SlotsBuffer* buffer = *buffer_address; |
| 32 if (buffer == NULL || !buffer->HasSpaceForTypedSlot()) { |
| 33 if (mode == FAIL_ON_OVERFLOW && ChainLengthThresholdReached(buffer)) { |
| 34 allocator->DeallocateChain(buffer_address); |
| 35 return false; |
| 36 } |
| 37 buffer = allocator->AllocateBuffer(buffer); |
| 38 *buffer_address = buffer; |
| 39 } |
| 40 DCHECK(buffer->HasSpaceForTypedSlot()); |
| 41 buffer->Add(reinterpret_cast<ObjectSlot>(type)); |
| 42 buffer->Add(reinterpret_cast<ObjectSlot>(addr)); |
| 43 return true; |
| 44 } |
| 45 |
| 46 |
| 47 void SlotsBuffer::RemoveInvalidSlots(Heap* heap, SlotsBuffer* buffer) { |
| 48 // Remove entries by replacing them with an old-space slot containing a smi |
| 49 // that is located in an unmovable page. |
| 50 const ObjectSlot kRemovedEntry = HeapObject::RawField( |
| 51 heap->empty_fixed_array(), FixedArrayBase::kLengthOffset); |
| 52 DCHECK(Page::FromAddress(reinterpret_cast<Address>(kRemovedEntry)) |
| 53 ->NeverEvacuate()); |
| 54 |
| 55 while (buffer != NULL) { |
| 56 SlotsBuffer::ObjectSlot* slots = buffer->slots_; |
| 57 intptr_t slots_count = buffer->idx_; |
| 58 |
| 59 for (int slot_idx = 0; slot_idx < slots_count; ++slot_idx) { |
| 60 ObjectSlot slot = slots[slot_idx]; |
| 61 if (!IsTypedSlot(slot)) { |
| 62 Object* object = *slot; |
| 63 // Slots are invalid when they currently: |
| 64 // - do not point to a heap object (SMI) |
| 65 // - point to a heap object in new space |
| 66 // - are not within a live heap object on a valid pointer slot |
| 67 // - point to a heap object not on an evacuation candidate |
| 68 if (!object->IsHeapObject() || heap->InNewSpace(object) || |
| 69 !heap->mark_compact_collector()->IsSlotInLiveObject( |
| 70 reinterpret_cast<Address>(slot)) || |
| 71 !Page::FromAddress(reinterpret_cast<Address>(object)) |
| 72 ->IsEvacuationCandidate()) { |
| 73 // TODO(hpayer): Instead of replacing slots with kRemovedEntry we |
| 74 // could shrink the slots buffer in-place. |
| 75 slots[slot_idx] = kRemovedEntry; |
| 76 } |
| 77 } else { |
| 78 ++slot_idx; |
| 79 DCHECK(slot_idx < slots_count); |
| 80 } |
| 81 } |
| 82 buffer = buffer->next(); |
| 83 } |
| 84 } |
| 85 |
| 86 |
| 87 void SlotsBuffer::RemoveObjectSlots(Heap* heap, SlotsBuffer* buffer, |
| 88 Address start_slot, Address end_slot) { |
| 89 // Remove entries by replacing them with an old-space slot containing a smi |
| 90 // that is located in an unmovable page. |
| 91 const ObjectSlot kRemovedEntry = HeapObject::RawField( |
| 92 heap->empty_fixed_array(), FixedArrayBase::kLengthOffset); |
| 93 DCHECK(Page::FromAddress(reinterpret_cast<Address>(kRemovedEntry)) |
| 94 ->NeverEvacuate()); |
| 95 |
| 96 while (buffer != NULL) { |
| 97 SlotsBuffer::ObjectSlot* slots = buffer->slots_; |
| 98 intptr_t slots_count = buffer->idx_; |
| 99 bool is_typed_slot = false; |
| 100 |
| 101 for (int slot_idx = 0; slot_idx < slots_count; ++slot_idx) { |
| 102 ObjectSlot slot = slots[slot_idx]; |
| 103 if (!IsTypedSlot(slot)) { |
| 104 Address slot_address = reinterpret_cast<Address>(slot); |
| 105 if (slot_address >= start_slot && slot_address < end_slot) { |
| 106 // TODO(hpayer): Instead of replacing slots with kRemovedEntry we |
| 107 // could shrink the slots buffer in-place. |
| 108 slots[slot_idx] = kRemovedEntry; |
| 109 if (is_typed_slot) { |
| 110 slots[slot_idx - 1] = kRemovedEntry; |
| 111 } |
| 112 } |
| 113 is_typed_slot = false; |
| 114 } else { |
| 115 is_typed_slot = true; |
| 116 DCHECK(slot_idx < slots_count); |
| 117 } |
| 118 } |
| 119 buffer = buffer->next(); |
| 120 } |
| 121 } |
| 122 |
| 123 |
| 124 void SlotsBuffer::VerifySlots(Heap* heap, SlotsBuffer* buffer) { |
| 125 while (buffer != NULL) { |
| 126 SlotsBuffer::ObjectSlot* slots = buffer->slots_; |
| 127 intptr_t slots_count = buffer->idx_; |
| 128 |
| 129 for (int slot_idx = 0; slot_idx < slots_count; ++slot_idx) { |
| 130 ObjectSlot slot = slots[slot_idx]; |
| 131 if (!IsTypedSlot(slot)) { |
| 132 Object* object = *slot; |
| 133 if (object->IsHeapObject()) { |
| 134 HeapObject* heap_object = HeapObject::cast(object); |
| 135 CHECK(!heap->InNewSpace(object)); |
| 136 heap->mark_compact_collector()->VerifyIsSlotInLiveObject( |
| 137 reinterpret_cast<Address>(slot), heap_object); |
| 138 } |
| 139 } else { |
| 140 ++slot_idx; |
| 141 DCHECK(slot_idx < slots_count); |
| 142 } |
| 143 } |
| 144 buffer = buffer->next(); |
| 145 } |
| 146 } |
| 147 |
| 148 |
| 149 SlotsBuffer* SlotsBufferAllocator::AllocateBuffer(SlotsBuffer* next_buffer) { |
| 150 return new SlotsBuffer(next_buffer); |
| 151 } |
| 152 |
| 153 |
| 154 void SlotsBufferAllocator::DeallocateBuffer(SlotsBuffer* buffer) { |
| 155 delete buffer; |
| 156 } |
| 157 |
| 158 |
| 159 void SlotsBufferAllocator::DeallocateChain(SlotsBuffer** buffer_address) { |
| 160 SlotsBuffer* buffer = *buffer_address; |
| 161 while (buffer != NULL) { |
| 162 SlotsBuffer* next_buffer = buffer->next(); |
| 163 DeallocateBuffer(buffer); |
| 164 buffer = next_buffer; |
| 165 } |
| 166 *buffer_address = NULL; |
| 167 } |
| 168 |
| 169 } // namespace internal |
| 170 } // namespace v8 |
OLD | NEW |