OLD | NEW |
| (Empty) |
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 "wtf/typed_arrays/ArrayBufferContents.h" | |
28 | |
29 #include "base/allocator/partition_allocator/partition_alloc.h" | |
30 #include "wtf/Assertions.h" | |
31 #include "wtf/allocator/Partitions.h" | |
32 #include <string.h> | |
33 | |
34 namespace WTF { | |
35 | |
36 void ArrayBufferContents::defaultAdjustAmountOfExternalAllocatedMemoryFunction( | |
37 int64_t diff) { | |
38 // Do nothing by default. | |
39 } | |
40 | |
41 ArrayBufferContents::AdjustAmountOfExternalAllocatedMemoryFunction | |
42 ArrayBufferContents::s_adjustAmountOfExternalAllocatedMemoryFunction = | |
43 defaultAdjustAmountOfExternalAllocatedMemoryFunction; | |
44 | |
45 #if DCHECK_IS_ON() | |
46 ArrayBufferContents::AdjustAmountOfExternalAllocatedMemoryFunction | |
47 ArrayBufferContents:: | |
48 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction; | |
49 #endif | |
50 | |
51 ArrayBufferContents::ArrayBufferContents() | |
52 : m_holder(adoptRef(new DataHolder())) {} | |
53 | |
54 ArrayBufferContents::ArrayBufferContents( | |
55 unsigned numElements, | |
56 unsigned elementByteSize, | |
57 SharingType isShared, | |
58 ArrayBufferContents::InitializationPolicy policy) | |
59 : m_holder(adoptRef(new DataHolder())) { | |
60 // Do not allow 32-bit overflow of the total size. | |
61 unsigned totalSize = numElements * elementByteSize; | |
62 if (numElements) { | |
63 if (totalSize / numElements != elementByteSize) { | |
64 return; | |
65 } | |
66 } | |
67 | |
68 m_holder->allocateNew(totalSize, isShared, policy); | |
69 } | |
70 | |
71 ArrayBufferContents::ArrayBufferContents(DataHandle data, | |
72 unsigned sizeInBytes, | |
73 SharingType isShared) | |
74 : m_holder(adoptRef(new DataHolder())) { | |
75 if (data) { | |
76 m_holder->adopt(std::move(data), sizeInBytes, isShared); | |
77 } else { | |
78 DCHECK_EQ(sizeInBytes, 0u); | |
79 sizeInBytes = 0; | |
80 // Allow null data if size is 0 bytes, make sure data is valid pointer. | |
81 // (PartitionAlloc guarantees valid pointer for size 0) | |
82 m_holder->allocateNew(sizeInBytes, isShared, ZeroInitialize); | |
83 } | |
84 } | |
85 | |
86 ArrayBufferContents::~ArrayBufferContents() {} | |
87 | |
88 void ArrayBufferContents::neuter() { | |
89 m_holder.clear(); | |
90 } | |
91 | |
92 void ArrayBufferContents::transfer(ArrayBufferContents& other) { | |
93 DCHECK(!isShared()); | |
94 DCHECK(!other.m_holder->data()); | |
95 other.m_holder = m_holder; | |
96 neuter(); | |
97 } | |
98 | |
99 void ArrayBufferContents::shareWith(ArrayBufferContents& other) { | |
100 DCHECK(isShared()); | |
101 DCHECK(!other.m_holder->data()); | |
102 other.m_holder = m_holder; | |
103 } | |
104 | |
105 void ArrayBufferContents::copyTo(ArrayBufferContents& other) { | |
106 DCHECK(!m_holder->isShared() && !other.m_holder->isShared()); | |
107 other.m_holder->copyMemoryFrom(*m_holder); | |
108 } | |
109 | |
110 void* ArrayBufferContents::allocateMemoryWithFlags(size_t size, | |
111 InitializationPolicy policy, | |
112 int flags) { | |
113 void* data = PartitionAllocGenericFlags( | |
114 Partitions::arrayBufferPartition(), flags, size, | |
115 WTF_HEAP_PROFILER_TYPE_NAME(ArrayBufferContents)); | |
116 if (policy == ZeroInitialize && data) | |
117 memset(data, '\0', size); | |
118 return data; | |
119 } | |
120 | |
121 void* ArrayBufferContents::allocateMemoryOrNull(size_t size, | |
122 InitializationPolicy policy) { | |
123 return allocateMemoryWithFlags(size, policy, base::PartitionAllocReturnNull); | |
124 } | |
125 | |
126 void ArrayBufferContents::freeMemory(void* data) { | |
127 PartitionFreeGeneric(Partitions::arrayBufferPartition(), data); | |
128 } | |
129 | |
130 ArrayBufferContents::DataHandle ArrayBufferContents::createDataHandle( | |
131 size_t size, | |
132 InitializationPolicy policy) { | |
133 return DataHandle(ArrayBufferContents::allocateMemoryOrNull(size, policy), | |
134 freeMemory); | |
135 } | |
136 | |
137 ArrayBufferContents::DataHolder::DataHolder() | |
138 : m_data(nullptr, freeMemory), | |
139 m_sizeInBytes(0), | |
140 m_isShared(NotShared), | |
141 m_hasRegisteredExternalAllocation(false) {} | |
142 | |
143 ArrayBufferContents::DataHolder::~DataHolder() { | |
144 if (m_hasRegisteredExternalAllocation) | |
145 adjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(m_sizeInBytes)); | |
146 | |
147 m_data.reset(); | |
148 m_sizeInBytes = 0; | |
149 m_isShared = NotShared; | |
150 } | |
151 | |
152 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, | |
153 SharingType isShared, | |
154 InitializationPolicy policy) { | |
155 DCHECK(!m_data); | |
156 DCHECK_EQ(m_sizeInBytes, 0u); | |
157 DCHECK(!m_hasRegisteredExternalAllocation); | |
158 | |
159 m_data = createDataHandle(sizeInBytes, policy); | |
160 if (!m_data) | |
161 return; | |
162 | |
163 m_sizeInBytes = sizeInBytes; | |
164 m_isShared = isShared; | |
165 | |
166 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); | |
167 } | |
168 | |
169 void ArrayBufferContents::DataHolder::adopt(DataHandle data, | |
170 unsigned sizeInBytes, | |
171 SharingType isShared) { | |
172 DCHECK(!m_data); | |
173 DCHECK_EQ(m_sizeInBytes, 0u); | |
174 DCHECK(!m_hasRegisteredExternalAllocation); | |
175 | |
176 m_data = std::move(data); | |
177 m_sizeInBytes = sizeInBytes; | |
178 m_isShared = isShared; | |
179 | |
180 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); | |
181 } | |
182 | |
183 void ArrayBufferContents::DataHolder::copyMemoryFrom(const DataHolder& source) { | |
184 DCHECK(!m_data); | |
185 DCHECK_EQ(m_sizeInBytes, 0u); | |
186 DCHECK(!m_hasRegisteredExternalAllocation); | |
187 | |
188 m_data = createDataHandle(source.sizeInBytes(), DontInitialize); | |
189 if (!m_data) | |
190 return; | |
191 | |
192 m_sizeInBytes = source.sizeInBytes(); | |
193 memcpy(m_data.get(), source.data(), source.sizeInBytes()); | |
194 | |
195 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); | |
196 } | |
197 | |
198 void ArrayBufferContents::DataHolder:: | |
199 registerExternalAllocationWithCurrentContext() { | |
200 DCHECK(!m_hasRegisteredExternalAllocation); | |
201 adjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(m_sizeInBytes)); | |
202 } | |
203 | |
204 void ArrayBufferContents::DataHolder:: | |
205 unregisterExternalAllocationWithCurrentContext() { | |
206 if (!m_hasRegisteredExternalAllocation) | |
207 return; | |
208 adjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(m_sizeInBytes)); | |
209 } | |
210 | |
211 } // namespace WTF | |
OLD | NEW |