• 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 #ifndef V8_HEAP_LOCAL_HEAP_INL_H_
6 #define V8_HEAP_LOCAL_HEAP_INL_H_
7 
8 #include <atomic>
9 
10 #include "src/common/assert-scope.h"
11 #include "src/handles/persistent-handles.h"
12 #include "src/heap/concurrent-allocator-inl.h"
13 #include "src/heap/heap.h"
14 #include "src/heap/local-heap.h"
15 
16 namespace v8 {
17 namespace internal {
18 
AllocateRaw(int size_in_bytes,AllocationType type,AllocationOrigin origin,AllocationAlignment alignment)19 AllocationResult LocalHeap::AllocateRaw(int size_in_bytes, AllocationType type,
20                                         AllocationOrigin origin,
21                                         AllocationAlignment alignment) {
22   DCHECK(!FLAG_enable_third_party_heap);
23 #if DEBUG
24   VerifyCurrent();
25   DCHECK(AllowHandleAllocation::IsAllowed());
26   DCHECK(AllowHeapAllocation::IsAllowed());
27   DCHECK_IMPLIES(type == AllocationType::kCode || type == AllocationType::kMap,
28                  alignment == AllocationAlignment::kTaggedAligned);
29   Heap::HeapState state = heap()->gc_state();
30   DCHECK(state == Heap::TEAR_DOWN || state == Heap::NOT_IN_GC);
31   DCHECK(IsRunning());
32 #endif
33 
34   // Each allocation is supposed to be a safepoint.
35   Safepoint();
36 
37   bool large_object = size_in_bytes > heap_->MaxRegularHeapObjectSize(type);
38 
39   if (type == AllocationType::kCode) {
40     AllocationResult alloc;
41     if (large_object) {
42       alloc =
43           heap()->code_lo_space()->AllocateRawBackground(this, size_in_bytes);
44     } else {
45       alloc =
46           code_space_allocator()->AllocateRaw(size_in_bytes, alignment, origin);
47     }
48     HeapObject object;
49     if (alloc.To(&object) && !V8_ENABLE_THIRD_PARTY_HEAP_BOOL) {
50       heap()->UnprotectAndRegisterMemoryChunk(
51           object, UnprotectMemoryOrigin::kMaybeOffMainThread);
52       heap()->ZapCodeObject(object.address(), size_in_bytes);
53     }
54     return alloc;
55   }
56 
57   if (type == AllocationType::kOld) {
58     if (large_object)
59       return heap()->lo_space()->AllocateRawBackground(this, size_in_bytes);
60     else
61       return old_space_allocator()->AllocateRaw(size_in_bytes, alignment,
62                                                 origin);
63   }
64 
65   DCHECK_EQ(type, AllocationType::kSharedOld);
66   return shared_old_space_allocator()->AllocateRaw(size_in_bytes, alignment,
67                                                    origin);
68 }
69 
AllocateRawOrFail(int object_size,AllocationType type,AllocationOrigin origin,AllocationAlignment alignment)70 Address LocalHeap::AllocateRawOrFail(int object_size, AllocationType type,
71                                      AllocationOrigin origin,
72                                      AllocationAlignment alignment) {
73   DCHECK(!FLAG_enable_third_party_heap);
74   AllocationResult result = AllocateRaw(object_size, type, origin, alignment);
75   HeapObject object;
76   if (result.To(&object)) return object.address();
77   return PerformCollectionAndAllocateAgain(object_size, type, origin,
78                                            alignment);
79 }
80 
CreateFillerObjectAt(Address addr,int size,ClearRecordedSlots clear_slots_mode)81 void LocalHeap::CreateFillerObjectAt(Address addr, int size,
82                                      ClearRecordedSlots clear_slots_mode) {
83   DCHECK_EQ(clear_slots_mode, ClearRecordedSlots::kNo);
84   heap()->CreateFillerObjectAtBackground(
85       addr, size, ClearFreedMemoryMode::kDontClearFreedMemory);
86 }
87 
88 }  // namespace internal
89 }  // namespace v8
90 
91 #endif  // V8_HEAP_LOCAL_HEAP_INL_H_
92