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