blob: 21f375b195c9b58bb89ea52fc4bb301a5af3eae7 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2011 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/heap/store-buffer.h"
6
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include <algorithm>
8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/counters.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include "src/heap/incremental-marking.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/heap/store-buffer-inl.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include "src/isolate.h"
13#include "src/objects-inl.h"
14#include "src/v8.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015
16namespace v8 {
17namespace internal {
18
19StoreBuffer::StoreBuffer(Heap* heap)
Ben Murdoch097c5b22016-05-18 11:27:45 +010020 : heap_(heap), start_(nullptr), limit_(nullptr), virtual_memory_(nullptr) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021
22void StoreBuffer::SetUp() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023 // Allocate 3x the buffer size, so that we can start the new store buffer
24 // aligned to 2x the size. This lets us use a bit test to detect the end of
25 // the area.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026 virtual_memory_ = new base::VirtualMemory(kStoreBufferSize * 3);
27 uintptr_t start_as_int =
28 reinterpret_cast<uintptr_t>(virtual_memory_->address());
29 start_ =
30 reinterpret_cast<Address*>(RoundUp(start_as_int, kStoreBufferSize * 2));
31 limit_ = start_ + (kStoreBufferSize / kPointerSize);
32
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033 DCHECK(reinterpret_cast<Address>(start_) >= virtual_memory_->address());
34 DCHECK(reinterpret_cast<Address>(limit_) >= virtual_memory_->address());
35 Address* vm_limit = reinterpret_cast<Address*>(
36 reinterpret_cast<char*>(virtual_memory_->address()) +
37 virtual_memory_->size());
38 DCHECK(start_ <= vm_limit);
39 DCHECK(limit_ <= vm_limit);
40 USE(vm_limit);
41 DCHECK((reinterpret_cast<uintptr_t>(limit_) & kStoreBufferOverflowBit) != 0);
42 DCHECK((reinterpret_cast<uintptr_t>(limit_ - 1) & kStoreBufferOverflowBit) ==
43 0);
44
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045 if (!virtual_memory_->Commit(reinterpret_cast<Address>(start_),
46 kStoreBufferSize,
47 false)) { // Not executable.
48 V8::FatalProcessOutOfMemory("StoreBuffer::SetUp");
49 }
50 heap_->set_store_buffer_top(reinterpret_cast<Smi*>(start_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051}
52
53
54void StoreBuffer::TearDown() {
55 delete virtual_memory_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 start_ = limit_ = NULL;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057 heap_->set_store_buffer_top(reinterpret_cast<Smi*>(start_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058}
59
60
61void StoreBuffer::StoreBufferOverflow(Isolate* isolate) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010062 isolate->heap()->store_buffer()->MoveEntriesToRememberedSet();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 isolate->counters()->store_buffer_overflows()->Increment();
64}
65
Ben Murdoch097c5b22016-05-18 11:27:45 +010066void StoreBuffer::MoveEntriesToRememberedSet() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 Address* top = reinterpret_cast<Address*>(heap_->store_buffer_top());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 if (top == start_) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 DCHECK(top <= limit_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 heap_->set_store_buffer_top(reinterpret_cast<Smi*>(start_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 for (Address* current = start_; current < top; current++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 DCHECK(!heap_->code_space()->Contains(*current));
Ben Murdoch097c5b22016-05-18 11:27:45 +010073 Address addr = *current;
74 Page* page = Page::FromAnyPointerAddress(heap_, addr);
75 RememberedSet<OLD_TO_NEW>::Insert(page, addr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078
79} // namespace internal
80} // namespace v8