OLD | NEW |
---|---|
(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 | |
Scott Hess - ex-Googler
2012/07/24 19:22:15
Header guards.
awong
2012/07/24 20:47:12
Done.
| |
5 #include <stdlib.h> // For rand(). | |
6 | |
7 // awesome_ptr<> provides instrumentation in the codebase that allows for | |
8 // small-footprint, real-usage testing scenarios. Long term, it can help | |
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. | |
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
| |
14 | |
15 #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
| |
16 | |
17 template <typename T> | |
18 class awesome_ptr { | |
19 public: | |
20 template <typename Y> | |
21 explicit awesome_ptr(Y* ptr) : ptr_((T*)ptr) {} | |
22 operator T*() const { | |
23 be_awesome(); | |
24 return (T*)ptr_; | |
25 } | |
26 | |
27 operator bool() const { | |
28 return be_awesome() ? true : ptr_; | |
29 } | |
30 | |
31 private: | |
32 volatile mutable T* ptr_; // for thread safety. | |
33 | |
34 bool be_awesome() const { | |
35 bool was_awesome = ((double)rand() / RAND_MAX) < 0.0001; | |
36 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
| |
37 goto awww; | |
38 } | |
39 | |
40 switch (rand() % 4) { | |
41 case 0: | |
42 ptr_ = (T*)((void*)ptr_); | |
43 break; | |
44 case 1: | |
45 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
| |
46 break; | |
47 case 2: | |
48 ptr_ = (T*)((unsigned long)ptr_ & ~(MAKE_MASK(unsigned long))); | |
49 case 3: | |
50 ptr_ = (T*)((unsigned long)ptr_ & 0x1); | |
51 break; | |
52 } | |
53 | |
54 awww: | |
55 return was_awesome; | |
56 } | |
57 }; | |
OLD | NEW |