local-allocator.h 2.43 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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include "src/globals.h"
#include "src/heap/heap.h"
#include "src/heap/spaces.h"

namespace v8 {
namespace internal {

// Allocator encapsulating thread-local allocation. Assumes that all other
// allocations also go through LocalAllocator.
class LocalAllocator {
 public:
  static const int kLabSize = 32 * KB;
  static const int kMaxLabObjectSize = 8 * KB;

  explicit LocalAllocator(Heap* heap)
      : heap_(heap),
        new_space_(heap->new_space()),
        compaction_spaces_(heap),
26 27
        new_space_lab_(LocalAllocationBuffer::InvalidBuffer()),
        lab_allocation_will_fail_(false) {}
28 29 30 31

  // Needs to be called from the main thread to finalize this LocalAllocator.
  void Finalize() {
    heap_->old_space()->MergeCompactionSpace(compaction_spaces_.Get(OLD_SPACE));
32 33
    heap_->code_space()->MergeCompactionSpace(
        compaction_spaces_.Get(CODE_SPACE));
34 35
    // Give back remaining LAB space if this LocalAllocator's new space LAB
    // sits right next to new space allocation top.
36
    const LinearAllocationArea info = new_space_lab_.Close();
37
    const Address top = new_space_->top();
38 39
    if (info.limit() != kNullAddress && info.limit() == top) {
      DCHECK_NE(info.top(), kNullAddress);
40 41 42 43
      *new_space_->allocation_top_address() = info.top();
    }
  }

44 45
  inline AllocationResult Allocate(AllocationSpace space, int object_size,
                                   AllocationAlignment alignment);
46
  inline void FreeLast(AllocationSpace space, HeapObject object,
47
                       int object_size);
48

49
 private:
50 51 52 53 54
  inline AllocationResult AllocateInNewSpace(int object_size,
                                             AllocationAlignment alignment);
  inline bool NewLocalAllocationBuffer();
  inline AllocationResult AllocateInLAB(int object_size,
                                        AllocationAlignment alignment);
55 56
  inline void FreeLastInNewSpace(HeapObject object, int object_size);
  inline void FreeLastInOldSpace(HeapObject object, int object_size);
57

58 59 60 61
  Heap* const heap_;
  NewSpace* const new_space_;
  CompactionSpaceCollection compaction_spaces_;
  LocalAllocationBuffer new_space_lab_;
62
  bool lab_allocation_will_fail_;
63 64 65 66
};

}  // namespace internal
}  // namespace v8
67 68

#endif  // V8_HEAP_LOCAL_ALLOCATOR_H_