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_CONCURRENT_ALLOCATOR_H_ 6 #define V8_HEAP_CONCURRENT_ALLOCATOR_H_ 7 8 #include "src/common/globals.h" 9 #include "src/heap/heap.h" 10 #include "src/heap/spaces.h" 11 #include "src/tasks/cancelable-task.h" 12 13 namespace v8 { 14 namespace internal { 15 16 class LocalHeap; 17 18 class StressConcurrentAllocatorTask : public CancelableTask { 19 public: StressConcurrentAllocatorTask(Isolate * isolate)20 explicit StressConcurrentAllocatorTask(Isolate* isolate) 21 : CancelableTask(isolate), isolate_(isolate) {} 22 23 void RunInternal() override; 24 25 // Schedules task on background thread 26 static void Schedule(Isolate* isolate); 27 28 private: 29 Isolate* isolate_; 30 }; 31 32 // Concurrent allocator for allocation from background threads/tasks. 33 // Allocations are served from a TLAB if possible. 34 class ConcurrentAllocator { 35 public: 36 static const int kLabSize = 4 * KB; 37 static const int kMaxLabSize = 32 * KB; 38 static const int kMaxLabObjectSize = 2 * KB; 39 ConcurrentAllocator(LocalHeap * local_heap,PagedSpace * space)40 explicit ConcurrentAllocator(LocalHeap* local_heap, PagedSpace* space) 41 : local_heap_(local_heap), 42 space_(space), 43 lab_(LocalAllocationBuffer::InvalidBuffer()) {} 44 45 inline AllocationResult AllocateRaw(int object_size, 46 AllocationAlignment alignment, 47 AllocationOrigin origin); 48 49 void FreeLinearAllocationArea(); 50 void MakeLinearAllocationAreaIterable(); 51 void MarkLinearAllocationAreaBlack(); 52 void UnmarkLinearAllocationArea(); 53 54 private: 55 V8_EXPORT_PRIVATE AllocationResult AllocateInLabSlow( 56 int object_size, AllocationAlignment alignment, AllocationOrigin origin); 57 bool EnsureLab(AllocationOrigin origin); 58 59 inline AllocationResult AllocateInLab(int object_size, 60 AllocationAlignment alignment, 61 AllocationOrigin origin); 62 63 V8_EXPORT_PRIVATE AllocationResult AllocateOutsideLab( 64 int object_size, AllocationAlignment alignment, AllocationOrigin origin); 65 66 bool IsBlackAllocationEnabled() const; 67 68 // Returns the Heap of space_. This might differ from the LocalHeap's Heap for 69 // shared spaces. 70 Heap* owning_heap() const; 71 72 LocalHeap* const local_heap_; 73 PagedSpace* const space_; 74 LocalAllocationBuffer lab_; 75 }; 76 77 } // namespace internal 78 } // namespace v8 79 80 #endif // V8_HEAP_CONCURRENT_ALLOCATOR_H_ 81