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..7774dc891b7aeec0d152b2874141008235cb9980 |
| --- /dev/null |
| +++ b/base/memory/awesome_ptr.h |
| @@ -0,0 +1,54 @@ |
| +// 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. |
| + |
| +#include <stdlib.h> // For rand(). |
| + |
| +// awesome_ptr<> provides instrumentation in the codebase that allows for |
| +// small-footpring, real-usage testing scenarios. Long term, it can help |
|
dmichael (off chromium)
2012/07/24 18:30:45
nit:footprint
Avi (use Gerrit)
2012/07/24 18:32:04
s/footpring/footprint/
awong
2012/07/24 19:09:16
Done.
awong
2012/07/24 19:09:16
Done.
|
| +// 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: |
| + awesome_ptr(T* ptr) : ptr_(ptr) {} |
|
dmichael (off chromium)
2012/07/24 18:30:45
shouldn't this be:
template <typename Y>
awesome_p
Avi (use Gerrit)
2012/07/24 18:32:04
indentation, |explicit|
dmichael (off chromium)
2012/07/24 19:04:35
I disagree, allowing implicit conversion is awesom
awong
2012/07/24 19:09:16
Good catch on the explicit.
awong
2012/07/24 19:09:16
I like it. Used a c-style case though since it's
awong
2012/07/24 19:09:16
No no...we have to follow the style guide.
|
| + operator T*() const { |
|
dmichael (off chromium)
2012/07/24 19:04:35
On 2nd look, this could also be a template so you
awong
2012/07/24 19:09:16
Unfortunately template inference only works for fu
|
| + be_awesome(); |
| + return ptr_; |
| + } |
| + |
| + operator bool() const { |
| + return be_awesome() ? true : ptr_; |
|
dmichael (off chromium)
2012/07/24 19:04:35
true and ptr_ are different types. Are you sure th
awong
2012/07/24 19:09:16
Agh. Good catch. fixed the type system.
|
| + } |
| + |
| + private: |
| + volatile mutable T* ptr_; // for thread safety. |
| + |
| + bool be_awesome() { |
| + bool was_awesome = ((double)rand() / RAND_MAX) < 0.0001; |
| + if (!was_awesome) { |
| + goto awww; |
| + } |
| + |
| + switch (rand() % 4) { |
| + case 0: |
| + ptr_ = (T*)((void*)ptr_); |
| + break; |
| + case 1: |
| + ptr_ = (T*)((int)ptr_ | (1 << (rand() % sizeof(int)))); |
|
Avi (use Gerrit)
2012/07/24 18:32:04
Can we abstract out the |(1 << (rand() % sizeof(in
awong
2012/07/24 19:09:16
Done. Also sped up the macro by using float instea
|
| + break; |
| + case 2: |
| + ptr_ = (T*)((int)ptr_ & ~(1 << (rand() % sizeof(int)))); |
| + case 3: |
| + ptr_ = ptr_ | 0x1; |
|
Avi (use Gerrit)
2012/07/24 18:32:04
This is subsumed by case 1. Can we find more aweso
awong
2012/07/24 19:09:16
It's not quite the same if you think about memory
|
| + break; |
|
dmichael (off chromium)
2012/07/24 18:30:45
I didn't really read this part, but I assume it's
awong
2012/07/24 19:09:16
vegan really. No animal byproducts used.
|
| + } |
| + |
| +awww: |
| + return was_awesome; |
| + } |
| +}; |