18e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project/* 2231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * Copyright (C) 2009 Apple Inc. All rights reserved. 38e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 48e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Redistribution and use in source and binary forms, with or without 58e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * modification, are permitted provided that the following conditions 68e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * are met: 78e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 1. Redistributions of source code must retain the above copyright 88e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * notice, this list of conditions and the following disclaimer. 98e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright 108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * notice, this list of conditions and the following disclaimer in the 118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * documentation and/or other materials provided with the distribution. 128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project */ 258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 26231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "config.h" 276c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen#include "ArrayBuffer.h" 28643ca7872b450ea4efacab6188849e5aac2ba161Steve Block 29643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#include <wtf/RefPtr.h> 30231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 31231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blocknamespace WebCore { 32643ca7872b450ea4efacab6188849e5aac2ba161Steve Block 336c2af9490927c3c5959b5cb07461b646f8b32f6cKristian MonsenPassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned elementByteSize) 34231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block{ 35dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void* data = tryAllocate(numElements, elementByteSize); 36dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block if (!data) 37dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block return 0; 386c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen return adoptRef(new ArrayBuffer(data, numElements * elementByteSize)); 39231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} 40231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 416c2af9490927c3c5959b5cb07461b646f8b32f6cKristian MonsenPassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other) 42231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block{ 43e14391e94c850b8bd03680c23b38978db68687a8John Reck return ArrayBuffer::create(other->data(), other->byteLength()); 44e14391e94c850b8bd03680c23b38978db68687a8John Reck} 45e14391e94c850b8bd03680c23b38978db68687a8John Reck 46e14391e94c850b8bd03680c23b38978db68687a8John ReckPassRefPtr<ArrayBuffer> ArrayBuffer::create(void* source, unsigned byteLength) 47e14391e94c850b8bd03680c23b38978db68687a8John Reck{ 48e14391e94c850b8bd03680c23b38978db68687a8John Reck void* data = tryAllocate(byteLength, 1); 49dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block if (!data) 50dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block return 0; 51e14391e94c850b8bd03680c23b38978db68687a8John Reck RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(data, byteLength)); 52e14391e94c850b8bd03680c23b38978db68687a8John Reck memcpy(buffer->data(), source, byteLength); 53643ca7872b450ea4efacab6188849e5aac2ba161Steve Block return buffer.release(); 54231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} 55231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 566c2af9490927c3c5959b5cb07461b646f8b32f6cKristian MonsenArrayBuffer::ArrayBuffer(void* data, unsigned sizeInBytes) 57dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block : m_sizeInBytes(sizeInBytes) 58dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch , m_data(data) 59dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch{ 60643ca7872b450ea4efacab6188849e5aac2ba161Steve Block} 61643ca7872b450ea4efacab6188849e5aac2ba161Steve Block 62dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdochvoid* ArrayBuffer::data() 63dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch{ 64643ca7872b450ea4efacab6188849e5aac2ba161Steve Block return m_data; 65643ca7872b450ea4efacab6188849e5aac2ba161Steve Block} 66643ca7872b450ea4efacab6188849e5aac2ba161Steve Block 67dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdochconst void* ArrayBuffer::data() const 68dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch{ 69643ca7872b450ea4efacab6188849e5aac2ba161Steve Block return m_data; 70643ca7872b450ea4efacab6188849e5aac2ba161Steve Block} 71643ca7872b450ea4efacab6188849e5aac2ba161Steve Block 72dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdochunsigned ArrayBuffer::byteLength() const 73dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch{ 74643ca7872b450ea4efacab6188849e5aac2ba161Steve Block return m_sizeInBytes; 75643ca7872b450ea4efacab6188849e5aac2ba161Steve Block} 76643ca7872b450ea4efacab6188849e5aac2ba161Steve Block 77dd8bb3de4f353a81954234999f1fea748aee2ea9Ben MurdochArrayBuffer::~ArrayBuffer() 78dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch{ 79643ca7872b450ea4efacab6188849e5aac2ba161Steve Block WTF::fastFree(m_data); 80231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} 818e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project 82dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdochvoid* ArrayBuffer::tryAllocate(unsigned numElements, unsigned elementByteSize) 83dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch{ 84dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void* result; 85f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch // Do not allow 32-bit overflow of the total size. 86f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch // FIXME: Why not? The tryFastCalloc function already checks its arguments, 87f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch // and will fail if there is any overflow, so why should we include a 88f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch // redudant unnecessarily restrictive check here? 89dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block if (numElements) { 90dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block unsigned totalSize = numElements * elementByteSize; 91dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block if (totalSize / numElements != elementByteSize) 92dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block return 0; 93dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block } 94dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block if (WTF::tryFastCalloc(numElements, elementByteSize).getValue(result)) 95dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block return result; 96dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block return 0; 97dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block} 98dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 998e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project} 100