stack_buffer.h 1.9 KB

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