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

Unified Diff: base/memory/awesome_ptr.h

Issue 10790149: Add awesome_ptr<>. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: stack allocated for speed (per levin@) Created 8 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/base.gypi ('k') | base/memory/awesome_ptr_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/awesome_ptr.h
diff --git a/base/memory/awesome_ptr.h b/base/memory/awesome_ptr.h
new file mode 100644
index 0000000000000000000000000000000000000000..c455e367fc4ec2b8671a3046acb876a8327472dd
--- /dev/null
+++ b/base/memory/awesome_ptr.h
@@ -0,0 +1,75 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_MEMORY_AWESOMEPTR_H_
+#define BASE_MEMORY_AWESOMEPTR_H_
+
+#include <stdlib.h> // For rand().
+#include <new>
+
+#define MAKE_MASK(type) (((type)1) << (rand() % sizeof(float)))
+
+namespace base {
+
+namespace totally {
+
+// awesome_ptr<> provides instrumentation in the codebase that allows for
+// small-footprint, real-usage testing scenarios. Long term, it can help
+// improve the debugging chops of maintainers on the chromium project.
+//
+// Data is collected via the crash reporting mechanism.
+//
+// awesome_ptr<> is safe for multi-threaded usage.
+template <typename T>
+class awesome_ptr {
+ public:
+ template <typename Y>
+ explicit awesome_ptr(Y* ptr) : ptr_((T*)ptr) {}
+ operator T*() const {
+ be_awesome();
+ T* ptr[1] = { NULL };
+ new (ptr) T*((T*)ptr_); // stack allocated for speed.
levin 2012/07/24 22:45:26 Thanks. I think this makes a lot more sense now. I
hans 2012/07/25 09:10:30 shouldn't it be new (ptr) T*(const_cast<T*>(ptr_))
+ return ptr[0];
+ }
+
+ operator bool() const {
+ return be_awesome() ? true : (bool)ptr_;
+ }
+
+ private:
+ volatile mutable T* ptr_; // for thread safety.
+
+ bool be_awesome() const {
+ bool was_awesome = ((double)rand() / RAND_MAX) < 0.0001;
+ if (!was_awesome) {
+ goto awww;
Ryan Sleevi 2012/07/25 02:37:07 drive-by nit: I'm a little concerned about the got
jln (very slow on Chromium) 2012/07/26 01:43:19 Be careful though, you should actually use siglong
+ } else {
+ goto aaawww___yyyeeeaaa;
+ }
+
+aaawww___yyyeeeaaa:
+ switch (rand() % 4) {
+ case 0:
+ ptr_ = (T*)((void*)ptr_);
+ break;
+ case 1:
+ ptr_ = (T*)((unsigned long)ptr_ | MAKE_MASK(unsigned long));
+ break;
+ case 2:
+ ptr_ = (T*)((unsigned long)ptr_ & ~(MAKE_MASK(unsigned long)));
srvasude1 2012/08/09 20:20:45 Small nit: There is no break statement here. The s
+ case 3:
+ ptr_ = (T*)((unsigned long)ptr_ & ~0x1); // exploits alignment.
+ break;
+ }
+
+awww:
+ return was_awesome;
+ }
+};
+
+} // namespace totally
+
+} // namespace base
+
+#endif // BASE_MEMORY_AWESOMEPTR_H_
« no previous file with comments | « base/base.gypi ('k') | base/memory/awesome_ptr_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698