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

Unified Diff: base/memory/awesome_ptr.h

Issue 10790149: Add awesome_ptr<>. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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..7774dc891b7aeec0d152b2874141008235cb9980
--- /dev/null
+++ b/base/memory/awesome_ptr.h
@@ -0,0 +1,54 @@
+// 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.
+
+#include <stdlib.h> // For rand().
+
+// awesome_ptr<> provides instrumentation in the codebase that allows for
+// small-footpring, real-usage testing scenarios. Long term, it can help
dmichael (off chromium) 2012/07/24 18:30:45 nit:footprint
Avi (use Gerrit) 2012/07/24 18:32:04 s/footpring/footprint/
awong 2012/07/24 19:09:16 Done.
awong 2012/07/24 19:09:16 Done.
+// 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:
+ awesome_ptr(T* ptr) : ptr_(ptr) {}
dmichael (off chromium) 2012/07/24 18:30:45 shouldn't this be: template <typename Y> awesome_p
Avi (use Gerrit) 2012/07/24 18:32:04 indentation, |explicit|
dmichael (off chromium) 2012/07/24 19:04:35 I disagree, allowing implicit conversion is awesom
awong 2012/07/24 19:09:16 Good catch on the explicit.
awong 2012/07/24 19:09:16 I like it. Used a c-style case though since it's
awong 2012/07/24 19:09:16 No no...we have to follow the style guide.
+ operator T*() const {
dmichael (off chromium) 2012/07/24 19:04:35 On 2nd look, this could also be a template so you
awong 2012/07/24 19:09:16 Unfortunately template inference only works for fu
+ be_awesome();
+ return ptr_;
+ }
+
+ operator bool() const {
+ return be_awesome() ? true : ptr_;
dmichael (off chromium) 2012/07/24 19:04:35 true and ptr_ are different types. Are you sure th
awong 2012/07/24 19:09:16 Agh. Good catch. fixed the type system.
+ }
+
+ private:
+ volatile mutable T* ptr_; // for thread safety.
+
+ bool be_awesome() {
+ bool was_awesome = ((double)rand() / RAND_MAX) < 0.0001;
+ if (!was_awesome) {
+ goto awww;
+ }
+
+ switch (rand() % 4) {
+ case 0:
+ ptr_ = (T*)((void*)ptr_);
+ break;
+ case 1:
+ ptr_ = (T*)((int)ptr_ | (1 << (rand() % sizeof(int))));
Avi (use Gerrit) 2012/07/24 18:32:04 Can we abstract out the |(1 << (rand() % sizeof(in
awong 2012/07/24 19:09:16 Done. Also sped up the macro by using float instea
+ break;
+ case 2:
+ ptr_ = (T*)((int)ptr_ & ~(1 << (rand() % sizeof(int))));
+ case 3:
+ ptr_ = ptr_ | 0x1;
Avi (use Gerrit) 2012/07/24 18:32:04 This is subsumed by case 1. Can we find more aweso
awong 2012/07/24 19:09:16 It's not quite the same if you think about memory
+ break;
dmichael (off chromium) 2012/07/24 18:30:45 I didn't really read this part, but I assume it's
awong 2012/07/24 19:09:16 vegan really. No animal byproducts used.
+ }
+
+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