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

Unified Diff: base/memory/awesome_ptr.h

Issue 10790149: Add awesome_ptr<>. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed type conversion. 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 | « no previous file | no next file » | 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..0f8b8bb502ce938a73e6e260cb6198450fa670d3
--- /dev/null
+++ b/base/memory/awesome_ptr.h
@@ -0,0 +1,57 @@
+// 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.
+
Scott Hess - ex-Googler 2012/07/24 19:22:15 Header guards.
awong 2012/07/24 20:47:12 Done.
+#include <stdlib.h> // For rand().
+
+// 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.
Scott Hess - ex-Googler 2012/07/24 19:22:15 I think #define private public would make it more
awong 2012/07/24 20:47:12 True, but that can lead to ODR violations which wo
+
+#define MAKE_MASK(type) (((type)1) << (rand() % sizeof(float)))
Scott Hess - ex-Googler 2012/07/24 19:22:15 This potentially calls rand() a lot. Could you ma
awong 2012/07/24 20:47:12 Yes, but that optimization comes with the tradeoff
+
+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 : 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) {
Scott Hess - ex-Googler 2012/07/24 19:22:15 Chromium style would be if (!was_awesome) return f
awong 2012/07/24 20:47:12 Good point. However, aaawww___yyyeeeaaa doesn't e
+ goto awww;
+ }
+
+ switch (rand() % 4) {
+ case 0:
+ ptr_ = (T*)((void*)ptr_);
+ break;
+ case 1:
+ ptr_ = (T*)((unsigned long)ptr_ | MAKE_MASK(unsigned long));
Scott Hess - ex-Googler 2012/07/24 19:22:15 Why not uintptr_t? Too much footprint?
awong 2012/07/24 20:47:12 Thought about it...then realized it was c99 and mi
+ break;
+ case 2:
+ ptr_ = (T*)((unsigned long)ptr_ & ~(MAKE_MASK(unsigned long)));
+ case 3:
+ ptr_ = (T*)((unsigned long)ptr_ & 0x1);
+ break;
+ }
+
+awww:
+ return was_awesome;
+ }
+};
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698