stack.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include <util/generic/array_ref.h>
  3. #include <util/generic/fwd.h>
  4. #include <util/generic/noncopyable.h>
  5. #include <cstdint>
  6. namespace NCoro::NStack {
  7. class IAllocator;
  8. namespace NDetails {
  9. //! Do not use directly, use TStackHolder instead
  10. class TStack final : private TMoveOnly {
  11. public:
  12. /*! rawMemory: can be used by unaligned allocator to free stack memory after use
  13. * alignedMemory: pointer to aligned memory on which stack workspace and guard are actually placed
  14. * alignedSize: size of workspace memory + memory for guard
  15. * guard: guard to protect this stack
  16. * name: name of coroutine for which this stack is allocated
  17. */
  18. TStack(void* rawMemory, void* alignedMemory, size_t alignedSize, const char* name);
  19. TStack(TStack&& rhs) noexcept;
  20. TStack& operator=(TStack&& rhs) noexcept;
  21. char* GetRawMemory() const noexcept {
  22. return RawMemory_;
  23. }
  24. char* GetAlignedMemory() const noexcept {
  25. return AlignedMemory_;
  26. }
  27. //! Stack size (includes memory for guard)
  28. size_t GetSize() const noexcept {
  29. return Size_;
  30. }
  31. //! Resets parameters, should be called after stack memory is freed
  32. void Reset() noexcept;
  33. private:
  34. char* RawMemory_ = nullptr; // not owned
  35. char* AlignedMemory_ = nullptr; // not owned
  36. size_t Size_ = 0;
  37. };
  38. } // namespace NDetails
  39. class TStackHolder final : private TMoveOnly {
  40. public:
  41. explicit TStackHolder(IAllocator& allocator, uint32_t size, const char* name) noexcept;
  42. TStackHolder(TStackHolder&&) = default;
  43. TStackHolder& operator=(TStackHolder&&) = default;
  44. ~TStackHolder();
  45. char* GetAlignedMemory() const noexcept {
  46. return Stack_.GetAlignedMemory();
  47. }
  48. size_t GetSize() const noexcept {
  49. return Stack_.GetSize();
  50. }
  51. TArrayRef<char> Get() noexcept;
  52. bool LowerCanaryOk() const noexcept;
  53. bool UpperCanaryOk() const noexcept;
  54. private:
  55. IAllocator& Allocator_;
  56. NDetails::TStack Stack_;
  57. };
  58. }