1 // Copyright 2017 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_ALLOCATOR_H_ 6 #define V8_HEAP_LOCAL_ALLOCATOR_H_ 7 8 #include "src/common/globals.h" 9 #include "src/heap/heap.h" 10 #include "src/heap/new-spaces.h" 11 #include "src/heap/paged-spaces.h" 12 #include "src/heap/spaces.h" 13 14 namespace v8 { 15 namespace internal { 16 17 // Allocator encapsulating thread-local allocation durning collection. Assumes 18 // that all other allocations also go through EvacuationAllocator. 19 class EvacuationAllocator { 20 public: 21 static const int kLabSize = 32 * KB; 22 static const int kMaxLabObjectSize = 8 * KB; 23 EvacuationAllocator(Heap * heap,LocalSpaceKind local_space_kind)24 explicit EvacuationAllocator(Heap* heap, LocalSpaceKind local_space_kind) 25 : heap_(heap), 26 new_space_(heap->new_space()), 27 compaction_spaces_(heap, local_space_kind), 28 new_space_lab_(LocalAllocationBuffer::InvalidBuffer()), 29 lab_allocation_will_fail_(false) {} 30 31 // Needs to be called from the main thread to finalize this 32 // EvacuationAllocator. Finalize()33 void Finalize() { 34 heap_->old_space()->MergeLocalSpace(compaction_spaces_.Get(OLD_SPACE)); 35 heap_->code_space()->MergeLocalSpace(compaction_spaces_.Get(CODE_SPACE)); 36 // Give back remaining LAB space if this EvacuationAllocator's new space LAB 37 // sits right next to new space allocation top. 38 const LinearAllocationArea info = new_space_lab_.CloseAndMakeIterable(); 39 new_space_->MaybeFreeUnusedLab(info); 40 } 41 42 inline AllocationResult Allocate(AllocationSpace space, int object_size, 43 AllocationOrigin origin, 44 AllocationAlignment alignment); 45 inline void FreeLast(AllocationSpace space, HeapObject object, 46 int object_size); 47 48 private: 49 inline AllocationResult AllocateInNewSpace(int object_size, 50 AllocationOrigin origin, 51 AllocationAlignment alignment); 52 inline bool NewLocalAllocationBuffer(); 53 inline AllocationResult AllocateInLAB(int object_size, 54 AllocationAlignment alignment); 55 inline void FreeLastInNewSpace(HeapObject object, int object_size); 56 inline void FreeLastInOldSpace(HeapObject object, int object_size); 57 58 Heap* const heap_; 59 NewSpace* const new_space_; 60 CompactionSpaceCollection compaction_spaces_; 61 LocalAllocationBuffer new_space_lab_; 62 bool lab_allocation_will_fail_; 63 }; 64 65 } // namespace internal 66 } // namespace v8 67 68 #endif // V8_HEAP_LOCAL_ALLOCATOR_H_ 69