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 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
439 | 439 |
440 if (isNeuterable) { | 440 if (isNeuterable) { |
441 for (const auto& bufferHandle : bufferHandles) | 441 for (const auto& bufferHandle : bufferHandles) |
442 bufferHandle->Neuter(); | 442 bufferHandle->Neuter(); |
443 } | 443 } |
444 } | 444 } |
445 } | 445 } |
446 return contents; | 446 return contents; |
447 } | 447 } |
448 | 448 |
449 void SerializedScriptValue::unregisterMemoryAllocatedByCurrentScriptContext() { | 449 void SerializedScriptValue::prepareForTransferringContext() { |
450 // If the caller is the only one holding a reference then this serialized | 450 // Unlikely that this is non-zero, but discount any already registered |
451 // value hasn't transferred ownership & no unregistration of allocation | 451 // external allocation for this SerializedScriptValue payload. |
452 // costs wanted. | |
453 if (hasOneRef() || m_adjustTransferableExternalAllocationOnContextTransfer) | |
454 return; | |
455 if (m_externallyAllocatedMemory) { | 452 if (m_externallyAllocatedMemory) { |
456 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | 453 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
457 -static_cast<int64_t>(m_externallyAllocatedMemory)); | 454 -static_cast<int64_t>(m_externallyAllocatedMemory)); |
458 m_externallyAllocatedMemory = 0; | 455 m_externallyAllocatedMemory = 0; |
459 } | 456 } |
457 | |
458 DCHECK(!m_adjustTransferableExternalAllocationOnContextTransfer); | |
459 // Mark value as needing re-registration of external allocation | |
460 // costs in its target context, as handled by | |
461 // |registerMemoryAllocatedWithCurrentScriptContext()|. | |
462 // | |
460 // TODO: if other transferables start accounting for their external | 463 // TODO: if other transferables start accounting for their external |
461 // allocations with V8, extend this with corresponding cases. | 464 // allocations with V8, extend this with corresponding cases. |
465 if (m_arrayBufferContentsArray) | |
466 m_adjustTransferableExternalAllocationOnContextTransfer = true; | |
467 } | |
468 | |
469 void SerializedScriptValue::finalizeTransferringContext() { | |
470 // If the caller is the only one holding a reference then this serialized | |
471 // value hasn't transferred ownership & no unregistration of allocation | |
472 // costs wanted. Re-registering m_externallyAllocatedMemory with the | |
473 // current context isn't done, redundant work as this unused value | |
474 // will be finalized shortly hereafter. | |
475 if (hasOneRef()) | |
jbroman
2017/03/10 20:08:04
I'm not thrilled by using |hasOneRef| to detect th
| |
476 return; | |
477 | |
462 if (m_arrayBufferContentsArray) { | 478 if (m_arrayBufferContentsArray) { |
463 for (auto& buffer : *m_arrayBufferContentsArray) { | 479 for (auto& buffer : *m_arrayBufferContentsArray) { |
464 buffer.adjustExternalAllocatedMemoryUponContextTransfer( | 480 buffer.adjustExternalAllocatedMemoryUponContextTransfer( |
465 WTF::ArrayBufferContents::Leave); | 481 WTF::ArrayBufferContents::Leave); |
466 } | 482 } |
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 } | 483 } |
472 } | 484 } |
473 | 485 |
474 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { | 486 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { |
475 if (m_externallyAllocatedMemory) | 487 if (m_externallyAllocatedMemory) |
476 return; | 488 return; |
477 | 489 |
478 m_externallyAllocatedMemory = dataLengthInBytes(); | 490 m_externallyAllocatedMemory = dataLengthInBytes(); |
479 int64_t diff = static_cast<int64_t>(m_externallyAllocatedMemory); | 491 int64_t diff = static_cast<int64_t>(m_externallyAllocatedMemory); |
480 DCHECK_GE(diff, 0); | 492 DCHECK_GE(diff, 0); |
481 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(diff); | 493 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(diff); |
482 if (m_adjustTransferableExternalAllocationOnContextTransfer) { | 494 if (m_adjustTransferableExternalAllocationOnContextTransfer) { |
483 DCHECK(m_arrayBufferContentsArray); | 495 DCHECK(m_arrayBufferContentsArray); |
484 for (size_t i = 0; i < m_arrayBufferContentsArray->size(); ++i) { | 496 for (size_t i = 0; i < m_arrayBufferContentsArray->size(); ++i) { |
485 WTF::ArrayBufferContents& buffer = m_arrayBufferContentsArray->at(i); | 497 WTF::ArrayBufferContents& buffer = m_arrayBufferContentsArray->at(i); |
486 buffer.adjustExternalAllocatedMemoryUponContextTransfer( | 498 buffer.adjustExternalAllocatedMemoryUponContextTransfer( |
487 WTF::ArrayBufferContents::Enter); | 499 WTF::ArrayBufferContents::Enter); |
488 } | 500 } |
489 m_adjustTransferableExternalAllocationOnContextTransfer = false; | 501 m_adjustTransferableExternalAllocationOnContextTransfer = false; |
490 } | 502 } |
491 } | 503 } |
492 | 504 |
493 } // namespace blink | 505 } // namespace blink |
OLD | NEW |