| 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 19 matching lines...) Expand all Loading... |
| 30 #include "wtf/Assertions.h" | 30 #include "wtf/Assertions.h" |
| 31 #include "wtf/PartitionAlloc.h" | 31 #include "wtf/PartitionAlloc.h" |
| 32 #include "wtf/Partitions.h" | 32 #include "wtf/Partitions.h" |
| 33 #include <string.h> | 33 #include <string.h> |
| 34 | 34 |
| 35 namespace WTF { | 35 namespace WTF { |
| 36 | 36 |
| 37 AdjustAmountOfExternalAllocatedMemoryFunction ArrayBufferContents::s_adjustAmoun
tOfExternalAllocatedMemoryFunction; | 37 AdjustAmountOfExternalAllocatedMemoryFunction ArrayBufferContents::s_adjustAmoun
tOfExternalAllocatedMemoryFunction; |
| 38 | 38 |
| 39 ArrayBufferContents::ArrayBufferContents() | 39 ArrayBufferContents::ArrayBufferContents() |
| 40 : m_holder(adoptRef(new DataHolder())) { } | 40 : m_holder(adoptRef(new DataHolder())) {} |
| 41 | 41 |
| 42 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB
yteSize, SharingType isShared, ArrayBufferContents::InitializationPolicy policy) | 42 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB
yteSize, SharingType isShared, ArrayBufferContents::InitializationPolicy policy) |
| 43 : m_holder(adoptRef(new DataHolder())) | 43 : m_holder(adoptRef(new DataHolder())) { |
| 44 { | 44 // Do not allow 32-bit overflow of the total size. |
| 45 // Do not allow 32-bit overflow of the total size. | 45 unsigned totalSize = numElements * elementByteSize; |
| 46 unsigned totalSize = numElements * elementByteSize; | 46 if (numElements) { |
| 47 if (numElements) { | 47 if (totalSize / numElements != elementByteSize) { |
| 48 if (totalSize / numElements != elementByteSize) { | 48 return; |
| 49 return; | |
| 50 } | |
| 51 } | 49 } |
| 50 } |
| 52 | 51 |
| 53 m_holder->allocateNew(totalSize, isShared, policy); | 52 m_holder->allocateNew(totalSize, isShared, policy); |
| 54 } | 53 } |
| 55 | 54 |
| 56 ArrayBufferContents::ArrayBufferContents( | 55 ArrayBufferContents::ArrayBufferContents( |
| 57 void* data, unsigned sizeInBytes, SharingType isShared) | 56 void* data, |
| 58 : m_holder(adoptRef(new DataHolder())) | 57 unsigned sizeInBytes, |
| 59 { | 58 SharingType isShared) |
| 60 if (data) { | 59 : m_holder(adoptRef(new DataHolder())) { |
| 61 m_holder->adopt(data, sizeInBytes, isShared); | 60 if (data) { |
| 62 } else { | 61 m_holder->adopt(data, sizeInBytes, isShared); |
| 63 ASSERT(!sizeInBytes); | 62 } else { |
| 64 sizeInBytes = 0; | 63 ASSERT(!sizeInBytes); |
| 65 // Allow null data if size is 0 bytes, make sure data is valid pointer. | 64 sizeInBytes = 0; |
| 66 // (PartitionAlloc guarantees valid pointer for size 0) | 65 // Allow null data if size is 0 bytes, make sure data is valid pointer. |
| 67 m_holder->allocateNew(sizeInBytes, isShared, ZeroInitialize); | 66 // (PartitionAlloc guarantees valid pointer for size 0) |
| 68 } | 67 m_holder->allocateNew(sizeInBytes, isShared, ZeroInitialize); |
| 68 } |
| 69 } | 69 } |
| 70 | 70 |
| 71 ArrayBufferContents::~ArrayBufferContents() | 71 ArrayBufferContents::~ArrayBufferContents() { |
| 72 { | |
| 73 } | 72 } |
| 74 | 73 |
| 75 void ArrayBufferContents::neuter() | 74 void ArrayBufferContents::neuter() { |
| 76 { | 75 m_holder.clear(); |
| 77 m_holder.clear(); | |
| 78 } | 76 } |
| 79 | 77 |
| 80 void ArrayBufferContents::transfer(ArrayBufferContents& other) | 78 void ArrayBufferContents::transfer(ArrayBufferContents& other) { |
| 81 { | 79 ASSERT(!isShared()); |
| 82 ASSERT(!isShared()); | 80 ASSERT(!other.m_holder->data()); |
| 83 ASSERT(!other.m_holder->data()); | 81 other.m_holder = m_holder; |
| 84 other.m_holder = m_holder; | 82 neuter(); |
| 85 neuter(); | |
| 86 } | 83 } |
| 87 | 84 |
| 88 void ArrayBufferContents::shareWith(ArrayBufferContents& other) | 85 void ArrayBufferContents::shareWith(ArrayBufferContents& other) { |
| 89 { | 86 ASSERT(isShared()); |
| 90 ASSERT(isShared()); | 87 ASSERT(!other.m_holder->data()); |
| 91 ASSERT(!other.m_holder->data()); | 88 other.m_holder = m_holder; |
| 92 other.m_holder = m_holder; | |
| 93 } | 89 } |
| 94 | 90 |
| 95 void ArrayBufferContents::copyTo(ArrayBufferContents& other) | 91 void ArrayBufferContents::copyTo(ArrayBufferContents& other) { |
| 96 { | 92 ASSERT(!m_holder->isShared() && !other.m_holder->isShared()); |
| 97 ASSERT(!m_holder->isShared() && !other.m_holder->isShared()); | 93 m_holder->copyMemoryTo(*other.m_holder); |
| 98 m_holder->copyMemoryTo(*other.m_holder); | |
| 99 } | 94 } |
| 100 | 95 |
| 101 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic
y, void*& data) | 96 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic
y, void*& data) { |
| 102 { | 97 if (s_adjustAmountOfExternalAllocatedMemoryFunction) |
| 103 if (s_adjustAmountOfExternalAllocatedMemoryFunction) | 98 s_adjustAmountOfExternalAllocatedMemoryFunction(static_cast<int>(size)); |
| 104 s_adjustAmountOfExternalAllocatedMemoryFunction(static_cast<int>(size)); | 99 data = partitionAllocGeneric(WTF::Partitions::bufferPartition(), size); |
| 105 data = partitionAllocGeneric(WTF::Partitions::bufferPartition(), size); | 100 if (policy == ZeroInitialize && data) |
| 106 if (policy == ZeroInitialize && data) | 101 memset(data, '\0', size); |
| 107 memset(data, '\0', size); | |
| 108 } | 102 } |
| 109 | 103 |
| 110 void ArrayBufferContents::freeMemory(void* data, size_t size) | 104 void ArrayBufferContents::freeMemory(void* data, size_t size) { |
| 111 { | 105 Partitions::bufferFree(data); |
| 112 Partitions::bufferFree(data); | 106 if (s_adjustAmountOfExternalAllocatedMemoryFunction) |
| 113 if (s_adjustAmountOfExternalAllocatedMemoryFunction) | 107 s_adjustAmountOfExternalAllocatedMemoryFunction(-static_cast<int>(size)); |
| 114 s_adjustAmountOfExternalAllocatedMemoryFunction(-static_cast<int>(size))
; | |
| 115 } | 108 } |
| 116 | 109 |
| 117 ArrayBufferContents::DataHolder::DataHolder() | 110 ArrayBufferContents::DataHolder::DataHolder() |
| 118 : m_data(nullptr) | 111 : m_data(nullptr), m_sizeInBytes(0), m_isShared(NotShared) {} |
| 119 , m_sizeInBytes(0) | |
| 120 , m_isShared(NotShared) { } | |
| 121 | 112 |
| 122 ArrayBufferContents::DataHolder::~DataHolder() | 113 ArrayBufferContents::DataHolder::~DataHolder() { |
| 123 { | 114 ArrayBufferContents::freeMemory(m_data, m_sizeInBytes); |
| 124 ArrayBufferContents::freeMemory(m_data, m_sizeInBytes); | |
| 125 | 115 |
| 126 m_data = nullptr; | 116 m_data = nullptr; |
| 127 m_sizeInBytes = 0; | 117 m_sizeInBytes = 0; |
| 128 m_isShared = NotShared; | 118 m_isShared = NotShared; |
| 129 } | 119 } |
| 130 | 120 |
| 131 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, SharingT
ype isShared, InitializationPolicy policy) | 121 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, SharingT
ype isShared, InitializationPolicy policy) { |
| 132 { | 122 ASSERT(!m_data); |
| 133 ASSERT(!m_data); | 123 void* data = nullptr; |
| 134 void* data = nullptr; | 124 allocateMemory(sizeInBytes, policy, data); |
| 135 allocateMemory(sizeInBytes, policy, data); | 125 m_data = data; |
| 136 m_data = data; | 126 m_sizeInBytes = data ? sizeInBytes : 0; |
| 137 m_sizeInBytes = data ? sizeInBytes : 0; | 127 m_isShared = isShared; |
| 138 m_isShared = isShared; | |
| 139 } | 128 } |
| 140 | 129 |
| 141 void ArrayBufferContents::DataHolder::adopt(void* data, unsigned sizeInBytes, Sh
aringType isShared) | 130 void ArrayBufferContents::DataHolder::adopt(void* data, unsigned sizeInBytes, Sh
aringType isShared) { |
| 142 { | 131 ASSERT(!m_data); |
| 143 ASSERT(!m_data); | 132 m_data = data; |
| 144 m_data = data; | 133 m_sizeInBytes = sizeInBytes; |
| 145 m_sizeInBytes = sizeInBytes; | 134 m_isShared = isShared; |
| 146 m_isShared = isShared; | |
| 147 } | 135 } |
| 148 | 136 |
| 149 void ArrayBufferContents::DataHolder::copyMemoryTo(DataHolder& other) | 137 void ArrayBufferContents::DataHolder::copyMemoryTo(DataHolder& other) { |
| 150 { | 138 ASSERT(!other.m_sizeInBytes); |
| 151 ASSERT(!other.m_sizeInBytes); | 139 ArrayBufferContents::freeMemory(other.m_data, other.m_sizeInBytes); |
| 152 ArrayBufferContents::freeMemory(other.m_data, other.m_sizeInBytes); | 140 ArrayBufferContents::allocateMemory(m_sizeInBytes, DontInitialize, other.m_dat
a); |
| 153 ArrayBufferContents::allocateMemory(m_sizeInBytes, DontInitialize, other.m_d
ata); | 141 if (!other.m_data) |
| 154 if (!other.m_data) | 142 return; |
| 155 return; | 143 memcpy(other.m_data, m_data, m_sizeInBytes); |
| 156 memcpy(other.m_data, m_data, m_sizeInBytes); | 144 other.m_sizeInBytes = m_sizeInBytes; |
| 157 other.m_sizeInBytes = m_sizeInBytes; | |
| 158 } | 145 } |
| 159 | 146 |
| 160 } // namespace WTF | 147 } // namespace WTF |
| OLD | NEW |