OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 (m_adjustTransferableExternalAllocationOnContextTransfer) | |
451 return; | |
452 if (m_externallyAllocatedMemory) { | |
453 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | |
454 -static_cast<int64_t>(m_externallyAllocatedMemory)); | |
455 m_externallyAllocatedMemory = 0; | |
456 } | |
457 // TODO: if other transferables start accounting for their external | |
458 // allocations with V8, extend this with corresponding cases. | |
459 if (m_arrayBufferContentsArray) { | |
460 for (size_t i = 0; i < m_arrayBufferContentsArray->size(); ++i) { | |
jbroman
2017/03/08 22:43:16
super-nit: here and below, a range-based loop woul
sof
2017/03/09 06:24:33
Switched idiom.
| |
461 WTF::ArrayBufferContents& buffer = m_arrayBufferContentsArray->at(i); | |
462 buffer.adjustExternalAllocatedMemoryUponContextTransfer( | |
463 WTF::ArrayBufferContents::Leave); | |
464 } | |
465 // Mark value as needing re-registration of external allocation | |
466 // costs in its target context, as handled by | |
467 // |registerMemoryAllocatedWithCurrentScriptContext()|. | |
468 m_adjustTransferableExternalAllocationOnContextTransfer = true; | |
469 } | |
470 } | |
471 | |
447 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { | 472 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { |
448 if (m_externallyAllocatedMemory) | 473 if (m_externallyAllocatedMemory) |
449 return; | 474 return; |
450 | 475 |
451 m_externallyAllocatedMemory = static_cast<intptr_t>(dataLengthInBytes()); | 476 m_externallyAllocatedMemory = dataLengthInBytes(); |
452 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | 477 int64_t diff = static_cast<int64_t>(m_externallyAllocatedMemory); |
jbroman
2017/03/08 22:43:16
nit: intptr_t, since that's what AdjustAmountOfExt
sof
2017/03/09 06:24:33
int64_t Isolate::AdjustAmountOfExternalAllocatedMe
| |
453 m_externallyAllocatedMemory); | 478 DCHECK_GE(diff, 0); |
479 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(diff); | |
480 if (m_adjustTransferableExternalAllocationOnContextTransfer) { | |
481 DCHECK(m_arrayBufferContentsArray); | |
482 for (size_t i = 0; i < m_arrayBufferContentsArray->size(); ++i) { | |
483 WTF::ArrayBufferContents& buffer = m_arrayBufferContentsArray->at(i); | |
484 buffer.adjustExternalAllocatedMemoryUponContextTransfer( | |
485 WTF::ArrayBufferContents::Enter); | |
486 } | |
487 m_adjustTransferableExternalAllocationOnContextTransfer = false; | |
488 } | |
454 } | 489 } |
455 | 490 |
456 } // namespace blink | 491 } // namespace blink |
OLD | NEW |