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

Unified Diff: Source/wtf/ArrayBufferContents.cpp

Issue 1097773004: Sharing of SharedArrayBuffer via PostMessage transfer (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: update for non-split typedarray hierarchy Created 5 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: Source/wtf/ArrayBufferContents.cpp
diff --git a/Source/wtf/ArrayBufferContents.cpp b/Source/wtf/ArrayBufferContents.cpp
index c71d829a4617c865d62bfcc4337d9cfafc69e840..bce31fb33f5d854138ebe391299adab0e137ab28 100644
--- a/Source/wtf/ArrayBufferContents.cpp
+++ b/Source/wtf/ArrayBufferContents.cpp
@@ -35,74 +35,73 @@
namespace WTF {
ArrayBufferContents::ArrayBufferContents()
- : m_data(0)
- , m_sizeInBytes(0)
- , m_deallocationObserver(0) { }
+ : m_holder(adoptRef(new DataHolder())) { }
-ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
- : m_data(0)
- , m_sizeInBytes(0)
- , m_deallocationObserver(0)
+ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementByteSize, bool isShared, ArrayBufferContents::InitializationPolicy policy)
+ : m_holder(adoptRef(new DataHolder()))
{
// Do not allow 32-bit overflow of the total size.
+ unsigned totalSize = numElements * elementByteSize;
if (numElements) {
- unsigned totalSize = numElements * elementByteSize;
if (totalSize / numElements != elementByteSize) {
- m_data = 0;
return;
}
}
- allocateMemory(numElements * elementByteSize, policy, m_data);
- m_sizeInBytes = numElements * elementByteSize;
+
+ void* data = 0;
+ allocateMemory(totalSize, policy, data);
+ m_holder->adopt(data, totalSize, isShared, 0);
}
ArrayBufferContents::ArrayBufferContents(
- void* data, unsigned sizeInBytes, ArrayBufferDeallocationObserver* observer)
- : m_data(data)
- , m_sizeInBytes(sizeInBytes)
- , m_deallocationObserver(observer)
+ void* data, unsigned sizeInBytes, bool isShared, ArrayBufferDeallocationObserver* observer)
+ : m_holder(adoptRef(new DataHolder()))
{
- if (!m_data) {
- ASSERT(!m_sizeInBytes);
- m_sizeInBytes = 0;
- // Allow null data if size is 0 bytes, make sure m_data is valid pointer.
+ if (!data) {
+ ASSERT(!sizeInBytes);
+ sizeInBytes = 0;
+ // Allow null data if size is 0 bytes, make sure data is valid pointer.
// (partitionAllocGeneric guarantees valid pointer for size 0)
- allocateMemory(0, ZeroInitialize, m_data);
+ allocateMemory(0, ZeroInitialize, data);
}
+ m_holder->adopt(data, sizeInBytes, isShared, observer);
}
ArrayBufferContents::~ArrayBufferContents()
{
- freeMemory(m_data, m_sizeInBytes);
clear();
}
void ArrayBufferContents::clear()
{
- if (m_data && m_deallocationObserver)
- m_deallocationObserver->arrayBufferDeallocated(m_sizeInBytes);
- m_data = 0;
- m_sizeInBytes = 0;
- m_deallocationObserver = 0;
+ m_holder.clear();
}
void ArrayBufferContents::transfer(ArrayBufferContents& other)
{
- ASSERT(!other.m_data);
- other.m_data = m_data;
- other.m_sizeInBytes = m_sizeInBytes;
- clear();
+ ASSERT(!other.m_holder->m_data);
+ other.m_holder = m_holder;
+ if (!m_holder->m_isShared)
+ clear();
}
void ArrayBufferContents::copyTo(ArrayBufferContents& other)
{
- ASSERT(!other.m_sizeInBytes);
- other.freeMemory(other.m_data, other.m_sizeInBytes);
- allocateMemory(m_sizeInBytes, DontInitialize, other.m_data);
- if (!other.m_data)
+ // TODO(binji): where is this function used? It probably shouldn't be used
+ // for shared array buffers.
+ ASSERT(!m_holder->m_isShared && !other.m_holder->m_isShared);
+
+ ASSERT(!other.m_holder->m_sizeInBytes);
+
+ other.m_holder = adoptRef(new DataHolder());
+
+ allocateMemory(m_holder->m_sizeInBytes, DontInitialize, other.m_holder->m_data);
+ if (!other.m_holder->m_data)
return;
- memcpy(other.m_data, m_data, m_sizeInBytes);
- other.m_sizeInBytes = m_sizeInBytes;
+
+ m_holder->copyToWithoutBuffer(*other.m_holder);
+ memcpy(other.m_holder->m_data, m_holder->m_data, m_holder->m_sizeInBytes);
+ other.m_holder->m_sizeInBytes = m_holder->m_sizeInBytes;
Yuki 2015/06/09 06:18:08 I'd recommend to use adopt() or create a new insta
binji 2015/06/10 06:04:39 Done.
}
void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy policy, void*& data)
@@ -117,4 +116,36 @@ void ArrayBufferContents::freeMemory(void* data, size_t)
partitionFreeGeneric(WTF::Partitions::getBufferPartition(), data);
}
+ArrayBufferContents::DataHolder::DataHolder()
+ : m_data(0)
+ , m_sizeInBytes(0)
+ , m_isShared(false) { }
+
+ArrayBufferContents::DataHolder::~DataHolder()
+{
+ ArrayBufferContents::freeMemory(m_data, m_sizeInBytes);
+
+ if (m_data && m_deallocationObserver)
+ m_deallocationObserver->arrayBufferDeallocated(m_sizeInBytes);
+
+ m_data = 0;
+ m_sizeInBytes = 0;
+ m_isShared = false;
+ m_deallocationObserver = 0;
+}
+
+void ArrayBufferContents::DataHolder::adopt(void* data, unsigned sizeInBytes, bool isShared, ArrayBufferDeallocationObserver* observer)
+{
+ m_data = data;
Yuki 2015/06/09 06:18:08 ASSERT(!m_data); ? It seems you're assuming that
binji 2015/06/10 06:04:39 Done.
+ m_sizeInBytes = sizeInBytes;
+ m_isShared = isShared;
+ m_deallocationObserver = observer;
+}
+
+void ArrayBufferContents::DataHolder::copyToWithoutBuffer(DataHolder& other)
+{
+ other.m_isShared = m_isShared;
+ other.m_deallocationObserver = m_deallocationObserver;
+}
+
} // namespace WTF

Powered by Google App Engine
This is Rietveld 408576698