1 // Copyright 2019 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/basic-memory-chunk.h"
6
7 #include <cstdlib>
8
9 #include "src/heap/heap-write-barrier-inl.h"
10 #include "src/heap/incremental-marking.h"
11 #include "src/objects/heap-object.h"
12
13 namespace v8 {
14 namespace internal {
15
16 // Verify write barrier offsets match the the real offsets.
17 STATIC_ASSERT(BasicMemoryChunk::Flag::INCREMENTAL_MARKING ==
18 heap_internals::MemoryChunk::kMarkingBit);
19 STATIC_ASSERT(BasicMemoryChunk::Flag::FROM_PAGE ==
20 heap_internals::MemoryChunk::kFromPageBit);
21 STATIC_ASSERT(BasicMemoryChunk::Flag::TO_PAGE ==
22 heap_internals::MemoryChunk::kToPageBit);
23 STATIC_ASSERT(BasicMemoryChunk::kFlagsOffset ==
24 heap_internals::MemoryChunk::kFlagsOffset);
25 STATIC_ASSERT(BasicMemoryChunk::kHeapOffset ==
26 heap_internals::MemoryChunk::kHeapOffset);
27
BasicMemoryChunk(size_t size,Address area_start,Address area_end)28 BasicMemoryChunk::BasicMemoryChunk(size_t size, Address area_start,
29 Address area_end) {
30 size_ = size;
31 area_start_ = area_start;
32 area_end_ = area_end;
33 }
34
35 // static
Initialize(Heap * heap,Address base,size_t size,Address area_start,Address area_end,BaseSpace * owner,VirtualMemory reservation)36 BasicMemoryChunk* BasicMemoryChunk::Initialize(Heap* heap, Address base,
37 size_t size, Address area_start,
38 Address area_end,
39 BaseSpace* owner,
40 VirtualMemory reservation) {
41 BasicMemoryChunk* chunk = FromAddress(base);
42 DCHECK_EQ(base, chunk->address());
43 new (chunk) BasicMemoryChunk(size, area_start, area_end);
44
45 chunk->heap_ = heap;
46 chunk->set_owner(owner);
47 chunk->reservation_ = std::move(reservation);
48 chunk->high_water_mark_ = static_cast<intptr_t>(area_start - base);
49 chunk->allocated_bytes_ = chunk->area_size();
50 chunk->wasted_memory_ = 0;
51 chunk->marking_bitmap<AccessMode::NON_ATOMIC>()->Clear();
52
53 return chunk;
54 }
55
InOldSpace() const56 bool BasicMemoryChunk::InOldSpace() const {
57 return owner()->identity() == OLD_SPACE;
58 }
59
InLargeObjectSpace() const60 bool BasicMemoryChunk::InLargeObjectSpace() const {
61 return owner()->identity() == LO_SPACE;
62 }
63
64 #ifdef THREAD_SANITIZER
SynchronizedHeapLoad()65 void BasicMemoryChunk::SynchronizedHeapLoad() {
66 CHECK(reinterpret_cast<Heap*>(base::Acquire_Load(
67 reinterpret_cast<base::AtomicWord*>(&heap_))) != nullptr ||
68 InReadOnlySpace());
69 }
70 #endif
71
72 class BasicMemoryChunkValidator {
73 // Computed offsets should match the compiler generated ones.
74 STATIC_ASSERT(BasicMemoryChunk::kSizeOffset ==
75 offsetof(BasicMemoryChunk, size_));
76 STATIC_ASSERT(BasicMemoryChunk::kFlagsOffset ==
77 offsetof(BasicMemoryChunk, flags_));
78 STATIC_ASSERT(BasicMemoryChunk::kHeapOffset ==
79 offsetof(BasicMemoryChunk, heap_));
80 STATIC_ASSERT(offsetof(BasicMemoryChunk, size_) ==
81 MemoryChunkLayout::kSizeOffset);
82 STATIC_ASSERT(offsetof(BasicMemoryChunk, flags_) ==
83 MemoryChunkLayout::kFlagsOffset);
84 STATIC_ASSERT(offsetof(BasicMemoryChunk, heap_) ==
85 MemoryChunkLayout::kHeapOffset);
86 STATIC_ASSERT(offsetof(BasicMemoryChunk, area_start_) ==
87 MemoryChunkLayout::kAreaStartOffset);
88 STATIC_ASSERT(offsetof(BasicMemoryChunk, area_end_) ==
89 MemoryChunkLayout::kAreaEndOffset);
90 STATIC_ASSERT(offsetof(BasicMemoryChunk, allocated_bytes_) ==
91 MemoryChunkLayout::kAllocatedBytesOffset);
92 STATIC_ASSERT(offsetof(BasicMemoryChunk, wasted_memory_) ==
93 MemoryChunkLayout::kWastedMemoryOffset);
94 STATIC_ASSERT(offsetof(BasicMemoryChunk, high_water_mark_) ==
95 MemoryChunkLayout::kHighWaterMarkOffset);
96 STATIC_ASSERT(offsetof(BasicMemoryChunk, owner_) ==
97 MemoryChunkLayout::kOwnerOffset);
98 STATIC_ASSERT(offsetof(BasicMemoryChunk, reservation_) ==
99 MemoryChunkLayout::kReservationOffset);
100 };
101
102 } // namespace internal
103 } // namespace v8
104