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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 } | 128 } |
129 | 129 |
130 ArrayBufferContents::DataHandle ArrayBufferContents::createDataHandle( | 130 ArrayBufferContents::DataHandle ArrayBufferContents::createDataHandle( |
131 size_t size, | 131 size_t size, |
132 InitializationPolicy policy) { | 132 InitializationPolicy policy) { |
133 return DataHandle(ArrayBufferContents::allocateMemoryOrNull(size, policy), | 133 return DataHandle(ArrayBufferContents::allocateMemoryOrNull(size, policy), |
134 freeMemory); | 134 freeMemory); |
135 } | 135 } |
136 | 136 |
137 ArrayBufferContents::DataHolder::DataHolder() | 137 ArrayBufferContents::DataHolder::DataHolder() |
138 : m_data(nullptr, freeMemory), m_sizeInBytes(0), m_isShared(NotShared) {} | 138 : m_data(nullptr, freeMemory), |
| 139 m_sizeInBytes(0), |
| 140 m_isShared(NotShared), |
| 141 m_hasRegisteredExternalAllocation(false) {} |
139 | 142 |
140 ArrayBufferContents::DataHolder::~DataHolder() { | 143 ArrayBufferContents::DataHolder::~DataHolder() { |
141 adjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(m_sizeInBytes)); | 144 if (m_hasRegisteredExternalAllocation) |
| 145 adjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(m_sizeInBytes)); |
142 | 146 |
143 m_data.reset(); | 147 m_data.reset(); |
144 m_sizeInBytes = 0; | 148 m_sizeInBytes = 0; |
145 m_isShared = NotShared; | 149 m_isShared = NotShared; |
146 } | 150 } |
147 | 151 |
148 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, | 152 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, |
149 SharingType isShared, | 153 SharingType isShared, |
150 InitializationPolicy policy) { | 154 InitializationPolicy policy) { |
151 DCHECK(!m_data); | 155 DCHECK(!m_data); |
152 DCHECK_EQ(m_sizeInBytes, 0u); | 156 DCHECK_EQ(m_sizeInBytes, 0u); |
| 157 DCHECK(!m_hasRegisteredExternalAllocation); |
153 | 158 |
154 m_data = createDataHandle(sizeInBytes, policy); | 159 m_data = createDataHandle(sizeInBytes, policy); |
155 if (!m_data) | 160 if (!m_data) |
156 return; | 161 return; |
157 | 162 |
158 m_sizeInBytes = sizeInBytes; | 163 m_sizeInBytes = sizeInBytes; |
159 m_isShared = isShared; | 164 m_isShared = isShared; |
160 | 165 |
161 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); | 166 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); |
162 } | 167 } |
163 | 168 |
164 void ArrayBufferContents::DataHolder::adopt(DataHandle data, | 169 void ArrayBufferContents::DataHolder::adopt(DataHandle data, |
165 unsigned sizeInBytes, | 170 unsigned sizeInBytes, |
166 SharingType isShared) { | 171 SharingType isShared) { |
167 DCHECK(!m_data); | 172 DCHECK(!m_data); |
168 DCHECK_EQ(m_sizeInBytes, 0u); | 173 DCHECK_EQ(m_sizeInBytes, 0u); |
| 174 DCHECK(!m_hasRegisteredExternalAllocation); |
169 | 175 |
170 m_data = std::move(data); | 176 m_data = std::move(data); |
171 m_sizeInBytes = sizeInBytes; | 177 m_sizeInBytes = sizeInBytes; |
172 m_isShared = isShared; | 178 m_isShared = isShared; |
173 | 179 |
174 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); | 180 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); |
175 } | 181 } |
176 | 182 |
177 void ArrayBufferContents::DataHolder::copyMemoryFrom(const DataHolder& source) { | 183 void ArrayBufferContents::DataHolder::copyMemoryFrom(const DataHolder& source) { |
178 DCHECK(!m_data); | 184 DCHECK(!m_data); |
179 DCHECK_EQ(m_sizeInBytes, 0u); | 185 DCHECK_EQ(m_sizeInBytes, 0u); |
| 186 DCHECK(!m_hasRegisteredExternalAllocation); |
180 | 187 |
181 m_data = createDataHandle(source.sizeInBytes(), DontInitialize); | 188 m_data = createDataHandle(source.sizeInBytes(), DontInitialize); |
182 if (!m_data) | 189 if (!m_data) |
183 return; | 190 return; |
184 | 191 |
185 m_sizeInBytes = source.sizeInBytes(); | 192 m_sizeInBytes = source.sizeInBytes(); |
186 memcpy(m_data.get(), source.data(), source.sizeInBytes()); | 193 memcpy(m_data.get(), source.data(), source.sizeInBytes()); |
187 | 194 |
188 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); | 195 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); |
189 } | 196 } |
190 | 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 |
191 } // namespace WTF | 211 } // namespace WTF |
OLD | NEW |