From 1110808a9d39d4b808aef724c861a2e1a38d2a69 Mon Sep 17 00:00:00 2001
From: Devtools Arcadia <arcadia-devtools@yandex-team.ru>
Date: Mon, 7 Feb 2022 18:08:42 +0300
Subject: intermediate changes ref:cde9a383711a11544ce7e107a78147fb96cc4029

---
 library/cpp/coroutine/engine/stack/stack.cpp | 67 ++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 library/cpp/coroutine/engine/stack/stack.cpp

(limited to 'library/cpp/coroutine/engine/stack/stack.cpp')

diff --git a/library/cpp/coroutine/engine/stack/stack.cpp b/library/cpp/coroutine/engine/stack/stack.cpp
new file mode 100644
index 0000000000..e29450261d
--- /dev/null
+++ b/library/cpp/coroutine/engine/stack/stack.cpp
@@ -0,0 +1,67 @@
+#include "stack.h"
+
+#include "stack_allocator.h"
+#include "stack_guards.h"
+
+
+namespace NCoro::NStack {
+
+namespace NDetails {
+
+    TStack::TStack(void* rawMemory, void* alignedMemory, uint64_t alignedSize, const char* /*name*/)
+            : RawMemory_((char*)rawMemory)
+            , AlignedMemory_((char*)alignedMemory)
+            , Size_(alignedSize)
+    {
+        Y_ASSERT(AlignedMemory_ && RawMemory_ && Size_);
+        Y_ASSERT(!(Size_ & PageSizeMask));
+        Y_ASSERT(!((uint64_t)AlignedMemory_ & PageSizeMask));
+    }
+
+    TStack::TStack(TStack&& rhs) noexcept
+            : RawMemory_(rhs.RawMemory_)
+            , AlignedMemory_(rhs.AlignedMemory_)
+            , Size_(rhs.Size_)
+    {
+        rhs.Reset();
+    }
+
+    TStack& TStack::operator=(TStack&& rhs) noexcept {
+        std::swap(*this, rhs);
+        rhs.Reset();
+        return *this;
+    }
+
+    void TStack::Reset() noexcept {
+        Y_ASSERT(AlignedMemory_ && RawMemory_ && Size_);
+
+        RawMemory_ = nullptr;
+        AlignedMemory_ = nullptr;
+        Size_ = 0;
+    }
+
+} // namespace NDetails
+
+
+    TStackHolder::TStackHolder(NStack::IAllocator& allocator, uint32_t size, const char* name) noexcept
+        : Allocator_(allocator)
+        , Stack_(Allocator_.AllocStack(size, name))
+    {}
+
+    TStackHolder::~TStackHolder() {
+        Allocator_.FreeStack(Stack_);
+    }
+
+    TArrayRef<char> TStackHolder::Get() noexcept {
+        return Allocator_.GetStackWorkspace(Stack_.GetAlignedMemory(), Stack_.GetSize());
+    }
+
+    bool TStackHolder::LowerCanaryOk() const noexcept {
+        return Allocator_.CheckStackOverflow(Stack_.GetAlignedMemory());
+    }
+
+    bool TStackHolder::UpperCanaryOk() const noexcept {
+        return Allocator_.CheckStackOverride(Stack_.GetAlignedMemory(), Stack_.GetSize());
+    }
+
+}
-- 
cgit v1.2.3