| 6 | #define BASE_PROFILER_STACK_BUFFER_H_ |
| 7 |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | #include <memory> |
| 11 |
| 12 | #include "base/base_export.h" |
| 13 | #include "base/macros.h" |
| 14 |
| 15 | namespace base { |
| 16 |
| 17 | // This class contains a buffer for stack copies that can be shared across |
| 18 | // multiple instances of StackSampler. |
| 19 | class BASE_EXPORT StackBuffer { |
| 20 | public: |
| 21 | // The expected alignment of the stack on the current platform. Windows and |
| 22 | // System V AMD64 ABIs on x86, x64, and ARM require the stack to be aligned |
| 23 | // to twice the pointer size. Excepted from this requirement is code setting |
| 24 | // up the stack during function calls (between pushing the return address |
| 25 | // and the end of the function prologue). The profiler will sometimes |
| 26 | // encounter this exceptional case for leaf frames. |
| 27 | static constexpr size_t kPlatformStackAlignment = 2 * sizeof(uintptr_t); |
| 28 |
| 29 | StackBuffer(size_t buffer_size); |
| 30 | ~StackBuffer(); |
| 31 |
| 32 | // Returns a kPlatformStackAlignment-aligned pointer to the stack buffer. |
| 33 | uintptr_t* buffer() const { |
| 34 | // Return the first address in the buffer aligned to |
| 35 | // kPlatformStackAlignment. The buffer is guaranteed to have enough space |
| 36 | // for size() bytes beyond this value. |
| 37 | return reinterpret_cast<uintptr_t*>( |
| 38 | (reinterpret_cast<uintptr_t>(buffer_.get()) + kPlatformStackAlignment - |
| 39 | 1) & |
| 40 | ~(kPlatformStackAlignment - 1)); |
| 41 | } |
| 42 |
| 43 | // Size in bytes. |
| 44 | size_t size() const { return size_; } |
| 45 |
| 46 | private: |
| 47 | // The buffer to store the stack. |
| 48 | const std::unique_ptr<uint8_t[]> buffer_; |
| 49 |
| 50 | // The size in bytes of the requested buffer allocation. The actual allocation |
| 51 | // is larger to accommodate alignment requirements. |
| 52 | const size_t size_; |
| 53 |
| 54 | DISALLOW_COPY_AND_ASSIGN(StackBuffer); |
| 55 | }; |
| 56 |
| 57 | } // namespace base |
| 58 |
| 59 | #endif // BASE_PROFILER_STACK_BUFFER_H_ |
| 60 |