2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2015-05-05 15:44:55 +00:00
|
|
|
|
|
|
|
#ifndef V8_HANDLES_INL_H_
|
|
|
|
#define V8_HANDLES_INL_H_
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/api.h"
|
|
|
|
#include "src/handles.h"
|
2014-08-05 08:18:22 +00:00
|
|
|
#include "src/heap/heap.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/isolate.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-05-05 15:44:55 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2015-07-15 11:05:00 +00:00
|
|
|
HandleBase::HandleBase(Object* object, Isolate* isolate)
|
2015-10-26 15:33:02 +00:00
|
|
|
: 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)));
|
|
|
|
}
|
2015-05-05 15:44:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
HandleScope::HandleScope(Isolate* isolate) {
|
2015-10-26 15:33:02 +00:00
|
|
|
HandleScopeData* data = isolate->handle_scope_data();
|
2015-05-05 15:44:55 +00:00
|
|
|
isolate_ = isolate;
|
2015-10-26 15:33:02 +00:00
|
|
|
prev_next_ = data->next;
|
|
|
|
prev_limit_ = data->limit;
|
|
|
|
data->level++;
|
2015-05-05 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-31 08:24:52 +00:00
|
|
|
template <typename T>
|
|
|
|
inline std::ostream& operator<<(std::ostream& os, Handle<T> handle) {
|
|
|
|
return os << Brief(*handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-05 15:44:55 +00:00
|
|
|
HandleScope::~HandleScope() {
|
Reland 'Additional HandleScopes to limit Handle consumption.'
v8 builds with --no-snap were hitting handle limits compiling natives for handle count unit tests that run with --check_handle_count. Patch now has higher handle limits (~4k more than required for failing natives compilation).
Original issue: https://codereview.chromium.org/1185633002/
Original issue's description:
> Additional HandleScopes to limit Handle consumption.
>
> erikcorry@chromium.org suggested digging into v8 handle usage. Found potential scopes in ast.cc and runtime-literals.cc and added tests.
>
> The runtime-literals.cc change reduces peak handles in imaging-darkroom.js from 1,282,610 to 428,218. The ast.cc change reduces the peak handles in string-t
agcloud.js from 80,738 to 8,176.
>
> No significant handle count issues found with major websites, but substantial savings on some benchmarks and demos:
>
> Kraken's imaging-darkroom.js down from 1,282,610 to 428,218 due to runtime-literals.cc scope.
> SunSpider's string-tagcloud.js down from 80,738 to 8.176 due to ast.cc
>
> http://www.flohofwoe.net/demos/dragons_asmjs.html (738,906 -> 478,296)
> http://www.flohofwoe.net/demos/instancing_asmjs.html (737,884 -> 477,274)
> https://dl.dropboxusercontent.com/u/16662598/Ports/DOSBox-web/doom.html?engine=dosbox-growth.js (1,724,114 -> 1,087,408)
> https://kripken.github.io/ammo.js/examples/new/ammo.html (175,784 -> 142,058)
>
> BUG=
>
> Committed: https://crrev.com/3a4c7538839186aa38910c66c986abb563f4ccd2
> Cr-Commit-Position: refs/heads/master@{#29155}
BUG=
Review URL: https://codereview.chromium.org/1192743005
Cr-Commit-Position: refs/heads/master@{#29322}
2015-06-26 10:58:09 +00:00
|
|
|
#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
|
2015-05-05 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
2015-10-26 15:33:02 +00:00
|
|
|
DCHECK(current->level > current->sealed_level);
|
2015-07-15 11:05:00 +00:00
|
|
|
Handle<T> result(value, isolate_);
|
2015-05-05 15:44:55 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-26 15:33:02 +00:00
|
|
|
Object** HandleScope::CreateHandle(Isolate* isolate, Object* value) {
|
2015-05-05 15:44:55 +00:00
|
|
|
DCHECK(AllowHandleAllocation::IsAllowed());
|
2015-10-26 15:33:02 +00:00
|
|
|
HandleScopeData* data = isolate->handle_scope_data();
|
2015-05-05 15:44:55 +00:00
|
|
|
|
2015-10-26 15:33:02 +00:00
|
|
|
Object** result = data->next;
|
|
|
|
if (result == data->limit) result = Extend(isolate);
|
2015-05-05 15:44:55 +00:00
|
|
|
// Update the current next field, set the value in the created
|
|
|
|
// handle, and return the result.
|
2015-10-26 15:33:02 +00:00
|
|
|
DCHECK(result < data->limit);
|
|
|
|
data->next = result + 1;
|
2015-05-05 15:44:55 +00:00
|
|
|
|
|
|
|
*result = value;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-26 15:33:02 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-05 15:44:55 +00:00
|
|
|
#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.
|
2015-10-26 15:33:02 +00:00
|
|
|
prev_limit_ = current->limit;
|
2015-05-05 15:44:55 +00:00
|
|
|
current->limit = current->next;
|
2015-10-26 15:33:02 +00:00
|
|
|
prev_sealed_level_ = current->sealed_level;
|
|
|
|
current->sealed_level = current->level;
|
2015-05-05 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2015-10-26 15:33:02 +00:00
|
|
|
current->limit = prev_limit_;
|
|
|
|
DCHECK_EQ(current->level, current->sealed_level);
|
|
|
|
current->sealed_level = prev_sealed_level_;
|
2015-05-05 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-07-15 11:05:00 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2015-05-05 15:44:55 +00:00
|
|
|
|
|
|
|
#endif // V8_HANDLES_INL_H_
|