local-allocator.h 2.72 KB
Newer Older
1 2 3 4
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
#ifndef V8_HEAP_LOCAL_ALLOCATOR_H_
#define V8_HEAP_LOCAL_ALLOCATOR_H_

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 13 14 15 16
#include "src/heap/spaces.h"

namespace v8 {
namespace internal {

17 18 19
// Allocator encapsulating thread-local allocation durning collection. Assumes
// that all other allocations also go through EvacuationAllocator.
class EvacuationAllocator {
20 21 22 23
 public:
  static const int kLabSize = 32 * KB;
  static const int kMaxLabObjectSize = 8 * KB;

24
  explicit EvacuationAllocator(Heap* heap, LocalSpaceKind local_space_kind)
25 26
      : heap_(heap),
        new_space_(heap->new_space()),
27
        compaction_spaces_(heap, local_space_kind),
28 29
        new_space_lab_(LocalAllocationBuffer::InvalidBuffer()),
        lab_allocation_will_fail_(false) {}
30

31 32
  // Needs to be called from the main thread to finalize this
  // EvacuationAllocator.
33
  void Finalize() {
34 35
    heap_->old_space()->MergeLocalSpace(compaction_spaces_.Get(OLD_SPACE));
    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
    const Address top = new_space_->top();
40 41
    if (info.limit() != kNullAddress && info.limit() == top) {
      DCHECK_NE(info.top(), kNullAddress);
42 43 44 45
      *new_space_->allocation_top_address() = info.top();
    }
  }

46
  inline AllocationResult Allocate(AllocationSpace space, int object_size,
47
                                   AllocationOrigin origin,
48
                                   AllocationAlignment alignment);
49
  inline void FreeLast(AllocationSpace space, HeapObject object,
50
                       int object_size);
51

52
 private:
53
  inline AllocationResult AllocateInNewSpace(int object_size,
54
                                             AllocationOrigin origin,
55 56 57 58
                                             AllocationAlignment alignment);
  inline bool NewLocalAllocationBuffer();
  inline AllocationResult AllocateInLAB(int object_size,
                                        AllocationAlignment alignment);
59 60
  inline void FreeLastInNewSpace(HeapObject object, int object_size);
  inline void FreeLastInOldSpace(HeapObject object, int object_size);
61

62 63 64 65
  Heap* const heap_;
  NewSpace* const new_space_;
  CompactionSpaceCollection compaction_spaces_;
  LocalAllocationBuffer new_space_lab_;
66
  bool lab_allocation_will_fail_;
67 68 69 70
};

}  // namespace internal
}  // namespace v8
71 72

#endif  // V8_HEAP_LOCAL_ALLOCATOR_H_