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

Unified Diff: base/memory/awesome_ptr.h

Issue 10790149: Add awesome_ptr<>. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address shess's comments. Add unittests and fix readability issue with single gotos. 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
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..77d59d6cd301ae96d0ce0a74dbba466f199507d8
--- /dev/null
+++ b/base/memory/awesome_ptr.h
@@ -0,0 +1,68 @@
+// 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().
+
+#define MAKE_MASK(type) (((type)1) << (rand() % sizeof(float)))
+
+namespace base {
Scott Hess - ex-Googler 2012/07/24 21:06:40 [Yearns for namespace basically, or perhaps totall
awong 2012/07/24 22:45:17 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();
+ return (T*)ptr_;
+ }
+
+ 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;
+ } 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)));
+ case 3:
+ ptr_ = (T*)((unsigned long)ptr_ & 0x1); // exploits alignment.
+ break;
+ }
+
+awww:
+ return was_awesome;
+ }
+};
+
+} // namespace base
+
+#endif // BASE_MEMORY_AWESOMEPTR_H_

Powered by Google App Engine
This is Rietveld 408576698