ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_MARK_COMPACT_H_ |
| 29 | #define V8_MARK_COMPACT_H_ |
| 30 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 31 | #include "compiler-intrinsics.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 32 | #include "spaces.h" |
| 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | // Callback function, returns whether an object is alive. The heap size |
| 38 | // of the object is returned in size. It optionally updates the offset |
| 39 | // to the first live object in the page (only used for old and map objects). |
| 40 | typedef bool (*IsAliveFunction)(HeapObject* obj, int* size, int* offset); |
| 41 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 42 | // Forward declarations. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 43 | class CodeFlusher; |
| 44 | class GCTracer; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 45 | class MarkingVisitor; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 46 | class RootMarkingVisitor; |
| 47 | |
| 48 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 49 | class Marking { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 50 | public: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 51 | explicit Marking(Heap* heap) |
| 52 | : heap_(heap) { |
| 53 | } |
| 54 | |
| 55 | static inline MarkBit MarkBitFrom(Address addr); |
| 56 | |
| 57 | static inline MarkBit MarkBitFrom(HeapObject* obj) { |
| 58 | return MarkBitFrom(reinterpret_cast<Address>(obj)); |
| 59 | } |
| 60 | |
| 61 | // Impossible markbits: 01 |
| 62 | static const char* kImpossibleBitPattern; |
| 63 | static inline bool IsImpossible(MarkBit mark_bit) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 64 | return !mark_bit.Get() && mark_bit.Next().Get(); |
| 65 | } |
| 66 | |
| 67 | // Black markbits: 10 - this is required by the sweeper. |
| 68 | static const char* kBlackBitPattern; |
| 69 | static inline bool IsBlack(MarkBit mark_bit) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 70 | return mark_bit.Get() && !mark_bit.Next().Get(); |
| 71 | } |
| 72 | |
| 73 | // White markbits: 00 - this is required by the mark bit clearer. |
| 74 | static const char* kWhiteBitPattern; |
| 75 | static inline bool IsWhite(MarkBit mark_bit) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 76 | return !mark_bit.Get(); |
| 77 | } |
| 78 | |
| 79 | // Grey markbits: 11 |
| 80 | static const char* kGreyBitPattern; |
| 81 | static inline bool IsGrey(MarkBit mark_bit) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 82 | return mark_bit.Get() && mark_bit.Next().Get(); |
| 83 | } |
| 84 | |
| 85 | static inline void MarkBlack(MarkBit mark_bit) { |
| 86 | mark_bit.Set(); |
| 87 | mark_bit.Next().Clear(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static inline void BlackToGrey(MarkBit markbit) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 91 | markbit.Next().Set(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static inline void WhiteToGrey(MarkBit markbit) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 95 | markbit.Set(); |
| 96 | markbit.Next().Set(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static inline void GreyToBlack(MarkBit markbit) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 100 | markbit.Next().Clear(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static inline void BlackToGrey(HeapObject* obj) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 104 | BlackToGrey(MarkBitFrom(obj)); |
| 105 | } |
| 106 | |
| 107 | static inline void AnyToGrey(MarkBit markbit) { |
| 108 | markbit.Set(); |
| 109 | markbit.Next().Set(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Returns true if the the object whose mark is transferred is marked black. |
| 113 | bool TransferMark(Address old_start, Address new_start); |
| 114 | |
| 115 | #ifdef DEBUG |
| 116 | enum ObjectColor { |
| 117 | BLACK_OBJECT, |
| 118 | WHITE_OBJECT, |
| 119 | GREY_OBJECT, |
| 120 | IMPOSSIBLE_COLOR |
| 121 | }; |
| 122 | |
| 123 | static const char* ColorName(ObjectColor color) { |
| 124 | switch (color) { |
| 125 | case BLACK_OBJECT: return "black"; |
| 126 | case WHITE_OBJECT: return "white"; |
| 127 | case GREY_OBJECT: return "grey"; |
| 128 | case IMPOSSIBLE_COLOR: return "impossible"; |
| 129 | } |
| 130 | return "error"; |
| 131 | } |
| 132 | |
| 133 | static ObjectColor Color(HeapObject* obj) { |
| 134 | return Color(Marking::MarkBitFrom(obj)); |
| 135 | } |
| 136 | |
| 137 | static ObjectColor Color(MarkBit mark_bit) { |
| 138 | if (IsBlack(mark_bit)) return BLACK_OBJECT; |
| 139 | if (IsWhite(mark_bit)) return WHITE_OBJECT; |
| 140 | if (IsGrey(mark_bit)) return GREY_OBJECT; |
| 141 | UNREACHABLE(); |
| 142 | return IMPOSSIBLE_COLOR; |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | // Returns true if the transferred color is black. |
| 147 | INLINE(static bool TransferColor(HeapObject* from, |
| 148 | HeapObject* to)) { |
| 149 | MarkBit from_mark_bit = MarkBitFrom(from); |
| 150 | MarkBit to_mark_bit = MarkBitFrom(to); |
| 151 | bool is_black = false; |
| 152 | if (from_mark_bit.Get()) { |
| 153 | to_mark_bit.Set(); |
| 154 | is_black = true; // Looks black so far. |
| 155 | } |
| 156 | if (from_mark_bit.Next().Get()) { |
| 157 | to_mark_bit.Next().Set(); |
| 158 | is_black = false; // Was actually gray. |
| 159 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 160 | return is_black; |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | Heap* heap_; |
| 165 | }; |
| 166 | |
| 167 | // ---------------------------------------------------------------------------- |
| 168 | // Marking deque for tracing live objects. |
| 169 | |
| 170 | class MarkingDeque { |
| 171 | public: |
| 172 | MarkingDeque() |
| 173 | : array_(NULL), top_(0), bottom_(0), mask_(0), overflowed_(false) { } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 174 | |
| 175 | void Initialize(Address low, Address high) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 176 | HeapObject** obj_low = reinterpret_cast<HeapObject**>(low); |
| 177 | HeapObject** obj_high = reinterpret_cast<HeapObject**>(high); |
| 178 | array_ = obj_low; |
| 179 | mask_ = RoundDownToPowerOf2(static_cast<int>(obj_high - obj_low)) - 1; |
| 180 | top_ = bottom_ = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 181 | overflowed_ = false; |
| 182 | } |
| 183 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 184 | inline bool IsFull() { return ((top_ + 1) & mask_) == bottom_; } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 185 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 186 | inline bool IsEmpty() { return top_ == bottom_; } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 187 | |
| 188 | bool overflowed() const { return overflowed_; } |
| 189 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 190 | void ClearOverflowed() { overflowed_ = false; } |
| 191 | |
| 192 | void SetOverflowed() { overflowed_ = true; } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 193 | |
| 194 | // Push the (marked) object on the marking stack if there is room, |
| 195 | // otherwise mark the object as overflowed and wait for a rescan of the |
| 196 | // heap. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 197 | inline void PushBlack(HeapObject* object) { |
| 198 | ASSERT(object->IsHeapObject()); |
| 199 | if (IsFull()) { |
| 200 | Marking::BlackToGrey(object); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 201 | MemoryChunk::IncrementLiveBytesFromGC(object->address(), -object->Size()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 202 | SetOverflowed(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 203 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 204 | array_[top_] = object; |
| 205 | top_ = ((top_ + 1) & mask_); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 209 | inline void PushGrey(HeapObject* object) { |
| 210 | ASSERT(object->IsHeapObject()); |
| 211 | if (IsFull()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 212 | SetOverflowed(); |
| 213 | } else { |
| 214 | array_[top_] = object; |
| 215 | top_ = ((top_ + 1) & mask_); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | inline HeapObject* Pop() { |
| 220 | ASSERT(!IsEmpty()); |
| 221 | top_ = ((top_ - 1) & mask_); |
| 222 | HeapObject* object = array_[top_]; |
| 223 | ASSERT(object->IsHeapObject()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 224 | return object; |
| 225 | } |
| 226 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 227 | inline void UnshiftGrey(HeapObject* object) { |
| 228 | ASSERT(object->IsHeapObject()); |
| 229 | if (IsFull()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 230 | SetOverflowed(); |
| 231 | } else { |
| 232 | bottom_ = ((bottom_ - 1) & mask_); |
| 233 | array_[bottom_] = object; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | HeapObject** array() { return array_; } |
| 238 | int bottom() { return bottom_; } |
| 239 | int top() { return top_; } |
| 240 | int mask() { return mask_; } |
| 241 | void set_top(int top) { top_ = top; } |
| 242 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 243 | private: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 244 | HeapObject** array_; |
| 245 | // array_[(top - 1) & mask_] is the top element in the deque. The Deque is |
| 246 | // empty when top_ == bottom_. It is full when top_ + 1 == bottom |
| 247 | // (mod mask + 1). |
| 248 | int top_; |
| 249 | int bottom_; |
| 250 | int mask_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 251 | bool overflowed_; |
| 252 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 253 | DISALLOW_COPY_AND_ASSIGN(MarkingDeque); |
| 254 | }; |
| 255 | |
| 256 | |
| 257 | class SlotsBufferAllocator { |
| 258 | public: |
| 259 | SlotsBuffer* AllocateBuffer(SlotsBuffer* next_buffer); |
| 260 | void DeallocateBuffer(SlotsBuffer* buffer); |
| 261 | |
| 262 | void DeallocateChain(SlotsBuffer** buffer_address); |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | // SlotsBuffer records a sequence of slots that has to be updated |
| 267 | // after live objects were relocated from evacuation candidates. |
| 268 | // All slots are either untyped or typed: |
| 269 | // - Untyped slots are expected to contain a tagged object pointer. |
| 270 | // They are recorded by an address. |
| 271 | // - Typed slots are expected to contain an encoded pointer to a heap |
| 272 | // object where the way of encoding depends on the type of the slot. |
| 273 | // They are recorded as a pair (SlotType, slot address). |
| 274 | // We assume that zero-page is never mapped this allows us to distinguish |
| 275 | // untyped slots from typed slots during iteration by a simple comparison: |
| 276 | // if element of slots buffer is less than NUMBER_OF_SLOT_TYPES then it |
| 277 | // is the first element of typed slot's pair. |
| 278 | class SlotsBuffer { |
| 279 | public: |
| 280 | typedef Object** ObjectSlot; |
| 281 | |
| 282 | explicit SlotsBuffer(SlotsBuffer* next_buffer) |
| 283 | : idx_(0), chain_length_(1), next_(next_buffer) { |
| 284 | if (next_ != NULL) { |
| 285 | chain_length_ = next_->chain_length_ + 1; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | ~SlotsBuffer() { |
| 290 | } |
| 291 | |
| 292 | void Add(ObjectSlot slot) { |
| 293 | ASSERT(0 <= idx_ && idx_ < kNumberOfElements); |
| 294 | slots_[idx_++] = slot; |
| 295 | } |
| 296 | |
| 297 | enum SlotType { |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 298 | EMBEDDED_OBJECT_SLOT, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 299 | RELOCATED_CODE_OBJECT, |
| 300 | CODE_TARGET_SLOT, |
| 301 | CODE_ENTRY_SLOT, |
| 302 | DEBUG_TARGET_SLOT, |
| 303 | JS_RETURN_SLOT, |
| 304 | NUMBER_OF_SLOT_TYPES |
| 305 | }; |
| 306 | |
| 307 | void UpdateSlots(Heap* heap); |
| 308 | |
| 309 | void UpdateSlotsWithFilter(Heap* heap); |
| 310 | |
| 311 | SlotsBuffer* next() { return next_; } |
| 312 | |
| 313 | static int SizeOfChain(SlotsBuffer* buffer) { |
| 314 | if (buffer == NULL) return 0; |
| 315 | return static_cast<int>(buffer->idx_ + |
| 316 | (buffer->chain_length_ - 1) * kNumberOfElements); |
| 317 | } |
| 318 | |
| 319 | inline bool IsFull() { |
| 320 | return idx_ == kNumberOfElements; |
| 321 | } |
| 322 | |
| 323 | inline bool HasSpaceForTypedSlot() { |
| 324 | return idx_ < kNumberOfElements - 1; |
| 325 | } |
| 326 | |
| 327 | static void UpdateSlotsRecordedIn(Heap* heap, |
| 328 | SlotsBuffer* buffer, |
| 329 | bool code_slots_filtering_required) { |
| 330 | while (buffer != NULL) { |
| 331 | if (code_slots_filtering_required) { |
| 332 | buffer->UpdateSlotsWithFilter(heap); |
| 333 | } else { |
| 334 | buffer->UpdateSlots(heap); |
| 335 | } |
| 336 | buffer = buffer->next(); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | enum AdditionMode { |
| 341 | FAIL_ON_OVERFLOW, |
| 342 | IGNORE_OVERFLOW |
| 343 | }; |
| 344 | |
| 345 | static bool ChainLengthThresholdReached(SlotsBuffer* buffer) { |
| 346 | return buffer != NULL && buffer->chain_length_ >= kChainLengthThreshold; |
| 347 | } |
| 348 | |
| 349 | static bool AddTo(SlotsBufferAllocator* allocator, |
| 350 | SlotsBuffer** buffer_address, |
| 351 | ObjectSlot slot, |
| 352 | AdditionMode mode) { |
| 353 | SlotsBuffer* buffer = *buffer_address; |
| 354 | if (buffer == NULL || buffer->IsFull()) { |
| 355 | if (mode == FAIL_ON_OVERFLOW && ChainLengthThresholdReached(buffer)) { |
| 356 | allocator->DeallocateChain(buffer_address); |
| 357 | return false; |
| 358 | } |
| 359 | buffer = allocator->AllocateBuffer(buffer); |
| 360 | *buffer_address = buffer; |
| 361 | } |
| 362 | buffer->Add(slot); |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | static bool IsTypedSlot(ObjectSlot slot); |
| 367 | |
| 368 | static bool AddTo(SlotsBufferAllocator* allocator, |
| 369 | SlotsBuffer** buffer_address, |
| 370 | SlotType type, |
| 371 | Address addr, |
| 372 | AdditionMode mode); |
| 373 | |
| 374 | static const int kNumberOfElements = 1021; |
| 375 | |
| 376 | private: |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 377 | static const int kChainLengthThreshold = 15; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 378 | |
| 379 | intptr_t idx_; |
| 380 | intptr_t chain_length_; |
| 381 | SlotsBuffer* next_; |
| 382 | ObjectSlot slots_[kNumberOfElements]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 383 | }; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 384 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 385 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 386 | // Defined in isolate.h. |
| 387 | class ThreadLocalTop; |
| 388 | |
| 389 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 390 | // ------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 391 | // Mark-Compact collector |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 392 | class MarkCompactCollector { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | public: |
| 394 | // Type of functions to compute forwarding addresses of objects in |
| 395 | // compacted spaces. Given an object and its size, return a (non-failure) |
| 396 | // Object* that will be the object after forwarding. There is a separate |
| 397 | // allocation function for each (compactable) space based on the location |
| 398 | // of the object before compaction. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 399 | typedef MaybeObject* (*AllocationFunction)(Heap* heap, |
| 400 | HeapObject* object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 401 | int object_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 402 | |
| 403 | // Type of functions to encode the forwarding address for an object. |
| 404 | // Given the object, its size, and the new (non-failure) object it will be |
| 405 | // forwarded to, encode the forwarding address. For paged spaces, the |
| 406 | // 'offset' input/output parameter contains the offset of the forwarded |
| 407 | // object from the forwarding address of the previous live object in the |
| 408 | // page as input, and is updated to contain the offset to be used for the |
| 409 | // next live object in the same page. For spaces using a different |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 410 | // encoding (i.e., contiguous spaces), the offset parameter is ignored. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 411 | typedef void (*EncodingFunction)(Heap* heap, |
| 412 | HeapObject* old_object, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 413 | int object_size, |
| 414 | Object* new_object, |
| 415 | int* offset); |
| 416 | |
| 417 | // Type of functions to process non-live objects. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 418 | typedef void (*ProcessNonLiveFunction)(HeapObject* object, Isolate* isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 419 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 420 | // Pointer to member function, used in IterateLiveObjects. |
| 421 | typedef int (MarkCompactCollector::*LiveObjectCallback)(HeapObject* obj); |
| 422 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 423 | // Set the global flags, it must be called before Prepare to take effect. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 424 | inline void SetFlags(int flags); |
| 425 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 426 | static void Initialize(); |
| 427 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 428 | void CollectEvacuationCandidates(PagedSpace* space); |
| 429 | |
| 430 | void AddEvacuationCandidate(Page* p); |
| 431 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 432 | // Prepares for GC by resetting relocation info in old and map spaces and |
| 433 | // choosing spaces to compact. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 434 | void Prepare(GCTracer* tracer); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 435 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 436 | // Performs a global garbage collection. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 437 | void CollectGarbage(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 438 | |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 439 | enum CompactionMode { |
| 440 | INCREMENTAL_COMPACTION, |
| 441 | NON_INCREMENTAL_COMPACTION |
| 442 | }; |
| 443 | |
| 444 | bool StartCompaction(CompactionMode mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 445 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 446 | void AbortCompaction(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 447 | |
| 448 | // During a full GC, there is a stack-allocated GCTracer that is used for |
| 449 | // bookkeeping information. Return a pointer to that tracer. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 450 | GCTracer* tracer() { return tracer_; } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 451 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 452 | #ifdef DEBUG |
| 453 | // Checks whether performing mark-compact collection. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 454 | bool in_use() { return state_ > PREPARE_GC; } |
| 455 | bool are_map_pointers_encoded() { return state_ == UPDATE_POINTERS; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 456 | #endif |
| 457 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 458 | // Determine type of object and emit deletion log event. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 459 | static void ReportDeleteIfNeeded(HeapObject* obj, Isolate* isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 460 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 461 | // Distinguishable invalid map encodings (for single word and multiple words) |
| 462 | // that indicate free regions. |
| 463 | static const uint32_t kSingleFreeEncoding = 0; |
| 464 | static const uint32_t kMultiFreeEncoding = 1; |
| 465 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 466 | static inline bool IsMarked(Object* obj); |
| 467 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 468 | inline Heap* heap() const { return heap_; } |
| 469 | |
| 470 | CodeFlusher* code_flusher() { return code_flusher_; } |
| 471 | inline bool is_code_flushing_enabled() const { return code_flusher_ != NULL; } |
| 472 | void EnableCodeFlushing(bool enable); |
| 473 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 474 | enum SweeperType { |
| 475 | CONSERVATIVE, |
| 476 | LAZY_CONSERVATIVE, |
| 477 | PRECISE |
| 478 | }; |
| 479 | |
| 480 | #ifdef DEBUG |
| 481 | void VerifyMarkbitsAreClean(); |
| 482 | static void VerifyMarkbitsAreClean(PagedSpace* space); |
| 483 | static void VerifyMarkbitsAreClean(NewSpace* space); |
| 484 | #endif |
| 485 | |
| 486 | // Sweep a single page from the given space conservatively. |
| 487 | // Return a number of reclaimed bytes. |
| 488 | static intptr_t SweepConservatively(PagedSpace* space, Page* p); |
| 489 | |
| 490 | INLINE(static bool ShouldSkipEvacuationSlotRecording(Object** anchor)) { |
| 491 | return Page::FromAddress(reinterpret_cast<Address>(anchor))-> |
| 492 | ShouldSkipEvacuationSlotRecording(); |
| 493 | } |
| 494 | |
| 495 | INLINE(static bool ShouldSkipEvacuationSlotRecording(Object* host)) { |
| 496 | return Page::FromAddress(reinterpret_cast<Address>(host))-> |
| 497 | ShouldSkipEvacuationSlotRecording(); |
| 498 | } |
| 499 | |
| 500 | INLINE(static bool IsOnEvacuationCandidate(Object* obj)) { |
| 501 | return Page::FromAddress(reinterpret_cast<Address>(obj))-> |
| 502 | IsEvacuationCandidate(); |
| 503 | } |
| 504 | |
| 505 | void EvictEvacuationCandidate(Page* page) { |
| 506 | if (FLAG_trace_fragmentation) { |
| 507 | PrintF("Page %p is too popular. Disabling evacuation.\n", |
| 508 | reinterpret_cast<void*>(page)); |
| 509 | } |
| 510 | |
| 511 | // TODO(gc) If all evacuation candidates are too popular we |
| 512 | // should stop slots recording entirely. |
| 513 | page->ClearEvacuationCandidate(); |
| 514 | |
| 515 | // We were not collecting slots on this page that point |
| 516 | // to other evacuation candidates thus we have to |
| 517 | // rescan the page after evacuation to discover and update all |
| 518 | // pointers to evacuated objects. |
| 519 | if (page->owner()->identity() == OLD_DATA_SPACE) { |
| 520 | evacuation_candidates_.RemoveElement(page); |
| 521 | } else { |
| 522 | page->SetFlag(Page::RESCAN_ON_EVACUATION); |
| 523 | } |
| 524 | } |
| 525 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 526 | void RecordRelocSlot(RelocInfo* rinfo, Object* target); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 527 | void RecordCodeEntrySlot(Address slot, Code* target); |
| 528 | |
| 529 | INLINE(void RecordSlot(Object** anchor_slot, Object** slot, Object* object)); |
| 530 | |
| 531 | void MigrateObject(Address dst, |
| 532 | Address src, |
| 533 | int size, |
| 534 | AllocationSpace to_old_space); |
| 535 | |
| 536 | bool TryPromoteObject(HeapObject* object, int object_size); |
| 537 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 538 | inline Object* encountered_weak_maps() { return encountered_weak_maps_; } |
| 539 | inline void set_encountered_weak_maps(Object* weak_map) { |
| 540 | encountered_weak_maps_ = weak_map; |
| 541 | } |
| 542 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 543 | void InvalidateCode(Code* code); |
| 544 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 545 | void ClearMarkbits(); |
| 546 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 547 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 548 | MarkCompactCollector(); |
| 549 | ~MarkCompactCollector(); |
| 550 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 551 | bool MarkInvalidatedCode(); |
| 552 | void RemoveDeadInvalidatedCode(); |
| 553 | void ProcessInvalidatedCode(ObjectVisitor* visitor); |
| 554 | |
| 555 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 556 | #ifdef DEBUG |
| 557 | enum CollectorState { |
| 558 | IDLE, |
| 559 | PREPARE_GC, |
| 560 | MARK_LIVE_OBJECTS, |
| 561 | SWEEP_SPACES, |
| 562 | ENCODE_FORWARDING_ADDRESSES, |
| 563 | UPDATE_POINTERS, |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 564 | RELOCATE_OBJECTS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 565 | }; |
| 566 | |
| 567 | // The current stage of the collector. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 568 | CollectorState state_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 569 | #endif |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 570 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 571 | // Global flag that forces sweeping to be precise, so we can traverse the |
| 572 | // heap. |
| 573 | bool sweep_precisely_; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 574 | |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 575 | bool reduce_memory_footprint_; |
| 576 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 577 | bool abort_incremental_marking_; |
| 578 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 579 | // True if we are collecting slots to perform evacuation from evacuation |
| 580 | // candidates. |
| 581 | bool compacting_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 582 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 583 | bool was_marked_incrementally_; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 584 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 585 | bool collect_maps_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 586 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 587 | bool flush_monomorphic_ics_; |
| 588 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 589 | // A pointer to the current stack-allocated GC tracer object during a full |
| 590 | // collection (NULL before and after). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 591 | GCTracer* tracer_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 592 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 593 | SlotsBufferAllocator slots_buffer_allocator_; |
| 594 | |
| 595 | SlotsBuffer* migration_slots_buffer_; |
| 596 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 597 | // Finishes GC, performs heap verification if enabled. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 598 | void Finish(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 599 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 600 | // ----------------------------------------------------------------------- |
| 601 | // Phase 1: Marking live objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 602 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 603 | // Before: The heap has been prepared for garbage collection by |
| 604 | // MarkCompactCollector::Prepare() and is otherwise in its |
| 605 | // normal state. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 606 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 607 | // After: Live objects are marked and non-live objects are unmarked. |
| 608 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 609 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 610 | friend class RootMarkingVisitor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 611 | friend class MarkingVisitor; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 612 | friend class StaticMarkingVisitor; |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 613 | friend class CodeMarkingVisitor; |
| 614 | friend class SharedFunctionInfoMarkingVisitor; |
| 615 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 616 | // Mark non-optimize code for functions inlined into the given optimized |
| 617 | // code. This will prevent it from being flushed. |
| 618 | void MarkInlinedFunctionsCode(Code* code); |
| 619 | |
| 620 | // Mark code objects that are active on the stack to prevent them |
| 621 | // from being flushed. |
| 622 | void PrepareThreadForCodeFlushing(Isolate* isolate, ThreadLocalTop* top); |
| 623 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 624 | void PrepareForCodeFlushing(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | |
| 626 | // Marking operations for objects reachable from roots. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 627 | void MarkLiveObjects(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 628 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 629 | void AfterMarking(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 630 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 631 | // Marks the object black and pushes it on the marking stack. |
| 632 | // This is for non-incremental marking. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 633 | INLINE(void MarkObject(HeapObject* obj, MarkBit mark_bit)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 634 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 635 | INLINE(bool MarkObjectWithoutPush(HeapObject* object)); |
| 636 | INLINE(void MarkObjectAndPush(HeapObject* value)); |
| 637 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 638 | // Marks the object black. This is for non-incremental marking. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 639 | INLINE(void SetMark(HeapObject* obj, MarkBit mark_bit)); |
| 640 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 641 | // Clears the cache of ICs related to this map. |
| 642 | INLINE(void ClearCacheOnMap(Map* map)); |
| 643 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 644 | void ProcessNewlyMarkedObject(HeapObject* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 645 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 646 | // Creates back pointers for all map transitions, stores them in |
| 647 | // the prototype field. The original prototype pointers are restored |
| 648 | // in ClearNonLiveTransitions(). All JSObject maps |
| 649 | // connected by map transitions have the same prototype object, which |
| 650 | // is why we can use this field temporarily for back pointers. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 651 | void CreateBackPointers(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 652 | |
| 653 | // Mark a Map and its DescriptorArray together, skipping transitions. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 654 | void MarkMapContents(Map* map); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 655 | void MarkAccessorPairSlot(HeapObject* accessors, int offset); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 656 | void MarkDescriptorArray(DescriptorArray* descriptors); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 657 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 658 | // Mark the heap roots and all objects reachable from them. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 659 | void MarkRoots(RootMarkingVisitor* visitor); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 660 | |
| 661 | // Mark the symbol table specially. References to symbols from the |
| 662 | // symbol table are weak. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 663 | void MarkSymbolTable(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 664 | |
| 665 | // Mark objects in object groups that have at least one object in the |
| 666 | // group marked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 667 | void MarkObjectGroups(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 668 | |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 669 | // Mark objects in implicit references groups if their parent object |
| 670 | // is marked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 671 | void MarkImplicitRefGroups(); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 672 | |
| 673 | // Mark all objects which are reachable due to host application |
| 674 | // logic like object groups or implicit references' groups. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 675 | void ProcessExternalMarking(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 676 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 677 | // Mark objects reachable (transitively) from objects in the marking stack |
| 678 | // or overflowed in the heap. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 679 | void ProcessMarkingDeque(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 680 | |
| 681 | // Mark objects reachable (transitively) from objects in the marking |
| 682 | // stack. This function empties the marking stack, but may leave |
| 683 | // overflowed objects in the heap, in which case the marking stack's |
| 684 | // overflow flag will be set. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 685 | void EmptyMarkingDeque(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 686 | |
| 687 | // Refill the marking stack with overflowed objects from the heap. This |
| 688 | // function either leaves the marking stack full or clears the overflow |
| 689 | // flag on the marking stack. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 690 | void RefillMarkingDeque(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 691 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 692 | // After reachable maps have been marked process per context object |
| 693 | // literal map caches removing unmarked entries. |
| 694 | void ProcessMapCaches(); |
| 695 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 696 | // Callback function for telling whether the object *p is an unmarked |
| 697 | // heap object. |
| 698 | static bool IsUnmarkedHeapObject(Object** p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 699 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 700 | // Map transitions from a live map to a dead map must be killed. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 701 | // We replace them with a null descriptor, with the same key. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 702 | void ClearNonLiveTransitions(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 703 | void ClearNonLivePrototypeTransitions(Map* map); |
| 704 | void ClearNonLiveMapTransitions(Map* map, MarkBit map_mark); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 705 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 706 | // Marking detaches initial maps from SharedFunctionInfo objects |
| 707 | // to make this reference weak. We need to reattach initial maps |
| 708 | // back after collection. This is either done during |
| 709 | // ClearNonLiveTransitions pass or by calling this function. |
| 710 | void ReattachInitialMaps(); |
| 711 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 712 | // Mark all values associated with reachable keys in weak maps encountered |
| 713 | // so far. This might push new object or even new weak maps onto the |
| 714 | // marking stack. |
| 715 | void ProcessWeakMaps(); |
| 716 | |
| 717 | // After all reachable objects have been marked those weak map entries |
| 718 | // with an unreachable key are removed from all encountered weak maps. |
| 719 | // The linked list of all encountered weak maps is destroyed. |
| 720 | void ClearWeakMaps(); |
| 721 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 722 | // ----------------------------------------------------------------------- |
| 723 | // Phase 2: Sweeping to clear mark bits and free non-live objects for |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 724 | // a non-compacting collection. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 725 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 726 | // Before: Live objects are marked and non-live objects are unmarked. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 727 | // |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 728 | // After: Live objects are unmarked, non-live regions have been added to |
| 729 | // their space's free list. Active eden semispace is compacted by |
| 730 | // evacuation. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 731 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 732 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 733 | // If we are not compacting the heap, we simply sweep the spaces except |
| 734 | // for the large object space, clearing mark bits and adding unmarked |
| 735 | // regions to each space's free list. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 736 | void SweepSpaces(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 737 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 738 | void EvacuateNewSpace(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 739 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 740 | void EvacuateLiveObjectsFromPage(Page* p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 741 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 742 | void EvacuatePages(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 743 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 744 | void EvacuateNewSpaceAndCandidates(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 745 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 746 | void SweepSpace(PagedSpace* space, SweeperType sweeper); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 747 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 748 | #ifdef DEBUG |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 749 | friend class MarkObjectVisitor; |
| 750 | static void VisitObject(HeapObject* obj); |
| 751 | |
| 752 | friend class UnmarkObjectVisitor; |
| 753 | static void UnmarkObject(HeapObject* obj); |
| 754 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 755 | |
| 756 | Heap* heap_; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 757 | MarkingDeque marking_deque_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 758 | CodeFlusher* code_flusher_; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 759 | Object* encountered_weak_maps_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 760 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 761 | List<Page*> evacuation_candidates_; |
| 762 | List<Code*> invalidated_code_; |
| 763 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 764 | friend class Heap; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 765 | }; |
| 766 | |
| 767 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 768 | const char* AllocationSpaceName(AllocationSpace space); |
| 769 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 770 | } } // namespace v8::internal |
| 771 | |
| 772 | #endif // V8_MARK_COMPACT_H_ |