Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/SerializedScriptValue.cpp

Issue 2734173002: postMessage(): transfer allocation costs along with value. (Closed)
Patch Set: rebased upto r455878 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 StringBuffer<UChar> buffer(stringLength); 102 StringBuffer<UChar> buffer(stringLength);
103 const UChar* src = reinterpret_cast<const UChar*>(data); 103 const UChar* src = reinterpret_cast<const UChar*>(data);
104 UChar* dst = buffer.characters(); 104 UChar* dst = buffer.characters();
105 for (size_t i = 0; i < stringLength; i++) 105 for (size_t i = 0; i < stringLength; i++)
106 dst[i] = ntohs(src[i]); 106 dst[i] = ntohs(src[i]);
107 107
108 return adoptRef(new SerializedScriptValue(String::adopt(buffer))); 108 return adoptRef(new SerializedScriptValue(String::adopt(buffer)));
109 } 109 }
110 110
111 SerializedScriptValue::SerializedScriptValue() 111 SerializedScriptValue::SerializedScriptValue()
112 : m_externallyAllocatedMemory(0) {} 112 : m_externallyAllocatedMemory(0),
113 m_adjustTransferableExternalAllocationOnContextTransfer(false) {}
113 114
114 SerializedScriptValue::SerializedScriptValue(const String& wireData) 115 SerializedScriptValue::SerializedScriptValue(const String& wireData)
115 : m_externallyAllocatedMemory(0) { 116 : m_externallyAllocatedMemory(0),
117 m_adjustTransferableExternalAllocationOnContextTransfer(false) {
116 size_t byteLength = wireData.length() * 2; 118 size_t byteLength = wireData.length() * 2;
117 m_dataBuffer.reset(static_cast<uint8_t*>(WTF::Partitions::bufferMalloc( 119 m_dataBuffer.reset(static_cast<uint8_t*>(WTF::Partitions::bufferMalloc(
118 byteLength, "SerializedScriptValue buffer"))); 120 byteLength, "SerializedScriptValue buffer")));
119 m_dataBufferSize = byteLength; 121 m_dataBufferSize = byteLength;
120 wireData.copyTo(reinterpret_cast<UChar*>(m_dataBuffer.get()), 0, 122 wireData.copyTo(reinterpret_cast<UChar*>(m_dataBuffer.get()), 0,
121 wireData.length()); 123 wireData.length());
122 } 124 }
123 125
124 SerializedScriptValue::~SerializedScriptValue() { 126 SerializedScriptValue::~SerializedScriptValue() {
125 // If the allocated memory was not registered before, then this class is 127 // If the allocated memory was not registered before, then this class is
126 // likely used in a context other than Worker's onmessage environment and the 128 // likely used in a context other than Worker's onmessage environment and the
127 // presence of current v8 context is not guaranteed. Avoid calling v8 then. 129 // presence of current v8 context is not guaranteed. Avoid calling v8 then.
128 if (m_externallyAllocatedMemory) { 130 if (m_externallyAllocatedMemory) {
129 ASSERT(v8::Isolate::GetCurrent()); 131 ASSERT(v8::Isolate::GetCurrent());
130 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( 132 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
131 -m_externallyAllocatedMemory); 133 -static_cast<int64_t>(m_externallyAllocatedMemory));
132 } 134 }
133 } 135 }
134 136
135 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() { 137 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() {
136 // UChar rather than uint8_t here to get host endian behavior. 138 // UChar rather than uint8_t here to get host endian behavior.
137 static const UChar kNullData[] = {0xff09, 0x3000}; 139 static const UChar kNullData[] = {0xff09, 0x3000};
138 return create(reinterpret_cast<const char*>(kNullData), sizeof(kNullData)); 140 return create(reinterpret_cast<const char*>(kNullData), sizeof(kNullData));
139 } 141 }
140 142
141 String SerializedScriptValue::toWireString() const { 143 String SerializedScriptValue::toWireString() const {
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 439
438 if (isNeuterable) { 440 if (isNeuterable) {
439 for (const auto& bufferHandle : bufferHandles) 441 for (const auto& bufferHandle : bufferHandles)
440 bufferHandle->Neuter(); 442 bufferHandle->Neuter();
441 } 443 }
442 } 444 }
443 } 445 }
444 return contents; 446 return contents;
445 } 447 }
446 448
449 void SerializedScriptValue::unregisterMemoryAllocatedByCurrentScriptContext() {
450 // If the caller is the only one holding a reference then this serialized
451 // value hasn't transferred ownership & no unregistration of allocation
452 // costs wanted.
453 if (hasOneRef() || m_adjustTransferableExternalAllocationOnContextTransfer)
454 return;
455 if (m_externallyAllocatedMemory) {
456 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
457 -static_cast<int64_t>(m_externallyAllocatedMemory));
458 m_externallyAllocatedMemory = 0;
459 }
460 // TODO: if other transferables start accounting for their external
461 // allocations with V8, extend this with corresponding cases.
462 if (m_arrayBufferContentsArray) {
463 for (auto& buffer : *m_arrayBufferContentsArray) {
464 buffer.adjustExternalAllocatedMemoryUponContextTransfer(
465 WTF::ArrayBufferContents::Leave);
466 }
467 // Mark value as needing re-registration of external allocation
468 // costs in its target context, as handled by
469 // |registerMemoryAllocatedWithCurrentScriptContext()|.
470 m_adjustTransferableExternalAllocationOnContextTransfer = true;
471 }
472 }
473
447 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { 474 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() {
448 if (m_externallyAllocatedMemory) 475 if (m_externallyAllocatedMemory)
449 return; 476 return;
450 477
451 m_externallyAllocatedMemory = static_cast<intptr_t>(dataLengthInBytes()); 478 m_externallyAllocatedMemory = dataLengthInBytes();
452 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( 479 int64_t diff = static_cast<int64_t>(m_externallyAllocatedMemory);
453 m_externallyAllocatedMemory); 480 DCHECK_GE(diff, 0);
481 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(diff);
482 if (m_adjustTransferableExternalAllocationOnContextTransfer) {
483 DCHECK(m_arrayBufferContentsArray);
484 for (size_t i = 0; i < m_arrayBufferContentsArray->size(); ++i) {
485 WTF::ArrayBufferContents& buffer = m_arrayBufferContentsArray->at(i);
486 buffer.adjustExternalAllocatedMemoryUponContextTransfer(
487 WTF::ArrayBufferContents::Enter);
488 }
489 m_adjustTransferableExternalAllocationOnContextTransfer = false;
490 }
454 } 491 }
455 492
456 } // namespace blink 493 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698