v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
heap-allocator.h
Go to the documentation of this file.
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_HEAP_ALLOCATOR_H_
6#define V8_HEAP_HEAP_ALLOCATOR_H_
7
8#include <optional>
9
10#include "include/v8config.h"
11#include "src/base/macros.h"
12#include "src/common/globals.h"
15
16namespace v8 {
17namespace internal {
18
19class AllocationObserver;
20class CodeLargeObjectSpace;
21class Heap;
22class LocalHeap;
23class LinearAllocationArea;
24class MainAllocator;
25class NewSpace;
26class NewLargeObjectSpace;
27class OldLargeObjectSpace;
28class PagedSpace;
29class ReadOnlySpace;
30class SharedTrustedLargeObjectSpace;
31class Space;
32
33// Allocator for the main thread. All exposed functions internally call the
34// right bottleneck.
36 public:
37 explicit HeapAllocator(LocalHeap*);
38
39 // Set up all LABs for this LocalHeap.
40 void Setup(LinearAllocationArea* new_allocation_info = nullptr,
41 LinearAllocationArea* old_allocation_info = nullptr);
42
43 void SetReadOnlySpace(ReadOnlySpace*);
44
45 // Supports all `AllocationType` types.
46 //
47 // Returns a failed result on an unsuccessful allocation attempt.
49 AllocateRaw(int size_in_bytes, AllocationType allocation,
50 AllocationOrigin origin = AllocationOrigin::kRuntime,
52
53 // Supports all `AllocationType` types. Use when type is statically known.
54 //
55 // Returns a failed result on an unsuccessful allocation attempt.
56 template <AllocationType type>
58 int size_in_bytes, AllocationOrigin origin = AllocationOrigin::kRuntime,
60
61 enum AllocationRetryMode { kLightRetry, kRetryOrFail };
62
63 // Supports all `AllocationType` types and allows specifying retry handling.
64 template <AllocationRetryMode mode>
66 int size, AllocationType allocation,
67 AllocationOrigin origin = AllocationOrigin::kRuntime,
68 AllocationAlignment alignment = kTaggedAligned);
69
70 V8_INLINE bool CanAllocateInReadOnlySpace() const;
71
72#ifdef V8_ENABLE_ALLOCATION_TIMEOUT
73 void UpdateAllocationTimeout();
74 // See `allocation_timeout_`.
75 void SetAllocationTimeout(int allocation_timeout);
76
77 static void SetAllocationGcInterval(int allocation_gc_interval);
78 static void InitializeOncePerProcess();
79
80 std::optional<int> get_allocation_timeout_for_testing() const {
81 return allocation_timeout_;
82 }
83#endif // V8_ENABLE_ALLOCATION_TIMEOUT
84
85 // Give up all LABs. Used for e.g. full GCs.
86 void FreeLinearAllocationAreas();
87
88 // Make all LABs iterable.
89 void MakeLinearAllocationAreasIterable();
90
91#if DEBUG
92 void VerifyLinearAllocationAreas() const;
93#endif // DEBUG
94
95 // Mark/Unmark all LABs except for new and shared space. Use for black
96 // allocation with sticky mark bits.
97 void MarkLinearAllocationAreasBlack();
98 void UnmarkLinearAllocationsArea();
99
100 // Mark/Unmark linear allocation areas in shared heap black. Used for black
101 // allocation with sticky mark bits.
102 void MarkSharedLinearAllocationAreasBlack();
103 void UnmarkSharedLinearAllocationAreas();
104
105 // Free linear allocation areas and reset free-lists.
106 void FreeLinearAllocationAreasAndResetFreeLists();
107 void FreeSharedLinearAllocationAreasAndResetFreeLists();
108
109 void PauseAllocationObservers();
110 void ResumeAllocationObservers();
111
112 void PublishPendingAllocations();
113
114 void AddAllocationObserver(AllocationObserver* observer,
115 AllocationObserver* new_space_observer);
116 void RemoveAllocationObserver(AllocationObserver* observer,
117 AllocationObserver* new_space_observer);
118
119 MainAllocator* new_space_allocator() { return &new_space_allocator_.value(); }
121 return &new_space_allocator_.value();
122 }
123 MainAllocator* old_space_allocator() { return &old_space_allocator_.value(); }
125 return &trusted_space_allocator_.value();
126 }
128 return &code_space_allocator_.value();
129 }
131 return &shared_space_allocator_.value();
132 }
133
134 template <typename Function>
136 Function&& Allocate, AllocationType allocation);
137
138 private:
139 V8_INLINE PagedSpace* code_space() const;
140 V8_INLINE CodeLargeObjectSpace* code_lo_space() const;
141 V8_INLINE NewSpace* new_space() const;
142 V8_INLINE NewLargeObjectSpace* new_lo_space() const;
143 V8_INLINE OldLargeObjectSpace* lo_space() const;
144 V8_INLINE OldLargeObjectSpace* shared_lo_space() const;
145 V8_INLINE OldLargeObjectSpace* shared_trusted_lo_space() const;
146 V8_INLINE PagedSpace* old_space() const;
147 V8_INLINE ReadOnlySpace* read_only_space() const;
148 V8_INLINE PagedSpace* trusted_space() const;
149 V8_INLINE OldLargeObjectSpace* trusted_lo_space() const;
150
151 V8_WARN_UNUSED_RESULT AllocationResult AllocateRawLargeInternal(
152 int size_in_bytes, AllocationType allocation, AllocationOrigin origin,
153 AllocationAlignment alignment);
154
155 template <typename AllocateFunction, typename RetryFunction>
157 AllocateFunction&& Allocate, RetryFunction&& RetryAllocate,
158 AllocationType allocation);
159
160 V8_WARN_UNUSED_RESULT AllocationResult AllocateRawWithRetryOrFailSlowPath(
161 int size, AllocationType allocation, AllocationOrigin origin,
162 AllocationAlignment alignment);
163
164 template <typename AllocateFunction, typename RetryFunction>
165 V8_WARN_UNUSED_RESULT inline auto AllocateRawWithLightRetrySlowPath(
166 AllocateFunction&& Allocate, RetryFunction&& RetryAllocate,
167 AllocationType allocation);
168
169 V8_WARN_UNUSED_RESULT AllocationResult AllocateRawWithLightRetrySlowPath(
170 int size, AllocationType allocation, AllocationOrigin origin,
171 AllocationAlignment alignment);
172
173 void CollectGarbage(AllocationType allocation);
174 void CollectAllAvailableGarbage(AllocationType allocation);
175
177 RetryAllocateRaw(int size_in_bytes, AllocationType allocation,
178 AllocationOrigin origin, AllocationAlignment alignment);
179
181
182#ifdef DEBUG
183 void IncrementObjectCounters();
184#endif // DEBUG
185
187 Heap* const heap_;
188 Space* spaces_[LAST_SPACE + 1];
190
191 std::optional<MainAllocator> new_space_allocator_;
192 std::optional<MainAllocator> old_space_allocator_;
193 std::optional<MainAllocator> trusted_space_allocator_;
194 std::optional<MainAllocator> code_space_allocator_;
195
196 // Allocators for the shared spaces.
197 std::optional<MainAllocator> shared_space_allocator_;
198 std::optional<MainAllocator> shared_trusted_space_allocator_;
201
202#ifdef V8_ENABLE_ALLOCATION_TIMEOUT
203 // Specifies how many allocations should be performed until returning
204 // allocation failure (which will eventually lead to garbage collection).
205 // Allocation will fail for any values <=0. See `UpdateAllocationTimeout()`
206 // for how the new timeout is computed.
207 std::optional<int> allocation_timeout_;
208
209 // The configured GC interval, initialized from --gc-interval during
210 // `InitializeOncePerProcess` and potentially dynamically updated by
211 // `%SetAllocationTimeout()`.
212 static std::atomic<int> allocation_gc_interval_;
213#endif // V8_ENABLE_ALLOCATION_TIMEOUT
214};
215
216} // namespace internal
217} // namespace v8
218
219#endif // V8_HEAP_HEAP_ALLOCATOR_H_
std::optional< MainAllocator > shared_space_allocator_
V8_WARN_UNUSED_RESULT V8_INLINE auto CustomAllocateWithRetryOrFail(Function &&Allocate, AllocationType allocation)
OldLargeObjectSpace * shared_lo_space_
const MainAllocator * new_space_allocator() const
MainAllocator * shared_space_allocator()
MainAllocator * new_space_allocator()
MainAllocator * old_space_allocator()
std::optional< MainAllocator > trusted_space_allocator_
MainAllocator * trusted_space_allocator()
V8_WARN_UNUSED_RESULT auto AllocateRawWithRetryOrFailSlowPath(AllocateFunction &&Allocate, RetryFunction &&RetryAllocate, AllocationType allocation)
std::optional< MainAllocator > old_space_allocator_
std::optional< MainAllocator > shared_trusted_space_allocator_
MainAllocator * code_space_allocator()
std::optional< MainAllocator > code_space_allocator_
SharedTrustedLargeObjectSpace * shared_trusted_lo_space_
std::optional< MainAllocator > new_space_allocator_
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_INLINE
Definition v8config.h:500
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671