stack.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "stack.h"
  2. #include "stack_allocator.h"
  3. #include "stack_guards.h"
  4. namespace NCoro::NStack {
  5. namespace NDetails {
  6. TStack::TStack(void* rawMemory, void* alignedMemory, size_t alignedSize, const char* /*name*/)
  7. : RawMemory_((char*)rawMemory)
  8. , AlignedMemory_((char*)alignedMemory)
  9. , Size_(alignedSize)
  10. {
  11. Y_ASSERT(AlignedMemory_ && RawMemory_ && Size_);
  12. Y_ASSERT(!(Size_ & PageSizeMask));
  13. Y_ASSERT(!((size_t)AlignedMemory_ & PageSizeMask));
  14. }
  15. TStack::TStack(TStack&& rhs) noexcept
  16. : RawMemory_(rhs.RawMemory_)
  17. , AlignedMemory_(rhs.AlignedMemory_)
  18. , Size_(rhs.Size_)
  19. {
  20. rhs.Reset();
  21. }
  22. TStack& TStack::operator=(TStack&& rhs) noexcept {
  23. std::swap(*this, rhs);
  24. rhs.Reset();
  25. return *this;
  26. }
  27. void TStack::Reset() noexcept {
  28. Y_ASSERT(AlignedMemory_ && RawMemory_ && Size_);
  29. RawMemory_ = nullptr;
  30. AlignedMemory_ = nullptr;
  31. Size_ = 0;
  32. }
  33. } // namespace NDetails
  34. TStackHolder::TStackHolder(NStack::IAllocator& allocator, uint32_t size, const char* name) noexcept
  35. : Allocator_(allocator)
  36. , Stack_(Allocator_.AllocStack(size, name))
  37. {}
  38. TStackHolder::~TStackHolder() {
  39. Allocator_.FreeStack(Stack_);
  40. }
  41. TArrayRef<char> TStackHolder::Get() noexcept {
  42. return Allocator_.GetStackWorkspace(Stack_.GetAlignedMemory(), Stack_.GetSize());
  43. }
  44. bool TStackHolder::LowerCanaryOk() const noexcept {
  45. return Allocator_.CheckStackOverflow(Stack_.GetAlignedMemory());
  46. }
  47. bool TStackHolder::UpperCanaryOk() const noexcept {
  48. return Allocator_.CheckStackOverride(Stack_.GetAlignedMemory(), Stack_.GetSize());
  49. }
  50. }