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..c455e367fc4ec2b8671a3046acb876a8327472dd |
| --- /dev/null |
| +++ b/base/memory/awesome_ptr.h |
| @@ -0,0 +1,75 @@ |
| +// 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(). |
| +#include <new> |
| + |
| +#define MAKE_MASK(type) (((type)1) << (rand() % sizeof(float))) |
| + |
| +namespace base { |
| + |
| +namespace 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(); |
| + T* ptr[1] = { NULL }; |
| + new (ptr) T*((T*)ptr_); // stack allocated for speed. |
|
levin
2012/07/24 22:45:26
Thanks. I think this makes a lot more sense now. I
hans
2012/07/25 09:10:30
shouldn't it be new (ptr) T*(const_cast<T*>(ptr_))
|
| + return ptr[0]; |
| + } |
| + |
| + 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; |
|
Ryan Sleevi
2012/07/25 02:37:07
drive-by nit: I'm a little concerned about the got
jln (very slow on Chromium)
2012/07/26 01:43:19
Be careful though, you should actually use siglong
|
| + } 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))); |
|
srvasude1
2012/08/09 20:20:45
Small nit: There is no break statement here. The s
|
| + case 3: |
| + ptr_ = (T*)((unsigned long)ptr_ & ~0x1); // exploits alignment. |
| + break; |
| + } |
| + |
| +awww: |
| + return was_awesome; |
| + } |
| +}; |
| + |
| +} // namespace totally |
| + |
| +} // namespace base |
| + |
| +#endif // BASE_MEMORY_AWESOMEPTR_H_ |