| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2013 Google Inc. All rights reserved. | 3 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #include "config.h" | 27 #include "config.h" |
| 28 #include "wtf/ArrayBufferContents.h" | 28 #include "wtf/ArrayBufferContents.h" |
| 29 | 29 |
| 30 #include "wtf/ArrayBufferDeallocationObserver.h" | 30 #include "wtf/ArrayBufferDeallocationObserver.h" |
| 31 #include "wtf/Assertions.h" |
| 31 #include <string.h> | 32 #include <string.h> |
| 32 | 33 |
| 33 namespace WTF { | 34 namespace WTF { |
| 34 | 35 |
| 35 ArrayBufferContents::ArrayBufferContents() | 36 ArrayBufferContents::ArrayBufferContents() |
| 36 : m_data(0) | 37 : m_data(0) |
| 37 , m_sizeInBytes(0) | 38 , m_sizeInBytes(0) |
| 38 , m_deallocationObserver(0) { } | 39 , m_deallocationObserver(0) { } |
| 39 | 40 |
| 40 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB
yteSize, ArrayBufferContents::InitializationPolicy policy) | 41 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB
yteSize, ArrayBufferContents::InitializationPolicy policy) |
| 41 : m_data(0) | 42 : m_data(0) |
| 42 , m_sizeInBytes(0) | 43 , m_sizeInBytes(0) |
| 43 , m_deallocationObserver(0) | 44 , m_deallocationObserver(0) |
| 44 { | 45 { |
| 45 // Do not allow 32-bit overflow of the total size. | 46 // Do not allow 32-bit overflow of the total size. |
| 46 // FIXME: Why not? The tryFastCalloc function already checks its arguments, | |
| 47 // and will fail if there is any overflow, so why should we include a | |
| 48 // redudant unnecessarily restrictive check here? | |
| 49 if (numElements) { | 47 if (numElements) { |
| 50 unsigned totalSize = numElements * elementByteSize; | 48 unsigned totalSize = numElements * elementByteSize; |
| 51 if (totalSize / numElements != elementByteSize) { | 49 if (totalSize / numElements != elementByteSize) { |
| 52 m_data = 0; | 50 m_data = 0; |
| 53 return; | 51 return; |
| 54 } | 52 } |
| 55 } | 53 } |
| 56 if (allocateMemory(numElements * elementByteSize, policy, m_data)) { | 54 allocateMemory(numElements * elementByteSize, policy, m_data); |
| 57 m_sizeInBytes = numElements * elementByteSize; | 55 m_sizeInBytes = numElements * elementByteSize; |
| 58 return; | |
| 59 } | |
| 60 m_data = 0; | |
| 61 } | 56 } |
| 62 | 57 |
| 63 ArrayBufferContents::ArrayBufferContents(void* data, unsigned sizeInBytes) | 58 ArrayBufferContents::ArrayBufferContents(void* data, unsigned sizeInBytes) |
| 64 : m_data(data) | 59 : m_data(data) |
| 65 , m_sizeInBytes(sizeInBytes) | 60 , m_sizeInBytes(sizeInBytes) |
| 66 , m_deallocationObserver(0) | 61 , m_deallocationObserver(0) |
| 67 { | 62 { |
| 68 if (!m_data) { | 63 if (!m_data) { |
| 69 ASSERT(!m_sizeInBytes); | 64 ASSERT(!m_sizeInBytes); |
| 70 m_sizeInBytes = 0; | 65 m_sizeInBytes = 0; |
| 71 // Allow null data if size is 0 bytes, make sure m_data is valid pointer
. | 66 // Allow null data if size is 0 bytes, make sure m_data is valid pointer
. |
| 72 // (fastMalloc guarantees valid pointer for size 0) | 67 // (fastMalloc guarantees valid pointer for size 0) |
| 73 if (!allocateMemory(0, ZeroInitialize, m_data)) | 68 allocateMemory(0, ZeroInitialize, m_data); |
| 74 ASSERT_NOT_REACHED(); | |
| 75 } | 69 } |
| 76 } | 70 } |
| 77 | 71 |
| 78 ArrayBufferContents::~ArrayBufferContents() | 72 ArrayBufferContents::~ArrayBufferContents() |
| 79 { | 73 { |
| 80 freeMemory(m_data); | 74 freeMemory(m_data); |
| 81 clear(); | 75 clear(); |
| 82 } | 76 } |
| 83 | 77 |
| 84 void ArrayBufferContents::clear() | 78 void ArrayBufferContents::clear() |
| (...skipping 17 matching lines...) Expand all Loading... |
| 102 { | 96 { |
| 103 ASSERT(!other.m_sizeInBytes); | 97 ASSERT(!other.m_sizeInBytes); |
| 104 other.freeMemory(other.m_data); | 98 other.freeMemory(other.m_data); |
| 105 allocateMemory(m_sizeInBytes, DontInitialize, other.m_data); | 99 allocateMemory(m_sizeInBytes, DontInitialize, other.m_data); |
| 106 if (!other.m_data) | 100 if (!other.m_data) |
| 107 return; | 101 return; |
| 108 memcpy(other.m_data, m_data, m_sizeInBytes); | 102 memcpy(other.m_data, m_data, m_sizeInBytes); |
| 109 other.m_sizeInBytes = m_sizeInBytes; | 103 other.m_sizeInBytes = m_sizeInBytes; |
| 110 } | 104 } |
| 111 | 105 |
| 112 bool ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic
y, void*& data) | 106 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic
y, void*& data) |
| 113 { | 107 { |
| 114 if (policy == ZeroInitialize) { | 108 data = WTF::fastMalloc(size); |
| 115 return WTF::tryFastCalloc(size, 1).getValue(data); | 109 if (policy == ZeroInitialize) |
| 116 } | 110 memset(data, '\0', size); |
| 117 ASSERT(policy == DontInitialize); | |
| 118 return WTF::tryFastMalloc(size).getValue(data); | |
| 119 } | 111 } |
| 120 | 112 |
| 121 void ArrayBufferContents::freeMemory(void * data) | 113 void ArrayBufferContents::freeMemory(void * data) |
| 122 { | 114 { |
| 123 WTF::fastFree(data); | 115 WTF::fastFree(data); |
| 124 } | 116 } |
| 125 | 117 |
| 126 } // namespace WTF | 118 } // namespace WTF |
| OLD | NEW |