14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_MARK_STACK_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_MARK_STACK_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
204514f5e3Sopenharmony_ci#include "ecmascript/mem/area.h"
214514f5e3Sopenharmony_ci#include "ecmascript/mem/ecma_list.h"
224514f5e3Sopenharmony_ci#include "ecmascript/mem/native_area_allocator.h"
234514f5e3Sopenharmony_ci#include "ecmascript/mem/space.h"
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cinamespace panda {
264514f5e3Sopenharmony_cinamespace ecmascript {
274514f5e3Sopenharmony_ciclass Stack {
284514f5e3Sopenharmony_cipublic:
294514f5e3Sopenharmony_ci    Stack() = default;
304514f5e3Sopenharmony_ci    virtual ~Stack() = default;
314514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(Stack);
324514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(Stack);
334514f5e3Sopenharmony_ci    uintptr_t GetBegin() const
344514f5e3Sopenharmony_ci    {
354514f5e3Sopenharmony_ci        return begin_;
364514f5e3Sopenharmony_ci    }
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_ci    uintptr_t PopBackChecked()
394514f5e3Sopenharmony_ci    {
404514f5e3Sopenharmony_ci        if (UNLIKELY(top_ <= reinterpret_cast<uintptr_t *>(begin_))) {
414514f5e3Sopenharmony_ci            return 0;
424514f5e3Sopenharmony_ci        }
434514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
444514f5e3Sopenharmony_ci        return *--top_;
454514f5e3Sopenharmony_ci    }
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    void PushBackUnchecked(uintptr_t obj)
484514f5e3Sopenharmony_ci    {
494514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
504514f5e3Sopenharmony_ci        *top_++ = obj;
514514f5e3Sopenharmony_ci    }
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci    uintptr_t PopBackUnchecked()
544514f5e3Sopenharmony_ci    {
554514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
564514f5e3Sopenharmony_ci        return *--top_;
574514f5e3Sopenharmony_ci    }
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci    bool PushBackChecked(uintptr_t obj)
604514f5e3Sopenharmony_ci    {
614514f5e3Sopenharmony_ci        if (UNLIKELY(top_ >= end_)) {
624514f5e3Sopenharmony_ci            return false;
634514f5e3Sopenharmony_ci        }
644514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
654514f5e3Sopenharmony_ci        *top_++ = obj;
664514f5e3Sopenharmony_ci        return true;
674514f5e3Sopenharmony_ci    }
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ci    bool IsEmpty() const
704514f5e3Sopenharmony_ci    {
714514f5e3Sopenharmony_ci        return top_ == reinterpret_cast<uintptr_t *>(begin_);
724514f5e3Sopenharmony_ci    }
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    void ResetBegin(uintptr_t begin, uintptr_t end)
754514f5e3Sopenharmony_ci    {
764514f5e3Sopenharmony_ci        begin_ = begin;
774514f5e3Sopenharmony_ci        top_ = reinterpret_cast<uintptr_t *>(begin);
784514f5e3Sopenharmony_ci        end_ = reinterpret_cast<uintptr_t *>(end);
794514f5e3Sopenharmony_ci    }
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    void ResetTop(uintptr_t begin, uintptr_t end)
824514f5e3Sopenharmony_ci    {
834514f5e3Sopenharmony_ci        begin_ = begin;
844514f5e3Sopenharmony_ci        top_ = end_ = reinterpret_cast<uintptr_t *>(end);
854514f5e3Sopenharmony_ci    }
864514f5e3Sopenharmony_ci
874514f5e3Sopenharmony_ciprivate:
884514f5e3Sopenharmony_ci    template<class T>
894514f5e3Sopenharmony_ci    friend class ContinuousStack;
904514f5e3Sopenharmony_ci    friend class WorkNode;
914514f5e3Sopenharmony_ci    uintptr_t begin_ {0};
924514f5e3Sopenharmony_ci    uintptr_t *end_ {nullptr};
934514f5e3Sopenharmony_ci    uintptr_t *top_ {nullptr};
944514f5e3Sopenharmony_ci};
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_citemplate<class T>
974514f5e3Sopenharmony_ciclass ContinuousStack : public Stack {
984514f5e3Sopenharmony_cipublic:
994514f5e3Sopenharmony_ci    ContinuousStack() = default;
1004514f5e3Sopenharmony_ci    ~ContinuousStack() override = default;
1014514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(ContinuousStack);
1024514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(ContinuousStack);
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_ci    inline void BeginMarking(ContinuousStack<T> *other)
1054514f5e3Sopenharmony_ci    {
1064514f5e3Sopenharmony_ci        currentArea_ = other->currentArea_;
1074514f5e3Sopenharmony_ci        if (currentArea_ == nullptr) {
1084514f5e3Sopenharmony_ci            currentArea_ = NativeAreaAllocator::AllocateSpace(DEFAULT_MARK_STACK_SIZE);
1094514f5e3Sopenharmony_ci        }
1104514f5e3Sopenharmony_ci        ResetBegin(currentArea_->GetBegin(), currentArea_->GetEnd());
1114514f5e3Sopenharmony_ci    }
1124514f5e3Sopenharmony_ci    inline void FinishMarking(ContinuousStack<T> *other)
1134514f5e3Sopenharmony_ci    {
1144514f5e3Sopenharmony_ci        other->currentArea_ = currentArea_;
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_ci        while (!unusedList_.IsEmpty()) {
1174514f5e3Sopenharmony_ci            Area *node = unusedList_.PopBack();
1184514f5e3Sopenharmony_ci            NativeAreaAllocator::FreeSpace(node);
1194514f5e3Sopenharmony_ci        }
1204514f5e3Sopenharmony_ci    }
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_ci    T *PopBack()
1234514f5e3Sopenharmony_ci    {
1244514f5e3Sopenharmony_ci        if (UNLIKELY(top_ <= reinterpret_cast<uintptr_t *>(begin_))) {
1254514f5e3Sopenharmony_ci            if (!areaList_.IsEmpty()) {
1264514f5e3Sopenharmony_ci                unusedList_.AddNode(currentArea_);
1274514f5e3Sopenharmony_ci                Area *last = areaList_.PopBack();
1284514f5e3Sopenharmony_ci                currentArea_ = last;
1294514f5e3Sopenharmony_ci                ResetTop(currentArea_->GetBegin(), currentArea_->GetEnd());
1304514f5e3Sopenharmony_ci            } else {
1314514f5e3Sopenharmony_ci                return nullptr;
1324514f5e3Sopenharmony_ci            }
1334514f5e3Sopenharmony_ci        }
1344514f5e3Sopenharmony_ci        return reinterpret_cast<T *>(PopBackUnchecked());
1354514f5e3Sopenharmony_ci    }
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci    void PushBack(T *obj)
1384514f5e3Sopenharmony_ci    {
1394514f5e3Sopenharmony_ci        if (UNLIKELY(top_ >= end_)) {
1404514f5e3Sopenharmony_ci            Extend();
1414514f5e3Sopenharmony_ci        }
1424514f5e3Sopenharmony_ci        PushBackUnchecked(ToUintPtr(obj));
1434514f5e3Sopenharmony_ci    }
1444514f5e3Sopenharmony_ci
1454514f5e3Sopenharmony_ci    inline void Destroy()
1464514f5e3Sopenharmony_ci    {
1474514f5e3Sopenharmony_ci        if (currentArea_ != nullptr) {
1484514f5e3Sopenharmony_ci            NativeAreaAllocator::FreeSpace(currentArea_);
1494514f5e3Sopenharmony_ci            currentArea_ = nullptr;
1504514f5e3Sopenharmony_ci        }
1514514f5e3Sopenharmony_ci    }
1524514f5e3Sopenharmony_ci
1534514f5e3Sopenharmony_ciprivate:
1544514f5e3Sopenharmony_ci    inline void Extend()
1554514f5e3Sopenharmony_ci    {
1564514f5e3Sopenharmony_ci        auto area = NativeAreaAllocator::AllocateSpace(DEFAULT_MARK_STACK_SIZE);
1574514f5e3Sopenharmony_ci        areaList_.AddNode(currentArea_);
1584514f5e3Sopenharmony_ci        currentArea_ = area;
1594514f5e3Sopenharmony_ci        ResetBegin(currentArea_->GetBegin(), currentArea_->GetEnd());
1604514f5e3Sopenharmony_ci    }
1614514f5e3Sopenharmony_ci
1624514f5e3Sopenharmony_ci    Area *currentArea_ {nullptr};
1634514f5e3Sopenharmony_ci    EcmaList<Area> areaList_ {};
1644514f5e3Sopenharmony_ci    EcmaList<Area> unusedList_ {};
1654514f5e3Sopenharmony_ci};
1664514f5e3Sopenharmony_ci
1674514f5e3Sopenharmony_ciusing MarkStack = ContinuousStack<TaggedObject>;
1684514f5e3Sopenharmony_ciusing ProcessQueue = ContinuousStack<JSTaggedType>;
1694514f5e3Sopenharmony_ci}  // namespace ecmascript
1704514f5e3Sopenharmony_ci}  // namespace panda
1714514f5e3Sopenharmony_ci
1724514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_MEM_MARK_STACK_H
173