Chromium Code Reviews| 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 | |
| 5 #ifndef BASE_MEMORY_AWESOMEPTR_H_ | |
| 6 #define BASE_MEMORY_AWESOMEPTR_H_ | |
| 7 | |
| 8 #include <stdlib.h> // For rand(). | |
| 9 | |
| 10 #define MAKE_MASK(type) (((type)1) << (rand() % sizeof(float))) | |
| 11 | |
| 12 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.
| |
| 13 | |
| 14 // awesome_ptr<> provides instrumentation in the codebase that allows for | |
| 15 // small-footprint, real-usage testing scenarios. Long term, it can help | |
| 16 // improve the debugging chops of maintainers on the chromium project. | |
| 17 // | |
| 18 // Data is collected via the crash reporting mechanism. | |
| 19 // | |
| 20 // awesome_ptr<> is safe for multi-threaded usage. | |
| 21 template <typename T> | |
| 22 class awesome_ptr { | |
| 23 public: | |
| 24 template <typename Y> | |
| 25 explicit awesome_ptr(Y* ptr) : ptr_((T*)ptr) {} | |
| 26 operator T*() const { | |
| 27 be_awesome(); | |
| 28 return (T*)ptr_; | |
| 29 } | |
| 30 | |
| 31 operator bool() const { | |
| 32 return be_awesome() ? true : (bool)ptr_; | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 volatile mutable T* ptr_; // for thread safety. | |
| 37 | |
| 38 bool be_awesome() const { | |
| 39 bool was_awesome = ((double)rand() / RAND_MAX) < 0.0001; | |
| 40 if (!was_awesome) { | |
| 41 goto awww; | |
| 42 } else { | |
| 43 goto aaawww___yyyeeeaaa; | |
| 44 } | |
| 45 | |
| 46 aaawww___yyyeeeaaa: | |
| 47 switch (rand() % 4) { | |
| 48 case 0: | |
| 49 ptr_ = (T*)((void*)ptr_); | |
| 50 break; | |
| 51 case 1: | |
| 52 ptr_ = (T*)((unsigned long)ptr_ | MAKE_MASK(unsigned long)); | |
| 53 break; | |
| 54 case 2: | |
| 55 ptr_ = (T*)((unsigned long)ptr_ & ~(MAKE_MASK(unsigned long))); | |
| 56 case 3: | |
| 57 ptr_ = (T*)((unsigned long)ptr_ & 0x1); // exploits alignment. | |
| 58 break; | |
| 59 } | |
| 60 | |
| 61 awww: | |
| 62 return was_awesome; | |
| 63 } | |
| 64 }; | |
| 65 | |
| 66 } // namespace base | |
| 67 | |
| 68 #endif // BASE_MEMORY_AWESOMEPTR_H_ | |
| OLD | NEW |