This source file includes following definitions.
- m_deallocationObserver
- m_deallocationObserver
- m_deallocationObserver
- clear
- transfer
- copyTo
- allocateMemory
- freeMemory
#include "config.h"
#include "wtf/ArrayBufferContents.h"
#include "wtf/Assertions.h"
#include "wtf/PartitionAlloc.h"
#include "wtf/WTF.h"
#include <string.h>
namespace WTF {
ArrayBufferContents::ArrayBufferContents()
: m_data(0)
, m_sizeInBytes(0)
, m_deallocationObserver(0) { }
ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
: m_data(0)
, m_sizeInBytes(0)
, m_deallocationObserver(0)
{
if (numElements) {
unsigned totalSize = numElements * elementByteSize;
if (totalSize / numElements != elementByteSize) {
m_data = 0;
return;
}
}
allocateMemory(numElements * elementByteSize, policy, m_data);
m_sizeInBytes = numElements * elementByteSize;
}
ArrayBufferContents::ArrayBufferContents(
void* data, unsigned sizeInBytes, ArrayBufferDeallocationObserver* observer)
: m_data(data)
, m_sizeInBytes(sizeInBytes)
, m_deallocationObserver(observer)
{
if (!m_data) {
ASSERT(!m_sizeInBytes);
m_sizeInBytes = 0;
allocateMemory(0, ZeroInitialize, m_data);
}
}
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;
}
void ArrayBufferContents::transfer(ArrayBufferContents& other)
{
ASSERT(!other.m_data);
other.m_data = m_data;
other.m_sizeInBytes = m_sizeInBytes;
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)
return;
memcpy(other.m_data, m_data, m_sizeInBytes);
other.m_sizeInBytes = m_sizeInBytes;
}
void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy policy, void*& data)
{
data = partitionAllocGenericFlags(WTF::Partitions::getBufferPartition(), PartitionAllocReturnNull, size);
if (policy == ZeroInitialize && data)
memset(data, '\0', size);
}
void ArrayBufferContents::freeMemory(void* data, size_t)
{
partitionFreeGeneric(WTF::Partitions::getBufferPartition(), data);
}
}