aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/coroutine/engine/stack/stack.cpp
blob: 9d7dbf8ae0eb0369047b113c74a7675fbcb4eaba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "stack.h"

#include "stack_allocator.h"
#include "stack_guards.h"


namespace NCoro::NStack {

namespace NDetails {

    TStack::TStack(void* rawMemory, void* alignedMemory, size_t alignedSize, const char* /*name*/)
            : RawMemory_((char*)rawMemory)
            , AlignedMemory_((char*)alignedMemory)
            , Size_(alignedSize)
    {
        Y_ASSERT(AlignedMemory_ && RawMemory_ && Size_);
        Y_ASSERT(!(Size_ & PageSizeMask));
        Y_ASSERT(!((size_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());
    }

}