• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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/memory-chunk-layout.h"
6 
7 #include "src/heap/marking.h"
8 #include "src/heap/memory-allocator.h"
9 #include "src/heap/memory-chunk.h"
10 
11 namespace v8 {
12 namespace internal {
13 
CodePageGuardStartOffset()14 size_t MemoryChunkLayout::CodePageGuardStartOffset() {
15   // We are guarding code pages: the first OS page after the header
16   // will be protected as non-writable.
17   return ::RoundUp(MemoryChunk::kHeaderSize + Bitmap::kSize,
18                    MemoryAllocator::GetCommitPageSize());
19 }
20 
CodePageGuardSize()21 size_t MemoryChunkLayout::CodePageGuardSize() {
22   return MemoryAllocator::GetCommitPageSize();
23 }
24 
ObjectStartOffsetInCodePage()25 intptr_t MemoryChunkLayout::ObjectStartOffsetInCodePage() {
26   // We are guarding code pages: the first OS page after the header
27   // will be protected as non-writable.
28   return CodePageGuardStartOffset() + CodePageGuardSize();
29 }
30 
ObjectEndOffsetInCodePage()31 intptr_t MemoryChunkLayout::ObjectEndOffsetInCodePage() {
32   // We are guarding code pages: the last OS page will be protected as
33   // non-writable.
34   return MemoryChunk::kPageSize -
35          static_cast<int>(MemoryAllocator::GetCommitPageSize());
36 }
37 
AllocatableMemoryInCodePage()38 size_t MemoryChunkLayout::AllocatableMemoryInCodePage() {
39   size_t memory = ObjectEndOffsetInCodePage() - ObjectStartOffsetInCodePage();
40   return memory;
41 }
42 
ObjectStartOffsetInDataPage()43 intptr_t MemoryChunkLayout::ObjectStartOffsetInDataPage() {
44   return RoundUp(MemoryChunk::kHeaderSize + Bitmap::kSize, kTaggedSize);
45 }
46 
ObjectStartOffsetInMemoryChunk(AllocationSpace space)47 size_t MemoryChunkLayout::ObjectStartOffsetInMemoryChunk(
48     AllocationSpace space) {
49   if (space == CODE_SPACE) {
50     return ObjectStartOffsetInCodePage();
51   }
52   return ObjectStartOffsetInDataPage();
53 }
54 
AllocatableMemoryInDataPage()55 size_t MemoryChunkLayout::AllocatableMemoryInDataPage() {
56   size_t memory = MemoryChunk::kPageSize - ObjectStartOffsetInDataPage();
57   DCHECK_LE(kMaxRegularHeapObjectSize, memory);
58   return memory;
59 }
60 
AllocatableMemoryInMemoryChunk(AllocationSpace space)61 size_t MemoryChunkLayout::AllocatableMemoryInMemoryChunk(
62     AllocationSpace space) {
63   if (space == CODE_SPACE) {
64     return AllocatableMemoryInCodePage();
65   }
66   return AllocatableMemoryInDataPage();
67 }
68 
MaxRegularCodeObjectSize()69 int MemoryChunkLayout::MaxRegularCodeObjectSize() {
70   int size = static_cast<int>(AllocatableMemoryInCodePage() / 2);
71   DCHECK_LE(size, kMaxRegularHeapObjectSize);
72   return size;
73 }
74 
75 }  // namespace internal
76 }  // namespace v8
77