local-handles-inl.h 2.71 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_HANDLES_LOCAL_HANDLES_INL_H_
#define V8_HANDLES_LOCAL_HANDLES_INL_H_

8
#include "src/execution/local-isolate.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include "src/handles/local-handles.h"
#include "src/sanitizer/msan.h"

namespace v8 {
namespace internal {

// static
V8_INLINE Address* LocalHandleScope::GetHandle(LocalHeap* local_heap,
                                               Address value) {
  LocalHandles* handles = local_heap->handles();
  Address* result = handles->scope_.next;
  if (result == handles->scope_.limit) {
    result = handles->AddBlock();
  }
  DCHECK_LT(result, handles->scope_.limit);
  handles->scope_.next++;
  *result = value;
  return result;
}

29 30 31
LocalHandleScope::LocalHandleScope(LocalIsolate* local_isolate)
    : LocalHandleScope(local_isolate->heap()) {}

32 33 34 35 36 37 38 39 40
LocalHandleScope::LocalHandleScope(LocalHeap* local_heap) {
  LocalHandles* handles = local_heap->handles();
  local_heap_ = local_heap;
  prev_next_ = handles->scope_.next;
  prev_limit_ = handles->scope_.limit;
  handles->scope_.level++;
}

LocalHandleScope::~LocalHandleScope() {
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  if (local_heap_ == nullptr) return;
  CloseScope(local_heap_, prev_next_, prev_limit_);
}

template <typename T>
Handle<T> LocalHandleScope::CloseAndEscape(Handle<T> handle_value) {
  HandleScopeData* current = &local_heap_->handles()->scope_;
  T value = *handle_value;
  // Throw away all handles in the current scope.
  CloseScope(local_heap_, prev_next_, prev_limit_);
  // Allocate one handle in the parent scope.
  DCHECK(current->level > current->sealed_level);
  Handle<T> result(value, local_heap_);
  // Reinitialize the current scope (so that it's ready
  // to be used or closed again).
  prev_next_ = current->next;
  prev_limit_ = current->limit;
  current->level++;
  return result;
}

void LocalHandleScope::CloseScope(LocalHeap* local_heap, Address* prev_next,
                                  Address* prev_limit) {
  LocalHandles* handles = local_heap->handles();
65 66
  Address* old_limit = handles->scope_.limit;

67 68
  handles->scope_.next = prev_next;
  handles->scope_.limit = prev_limit;
69 70 71
  handles->scope_.level--;

  if (old_limit != handles->scope_.limit) {
72
    handles->RemoveUnusedBlocks();
73 74 75
    old_limit = handles->scope_.limit;
  }

76 77 78
#ifdef ENABLE_HANDLE_ZAPPING
  LocalHandles::ZapRange(handles->scope_.next, old_limit);
#endif
79 80 81 82 83 84 85 86 87 88 89

  MSAN_ALLOCATED_UNINITIALIZED_MEMORY(
      handles->scope_.next,
      static_cast<size_t>(reinterpret_cast<Address>(old_limit) -
                          reinterpret_cast<Address>(handles->scope_.next)));
}

}  // namespace internal
}  // namespace v8

#endif  // V8_HANDLES_LOCAL_HANDLES_INL_H_