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

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: passes unique_ptr 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
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(ScopedData 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::allocateMemory(size_t size,
122 InitializationPolicy policy, 122 InitializationPolicy policy) {
123 void*& data) { 123 return allocateMemoryWithFlags(size, policy, 0);
124 allocateMemoryWithFlags(size, policy, 0, data);
125 } 124 }
126 125
127 void ArrayBufferContents::allocateMemoryOrNull(size_t size, 126 void* ArrayBufferContents::allocateMemoryOrNull(size_t size,
128 InitializationPolicy policy, 127 InitializationPolicy policy) {
129 void*& data) { 128 return allocateMemoryWithFlags(size, policy, base::PartitionAllocReturnNull);
130 allocateMemoryWithFlags(size, policy, base::PartitionAllocReturnNull, data);
131 } 129 }
132 130
133 void ArrayBufferContents::freeMemory(void* data, size_t size) { 131 void ArrayBufferContents::freeMemory(void* data) {
134 PartitionFreeGeneric(Partitions::arrayBufferPartition(), data); 132 PartitionFreeGeneric(Partitions::arrayBufferPartition(), data);
135 } 133 }
136 134
137 ArrayBufferContents::DataHolder::DataHolder() 135 ArrayBufferContents::DataHolder::DataHolder()
138 : m_data(nullptr), m_sizeInBytes(0), m_isShared(NotShared) {} 136 : m_data(nullptr, freeMemory), m_sizeInBytes(0), m_isShared(NotShared) {}
139 137
140 ArrayBufferContents::DataHolder::~DataHolder() { 138 ArrayBufferContents::DataHolder::~DataHolder() {
141 ArrayBufferContents::freeMemory(m_data, m_sizeInBytes);
142
143 adjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(m_sizeInBytes)); 139 adjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(m_sizeInBytes));
144 140
145 m_data = nullptr; 141 m_data.reset();
146 m_sizeInBytes = 0; 142 m_sizeInBytes = 0;
147 m_isShared = NotShared; 143 m_isShared = NotShared;
148 } 144 }
149 145
150 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, 146 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes,
151 SharingType isShared, 147 SharingType isShared,
152 InitializationPolicy policy) { 148 InitializationPolicy policy) {
153 DCHECK(!m_data); 149 DCHECK(!m_data);
154 DCHECK_EQ(m_sizeInBytes, 0u); 150 DCHECK_EQ(m_sizeInBytes, 0u);
155 151
156 ArrayBufferContents::allocateMemory(sizeInBytes, policy, m_data); 152 m_data.reset(ArrayBufferContents::allocateMemory(sizeInBytes, policy));
jbroman 2017/02/28 20:01:19 While I'm pretty sure the last deleter set here wi
alokp 2017/02/28 20:56:08 Replaced with createData.
157 if (!m_data) 153 if (!m_data)
158 return; 154 return;
159 155
160 m_sizeInBytes = sizeInBytes; 156 m_sizeInBytes = sizeInBytes;
161 m_isShared = isShared; 157 m_isShared = isShared;
162 158
163 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); 159 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes);
164 } 160 }
165 161
166 void ArrayBufferContents::DataHolder::adopt(void* data, 162 void ArrayBufferContents::DataHolder::adopt(ScopedData data,
167 unsigned sizeInBytes, 163 unsigned sizeInBytes,
168 SharingType isShared) { 164 SharingType isShared) {
169 DCHECK(!m_data); 165 DCHECK(!m_data);
170 DCHECK_EQ(m_sizeInBytes, 0u); 166 DCHECK_EQ(m_sizeInBytes, 0u);
171 167
172 m_data = data; 168 m_data = std::move(data);
173 m_sizeInBytes = sizeInBytes; 169 m_sizeInBytes = sizeInBytes;
174 m_isShared = isShared; 170 m_isShared = isShared;
175 171
176 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); 172 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes);
177 } 173 }
178 174
179 void ArrayBufferContents::DataHolder::copyMemoryFrom(const DataHolder& source) { 175 void ArrayBufferContents::DataHolder::copyMemoryFrom(const DataHolder& source) {
180 DCHECK(!m_data); 176 DCHECK(!m_data);
181 DCHECK_EQ(m_sizeInBytes, 0u); 177 DCHECK_EQ(m_sizeInBytes, 0u);
182 178
183 ArrayBufferContents::allocateMemory(source.sizeInBytes(), DontInitialize, 179 m_data.reset(ArrayBufferContents::allocateMemory(source.sizeInBytes(),
jbroman 2017/02/28 20:01:19 While I'm pretty sure the last deleter set here wi
alokp 2017/02/28 20:56:08 Replaced with createData.
184 m_data); 180 DontInitialize));
185 if (!m_data) 181 if (!m_data)
186 return; 182 return;
187 183
188 m_sizeInBytes = source.sizeInBytes(); 184 m_sizeInBytes = source.sizeInBytes();
189 memcpy(m_data, source.data(), source.sizeInBytes()); 185 memcpy(m_data.get(), source.data(), source.sizeInBytes());
190 186
191 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes); 187 adjustAmountOfExternalAllocatedMemory(m_sizeInBytes);
192 } 188 }
193 189
194 } // namespace WTF 190 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698