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/globals.h" 9 #include "src/heap/heap.h" 10 #include "src/heap/spaces.h" 11 12 namespace v8 { 13 namespace internal { 14 15 // Allocator encapsulating thread-local allocation. Assumes that all other 16 // allocations also go through LocalAllocator. 17 class LocalAllocator { 18 public: 19 static const int kLabSize = 32 * KB; 20 static const int kMaxLabObjectSize = 8 * KB; 21 LocalAllocator(Heap * heap)22 explicit LocalAllocator(Heap* heap) 23 : heap_(heap), 24 new_space_(heap->new_space()), 25 compaction_spaces_(heap), 26 new_space_lab_(LocalAllocationBuffer::InvalidBuffer()), 27 lab_allocation_will_fail_(false) {} 28 29 // Needs to be called from the main thread to finalize this LocalAllocator. Finalize()30 void Finalize() { 31 heap_->old_space()->MergeCompactionSpace(compaction_spaces_.Get(OLD_SPACE)); 32 heap_->code_space()->MergeCompactionSpace( 33 compaction_spaces_.Get(CODE_SPACE)); 34 // Give back remaining LAB space if this LocalAllocator's new space LAB 35 // sits right next to new space allocation top. 36 const LinearAllocationArea info = new_space_lab_.Close(); 37 const Address top = new_space_->top(); 38 if (info.limit() != kNullAddress && info.limit() == top) { 39 DCHECK_NE(info.top(), kNullAddress); 40 *new_space_->allocation_top_address() = info.top(); 41 } 42 } 43 44 inline AllocationResult Allocate(AllocationSpace space, int object_size, 45 AllocationAlignment alignment); 46 inline void FreeLast(AllocationSpace space, HeapObject* object, 47 int object_size); 48 49 private: 50 inline AllocationResult AllocateInNewSpace(int object_size, 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