Commit a01a6b31 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[arraybuffer] Don't adjust V8 externally allocated memory for ArrayBuffers

With recent changes in V8 it is not necessary anymore to call
{AdjustAmountOfExternalAllocatedMemory} for memory of ArrayBuffers. V8
get this information now from ArrayBuffer objects on the heap.

R=ulan@chromium.org, haraken@chromium.org

Bug: chromium:1008840
Change-Id: I886b391e1655835ade39dc43027a392490d30cad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1871876Reviewed-by: default avatarUlan Degenbaev <ulan@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708130}
parent bf17d56b
...@@ -259,15 +259,13 @@ scoped_refptr<SerializedScriptValue> SerializedScriptValue::Create( ...@@ -259,15 +259,13 @@ scoped_refptr<SerializedScriptValue> SerializedScriptValue::Create(
} }
SerializedScriptValue::SerializedScriptValue() SerializedScriptValue::SerializedScriptValue()
: has_registered_external_allocation_(false), : has_registered_external_allocation_(false) {}
transferables_need_external_allocation_registration_(false) {}
SerializedScriptValue::SerializedScriptValue(DataBufferPtr data, SerializedScriptValue::SerializedScriptValue(DataBufferPtr data,
size_t data_size) size_t data_size)
: data_buffer_(std::move(data)), : data_buffer_(std::move(data)),
data_buffer_size_(data_size), data_buffer_size_(data_size),
has_registered_external_allocation_(false), has_registered_external_allocation_(false) {}
transferables_need_external_allocation_registration_(false) {}
void SerializedScriptValue::SetImageBitmapContentsArray( void SerializedScriptValue::SetImageBitmapContentsArray(
ImageBitmapContentsArray contents) { ImageBitmapContentsArray contents) {
...@@ -751,16 +749,6 @@ void SerializedScriptValue:: ...@@ -751,16 +749,6 @@ void SerializedScriptValue::
-static_cast<int64_t>(DataLengthInBytes())); -static_cast<int64_t>(DataLengthInBytes()));
has_registered_external_allocation_ = false; has_registered_external_allocation_ = false;
} }
// TODO: if other transferables start accounting for their external
// allocations with V8, extend this with corresponding cases.
if (!transferables_need_external_allocation_registration_) {
for (auto& buffer : array_buffer_contents_array_)
buffer.UnregisterExternalAllocationWithCurrentContext();
for (auto& buffer : shared_array_buffers_contents_)
buffer.UnregisterExternalAllocationWithCurrentContext();
transferables_need_external_allocation_registration_ = true;
}
} }
void SerializedScriptValue::RegisterMemoryAllocatedWithCurrentScriptContext() { void SerializedScriptValue::RegisterMemoryAllocatedWithCurrentScriptContext() {
...@@ -771,15 +759,6 @@ void SerializedScriptValue::RegisterMemoryAllocatedWithCurrentScriptContext() { ...@@ -771,15 +759,6 @@ void SerializedScriptValue::RegisterMemoryAllocatedWithCurrentScriptContext() {
int64_t diff = static_cast<int64_t>(DataLengthInBytes()); int64_t diff = static_cast<int64_t>(DataLengthInBytes());
DCHECK_GE(diff, 0); DCHECK_GE(diff, 0);
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(diff); v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(diff);
// Only (re)register allocation cost for transferables if this
// SerializedScriptValue has explicitly unregistered them before.
if (transferables_need_external_allocation_registration_) {
for (auto& buffer : array_buffer_contents_array_)
buffer.RegisterExternalAllocationWithCurrentContext();
for (auto& buffer : shared_array_buffers_contents_)
buffer.RegisterExternalAllocationWithCurrentContext();
}
} }
// This ensures that the version number published in // This ensures that the version number published in
......
...@@ -333,7 +333,6 @@ class CORE_EXPORT SerializedScriptValue ...@@ -333,7 +333,6 @@ class CORE_EXPORT SerializedScriptValue
SharedArrayBufferContentsArray shared_array_buffers_contents_; SharedArrayBufferContentsArray shared_array_buffers_contents_;
bool has_registered_external_allocation_; bool has_registered_external_allocation_;
bool transferables_need_external_allocation_registration_;
#if DCHECK_IS_ON() #if DCHECK_IS_ON()
bool was_unpacked_ = false; bool was_unpacked_ = false;
#endif #endif
......
...@@ -652,27 +652,9 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { ...@@ -652,27 +652,9 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
} // namespace } // namespace
static void AdjustAmountOfExternalAllocatedMemory(int64_t diff) {
#if DCHECK_IS_ON()
static int64_t process_total = 0;
DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, ());
{
MutexLocker locker(mutex);
process_total += diff;
DCHECK_GE(process_total, 0)
<< "total amount = " << process_total << ", diff = " << diff;
}
#endif
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(diff);
}
void V8Initializer::InitializeMainThread(const intptr_t* reference_table) { void V8Initializer::InitializeMainThread(const intptr_t* reference_table) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
WTF::ArrayBufferContents::Initialize(AdjustAmountOfExternalAllocatedMemory);
DEFINE_STATIC_LOCAL(ArrayBufferAllocator, array_buffer_allocator, ()); DEFINE_STATIC_LOCAL(ArrayBufferAllocator, array_buffer_allocator, ());
gin::IsolateHolder::Initialize(gin::IsolateHolder::kNonStrictMode, gin::IsolateHolder::Initialize(gin::IsolateHolder::kNonStrictMode,
&array_buffer_allocator, reference_table); &array_buffer_allocator, reference_table);
......
...@@ -34,21 +34,6 @@ ...@@ -34,21 +34,6 @@
namespace WTF { namespace WTF {
void ArrayBufferContents::DefaultAdjustAmountOfExternalAllocatedMemoryFunction(
int64_t diff) {
// Do nothing by default.
}
ArrayBufferContents::AdjustAmountOfExternalAllocatedMemoryFunction
ArrayBufferContents::adjust_amount_of_external_allocated_memory_function_ =
DefaultAdjustAmountOfExternalAllocatedMemoryFunction;
#if DCHECK_IS_ON()
ArrayBufferContents::AdjustAmountOfExternalAllocatedMemoryFunction
ArrayBufferContents::
last_used_adjust_amount_of_external_allocated_memory_function_;
#endif
ArrayBufferContents::ArrayBufferContents() ArrayBufferContents::ArrayBufferContents()
: holder_(base::AdoptRef(new DataHolder())) {} : holder_(base::AdoptRef(new DataHolder())) {}
...@@ -148,9 +133,6 @@ ArrayBufferContents::DataHolder::DataHolder() ...@@ -148,9 +133,6 @@ ArrayBufferContents::DataHolder::DataHolder()
has_registered_external_allocation_(false) {} has_registered_external_allocation_(false) {}
ArrayBufferContents::DataHolder::~DataHolder() { ArrayBufferContents::DataHolder::~DataHolder() {
if (has_registered_external_allocation_)
AdjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(DataLength()));
is_shared_ = kNotShared; is_shared_ = kNotShared;
} }
...@@ -165,8 +147,6 @@ void ArrayBufferContents::DataHolder::AllocateNew(size_t length, ...@@ -165,8 +147,6 @@ void ArrayBufferContents::DataHolder::AllocateNew(size_t length,
return; return;
is_shared_ = is_shared; is_shared_ = is_shared;
RegisterExternalAllocationWithCurrentContext();
} }
void ArrayBufferContents::DataHolder::Adopt(DataHandle data, void ArrayBufferContents::DataHolder::Adopt(DataHandle data,
...@@ -176,8 +156,6 @@ void ArrayBufferContents::DataHolder::Adopt(DataHandle data, ...@@ -176,8 +156,6 @@ void ArrayBufferContents::DataHolder::Adopt(DataHandle data,
data_ = std::move(data); data_ = std::move(data);
is_shared_ = is_shared; is_shared_ = is_shared;
RegisterExternalAllocationWithCurrentContext();
} }
void ArrayBufferContents::DataHolder::CopyMemoryFrom(const DataHolder& source) { void ArrayBufferContents::DataHolder::CopyMemoryFrom(const DataHolder& source) {
...@@ -189,27 +167,6 @@ void ArrayBufferContents::DataHolder::CopyMemoryFrom(const DataHolder& source) { ...@@ -189,27 +167,6 @@ void ArrayBufferContents::DataHolder::CopyMemoryFrom(const DataHolder& source) {
return; return;
memcpy(data_.Data(), source.Data(), source.DataLength()); memcpy(data_.Data(), source.Data(), source.DataLength());
RegisterExternalAllocationWithCurrentContext();
}
void ArrayBufferContents::DataHolder::
RegisterExternalAllocationWithCurrentContext() {
DCHECK(!has_registered_external_allocation_);
// Currently, we can only track an allocation if we have a single owner. For
// shared data this is not true, hence do not attempt to track at all.
// TODO(crbug.com/877055) Implement tracking of shared external allocations.
if (IsShared())
return;
AdjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(DataLength()));
}
void ArrayBufferContents::DataHolder::
UnregisterExternalAllocationWithCurrentContext() {
if (!has_registered_external_allocation_)
return;
DCHECK(!IsShared());
AdjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(DataLength()));
} }
} // namespace WTF } // namespace WTF
...@@ -144,23 +144,6 @@ class WTF_EXPORT ArrayBufferContents { ...@@ -144,23 +144,6 @@ class WTF_EXPORT ArrayBufferContents {
static void* AllocateMemoryOrNull(size_t, InitializationPolicy); static void* AllocateMemoryOrNull(size_t, InitializationPolicy);
static void FreeMemory(void*); static void FreeMemory(void*);
static DataHandle CreateDataHandle(size_t, InitializationPolicy); static DataHandle CreateDataHandle(size_t, InitializationPolicy);
static void Initialize(
AdjustAmountOfExternalAllocatedMemoryFunction function) {
DCHECK(IsMainThread());
DCHECK_EQ(adjust_amount_of_external_allocated_memory_function_,
DefaultAdjustAmountOfExternalAllocatedMemoryFunction);
adjust_amount_of_external_allocated_memory_function_ = function;
}
void RegisterExternalAllocationWithCurrentContext() {
if (holder_)
holder_->RegisterExternalAllocationWithCurrentContext();
}
void UnregisterExternalAllocationWithCurrentContext() {
if (holder_)
holder_->UnregisterExternalAllocationWithCurrentContext();
}
private: private:
static void* AllocateMemoryWithFlags(size_t, InitializationPolicy, int); static void* AllocateMemoryWithFlags(size_t, InitializationPolicy, int);
...@@ -186,49 +169,13 @@ class WTF_EXPORT ArrayBufferContents { ...@@ -186,49 +169,13 @@ class WTF_EXPORT ArrayBufferContents {
size_t DataLength() const { return data_.DataLength(); } size_t DataLength() const { return data_.DataLength(); }
bool IsShared() const { return is_shared_ == kShared; } bool IsShared() const { return is_shared_ == kShared; }
void RegisterExternalAllocationWithCurrentContext();
void UnregisterExternalAllocationWithCurrentContext();
private: private:
void AdjustAmountOfExternalAllocatedMemory(int64_t diff) {
has_registered_external_allocation_ =
!has_registered_external_allocation_;
DCHECK(!diff || (has_registered_external_allocation_ == (diff > 0)));
CheckIfAdjustAmountOfExternalAllocatedMemoryIsConsistent();
adjust_amount_of_external_allocated_memory_function_(diff);
}
void AdjustAmountOfExternalAllocatedMemory(size_t diff) {
AdjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(diff));
}
void CheckIfAdjustAmountOfExternalAllocatedMemoryIsConsistent() {
DCHECK(adjust_amount_of_external_allocated_memory_function_);
#if DCHECK_IS_ON()
// Make sure that the function actually used is always the same.
// Shouldn't be updated during its use.
if (!last_used_adjust_amount_of_external_allocated_memory_function_) {
last_used_adjust_amount_of_external_allocated_memory_function_ =
adjust_amount_of_external_allocated_memory_function_;
}
DCHECK_EQ(adjust_amount_of_external_allocated_memory_function_,
last_used_adjust_amount_of_external_allocated_memory_function_);
#endif
}
DataHandle data_; DataHandle data_;
SharingType is_shared_; SharingType is_shared_;
bool has_registered_external_allocation_; bool has_registered_external_allocation_;
}; };
scoped_refptr<DataHolder> holder_; scoped_refptr<DataHolder> holder_;
static AdjustAmountOfExternalAllocatedMemoryFunction
adjust_amount_of_external_allocated_memory_function_;
#if DCHECK_IS_ON()
static AdjustAmountOfExternalAllocatedMemoryFunction
last_used_adjust_amount_of_external_allocated_memory_function_;
#endif
DISALLOW_COPY_AND_ASSIGN(ArrayBufferContents); DISALLOW_COPY_AND_ASSIGN(ArrayBufferContents);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment