| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/handles.h" | 7 #include "src/handles.h" |
| 8 | 8 |
| 9 namespace v8 { | 9 namespace v8 { |
| 10 namespace internal { | 10 namespace internal { |
| 11 | 11 |
| 12 | 12 |
| 13 int HandleScope::NumberOfHandles(Isolate* isolate) { | 13 int HandleScope::NumberOfHandles(Isolate* isolate) { |
| 14 HandleScopeImplementer* impl = isolate->handle_scope_implementer(); | 14 HandleScopeImplementer* impl = isolate->handle_scope_implementer(); |
| 15 int n = impl->blocks()->length(); | 15 int n = impl->blocks()->length(); |
| 16 if (n == 0) return 0; | 16 if (n == 0) return 0; |
| 17 return ((n - 1) * kHandleBlockSize) + static_cast<int>( | 17 return ((n - 1) * kHandleBlockSize) + static_cast<int>( |
| 18 (isolate->handle_scope_data()->next - impl->blocks()->last())); | 18 (isolate->handle_scope_data()->next - impl->blocks()->last())); |
| 19 } | 19 } |
| 20 | 20 |
| 21 | 21 |
| 22 Object** HandleScope::Extend(Isolate* isolate) { | 22 Object** HandleScope::Extend(Isolate* isolate) { |
| 23 HandleScopeData* current = isolate->handle_scope_data(); | 23 HandleScopeData* current = isolate->handle_scope_data(); |
| 24 | 24 |
| 25 Object** result = current->next; | 25 Object** result = current->next; |
| 26 | 26 |
| 27 ASSERT(result == current->limit); | 27 DCHECK(result == current->limit); |
| 28 // Make sure there's at least one scope on the stack and that the | 28 // Make sure there's at least one scope on the stack and that the |
| 29 // top of the scope stack isn't a barrier. | 29 // top of the scope stack isn't a barrier. |
| 30 if (!Utils::ApiCheck(current->level != 0, | 30 if (!Utils::ApiCheck(current->level != 0, |
| 31 "v8::HandleScope::CreateHandle()", | 31 "v8::HandleScope::CreateHandle()", |
| 32 "Cannot create a handle without a HandleScope")) { | 32 "Cannot create a handle without a HandleScope")) { |
| 33 return NULL; | 33 return NULL; |
| 34 } | 34 } |
| 35 HandleScopeImplementer* impl = isolate->handle_scope_implementer(); | 35 HandleScopeImplementer* impl = isolate->handle_scope_implementer(); |
| 36 // If there's more room in the last block, we use that. This is used | 36 // If there's more room in the last block, we use that. This is used |
| 37 // for fast creation of scopes after scope barriers. | 37 // for fast creation of scopes after scope barriers. |
| 38 if (!impl->blocks()->is_empty()) { | 38 if (!impl->blocks()->is_empty()) { |
| 39 Object** limit = &impl->blocks()->last()[kHandleBlockSize]; | 39 Object** limit = &impl->blocks()->last()[kHandleBlockSize]; |
| 40 if (current->limit != limit) { | 40 if (current->limit != limit) { |
| 41 current->limit = limit; | 41 current->limit = limit; |
| 42 ASSERT(limit - current->next < kHandleBlockSize); | 42 DCHECK(limit - current->next < kHandleBlockSize); |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 | 45 |
| 46 // If we still haven't found a slot for the handle, we extend the | 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. | 47 // current handle scope by allocating a new handle block. |
| 48 if (result == current->limit) { | 48 if (result == current->limit) { |
| 49 // If there's a spare block, use it for growing the current scope. | 49 // If there's a spare block, use it for growing the current scope. |
| 50 result = impl->GetSpareOrNewBlock(); | 50 result = impl->GetSpareOrNewBlock(); |
| 51 // Add the extension to the global list of blocks, but count the | 51 // Add the extension to the global list of blocks, but count the |
| 52 // extension as part of the current scope. | 52 // extension as part of the current scope. |
| 53 impl->blocks()->Add(result); | 53 impl->blocks()->Add(result); |
| 54 current->limit = &result[kHandleBlockSize]; | 54 current->limit = &result[kHandleBlockSize]; |
| 55 } | 55 } |
| 56 | 56 |
| 57 return result; | 57 return result; |
| 58 } | 58 } |
| 59 | 59 |
| 60 | 60 |
| 61 void HandleScope::DeleteExtensions(Isolate* isolate) { | 61 void HandleScope::DeleteExtensions(Isolate* isolate) { |
| 62 HandleScopeData* current = isolate->handle_scope_data(); | 62 HandleScopeData* current = isolate->handle_scope_data(); |
| 63 isolate->handle_scope_implementer()->DeleteExtensions(current->limit); | 63 isolate->handle_scope_implementer()->DeleteExtensions(current->limit); |
| 64 } | 64 } |
| 65 | 65 |
| 66 | 66 |
| 67 #ifdef ENABLE_HANDLE_ZAPPING | 67 #ifdef ENABLE_HANDLE_ZAPPING |
| 68 void HandleScope::ZapRange(Object** start, Object** end) { | 68 void HandleScope::ZapRange(Object** start, Object** end) { |
| 69 ASSERT(end - start <= kHandleBlockSize); | 69 DCHECK(end - start <= kHandleBlockSize); |
| 70 for (Object** p = start; p != end; p++) { | 70 for (Object** p = start; p != end; p++) { |
| 71 *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue; | 71 *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue; |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 #endif | 74 #endif |
| 75 | 75 |
| 76 | 76 |
| 77 Address HandleScope::current_level_address(Isolate* isolate) { | 77 Address HandleScope::current_level_address(Isolate* isolate) { |
| 78 return reinterpret_cast<Address>(&isolate->handle_scope_data()->level); | 78 return reinterpret_cast<Address>(&isolate->handle_scope_data()->level); |
| 79 } | 79 } |
| 80 | 80 |
| 81 | 81 |
| 82 Address HandleScope::current_next_address(Isolate* isolate) { | 82 Address HandleScope::current_next_address(Isolate* isolate) { |
| 83 return reinterpret_cast<Address>(&isolate->handle_scope_data()->next); | 83 return reinterpret_cast<Address>(&isolate->handle_scope_data()->next); |
| 84 } | 84 } |
| 85 | 85 |
| 86 | 86 |
| 87 Address HandleScope::current_limit_address(Isolate* isolate) { | 87 Address HandleScope::current_limit_address(Isolate* isolate) { |
| 88 return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit); | 88 return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit); |
| 89 } | 89 } |
| 90 | 90 |
| 91 | 91 |
| 92 DeferredHandleScope::DeferredHandleScope(Isolate* isolate) | 92 DeferredHandleScope::DeferredHandleScope(Isolate* isolate) |
| 93 : impl_(isolate->handle_scope_implementer()) { | 93 : impl_(isolate->handle_scope_implementer()) { |
| 94 impl_->BeginDeferredScope(); | 94 impl_->BeginDeferredScope(); |
| 95 HandleScopeData* data = impl_->isolate()->handle_scope_data(); | 95 HandleScopeData* data = impl_->isolate()->handle_scope_data(); |
| 96 Object** new_next = impl_->GetSpareOrNewBlock(); | 96 Object** new_next = impl_->GetSpareOrNewBlock(); |
| 97 Object** new_limit = &new_next[kHandleBlockSize]; | 97 Object** new_limit = &new_next[kHandleBlockSize]; |
| 98 ASSERT(data->limit == &impl_->blocks()->last()[kHandleBlockSize]); | 98 DCHECK(data->limit == &impl_->blocks()->last()[kHandleBlockSize]); |
| 99 impl_->blocks()->Add(new_next); | 99 impl_->blocks()->Add(new_next); |
| 100 | 100 |
| 101 #ifdef DEBUG | 101 #ifdef DEBUG |
| 102 prev_level_ = data->level; | 102 prev_level_ = data->level; |
| 103 #endif | 103 #endif |
| 104 data->level++; | 104 data->level++; |
| 105 prev_limit_ = data->limit; | 105 prev_limit_ = data->limit; |
| 106 prev_next_ = data->next; | 106 prev_next_ = data->next; |
| 107 data->next = new_next; | 107 data->next = new_next; |
| 108 data->limit = new_limit; | 108 data->limit = new_limit; |
| 109 } | 109 } |
| 110 | 110 |
| 111 | 111 |
| 112 DeferredHandleScope::~DeferredHandleScope() { | 112 DeferredHandleScope::~DeferredHandleScope() { |
| 113 impl_->isolate()->handle_scope_data()->level--; | 113 impl_->isolate()->handle_scope_data()->level--; |
| 114 ASSERT(handles_detached_); | 114 DCHECK(handles_detached_); |
| 115 ASSERT(impl_->isolate()->handle_scope_data()->level == prev_level_); | 115 DCHECK(impl_->isolate()->handle_scope_data()->level == prev_level_); |
| 116 } | 116 } |
| 117 | 117 |
| 118 | 118 |
| 119 DeferredHandles* DeferredHandleScope::Detach() { | 119 DeferredHandles* DeferredHandleScope::Detach() { |
| 120 DeferredHandles* deferred = impl_->Detach(prev_limit_); | 120 DeferredHandles* deferred = impl_->Detach(prev_limit_); |
| 121 HandleScopeData* data = impl_->isolate()->handle_scope_data(); | 121 HandleScopeData* data = impl_->isolate()->handle_scope_data(); |
| 122 data->next = prev_next_; | 122 data->next = prev_next_; |
| 123 data->limit = prev_limit_; | 123 data->limit = prev_limit_; |
| 124 #ifdef DEBUG | 124 #ifdef DEBUG |
| 125 handles_detached_ = true; | 125 handles_detached_ = true; |
| 126 #endif | 126 #endif |
| 127 return deferred; | 127 return deferred; |
| 128 } | 128 } |
| 129 | 129 |
| 130 } } // namespace v8::internal | 130 } } // namespace v8::internal |
| OLD | NEW |