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..0f8b8bb502ce938a73e6e260cb6198450fa670d3 |
| --- /dev/null |
| +++ b/base/memory/awesome_ptr.h |
| @@ -0,0 +1,57 @@ |
| +// 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. |
| + |
|
Scott Hess - ex-Googler
2012/07/24 19:22:15
Header guards.
awong
2012/07/24 20:47:12
Done.
|
| +#include <stdlib.h> // For rand(). |
| + |
| +// 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. |
|
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
|
| + |
| +#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
|
| + |
| +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 : 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) { |
|
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
|
| + goto awww; |
| + } |
| + |
| + switch (rand() % 4) { |
| + case 0: |
| + ptr_ = (T*)((void*)ptr_); |
| + break; |
| + case 1: |
| + 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
|
| + break; |
| + case 2: |
| + ptr_ = (T*)((unsigned long)ptr_ & ~(MAKE_MASK(unsigned long))); |
| + case 3: |
| + ptr_ = (T*)((unsigned long)ptr_ & 0x1); |
| + break; |
| + } |
| + |
| +awww: |
| + return was_awesome; |
| + } |
| +}; |