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

Side by Side Diff: base/memory/scoped_ptr.h

Issue 610533003: Allow custom deleters to opt out of self reset checks for scoped_ptr. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Example usage Created 6 years, 2 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 | « no previous file | base/memory/scoped_ptr_unittest.cc » ('j') | net/ssl/openssl_client_key_store.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Scopers help you manage ownership of a pointer, helping you easily manage a 5 // Scopers help you manage ownership of a pointer, helping you easily manage a
6 // pointer within a scope, and automatically destroying the pointer at the end 6 // pointer within a scope, and automatically destroying the pointer at the end
7 // of a scope. There are two main classes you will use, which correspond to the 7 // of a scope. There are two main classes you will use, which correspond to the
8 // operators new/delete and new[]/delete[]. 8 // operators new/delete and new[]/delete[].
9 // 9 //
10 // Example usage (scoped_ptr<T>): 10 // Example usage (scoped_ptr<T>):
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 namespace internal { 177 namespace internal {
178 178
179 template <typename T> struct IsNotRefCounted { 179 template <typename T> struct IsNotRefCounted {
180 enum { 180 enum {
181 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value && 181 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value &&
182 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>:: 182 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>::
183 value 183 value
184 }; 184 };
185 }; 185 };
186 186
187 template <typename T>
188 struct ShouldAbortOnSelfReset {
189 template <typename U>
190 static NoType Test(const typename U::AllowSelfReset*);
191
192 template <typename U>
193 static YesType Test(...);
194
195 static const bool value = sizeof(Test<T>(0)) == sizeof(YesType);
196 };
197
187 // Minimal implementation of the core logic of scoped_ptr, suitable for 198 // Minimal implementation of the core logic of scoped_ptr, suitable for
188 // reuse in both scoped_ptr and its specializations. 199 // reuse in both scoped_ptr and its specializations.
189 template <class T, class D> 200 template <class T, class D>
190 class scoped_ptr_impl { 201 class scoped_ptr_impl {
191 public: 202 public:
192 explicit scoped_ptr_impl(T* p) : data_(p) {} 203 explicit scoped_ptr_impl(T* p) : data_(p) {}
193 204
194 // Initializer for deleters that have data parameters. 205 // Initializer for deleters that have data parameters.
195 scoped_ptr_impl(T* p, const D& d) : data_(p, d) {} 206 scoped_ptr_impl(T* p, const D& d) : data_(p, d) {}
196 207
(...skipping 18 matching lines...) Expand all
215 226
216 ~scoped_ptr_impl() { 227 ~scoped_ptr_impl() {
217 if (data_.ptr != nullptr) { 228 if (data_.ptr != nullptr) {
218 // Not using get_deleter() saves one function call in non-optimized 229 // Not using get_deleter() saves one function call in non-optimized
219 // builds. 230 // builds.
220 static_cast<D&>(data_)(data_.ptr); 231 static_cast<D&>(data_)(data_.ptr);
221 } 232 }
222 } 233 }
223 234
224 void reset(T* p) { 235 void reset(T* p) {
225 // This is a self-reset, which is no longer allowed: http://crbug.com/162971 236 // This is a self-reset, which is no longer allowed for default deleters:
226 if (p != nullptr && p == data_.ptr) 237 // https://crbug.com/162971
238 if (ShouldAbortOnSelfReset<D>::value && p != nullptr && p == data_.ptr)
227 abort(); 239 abort();
228 240
229 // Note that running data_.ptr = p can lead to undefined behavior if 241 // Note that running data_.ptr = p can lead to undefined behavior if
230 // get_deleter()(get()) deletes this. In order to prevent this, reset() 242 // get_deleter()(get()) deletes this. In order to prevent this, reset()
231 // should update the stored pointer before deleting its old value. 243 // should update the stored pointer before deleting its old value.
232 // 244 //
233 // However, changing reset() to use that behavior may cause current code to 245 // However, changing reset() to use that behavior may cause current code to
234 // break in unexpected ways. If the destruction of the owned object 246 // break in unexpected ways. If the destruction of the owned object
235 // dereferences the scoped_ptr when it is destroyed by a call to reset(), 247 // dereferences the scoped_ptr when it is destroyed by a call to reset(),
236 // then it will incorrectly dispatch calls to |p| rather than the original 248 // then it will incorrectly dispatch calls to |p| rather than the original
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 613
602 // A function to convert T* into scoped_ptr<T> 614 // A function to convert T* into scoped_ptr<T>
603 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation 615 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
604 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) 616 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
605 template <typename T> 617 template <typename T>
606 scoped_ptr<T> make_scoped_ptr(T* ptr) { 618 scoped_ptr<T> make_scoped_ptr(T* ptr) {
607 return scoped_ptr<T>(ptr); 619 return scoped_ptr<T>(ptr);
608 } 620 }
609 621
610 #endif // BASE_MEMORY_SCOPED_PTR_H_ 622 #endif // BASE_MEMORY_SCOPED_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/scoped_ptr_unittest.cc » ('j') | net/ssl/openssl_client_key_store.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698