handles-inl.h 4.25 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6

#ifndef V8_HANDLES_INL_H_
#define V8_HANDLES_INL_H_
7

8 9
#include "src/api.h"
#include "src/handles.h"
10
#include "src/heap/heap.h"
11
#include "src/isolate.h"
12

13 14 15
namespace v8 {
namespace internal {

16
HandleBase::HandleBase(Object* object, Isolate* isolate)
17 18 19 20 21 22 23 24 25
    : location_(HandleScope::GetHandle(isolate, object)) {}


template <typename T>
// Allocate a new handle for the object, do not canonicalize.
Handle<T> Handle<T>::New(T* object, Isolate* isolate) {
  return Handle(
      reinterpret_cast<T**>(HandleScope::CreateHandle(isolate, object)));
}
26 27 28


HandleScope::HandleScope(Isolate* isolate) {
29
  HandleScopeData* data = isolate->handle_scope_data();
30
  isolate_ = isolate;
31 32 33
  prev_next_ = data->next;
  prev_limit_ = data->limit;
  data->level++;
34 35 36
}


37 38 39 40 41 42
template <typename T>
inline std::ostream& operator<<(std::ostream& os, Handle<T> handle) {
  return os << Brief(*handle);
}


43
HandleScope::~HandleScope() {
44 45 46 47 48 49 50 51 52 53 54 55 56
#ifdef DEBUG
  if (FLAG_check_handle_count) {
    int before = NumberOfHandles(isolate_);
    CloseScope(isolate_, prev_next_, prev_limit_);
    int after = NumberOfHandles(isolate_);
    DCHECK(after - before < kCheckHandleThreshold);
    DCHECK(before < kCheckHandleThreshold);
  } else {
#endif  // DEBUG
    CloseScope(isolate_, prev_next_, prev_limit_);
#ifdef DEBUG
  }
#endif  // DEBUG
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}


void HandleScope::CloseScope(Isolate* isolate,
                             Object** prev_next,
                             Object** prev_limit) {
  HandleScopeData* current = isolate->handle_scope_data();

  std::swap(current->next, prev_next);
  current->level--;
  if (current->limit != prev_limit) {
    current->limit = prev_limit;
    DeleteExtensions(isolate);
#ifdef ENABLE_HANDLE_ZAPPING
    ZapRange(current->next, prev_limit);
  } else {
    ZapRange(current->next, prev_next);
#endif
  }
}


template <typename T>
Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) {
  HandleScopeData* current = isolate_->handle_scope_data();

  T* value = *handle_value;
  // Throw away all handles in the current scope.
  CloseScope(isolate_, prev_next_, prev_limit_);
  // Allocate one handle in the parent scope.
87
  DCHECK(current->level > current->sealed_level);
88
  Handle<T> result(value, isolate_);
89 90 91 92 93 94 95 96 97
  // 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;
}


98
Object** HandleScope::CreateHandle(Isolate* isolate, Object* value) {
99
  DCHECK(AllowHandleAllocation::IsAllowed());
100
  HandleScopeData* data = isolate->handle_scope_data();
101

102 103
  Object** result = data->next;
  if (result == data->limit) result = Extend(isolate);
104 105
  // Update the current next field, set the value in the created
  // handle, and return the result.
106 107
  DCHECK(result < data->limit);
  data->next = result + 1;
108 109 110 111 112 113

  *result = value;
  return result;
}


114 115 116 117 118 119 120 121
Object** HandleScope::GetHandle(Isolate* isolate, Object* value) {
  DCHECK(AllowHandleAllocation::IsAllowed());
  HandleScopeData* data = isolate->handle_scope_data();
  CanonicalHandleScope* canonical = data->canonical_scope;
  return canonical ? canonical->Lookup(value) : CreateHandle(isolate, value);
}


122 123 124 125 126 127 128
#ifdef DEBUG
inline SealHandleScope::SealHandleScope(Isolate* isolate) : isolate_(isolate) {
  // Make sure the current thread is allowed to create handles to begin with.
  CHECK(AllowHandleAllocation::IsAllowed());
  HandleScopeData* current = isolate_->handle_scope_data();
  // Shrink the current handle scope to make it impossible to do
  // handle allocations without an explicit handle scope.
129
  prev_limit_ = current->limit;
130
  current->limit = current->next;
131 132
  prev_sealed_level_ = current->sealed_level;
  current->sealed_level = current->level;
133 134 135 136 137 138 139 140
}


inline SealHandleScope::~SealHandleScope() {
  // Restore state in current handle scope to re-enable handle
  // allocations.
  HandleScopeData* current = isolate_->handle_scope_data();
  DCHECK_EQ(current->next, current->limit);
141 142 143
  current->limit = prev_limit_;
  DCHECK_EQ(current->level, current->sealed_level);
  current->sealed_level = prev_sealed_level_;
144 145 146 147
}

#endif

148 149
}  // namespace internal
}  // namespace v8
150 151

#endif  // V8_HANDLES_INL_H_