1 /* 2 * Copyright (C) 2009 Apple Inc. All rights reserved. 3 * Copyright (C) 2013 Google Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "config.h" 28 #include "wtf/ArrayBufferContents.h" 29 30 #include "wtf/Assertions.h" 31 #include "wtf/WTF.h" 32 #include <string.h> 33 34 namespace WTF { 35 36 ArrayBufferContents::ArrayBufferContents() 37 : m_data(0) 38 , m_sizeInBytes(0) 39 , m_deallocationObserver(0) { } 40 41 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy) 42 : m_data(0) 43 , m_sizeInBytes(0) 44 , m_deallocationObserver(0) 45 { 46 // Do not allow 32-bit overflow of the total size. 47 if (numElements) { 48 unsigned totalSize = numElements * elementByteSize; 49 if (totalSize / numElements != elementByteSize) { 50 m_data = 0; 51 return; 52 } 53 } 54 allocateMemory(numElements * elementByteSize, policy, m_data); 55 m_sizeInBytes = numElements * elementByteSize; 56 } 57 58 ArrayBufferContents::ArrayBufferContents( 59 void* data, unsigned sizeInBytes, ArrayBufferDeallocationObserver* observer) 60 : m_data(data) 61 , m_sizeInBytes(sizeInBytes) 62 , m_deallocationObserver(observer) 63 { 64 if (!m_data) { 65 ASSERT(!m_sizeInBytes); 66 m_sizeInBytes = 0; 67 // Allow null data if size is 0 bytes, make sure m_data is valid pointer. 68 // (partitionAllocGeneric guarantees valid pointer for size 0) 69 allocateMemory(0, ZeroInitialize, m_data); 70 } 71 } 72 73 ArrayBufferContents::~ArrayBufferContents() 74 { 75 freeMemory(m_data, m_sizeInBytes); 76 clear(); 77 } 78 79 void ArrayBufferContents::clear() 80 { 81 if (m_data && m_deallocationObserver) 82 m_deallocationObserver->arrayBufferDeallocated(m_sizeInBytes); 83 m_data = 0; 84 m_sizeInBytes = 0; 85 m_deallocationObserver = 0; 86 } 87 88 void ArrayBufferContents::transfer(ArrayBufferContents& other) 89 { 90 ASSERT(!other.m_data); 91 other.m_data = m_data; 92 other.m_sizeInBytes = m_sizeInBytes; 93 clear(); 94 } 95 96 void ArrayBufferContents::copyTo(ArrayBufferContents& other) 97 { 98 ASSERT(!other.m_sizeInBytes); 99 other.freeMemory(other.m_data, other.m_sizeInBytes); 100 allocateMemory(m_sizeInBytes, DontInitialize, other.m_data); 101 if (!other.m_data) 102 return; 103 memcpy(other.m_data, m_data, m_sizeInBytes); 104 other.m_sizeInBytes = m_sizeInBytes; 105 } 106 107 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy policy, void*& data) 108 { 109 data = partitionAllocGeneric(WTF::Partitions::getBufferPartition(), size); 110 if (policy == ZeroInitialize) 111 memset(data, '\0', size); 112 } 113 114 void ArrayBufferContents::freeMemory(void* data, size_t) 115 { 116 if (data) 117 partitionFreeGeneric(WTF::Partitions::getBufferPartition(), data); 118 } 119 120 } // namespace WTF 121