WebGLArrayBuffer.cpp [plain text]
#include "config.h"
#if ENABLE(3D_CANVAS)
#include "WebGLArrayBuffer.h"
#include <wtf/RefPtr.h>
namespace WebCore {
PassRefPtr<WebGLArrayBuffer> WebGLArrayBuffer::create(unsigned numElements, unsigned elementByteSize)
{
void* data = tryAllocate(numElements, elementByteSize);
if (!data)
return 0;
return adoptRef(new WebGLArrayBuffer(data, numElements * elementByteSize));
}
PassRefPtr<WebGLArrayBuffer> WebGLArrayBuffer::create(WebGLArrayBuffer* other)
{
void* data = tryAllocate(other->byteLength(), 1);
if (!data)
return 0;
RefPtr<WebGLArrayBuffer> buffer = adoptRef(new WebGLArrayBuffer(data, other->byteLength()));
memcpy(buffer->data(), other->data(), other->byteLength());
return buffer.release();
}
WebGLArrayBuffer::WebGLArrayBuffer(void* data, unsigned sizeInBytes)
: m_sizeInBytes(sizeInBytes)
, m_data(data) {
}
void* WebGLArrayBuffer::data() {
return m_data;
}
const void* WebGLArrayBuffer::data() const {
return m_data;
}
unsigned WebGLArrayBuffer::byteLength() const {
return m_sizeInBytes;
}
WebGLArrayBuffer::~WebGLArrayBuffer() {
WTF::fastFree(m_data);
}
void* WebGLArrayBuffer::tryAllocate(unsigned numElements, unsigned elementByteSize) {
void* result;
if (numElements) {
unsigned totalSize = numElements * elementByteSize;
if (totalSize / numElements != elementByteSize)
return 0;
}
if (WTF::tryFastCalloc(numElements, elementByteSize).getValue(result))
return result;
return 0;
}
}
#endif // ENABLE(3D_CANVAS)