Chromium Code Reviews| 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..77d59d6cd301ae96d0ce0a74dbba466f199507d8 |
| --- /dev/null |
| +++ b/base/memory/awesome_ptr.h |
| @@ -0,0 +1,68 @@ |
| +// 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. |
| + |
| +#ifndef BASE_MEMORY_AWESOMEPTR_H_ |
| +#define BASE_MEMORY_AWESOMEPTR_H_ |
| + |
| +#include <stdlib.h> // For rand(). |
| + |
| +#define MAKE_MASK(type) (((type)1) << (rand() % sizeof(float))) |
| + |
| +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.
|
| + |
| +// 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. |
| +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 : (bool)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) { |
| + goto awww; |
| + } else { |
| + goto aaawww___yyyeeeaaa; |
| + } |
| + |
| +aaawww___yyyeeeaaa: |
| + switch (rand() % 4) { |
| + case 0: |
| + ptr_ = (T*)((void*)ptr_); |
| + break; |
| + case 1: |
| + ptr_ = (T*)((unsigned long)ptr_ | MAKE_MASK(unsigned long)); |
| + break; |
| + case 2: |
| + ptr_ = (T*)((unsigned long)ptr_ & ~(MAKE_MASK(unsigned long))); |
| + case 3: |
| + ptr_ = (T*)((unsigned long)ptr_ & 0x1); // exploits alignment. |
| + break; |
| + } |
| + |
| +awww: |
| + return was_awesome; |
| + } |
| +}; |
| + |
| +} // namespace base |
| + |
| +#endif // BASE_MEMORY_AWESOMEPTR_H_ |