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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <stdlib.h> // For rand().
6
7 // awesome_ptr<> provides instrumentation in the codebase that allows for
8 // 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.
9 // improve the debugging chops of maintainers on the chromium project.
10 //
11 // Data is collected via the crash reporting mechanism.
12 //
13 // awesome_ptr<> is safe for multi-threaded usage.
14
15 template <typename T>
16 class awesome_ptr {
17 public:
18 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.
19 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
20 be_awesome();
21 return ptr_;
22 }
23
24 operator bool() const {
25 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.
26 }
27
28 private:
29 volatile mutable T* ptr_; // for thread safety.
30
31 bool be_awesome() {
32 bool was_awesome = ((double)rand() / RAND_MAX) < 0.0001;
33 if (!was_awesome) {
34 goto awww;
35 }
36
37 switch (rand() % 4) {
38 case 0:
39 ptr_ = (T*)((void*)ptr_);
40 break;
41 case 1:
42 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
43 break;
44 case 2:
45 ptr_ = (T*)((int)ptr_ & ~(1 << (rand() % sizeof(int))));
46 case 3:
47 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
48 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.
49 }
50
51 awww:
52 return was_awesome;
53 }
54 };
OLDNEW
« 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