local-heap-inl.h 3.19 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2020 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.

#ifndef V8_HEAP_LOCAL_HEAP_INL_H_
#define V8_HEAP_LOCAL_HEAP_INL_H_

8 9
#include <atomic>

10
#include "src/common/assert-scope.h"
11
#include "src/handles/persistent-handles.h"
12
#include "src/heap/concurrent-allocator-inl.h"
13
#include "src/heap/heap.h"
14 15 16 17 18
#include "src/heap/local-heap.h"

namespace v8 {
namespace internal {

19 20 21
AllocationResult LocalHeap::AllocateRaw(int size_in_bytes, AllocationType type,
                                        AllocationOrigin origin,
                                        AllocationAlignment alignment) {
22
  DCHECK(!FLAG_enable_third_party_heap);
23
#if DEBUG
24
  VerifyCurrent();
25 26
  DCHECK(AllowHandleAllocation::IsAllowed());
  DCHECK(AllowHeapAllocation::IsAllowed());
27
  DCHECK_IMPLIES(type == AllocationType::kCode || type == AllocationType::kMap,
28
                 alignment == AllocationAlignment::kTaggedAligned);
29 30
  Heap::HeapState state = heap()->gc_state();
  DCHECK(state == Heap::TEAR_DOWN || state == Heap::NOT_IN_GC);
31
  DCHECK(IsRunning());
32
#endif
33

34 35 36
  // Each allocation is supposed to be a safepoint.
  Safepoint();

37
  bool large_object = size_in_bytes > heap_->MaxRegularHeapObjectSize(type);
38

39 40 41 42 43 44 45 46 47 48 49
  if (type == AllocationType::kCode) {
    AllocationResult alloc;
    if (large_object) {
      alloc =
          heap()->code_lo_space()->AllocateRawBackground(this, size_in_bytes);
    } else {
      alloc =
          code_space_allocator()->AllocateRaw(size_in_bytes, alignment, origin);
    }
    HeapObject object;
    if (alloc.To(&object) && !V8_ENABLE_THIRD_PARTY_HEAP_BOOL) {
50 51
      heap()->UnprotectAndRegisterMemoryChunk(
          object, UnprotectMemoryOrigin::kMaybeOffMainThread);
52 53 54 55 56
      heap()->ZapCodeObject(object.address(), size_in_bytes);
    }
    return alloc;
  }

57 58 59 60 61 62 63 64 65 66 67
  if (type == AllocationType::kOld) {
    if (large_object)
      return heap()->lo_space()->AllocateRawBackground(this, size_in_bytes);
    else
      return old_space_allocator()->AllocateRaw(size_in_bytes, alignment,
                                                origin);
  }

  DCHECK_EQ(type, AllocationType::kSharedOld);
  return shared_old_space_allocator()->AllocateRaw(size_in_bytes, alignment,
                                                   origin);
68 69 70 71 72
}

Address LocalHeap::AllocateRawOrFail(int object_size, AllocationType type,
                                     AllocationOrigin origin,
                                     AllocationAlignment alignment) {
73
  DCHECK(!FLAG_enable_third_party_heap);
74
  AllocationResult result = AllocateRaw(object_size, type, origin, alignment);
75 76
  HeapObject object;
  if (result.To(&object)) return object.address();
77 78 79 80
  return PerformCollectionAndAllocateAgain(object_size, type, origin,
                                           alignment);
}

81 82 83 84 85 86 87
void LocalHeap::CreateFillerObjectAt(Address addr, int size,
                                     ClearRecordedSlots clear_slots_mode) {
  DCHECK_EQ(clear_slots_mode, ClearRecordedSlots::kNo);
  heap()->CreateFillerObjectAtBackground(
      addr, size, ClearFreedMemoryMode::kDontClearFreedMemory);
}

88 89 90 91
}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_LOCAL_HEAP_INL_H_