handles.cc 3.88 KB
Newer Older
1
// Copyright 2012 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
#include "src/v8.h"
6

7
#include "src/handles.h"
8

9 10
namespace v8 {
namespace internal {
11 12


13
int HandleScope::NumberOfHandles(Isolate* isolate) {
14 15
  HandleScopeImplementer* impl = isolate->handle_scope_implementer();
  int n = impl->blocks()->length();
16
  if (n == 0) return 0;
17
  return ((n - 1) * kHandleBlockSize) + static_cast<int>(
18
      (isolate->handle_scope_data()->next - impl->blocks()->last()));
19 20 21
}


22
Object** HandleScope::Extend(Isolate* isolate) {
23
  HandleScopeData* current = isolate->handle_scope_data();
24

25 26
  Object** result = current->next;

27
  DCHECK(result == current->limit);
28 29
  // Make sure there's at least one scope on the stack and that the
  // top of the scope stack isn't a barrier.
30 31 32
  if (!Utils::ApiCheck(current->level != 0,
                       "v8::HandleScope::CreateHandle()",
                       "Cannot create a handle without a HandleScope")) {
33 34
    return NULL;
  }
35
  HandleScopeImplementer* impl = isolate->handle_scope_implementer();
36 37
  // If there's more room in the last block, we use that. This is used
  // for fast creation of scopes after scope barriers.
38 39
  if (!impl->blocks()->is_empty()) {
    Object** limit = &impl->blocks()->last()[kHandleBlockSize];
40 41
    if (current->limit != limit) {
      current->limit = limit;
42
      DCHECK(limit - current->next < kHandleBlockSize);
43 44
    }
  }
45

46 47
  // If we still haven't found a slot for the handle, we extend the
  // current handle scope by allocating a new handle block.
48
  if (result == current->limit) {
49 50 51 52
    // If there's a spare block, use it for growing the current scope.
    result = impl->GetSpareOrNewBlock();
    // Add the extension to the global list of blocks, but count the
    // extension as part of the current scope.
53
    impl->blocks()->Add(result);
54
    current->limit = &result[kHandleBlockSize];
55
  }
56 57 58 59 60

  return result;
}


61
void HandleScope::DeleteExtensions(Isolate* isolate) {
62
  HandleScopeData* current = isolate->handle_scope_data();
63
  isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
64 65 66
}


67
#ifdef ENABLE_HANDLE_ZAPPING
68
void HandleScope::ZapRange(Object** start, Object** end) {
69
  DCHECK(end - start <= kHandleBlockSize);
70
  for (Object** p = start; p != end; p++) {
71
    *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue;
72 73
  }
}
74
#endif
75 76


77 78
Address HandleScope::current_level_address(Isolate* isolate) {
  return reinterpret_cast<Address>(&isolate->handle_scope_data()->level);
79 80 81
}


82 83
Address HandleScope::current_next_address(Isolate* isolate) {
  return reinterpret_cast<Address>(&isolate->handle_scope_data()->next);
84 85 86
}


87 88
Address HandleScope::current_limit_address(Isolate* isolate) {
  return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit);
89 90 91
}


92 93 94
DeferredHandleScope::DeferredHandleScope(Isolate* isolate)
    : impl_(isolate->handle_scope_implementer()) {
  impl_->BeginDeferredScope();
95
  HandleScopeData* data = impl_->isolate()->handle_scope_data();
96 97
  Object** new_next = impl_->GetSpareOrNewBlock();
  Object** new_limit = &new_next[kHandleBlockSize];
98
  DCHECK(data->limit == &impl_->blocks()->last()[kHandleBlockSize]);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  impl_->blocks()->Add(new_next);

#ifdef DEBUG
  prev_level_ = data->level;
#endif
  data->level++;
  prev_limit_ = data->limit;
  prev_next_ = data->next;
  data->next = new_next;
  data->limit = new_limit;
}


DeferredHandleScope::~DeferredHandleScope() {
  impl_->isolate()->handle_scope_data()->level--;
114 115
  DCHECK(handles_detached_);
  DCHECK(impl_->isolate()->handle_scope_data()->level == prev_level_);
116 117 118 119 120
}


DeferredHandles* DeferredHandleScope::Detach() {
  DeferredHandles* deferred = impl_->Detach(prev_limit_);
121
  HandleScopeData* data = impl_->isolate()->handle_scope_data();
122 123 124 125 126 127 128 129
  data->next = prev_next_;
  data->limit = prev_limit_;
#ifdef DEBUG
  handles_detached_ = true;
#endif
  return deferred;
}

130
} }  // namespace v8::internal