• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EVACUATION_ALLOCATOR_H_
6 #define V8_HEAP_EVACUATION_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,CompactionSpaceKind compaction_space_kind)24   explicit EvacuationAllocator(Heap* heap,
25                                CompactionSpaceKind compaction_space_kind)
26       : heap_(heap),
27         new_space_(heap->new_space()),
28         compaction_spaces_(heap, compaction_space_kind),
29         new_space_lab_(LocalAllocationBuffer::InvalidBuffer()),
30         lab_allocation_will_fail_(false) {}
31 
32   // Needs to be called from the main thread to finalize this
33   // EvacuationAllocator.
Finalize()34   void Finalize() {
35     heap_->old_space()->MergeCompactionSpace(compaction_spaces_.Get(OLD_SPACE));
36     heap_->code_space()->MergeCompactionSpace(
37         compaction_spaces_.Get(CODE_SPACE));
38     if (heap_->map_space()) {
39       heap_->map_space()->MergeCompactionSpace(
40           compaction_spaces_.Get(MAP_SPACE));
41     }
42 
43     // Give back remaining LAB space if this EvacuationAllocator's new space LAB
44     // sits right next to new space allocation top.
45     const LinearAllocationArea info = new_space_lab_.CloseAndMakeIterable();
46     if (new_space_) new_space_->MaybeFreeUnusedLab(info);
47   }
48 
49   inline AllocationResult Allocate(AllocationSpace space, int object_size,
50                                    AllocationOrigin origin,
51                                    AllocationAlignment alignment);
52   inline void FreeLast(AllocationSpace space, HeapObject object,
53                        int object_size);
54 
55  private:
56   inline AllocationResult AllocateInNewSpace(int object_size,
57                                              AllocationOrigin origin,
58                                              AllocationAlignment alignment);
59   inline bool NewLocalAllocationBuffer();
60   inline AllocationResult AllocateInLAB(int object_size,
61                                         AllocationAlignment alignment);
62   inline void FreeLastInNewSpace(HeapObject object, int object_size);
63   inline void FreeLastInOldSpace(HeapObject object, int object_size);
64   inline void FreeLastInMapSpace(HeapObject object, int object_size);
65 
66   Heap* const heap_;
67   NewSpace* const new_space_;
68   CompactionSpaceCollection compaction_spaces_;
69   LocalAllocationBuffer new_space_lab_;
70   bool lab_allocation_will_fail_;
71 };
72 
73 }  // namespace internal
74 }  // namespace v8
75 
76 #endif  // V8_HEAP_EVACUATION_ALLOCATOR_H_
77