blob: d9b130f3cac3d0d25dfd8072e347e4e5614fa1e7 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/handles.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00008
9namespace v8 {
10namespace internal {
11
12
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013int HandleScope::NumberOfHandles(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +010014 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
15 int n = impl->blocks()->length();
Steve Blocka7e24c12009-10-30 11:49:00 +000016 if (n == 0) return 0;
Steve Blockd0582a62009-12-15 09:54:21 +000017 return ((n - 1) * kHandleBlockSize) + static_cast<int>(
Steve Block44f0eee2011-05-26 01:26:41 +010018 (isolate->handle_scope_data()->next - impl->blocks()->last()));
Steve Blocka7e24c12009-10-30 11:49:00 +000019}
20
21
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022Object** HandleScope::Extend(Isolate* isolate) {
23 HandleScopeData* current = isolate->handle_scope_data();
Steve Blocka7e24c12009-10-30 11:49:00 +000024
Steve Block44f0eee2011-05-26 01:26:41 +010025 Object** result = current->next;
26
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027 DCHECK(result == current->limit);
Steve Blocka7e24c12009-10-30 11:49:00 +000028 // Make sure there's at least one scope on the stack and that the
29 // top of the scope stack isn't a barrier.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 if (!Utils::ApiCheck(current->level != 0,
31 "v8::HandleScope::CreateHandle()",
32 "Cannot create a handle without a HandleScope")) {
Steve Blocka7e24c12009-10-30 11:49:00 +000033 return NULL;
34 }
Steve Block44f0eee2011-05-26 01:26:41 +010035 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
Steve Blocka7e24c12009-10-30 11:49:00 +000036 // If there's more room in the last block, we use that. This is used
37 // for fast creation of scopes after scope barriers.
38 if (!impl->blocks()->is_empty()) {
39 Object** limit = &impl->blocks()->last()[kHandleBlockSize];
Steve Block44f0eee2011-05-26 01:26:41 +010040 if (current->limit != limit) {
41 current->limit = limit;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 DCHECK(limit - current->next < kHandleBlockSize);
Steve Blocka7e24c12009-10-30 11:49:00 +000043 }
44 }
45
46 // If we still haven't found a slot for the handle, we extend the
47 // current handle scope by allocating a new handle block.
Steve Block44f0eee2011-05-26 01:26:41 +010048 if (result == current->limit) {
Steve Blocka7e24c12009-10-30 11:49:00 +000049 // If there's a spare block, use it for growing the current scope.
50 result = impl->GetSpareOrNewBlock();
51 // Add the extension to the global list of blocks, but count the
52 // extension as part of the current scope.
53 impl->blocks()->Add(result);
Steve Block44f0eee2011-05-26 01:26:41 +010054 current->limit = &result[kHandleBlockSize];
Steve Blocka7e24c12009-10-30 11:49:00 +000055 }
56
57 return result;
58}
59
60
Steve Block44f0eee2011-05-26 01:26:41 +010061void HandleScope::DeleteExtensions(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 HandleScopeData* current = isolate->handle_scope_data();
Steve Block44f0eee2011-05-26 01:26:41 +010063 isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
Steve Blocka7e24c12009-10-30 11:49:00 +000064}
65
66
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067#ifdef ENABLE_HANDLE_ZAPPING
Steve Blocka7e24c12009-10-30 11:49:00 +000068void HandleScope::ZapRange(Object** start, Object** end) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 DCHECK(end - start <= kHandleBlockSize);
John Reck59135872010-11-02 12:39:01 -070070 for (Object** p = start; p != end; p++) {
Steve Blocka7e24c12009-10-30 11:49:00 +000071 *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue;
72 }
73}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000075
76
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077Address HandleScope::current_level_address(Isolate* isolate) {
78 return reinterpret_cast<Address>(&isolate->handle_scope_data()->level);
Steve Blockd0582a62009-12-15 09:54:21 +000079}
80
81
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082Address HandleScope::current_next_address(Isolate* isolate) {
83 return reinterpret_cast<Address>(&isolate->handle_scope_data()->next);
Steve Blockd0582a62009-12-15 09:54:21 +000084}
85
86
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087Address HandleScope::current_limit_address(Isolate* isolate) {
88 return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit);
Steve Blockd0582a62009-12-15 09:54:21 +000089}
90
91
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092DeferredHandleScope::DeferredHandleScope(Isolate* isolate)
93 : impl_(isolate->handle_scope_implementer()) {
94 impl_->BeginDeferredScope();
95 HandleScopeData* data = impl_->isolate()->handle_scope_data();
96 Object** new_next = impl_->GetSpareOrNewBlock();
97 Object** new_limit = &new_next[kHandleBlockSize];
98 DCHECK(data->limit == &impl_->blocks()->last()[kHandleBlockSize]);
99 impl_->blocks()->Add(new_next);
100
101#ifdef DEBUG
102 prev_level_ = data->level;
103#endif
104 data->level++;
105 prev_limit_ = data->limit;
106 prev_next_ = data->next;
107 data->next = new_next;
108 data->limit = new_limit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000109}
110
111
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112DeferredHandleScope::~DeferredHandleScope() {
113 impl_->isolate()->handle_scope_data()->level--;
114 DCHECK(handles_detached_);
115 DCHECK(impl_->isolate()->handle_scope_data()->level == prev_level_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000116}
117
118
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119DeferredHandles* DeferredHandleScope::Detach() {
120 DeferredHandles* deferred = impl_->Detach(prev_limit_);
121 HandleScopeData* data = impl_->isolate()->handle_scope_data();
122 data->next = prev_next_;
123 data->limit = prev_limit_;
124#ifdef DEBUG
125 handles_detached_ = true;
126#endif
127 return deferred;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100128}
129
Steve Blocka7e24c12009-10-30 11:49:00 +0000130} } // namespace v8::internal