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 "include/v8config.h" 9 #include "src/base/macros.h" 10 #include "src/common/globals.h" 11 #include "src/heap/allocation-result.h" 12 13 namespace v8 { 14 namespace internal { 15 16 class CodeLargeObjectSpace; 17 class ConcurrentAllocator; 18 class Heap; 19 class NewSpace; 20 class NewLargeObjectSpace; 21 class OldLargeObjectSpace; 22 class PagedSpace; 23 class ReadOnlySpace; 24 class Space; 25 26 // Allocator for the main thread. All exposed functions internally call the 27 // right bottleneck. 28 class V8_EXPORT_PRIVATE HeapAllocator final { 29 public: 30 explicit HeapAllocator(Heap*); 31 32 void Setup(); 33 void SetReadOnlySpace(ReadOnlySpace*); 34 35 // Supports all `AllocationType` types. 36 // 37 // Returns a failed result on an unsuccessful allocation attempt. 38 V8_WARN_UNUSED_RESULT V8_INLINE AllocationResult 39 AllocateRaw(int size_in_bytes, AllocationType allocation, 40 AllocationOrigin origin = AllocationOrigin::kRuntime, 41 AllocationAlignment alignment = kTaggedAligned); 42 43 // Supports all `AllocationType` types. Use when type is statically known. 44 // 45 // Returns a failed result on an unsuccessful allocation attempt. 46 template <AllocationType type> 47 V8_WARN_UNUSED_RESULT V8_INLINE AllocationResult AllocateRaw( 48 int size_in_bytes, AllocationOrigin origin = AllocationOrigin::kRuntime, 49 AllocationAlignment alignment = kTaggedAligned); 50 51 // Supports only `AllocationType::kYoung` and `AllocationType::kOld`. 52 // 53 // Returns a failed result on an unsuccessful allocation attempt. 54 V8_WARN_UNUSED_RESULT V8_INLINE AllocationResult 55 AllocateRawData(int size_in_bytes, AllocationType allocation, 56 AllocationOrigin origin = AllocationOrigin::kRuntime, 57 AllocationAlignment alignment = kTaggedAligned); 58 59 enum AllocationRetryMode { kLightRetry, kRetryOrFail }; 60 61 // Supports all `AllocationType` types and allows specifying retry handling. 62 template <AllocationRetryMode mode> 63 V8_WARN_UNUSED_RESULT V8_INLINE HeapObject 64 AllocateRawWith(int size, AllocationType allocation, 65 AllocationOrigin origin = AllocationOrigin::kRuntime, 66 AllocationAlignment alignment = kTaggedAligned); 67 68 V8_INLINE bool CanAllocateInReadOnlySpace() const; 69 70 #ifdef V8_ENABLE_ALLOCATION_TIMEOUT 71 void UpdateAllocationTimeout(); 72 void SetAllocationTimeout(int allocation_timeout); 73 #endif // V8_ENABLE_ALLOCATION_TIMEOUT 74 75 private: 76 V8_INLINE PagedSpace* code_space() const; 77 V8_INLINE CodeLargeObjectSpace* code_lo_space() const; 78 V8_INLINE PagedSpace* space_for_maps() const; 79 V8_INLINE NewSpace* new_space() const; 80 V8_INLINE NewLargeObjectSpace* new_lo_space() const; 81 V8_INLINE OldLargeObjectSpace* lo_space() const; 82 V8_INLINE PagedSpace* old_space() const; 83 V8_INLINE ReadOnlySpace* read_only_space() const; 84 85 V8_WARN_UNUSED_RESULT AllocationResult AllocateRawLargeInternal( 86 int size_in_bytes, AllocationType allocation, AllocationOrigin origin, 87 AllocationAlignment alignment); 88 89 V8_WARN_UNUSED_RESULT AllocationResult AllocateRawWithRetryOrFailSlowPath( 90 int size, AllocationType allocation, AllocationOrigin origin, 91 AllocationAlignment alignment); 92 93 V8_WARN_UNUSED_RESULT AllocationResult AllocateRawWithLightRetrySlowPath( 94 int size, AllocationType allocation, AllocationOrigin origin, 95 AllocationAlignment alignment); 96 97 #ifdef DEBUG 98 void IncrementObjectCounters(); 99 #endif // DEBUG 100 101 Heap* const heap_; 102 Space* spaces_[LAST_SPACE + 1]; 103 PagedSpace* space_for_maps_; 104 ReadOnlySpace* read_only_space_; 105 106 ConcurrentAllocator* shared_old_allocator_; 107 ConcurrentAllocator* shared_map_allocator_; 108 109 #ifdef V8_ENABLE_ALLOCATION_TIMEOUT 110 // If the --gc-interval flag is set to a positive value, this variable 111 // holds the value indicating the number of allocations remain until the 112 // next failure and garbage collection. 113 int allocation_timeout_ = 0; 114 #endif // V8_ENABLE_ALLOCATION_TIMEOUT 115 }; 116 117 } // namespace internal 118 } // namespace v8 119 120 #endif // V8_HEAP_HEAP_ALLOCATOR_H_ 121