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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 byteLength, "SerializedScriptValue buffer"))); | 118 byteLength, "SerializedScriptValue buffer"))); |
119 m_dataBufferSize = byteLength; | 119 m_dataBufferSize = byteLength; |
120 wireData.copyTo(reinterpret_cast<UChar*>(m_dataBuffer.get()), 0, | 120 wireData.copyTo(reinterpret_cast<UChar*>(m_dataBuffer.get()), 0, |
121 wireData.length()); | 121 wireData.length()); |
122 } | 122 } |
123 | 123 |
124 SerializedScriptValue::~SerializedScriptValue() { | 124 SerializedScriptValue::~SerializedScriptValue() { |
125 // If the allocated memory was not registered before, then this class is | 125 // 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 | 126 // 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. | 127 // presence of current v8 context is not guaranteed. Avoid calling v8 then. |
128 if (m_externallyAllocatedMemory) { | 128 if (m_externallyAllocatedMemory > 0) { |
129 ASSERT(v8::Isolate::GetCurrent()); | 129 ASSERT(v8::Isolate::GetCurrent()); |
130 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | 130 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
131 -m_externallyAllocatedMemory); | 131 -m_externallyAllocatedMemory); |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() { | 135 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() { |
136 // UChar rather than uint8_t here to get host endian behavior. | 136 // UChar rather than uint8_t here to get host endian behavior. |
137 static const UChar kNullData[] = {0xff09, 0x3000}; | 137 static const UChar kNullData[] = {0xff09, 0x3000}; |
138 return create(reinterpret_cast<const char*>(kNullData), sizeof(kNullData)); | 138 return create(reinterpret_cast<const char*>(kNullData), sizeof(kNullData)); |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 | 437 |
438 if (isNeuterable) { | 438 if (isNeuterable) { |
439 for (const auto& bufferHandle : bufferHandles) | 439 for (const auto& bufferHandle : bufferHandles) |
440 bufferHandle->Neuter(); | 440 bufferHandle->Neuter(); |
441 } | 441 } |
442 } | 442 } |
443 } | 443 } |
444 return contents; | 444 return contents; |
445 } | 445 } |
446 | 446 |
447 void SerializedScriptValue::unregisterMemoryAllocatedByCurrentScriptContext() { | |
448 if (m_externallyAllocatedMemory < 0) | |
449 return; | |
450 if (m_externallyAllocatedMemory) { | |
451 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | |
452 -(m_externallyAllocatedMemory - 1)); | |
453 m_externallyAllocatedMemory = 0; | |
454 } | |
455 // TODO: if other transferables start accounting for their external | |
456 // allocations with V8, extend this with corresponding cases. | |
457 if (m_arrayBufferContentsArray) { | |
458 for (size_t i = 0; i < m_arrayBufferContentsArray->size(); ++i) { | |
459 WTF::ArrayBufferContents& buffer = m_arrayBufferContentsArray->at(i); | |
460 buffer.adjustExternalAllocatedMemoryUponContextTransfer( | |
461 WTF::ArrayBufferContents::Leave); | |
462 } | |
463 // Assign a negative size to mark the value as needing re-registration | |
464 // of transferable allocation costs by its target context in | |
465 // registerMemoryAllocatedWithCurrentScriptContext(). | |
466 m_externallyAllocatedMemory = -1; | |
haraken
2017/03/08 09:15:22
I'd prefer preparing a dedicated boolean flag rath
sof
2017/03/08 09:40:19
Given that all allocations are within base::kGener
jbroman
2017/03/08 16:56:46
If you are concerned about the size of this object
| |
467 } | |
468 } | |
469 | |
447 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { | 470 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { |
448 if (m_externallyAllocatedMemory) | 471 if (m_externallyAllocatedMemory > 0) |
449 return; | 472 return; |
450 | 473 |
451 m_externallyAllocatedMemory = static_cast<intptr_t>(dataLengthInBytes()); | 474 bool registerTransferables = m_externallyAllocatedMemory < 0; |
475 m_externallyAllocatedMemory = static_cast<int64_t>(dataLengthInBytes()); | |
476 DCHECK_GE(m_externallyAllocatedMemory, 0); | |
452 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | 477 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
453 m_externallyAllocatedMemory); | 478 m_externallyAllocatedMemory); |
479 if (registerTransferables) { | |
480 DCHECK(m_arrayBufferContentsArray); | |
481 for (size_t i = 0; i < m_arrayBufferContentsArray->size(); ++i) { | |
482 WTF::ArrayBufferContents& buffer = m_arrayBufferContentsArray->at(i); | |
483 buffer.adjustExternalAllocatedMemoryUponContextTransfer( | |
484 WTF::ArrayBufferContents::Enter); | |
485 } | |
486 } | |
454 } | 487 } |
455 | 488 |
456 } // namespace blink | 489 } // namespace blink |
OLD | NEW |