Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: third_party/WebKit/Source/wtf/typed_arrays/ArrayBufferContents.cpp

Issue 2719883004: Adds support for ArrayBufferContents with external buffer. (Closed)
Patch Set: renames ScopedData -> DataHandle Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/typed_arrays/ArrayBufferContents.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 unsigned totalSize = numElements * elementByteSize; 61 unsigned totalSize = numElements * elementByteSize;
62 if (numElements) { 62 if (numElements) {
63 if (totalSize / numElements != elementByteSize) { 63 if (totalSize / numElements != elementByteSize) {
64 return; 64 return;
65 } 65 }
66 } 66 }
67 67
68 m_holder->allocateNew(totalSize, isShared, policy); 68 m_holder->allocateNew(totalSize, isShared, policy);
69 } 69 }
70 70
71 ArrayBufferContents::ArrayBufferContents(void* data, 71 ArrayBufferContents::ArrayBufferContents(DataHandle data,
72 unsigned sizeInBytes, 72 unsigned sizeInBytes,
73 SharingType isShared) 73 SharingType isShared)
74 : m_holder(adoptRef(new DataHolder())) { 74 : m_holder(adoptRef(new DataHolder())) {
75 if (data) { 75 if (data) {
76 m_holder->adopt(data, sizeInBytes, isShared); 76 m_holder->adopt(std::move(data), sizeInBytes, isShared);
77 } else { 77 } else {
78 DCHECK_EQ(sizeInBytes, 0u); 78 DCHECK_EQ(sizeInBytes, 0u);
79 sizeInBytes = 0; 79 sizeInBytes = 0;
80 // Allow null data if size is 0 bytes, make sure data is valid pointer. 80 // Allow null data if size is 0 bytes, make sure data is valid pointer.
81 // (PartitionAlloc guarantees valid pointer for size 0) 81 // (PartitionAlloc guarantees valid pointer for size 0)
82 m_holder->allocateNew(sizeInBytes, isShared, ZeroInitialize); 82 m_holder->allocateNew(sizeInBytes, isShared, ZeroInitialize);
83 } 83 }
84 } 84 }
85 85
86 ArrayBufferContents::~ArrayBufferContents() {} 86 ArrayBufferContents::~ArrayBufferContents() {}
(...skipping 13 matching lines...) Expand all
100 DCHECK(isShared()); 100 DCHECK(isShared());
101 DCHECK(!other.m_holder->data()); 101 DCHECK(!other.m_holder->data());
102 other.m_holder = m_holder; 102 other.m_holder = m_holder;
103 } 103 }
104 104
105 void ArrayBufferContents::copyTo(ArrayBufferContents& other) { 105 void ArrayBufferContents::copyTo(ArrayBufferContents& other) {
106 DCHECK(!m_holder->isShared() && !other.m_holder->isShared()); 106 DCHECK(!m_holder->isShared() && !other.m_holder->isShared());
107 other.m_holder->copyMemoryFrom(*m_holder); 107 other.m_holder->copyMemoryFrom(*m_holder);
108 } 108 }
109 109
110 void ArrayBufferContents::allocateMemoryWithFlags(size_t size, 110 void* ArrayBufferContents::allocateMemoryWithFlags(size_t size,
111 InitializationPolicy policy, 111 InitializationPolicy policy,
112 int flags, 112 int flags) {
113 void*& data) { 113 void* data = PartitionAllocGenericFlags(
114 data = PartitionAllocGenericFlags(
115 Partitions::arrayBufferPartition(), flags, size, 114 Partitions::arrayBufferPartition(), flags, size,
116 WTF_HEAP_PROFILER_TYPE_NAME(ArrayBufferContents)); 115 WTF_HEAP_PROFILER_TYPE_NAME(ArrayBufferContents));
117 if (policy == ZeroInitialize && data) 116 if (policy == ZeroInitialize && data)
118 memset(data, '\0', size); 117 memset(data, '\0', size);
118 return data;
119 } 119 }
120 120
121 void ArrayBufferContents::allocateMemory(size_t size, 121 void* ArrayBufferContents::allocateMemoryOrNull(size_t size,
122 InitializationPolicy policy, 122 InitializationPolicy policy) {
123 void*& data) { 123 return allocateMemoryWithFlags(size, policy, base::PartitionAllocReturnNull);
124 allocateMemoryWithFlags(size, policy, 0, data);
125 } 124 }
126 125
127 void ArrayBufferContents::allocateMemoryOrNull(size_t size, 126 void ArrayBufferContents::freeMemory(void* data) {
128 InitializationPolicy policy,
129 void*& data) {
130 allocateMemoryWithFlags(size, policy, base::PartitionAllocReturnNull, data);
131 }
132
133 void ArrayBufferContents::freeMemory(void* data, size_t size) {
134 PartitionFreeGeneric(Partitions::arrayBufferPartition(), data); 127 PartitionFreeGeneric(Partitions::arrayBufferPartition(), data);
135 } 128 }
136 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() 137 ArrayBufferContents::DataHolder::DataHolder()
138 : m_data(nullptr), m_sizeInBytes(0), m_isShared(NotShared) {} 138 : m_data(nullptr, freeMemory), m_sizeInBytes(0), m_isShared(NotShared) {}
139 139
140 ArrayBufferContents::DataHolder::~DataHolder() { 140 ArrayBufferContents::DataHolder::~DataHolder() {
141 ArrayBufferContents::freeMemory(m_data, m_sizeInBytes);
142
143 adjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(m_sizeInBytes)); 141 adjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(m_sizeInBytes));
144 142
145 m_data = nullptr; 143 m_data.reset();
146 m_sizeInBytes = 0; 144 m_sizeInBytes = 0;
147 m_isShared = NotShared; 145 m_isShared = NotShared;
148 } 146 }
149 147
150 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, 148 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes,
151 SharingType isShared, 149 SharingType isShared,
152 InitializationPolicy policy) { 150 InitializationPolicy policy) {
153 DCHECK(!m_data); 151 DCHECK(!m_data);
154 DCHECK_EQ(m_sizeInBytes, 0u); 152 DCHECK_EQ(m_sizeInBytes, 0u);
155 153
156 ArrayBufferContents::allocateMemory(sizeInBytes, policy, m_data); 154 m_data = createDataHandle(sizeInBytes, policy);
157 if (!m_data) 155 if (!m_data)
158 return; 156 return;
159 157
160 m_sizeInBytes = sizeInBytes; 158 m_sizeInBytes = sizeInBytes;
161 m_isShared = isShared; 159 m_isShared = isShared;
162 160
163 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); 161 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes);
164 } 162 }
165 163
166 void ArrayBufferContents::DataHolder::adopt(void* data, 164 void ArrayBufferContents::DataHolder::adopt(DataHandle data,
167 unsigned sizeInBytes, 165 unsigned sizeInBytes,
168 SharingType isShared) { 166 SharingType isShared) {
169 DCHECK(!m_data); 167 DCHECK(!m_data);
170 DCHECK_EQ(m_sizeInBytes, 0u); 168 DCHECK_EQ(m_sizeInBytes, 0u);
171 169
172 m_data = data; 170 m_data = std::move(data);
173 m_sizeInBytes = sizeInBytes; 171 m_sizeInBytes = sizeInBytes;
174 m_isShared = isShared; 172 m_isShared = isShared;
175 173
176 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); 174 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes);
177 } 175 }
178 176
179 void ArrayBufferContents::DataHolder::copyMemoryFrom(const DataHolder& source) { 177 void ArrayBufferContents::DataHolder::copyMemoryFrom(const DataHolder& source) {
180 DCHECK(!m_data); 178 DCHECK(!m_data);
181 DCHECK_EQ(m_sizeInBytes, 0u); 179 DCHECK_EQ(m_sizeInBytes, 0u);
182 180
183 ArrayBufferContents::allocateMemory(source.sizeInBytes(), DontInitialize, 181 m_data = createDataHandle(source.sizeInBytes(), DontInitialize);
184 m_data);
185 if (!m_data) 182 if (!m_data)
186 return; 183 return;
187 184
188 m_sizeInBytes = source.sizeInBytes(); 185 m_sizeInBytes = source.sizeInBytes();
189 memcpy(m_data, source.data(), source.sizeInBytes()); 186 memcpy(m_data.get(), source.data(), source.sizeInBytes());
190 187
191 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); 188 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes);
192 } 189 }
193 190
194 } // namespace WTF 191 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/typed_arrays/ArrayBufferContents.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698