store-buffer.cc 22.1 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/heap/store-buffer.h"
6

7
#include <algorithm>
8

9
#include "src/counters.h"
10
#include "src/heap/incremental-marking.h"
11
#include "src/heap/store-buffer-inl.h"
12 13
#include "src/isolate.h"
#include "src/objects-inl.h"
14
#include "src/v8.h"
15 16 17 18 19 20 21 22 23 24 25

namespace v8 {
namespace internal {

StoreBuffer::StoreBuffer(Heap* heap)
    : heap_(heap),
      start_(NULL),
      limit_(NULL),
      old_start_(NULL),
      old_limit_(NULL),
      old_top_(NULL),
26
      old_reserved_limit_(NULL),
27 28
      old_buffer_is_sorted_(false),
      old_buffer_is_filtered_(false),
29
      during_gc_(false),
30 31 32 33
      store_buffer_rebuilding_enabled_(false),
      callback_(NULL),
      may_move_store_buffer_entries_(true),
      virtual_memory_(NULL),
34 35
      hash_set_1_(NULL),
      hash_set_2_(NULL),
36
      hash_sets_are_empty_(true) {}
37 38


39
void StoreBuffer::SetUp() {
40 41 42
  // Allocate 3x the buffer size, so that we can start the new store buffer
  // aligned to 2x the size.  This lets us use a bit test to detect the end of
  // the area.
43
  virtual_memory_ = new base::VirtualMemory(kStoreBufferSize * 3);
44 45 46 47
  uintptr_t start_as_int =
      reinterpret_cast<uintptr_t>(virtual_memory_->address());
  start_ =
      reinterpret_cast<Address*>(RoundUp(start_as_int, kStoreBufferSize * 2));
48 49
  limit_ = start_ + (kStoreBufferSize / kPointerSize);

50
  // Reserve space for the larger old buffer.
51
  old_virtual_memory_ =
52
      new base::VirtualMemory(kOldStoreBufferLength * kPointerSize);
53 54 55 56
  old_top_ = old_start_ =
      reinterpret_cast<Address*>(old_virtual_memory_->address());
  // Don't know the alignment requirements of the OS, but it is certainly not
  // less than 0xfff.
57 58 59 60 61 62 63 64 65
  CHECK((reinterpret_cast<uintptr_t>(old_start_) & 0xfff) == 0);
  CHECK(kStoreBufferSize >= base::OS::CommitPageSize());
  // Initial size of the old buffer is as big as the buffer for new pointers.
  // This means even if we later fail to enlarge the old buffer due to OOM from
  // the OS, we will still be able to empty the new pointer buffer into the old
  // buffer.
  int initial_length = static_cast<int>(kStoreBufferSize / kPointerSize);
  CHECK(initial_length > 0);
  CHECK(initial_length <= kOldStoreBufferLength);
66
  old_limit_ = old_start_ + initial_length;
67
  old_reserved_limit_ = old_start_ + kOldStoreBufferLength;
68

69 70 71 72 73
  if (!old_virtual_memory_->Commit(reinterpret_cast<void*>(old_start_),
                                   (old_limit_ - old_start_) * kPointerSize,
                                   false)) {
    V8::FatalProcessOutOfMemory("StoreBuffer::SetUp");
  }
74

75 76
  DCHECK(reinterpret_cast<Address>(start_) >= virtual_memory_->address());
  DCHECK(reinterpret_cast<Address>(limit_) >= virtual_memory_->address());
77 78
  Address* vm_limit = reinterpret_cast<Address*>(
      reinterpret_cast<char*>(virtual_memory_->address()) +
79
      virtual_memory_->size());
80 81
  DCHECK(start_ <= vm_limit);
  DCHECK(limit_ <= vm_limit);
82
  USE(vm_limit);
83 84
  DCHECK((reinterpret_cast<uintptr_t>(limit_) & kStoreBufferOverflowBit) != 0);
  DCHECK((reinterpret_cast<uintptr_t>(limit_ - 1) & kStoreBufferOverflowBit) ==
85 86
         0);

87 88 89 90 91
  if (!virtual_memory_->Commit(reinterpret_cast<Address>(start_),
                               kStoreBufferSize,
                               false)) {  // Not executable.
    V8::FatalProcessOutOfMemory("StoreBuffer::SetUp");
  }
92
  heap_->set_store_buffer_top(reinterpret_cast<Smi*>(start_));
93

94 95 96
  hash_set_1_ = new uintptr_t[kHashSetLength];
  hash_set_2_ = new uintptr_t[kHashSetLength];
  hash_sets_are_empty_ = false;
97

98
  ClearFilteringHashSets();
99 100 101 102 103
}


void StoreBuffer::TearDown() {
  delete virtual_memory_;
104
  delete old_virtual_memory_;
105 106
  delete[] hash_set_1_;
  delete[] hash_set_2_;
107 108
  old_start_ = old_top_ = old_limit_ = old_reserved_limit_ = NULL;
  start_ = limit_ = NULL;
109
  heap_->set_store_buffer_top(reinterpret_cast<Smi*>(start_));
110 111 112 113 114
}


void StoreBuffer::StoreBufferOverflow(Isolate* isolate) {
  isolate->heap()->store_buffer()->Compact();
115
  isolate->counters()->store_buffer_overflows()->Increment();
116 117 118
}


119 120 121 122 123
bool StoreBuffer::SpaceAvailable(intptr_t space_needed) {
  return old_limit_ - old_top_ >= space_needed;
}


124 125
void StoreBuffer::EnsureSpace(intptr_t space_needed) {
  while (old_limit_ - old_top_ < space_needed &&
126
         old_limit_ < old_reserved_limit_) {
127
    size_t grow = old_limit_ - old_start_;  // Double size.
128 129 130 131 132
    if (old_virtual_memory_->Commit(reinterpret_cast<void*>(old_limit_),
                                    grow * kPointerSize, false)) {
      old_limit_ += grow;
    } else {
      break;
133
    }
134 135
  }

136
  if (SpaceAvailable(space_needed)) return;
137

138
  if (old_buffer_is_filtered_) return;
139
  DCHECK(may_move_store_buffer_entries_);
140 141 142 143 144 145 146 147
  Compact();

  old_buffer_is_filtered_ = true;
  bool page_has_scan_on_scavenge_flag = false;

  PointerChunkIterator it(heap_);
  MemoryChunk* chunk;
  while ((chunk = it.next()) != NULL) {
148 149 150 151
    if (chunk->scan_on_scavenge()) {
      page_has_scan_on_scavenge_flag = true;
      break;
    }
152 153 154 155 156 157
  }

  if (page_has_scan_on_scavenge_flag) {
    Filter(MemoryChunk::SCAN_ON_SCAVENGE);
  }

158
  if (SpaceAvailable(space_needed)) return;
159

160 161 162 163 164 165
  // Sample 1 entry in 97 and filter out the pages where we estimate that more
  // than 1 in 8 pointers are to new space.
  static const int kSampleFinenesses = 5;
  static const struct Samples {
    int prime_sample_step;
    int threshold;
166 167 168 169 170 171
  } samples[kSampleFinenesses] = {
        {97, ((Page::kPageSize / kPointerSize) / 97) / 8},
        {23, ((Page::kPageSize / kPointerSize) / 23) / 16},
        {7, ((Page::kPageSize / kPointerSize) / 7) / 32},
        {3, ((Page::kPageSize / kPointerSize) / 3) / 256},
        {1, 0}};
172 173 174
  for (int i = 0; i < kSampleFinenesses; i++) {
    ExemptPopularPages(samples[i].prime_sample_step, samples[i].threshold);
    // As a last resort we mark all pages as being exempt from the store buffer.
175
    DCHECK(i != (kSampleFinenesses - 1) || old_top_ == old_start_);
176 177 178
    if (SpaceAvailable(space_needed)) return;
  }
  UNREACHABLE();
179 180 181 182 183 184
}


// Sample the store buffer to see if some pages are taking up a lot of space
// in the store buffer.
void StoreBuffer::ExemptPopularPages(int prime_sample_step, int threshold) {
185
  HashMap store_buffer_counts(HashMap::PointersMatch, 16);
186 187 188 189 190 191 192 193
  bool created_new_scan_on_scavenge_pages = false;
  MemoryChunk* previous_chunk = NULL;
  for (Address* p = old_start_; p < old_top_; p += prime_sample_step) {
    Address addr = *p;
    MemoryChunk* containing_chunk = NULL;
    if (previous_chunk != NULL && previous_chunk->Contains(addr)) {
      containing_chunk = previous_chunk;
    } else {
194
      containing_chunk = MemoryChunk::FromAnyPointerAddress(heap_, addr);
195
    }
196 197 198 199 200
    HashMap::Entry* e = store_buffer_counts.LookupOrInsert(
        containing_chunk,
        static_cast<uint32_t>(reinterpret_cast<uintptr_t>(containing_chunk) >>
                              kPageSizeBits));
    intptr_t old_counter = bit_cast<intptr_t>(e->value);
201
    if (old_counter >= threshold) {
202 203 204
      containing_chunk->set_scan_on_scavenge(true);
      created_new_scan_on_scavenge_pages = true;
    }
205
    (*bit_cast<intptr_t*>(&e->value))++;
206 207 208 209
    previous_chunk = containing_chunk;
  }
  if (created_new_scan_on_scavenge_pages) {
    Filter(MemoryChunk::SCAN_ON_SCAVENGE);
210 211
    heap_->isolate()->CountUsage(
        v8::Isolate::UseCounterFeature::kStoreBufferOverflow);
212 213 214 215 216 217 218 219 220 221 222 223 224 225
  }
  old_buffer_is_filtered_ = true;
}


void StoreBuffer::Filter(int flag) {
  Address* new_top = old_start_;
  MemoryChunk* previous_chunk = NULL;
  for (Address* p = old_start_; p < old_top_; p++) {
    Address addr = *p;
    MemoryChunk* containing_chunk = NULL;
    if (previous_chunk != NULL && previous_chunk->Contains(addr)) {
      containing_chunk = previous_chunk;
    } else {
226
      containing_chunk = MemoryChunk::FromAnyPointerAddress(heap_, addr);
227 228 229 230 231 232 233
      previous_chunk = containing_chunk;
    }
    if (!containing_chunk->IsFlagSet(flag)) {
      *new_top++ = addr;
    }
  }
  old_top_ = new_top;
234 235 236 237

  // Filtering hash sets are inconsistent with the store buffer after this
  // operation.
  ClearFilteringHashSets();
238 239 240 241 242 243 244 245 246
}


bool StoreBuffer::PrepareForIteration() {
  Compact();
  PointerChunkIterator it(heap_);
  MemoryChunk* chunk;
  bool page_has_scan_on_scavenge_flag = false;
  while ((chunk = it.next()) != NULL) {
247 248 249 250
    if (chunk->scan_on_scavenge()) {
      page_has_scan_on_scavenge_flag = true;
      break;
    }
251 252 253 254 255
  }

  if (page_has_scan_on_scavenge_flag) {
    Filter(MemoryChunk::SCAN_ON_SCAVENGE);
  }
256 257 258 259 260

  // Filtering hash sets are inconsistent with the store buffer after
  // iteration.
  ClearFilteringHashSets();

261 262 263 264
  return page_has_scan_on_scavenge_flag;
}


265 266
void StoreBuffer::ClearFilteringHashSets() {
  if (!hash_sets_are_empty_) {
267
    memset(reinterpret_cast<void*>(hash_set_1_), 0,
268
           sizeof(uintptr_t) * kHashSetLength);
269
    memset(reinterpret_cast<void*>(hash_set_2_), 0,
270 271 272
           sizeof(uintptr_t) * kHashSetLength);
    hash_sets_are_empty_ = true;
  }
273 274 275
}


276
void StoreBuffer::GCPrologue() {
277
  ClearFilteringHashSets();
278
  during_gc_ = true;
279 280 281
}


282
#ifdef VERIFY_HEAP
283 284 285 286 287 288 289 290 291 292 293 294
void StoreBuffer::VerifyPointers(LargeObjectSpace* space) {
  LargeObjectIterator it(space);
  for (HeapObject* object = it.Next(); object != NULL; object = it.Next()) {
    if (object->IsFixedArray()) {
      Address slot_address = object->address();
      Address end = object->address() + object->Size();

      while (slot_address < end) {
        HeapObject** slot = reinterpret_cast<HeapObject**>(slot_address);
        // When we are not in GC the Heap::InNewSpace() predicate
        // checks that pointers which satisfy predicate point into
        // the active semispace.
295
        Object* object = *slot;
296
        heap_->InNewSpace(object);
297 298 299 300 301 302 303 304 305
        slot_address += kPointerSize;
      }
    }
  }
}
#endif


void StoreBuffer::Verify() {
306
#ifdef VERIFY_HEAP
307
  VerifyPointers(heap_->lo_space());
308 309 310 311 312
#endif
}


void StoreBuffer::GCEpilogue() {
313
  during_gc_ = false;
314
#ifdef VERIFY_HEAP
315 316 317
  if (FLAG_verify_heap) {
    Verify();
  }
318
#endif
319 320 321
}


322
void StoreBuffer::ProcessOldToNewSlot(Address slot_address,
323
                                      ObjectSlotCallback slot_callback) {
324
  Object** slot = reinterpret_cast<Object**>(slot_address);
325
  Object* object = *slot;
326 327 328 329 330 331 332

  // If the object is not in from space, it must be a duplicate store buffer
  // entry and the slot was already updated.
  if (heap_->InFromSpace(object)) {
    HeapObject* heap_object = reinterpret_cast<HeapObject*>(object);
    DCHECK(heap_object->IsHeapObject());
    slot_callback(reinterpret_cast<HeapObject**>(slot), heap_object);
333
    object = *slot;
334 335 336 337 338 339 340 341 342 343 344
    // If the object was in from space before and is after executing the
    // callback in to space, the object is still live.
    // Unfortunately, we do not know about the slot. It could be in a
    // just freed free space object.
    if (heap_->InToSpace(object)) {
      EnterDirectlyIntoStoreBuffer(reinterpret_cast<Address>(slot));
    }
  }
}


345
void StoreBuffer::FindPointersToNewSpaceInRegion(
346
    Address start, Address end, ObjectSlotCallback slot_callback) {
347
  for (Address slot_address = start; slot_address < end;
348
       slot_address += kPointerSize) {
349
    ProcessOldToNewSlot(slot_address, slot_callback);
350 351 352 353
  }
}


354 355
void StoreBuffer::IteratePointersInStoreBuffer(
    ObjectSlotCallback slot_callback) {
356 357 358 359 360 361 362 363
  Address* limit = old_top_;
  old_top_ = old_start_;
  {
    DontMoveStoreBufferEntriesScope scope(this);
    for (Address* current = old_start_; current < limit; current++) {
#ifdef DEBUG
      Address* saved_top = old_top_;
#endif
364
      ProcessOldToNewSlot(*current, slot_callback);
365
      DCHECK(old_top_ == saved_top + 1 || old_top_ == saved_top);
366 367 368 369 370
    }
  }
}


371 372 373 374 375
void StoreBuffer::ClearInvalidStoreBufferEntries() {
  Compact();
  Address* new_top = old_start_;
  for (Address* current = old_start_; current < old_top_; current++) {
    Address addr = *current;
376
    Object** slot = reinterpret_cast<Object**>(addr);
377
    Object* object = *slot;
378 379 380 381 382 383
    if (heap_->InNewSpace(object) && object->IsHeapObject()) {
      // If the target object is not black, the source slot must be part
      // of a non-black (dead) object.
      HeapObject* heap_object = HeapObject::cast(object);
      if (Marking::IsBlack(Marking::MarkBitFrom(heap_object)) &&
          heap_->mark_compact_collector()->IsSlotInLiveObject(addr)) {
384 385 386 387 388 389
        *new_top++ = addr;
      }
    }
  }
  old_top_ = new_top;
  ClearFilteringHashSets();
390 391 392 393 394

  // Don't scan on scavenge dead large objects.
  LargeObjectIterator it(heap_->lo_space());
  for (HeapObject* object = it.Next(); object != NULL; object = it.Next()) {
    MemoryChunk* chunk = MemoryChunk::FromAddress(object->address());
395 396
    if (chunk->scan_on_scavenge() &&
        Marking::IsWhite(Marking::MarkBitFrom(object))) {
397 398 399
      chunk->set_scan_on_scavenge(false);
    }
  }
400 401 402 403 404 405
}


void StoreBuffer::VerifyValidStoreBufferEntries() {
  for (Address* current = old_start_; current < old_top_; current++) {
    Object** slot = reinterpret_cast<Object**>(*current);
406
    Object* object = *slot;
407
    CHECK(object->IsHeapObject());
408 409
    CHECK(heap_->InNewSpace(object));
    heap_->mark_compact_collector()->VerifyIsSlotInLiveObject(
410
        reinterpret_cast<Address>(slot), HeapObject::cast(object));
411 412 413 414
  }
}


415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
class FindPointersToNewSpaceVisitor final : public ObjectVisitor {
 public:
  FindPointersToNewSpaceVisitor(StoreBuffer* store_buffer,
                                ObjectSlotCallback callback)
      : store_buffer_(store_buffer), callback_(callback) {}

  V8_INLINE void VisitPointers(Object** start, Object** end) override {
    store_buffer_->FindPointersToNewSpaceInRegion(
        reinterpret_cast<Address>(start), reinterpret_cast<Address>(end),
        callback_);
  }

  V8_INLINE void VisitCodeEntry(Address code_entry_slot) override {}

 private:
  StoreBuffer* store_buffer_;
  ObjectSlotCallback callback_;
};


435 436 437 438 439 440 441 442 443
void StoreBuffer::IteratePointersToNewSpace(ObjectSlotCallback slot_callback) {
  // We do not sort or remove duplicated entries from the store buffer because
  // we expect that callback will rebuild the store buffer thus removing
  // all duplicates and pointers to old space.
  bool some_pages_to_scan = PrepareForIteration();

  // TODO(gc): we want to skip slots on evacuation candidates
  // but we can't simply figure that out from slot address
  // because slot can belong to a large object.
444
  IteratePointersInStoreBuffer(slot_callback);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

  // We are done scanning all the pointers that were in the store buffer, but
  // there may be some pages marked scan_on_scavenge that have pointers to new
  // space that are not in the store buffer.  We must scan them now.  As we
  // scan, the surviving pointers to new space will be added to the store
  // buffer.  If there are still a lot of pointers to new space then we will
  // keep the scan_on_scavenge flag on the page and discard the pointers that
  // were added to the store buffer.  If there are not many pointers to new
  // space left on the page we will keep the pointers in the store buffer and
  // remove the flag from the page.
  if (some_pages_to_scan) {
    if (callback_ != NULL) {
      (*callback_)(heap_, NULL, kStoreBufferStartScanningPagesEvent);
    }
    PointerChunkIterator it(heap_);
    MemoryChunk* chunk;
461
    FindPointersToNewSpaceVisitor visitor(this, slot_callback);
462 463 464 465 466 467 468 469 470
    while ((chunk = it.next()) != NULL) {
      if (chunk->scan_on_scavenge()) {
        chunk->set_scan_on_scavenge(false);
        if (callback_ != NULL) {
          (*callback_)(heap_, chunk, kStoreBufferScanningPageEvent);
        }
        if (chunk->owner() == heap_->lo_space()) {
          LargePage* large_page = reinterpret_cast<LargePage*>(chunk);
          HeapObject* array = large_page->GetObject();
471
          DCHECK(array->IsFixedArray());
472 473
          Address start = array->address();
          Address end = start + array->Size();
474
          FindPointersToNewSpaceInRegion(start, end, slot_callback);
475 476
        } else {
          Page* page = reinterpret_cast<Page*>(chunk);
477 478
          PagedSpace* owner = reinterpret_cast<PagedSpace*>(page->owner());
          if (owner == heap_->map_space()) {
479
            DCHECK(page->WasSwept());
480
            HeapObjectIterator iterator(page);
481 482 483 484
            for (HeapObject* heap_object = iterator.Next(); heap_object != NULL;
                 heap_object = iterator.Next()) {
              // We skip free space objects.
              if (!heap_object->IsFiller()) {
485
                DCHECK(heap_object->IsMap());
486
                FindPointersToNewSpaceInRegion(
487 488
                    heap_object->address() + Map::kPointerFieldsBeginOffset,
                    heap_object->address() + Map::kPointerFieldsEndOffset,
489
                    slot_callback);
490
              }
491
            }
492
          } else {
493 494 495 496 497 498
            if (page->IsFlagSet(Page::COMPACTION_WAS_ABORTED)) {
              // Aborted pages require iterating using mark bits because they
              // don't have an iterable object layout before sweeping (which can
              // only happen later). Note that we can never reach an
              // aborted page through the scavenger.
              DCHECK_EQ(heap_->gc_state(), Heap::MARK_COMPACT);
499 500
              heap_->mark_compact_collector()->VisitLiveObjectsBody(page,
                                                                    &visitor);
501 502 503 504 505 506 507 508 509
            } else {
              heap_->mark_compact_collector()
                  ->SweepOrWaitUntilSweepingCompleted(page);
              HeapObjectIterator iterator(page);
              for (HeapObject* heap_object = iterator.Next();
                   heap_object != nullptr; heap_object = iterator.Next()) {
                // We iterate over objects that contain new space pointers only.
                heap_object->IterateBody(&visitor);
              }
510
            }
511
          }
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
        }
      }
    }
    if (callback_ != NULL) {
      (*callback_)(heap_, NULL, kStoreBufferScanningPageEvent);
    }
  }
}


void StoreBuffer::Compact() {
  Address* top = reinterpret_cast<Address*>(heap_->store_buffer_top());

  if (top == start_) return;

  // There's no check of the limit in the loop below so we check here for
  // the worst case (compaction doesn't eliminate any pointers).
529
  DCHECK(top <= limit_);
530
  heap_->set_store_buffer_top(reinterpret_cast<Smi*>(start_));
531
  EnsureSpace(top - start_);
532
  DCHECK(may_move_store_buffer_entries_);
533 534
  // Goes through the addresses in the store buffer attempting to remove
  // duplicates.  In the interest of speed this is a lossy operation.  Some
535
  // duplicates will remain.  We have two hash sets with different hash
536
  // functions to reduce the number of unnecessary clashes.
537
  hash_sets_are_empty_ = false;  // Hash sets are in use.
538
  for (Address* current = start_; current < top; current++) {
539
    DCHECK(!heap_->code_space()->Contains(*current));
540 541 542
    uintptr_t int_addr = reinterpret_cast<uintptr_t>(*current);
    // Shift out the last bits including any tags.
    int_addr >>= kPointerSizeLog2;
543 544 545 546 547 548 549
    // The upper part of an address is basically random because of ASLR and OS
    // non-determinism, so we use only the bits within a page for hashing to
    // make v8's behavior (more) deterministic.
    uintptr_t hash_addr =
        int_addr & (Page::kPageAlignmentMask >> kPointerSizeLog2);
    int hash1 = ((hash_addr ^ (hash_addr >> kHashSetLengthLog2)) &
                 (kHashSetLength - 1));
550
    if (hash_set_1_[hash1] == int_addr) continue;
551
    uintptr_t hash2 = (hash_addr - (hash_addr >> kHashSetLengthLog2));
552
    hash2 ^= hash2 >> (kHashSetLengthLog2 * 2);
553
    hash2 &= (kHashSetLength - 1);
554 555 556 557 558
    if (hash_set_2_[hash2] == int_addr) continue;
    if (hash_set_1_[hash1] == 0) {
      hash_set_1_[hash1] = int_addr;
    } else if (hash_set_2_[hash2] == 0) {
      hash_set_2_[hash2] = int_addr;
559 560 561
    } else {
      // Rather than slowing down we just throw away some entries.  This will
      // cause some duplicates to remain undetected.
562 563
      hash_set_1_[hash1] = int_addr;
      hash_set_2_[hash2] = 0;
564 565 566 567
    }
    old_buffer_is_sorted_ = false;
    old_buffer_is_filtered_ = false;
    *old_top_++ = reinterpret_cast<Address>(int_addr << kPointerSizeLog2);
568
    DCHECK(old_top_ <= old_limit_);
569 570 571
  }
  heap_->isolate()->counters()->store_buffer_compactions()->Increment();
}
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622


void StoreBufferRebuilder::Callback(MemoryChunk* page, StoreBufferEvent event) {
  if (event == kStoreBufferStartScanningPagesEvent) {
    start_of_current_page_ = NULL;
    current_page_ = NULL;
  } else if (event == kStoreBufferScanningPageEvent) {
    if (current_page_ != NULL) {
      // If this page already overflowed the store buffer during this iteration.
      if (current_page_->scan_on_scavenge()) {
        // Then we should wipe out the entries that have been added for it.
        store_buffer_->SetTop(start_of_current_page_);
      } else if (store_buffer_->Top() - start_of_current_page_ >=
                 (store_buffer_->Limit() - store_buffer_->Top()) >> 2) {
        // Did we find too many pointers in the previous page?  The heuristic is
        // that no page can take more then 1/5 the remaining slots in the store
        // buffer.
        current_page_->set_scan_on_scavenge(true);
        store_buffer_->SetTop(start_of_current_page_);
      } else {
        // In this case the page we scanned took a reasonable number of slots in
        // the store buffer.  It has now been rehabilitated and is no longer
        // marked scan_on_scavenge.
        DCHECK(!current_page_->scan_on_scavenge());
      }
    }
    start_of_current_page_ = store_buffer_->Top();
    current_page_ = page;
  } else if (event == kStoreBufferFullEvent) {
    // The current page overflowed the store buffer again.  Wipe out its entries
    // in the store buffer and mark it scan-on-scavenge again.  This may happen
    // several times while scanning.
    if (current_page_ == NULL) {
      // Store Buffer overflowed while scanning promoted objects.  These are not
      // in any particular page, though they are likely to be clustered by the
      // allocation routines.
      store_buffer_->EnsureSpace(StoreBuffer::kStoreBufferSize / 2);
    } else {
      // Store Buffer overflowed while scanning a particular old space page for
      // pointers to new space.
      DCHECK(current_page_ == page);
      DCHECK(page != NULL);
      current_page_->set_scan_on_scavenge(true);
      DCHECK(start_of_current_page_ != store_buffer_->Top());
      store_buffer_->SetTop(start_of_current_page_);
    }
  } else {
    UNREACHABLE();
  }
}

623 624
}  // namespace internal
}  // namespace v8