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_ECMALIST_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_ECMALIST_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/mem/mem.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cinamespace panda::ecmascript {
224514f5e3Sopenharmony_ci//  Invoking std::list will cause cross invoking, which is time-consuming.
234514f5e3Sopenharmony_ci//  Therefore, we implement ecma list inside the vm.
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_citemplate<class T>
264514f5e3Sopenharmony_ciclass EcmaList {
274514f5e3Sopenharmony_cipublic:
284514f5e3Sopenharmony_ci    EcmaList() : first_(nullptr), last_(nullptr) {}
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_ci    explicit EcmaList(T *node) : first_(node), last_(node)
314514f5e3Sopenharmony_ci    {
324514f5e3Sopenharmony_ci        node->LinkPrev(nullptr);
334514f5e3Sopenharmony_ci        node->LinkNext(nullptr);
344514f5e3Sopenharmony_ci    }
354514f5e3Sopenharmony_ci    ~EcmaList() = default;
364514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(EcmaList);
374514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(EcmaList);
384514f5e3Sopenharmony_ci    void AddNode(T *node)
394514f5e3Sopenharmony_ci    {
404514f5e3Sopenharmony_ci        ASSERT(node != nullptr);
414514f5e3Sopenharmony_ci        if (LIKELY(first_ != nullptr)) {
424514f5e3Sopenharmony_ci            T *lastNext = last_->GetNext();
434514f5e3Sopenharmony_ci            node->LinkNext(lastNext);
444514f5e3Sopenharmony_ci            node->LinkPrev(last_);
454514f5e3Sopenharmony_ci            last_->LinkNext(node);
464514f5e3Sopenharmony_ci            if (lastNext) {
474514f5e3Sopenharmony_ci                lastNext->LinkPrev(node);
484514f5e3Sopenharmony_ci            } else {
494514f5e3Sopenharmony_ci                last_ = node;
504514f5e3Sopenharmony_ci            }
514514f5e3Sopenharmony_ci        } else {
524514f5e3Sopenharmony_ci            node->LinkPrev(nullptr);
534514f5e3Sopenharmony_ci            node->LinkNext(nullptr);
544514f5e3Sopenharmony_ci            first_ = last_ = node;
554514f5e3Sopenharmony_ci        }
564514f5e3Sopenharmony_ci        length_++;
574514f5e3Sopenharmony_ci    }
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci    void AddNodeToFront(T *node)
604514f5e3Sopenharmony_ci    {
614514f5e3Sopenharmony_ci        ASSERT(node != nullptr);
624514f5e3Sopenharmony_ci        if (LIKELY(last_ != nullptr)) {
634514f5e3Sopenharmony_ci            node->LinkNext(first_);
644514f5e3Sopenharmony_ci            node->LinkPrev(first_->GetPrev());
654514f5e3Sopenharmony_ci            first_->LinkPrev(node);
664514f5e3Sopenharmony_ci            first_ = node;
674514f5e3Sopenharmony_ci        } else {
684514f5e3Sopenharmony_ci            node->LinkPrev(nullptr);
694514f5e3Sopenharmony_ci            node->LinkNext(nullptr);
704514f5e3Sopenharmony_ci            first_ = last_ = node;
714514f5e3Sopenharmony_ci        }
724514f5e3Sopenharmony_ci        length_++;
734514f5e3Sopenharmony_ci    }
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ci    T *PopBack()
764514f5e3Sopenharmony_ci    {
774514f5e3Sopenharmony_ci        T *node = last_;
784514f5e3Sopenharmony_ci        RemoveNode(last_);
794514f5e3Sopenharmony_ci        return node;
804514f5e3Sopenharmony_ci    }
814514f5e3Sopenharmony_ci
824514f5e3Sopenharmony_ci    void RemoveNode(T *node)
834514f5e3Sopenharmony_ci    {
844514f5e3Sopenharmony_ci        ASSERT(HasNode(node));
854514f5e3Sopenharmony_ci        if (last_ == node) {
864514f5e3Sopenharmony_ci            // NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
874514f5e3Sopenharmony_ci            last_ = node->GetPrev();
884514f5e3Sopenharmony_ci        }
894514f5e3Sopenharmony_ci        if (first_ == node) {
904514f5e3Sopenharmony_ci            // NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
914514f5e3Sopenharmony_ci            first_ = node->GetNext();
924514f5e3Sopenharmony_ci        }
934514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
944514f5e3Sopenharmony_ci        T *next = node->GetNext();
954514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
964514f5e3Sopenharmony_ci        T *prev = node->GetPrev();
974514f5e3Sopenharmony_ci        if (next != nullptr) {
984514f5e3Sopenharmony_ci            next->LinkPrev(prev);
994514f5e3Sopenharmony_ci        }
1004514f5e3Sopenharmony_ci        if (prev != nullptr) {
1014514f5e3Sopenharmony_ci            prev->LinkNext(next);
1024514f5e3Sopenharmony_ci        }
1034514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
1044514f5e3Sopenharmony_ci        node->LinkPrev(nullptr);
1054514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
1064514f5e3Sopenharmony_ci        node->LinkNext(nullptr);
1074514f5e3Sopenharmony_ci        length_--;
1084514f5e3Sopenharmony_ci    }
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    bool HasNode(T *node)
1114514f5e3Sopenharmony_ci    {
1124514f5e3Sopenharmony_ci        T *it = first_;
1134514f5e3Sopenharmony_ci        while (it != nullptr) {
1144514f5e3Sopenharmony_ci            if (it == node) {
1154514f5e3Sopenharmony_ci                return true;
1164514f5e3Sopenharmony_ci            }
1174514f5e3Sopenharmony_ci            it = it->GetNext();
1184514f5e3Sopenharmony_ci        }
1194514f5e3Sopenharmony_ci        return false;
1204514f5e3Sopenharmony_ci    }
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_ci    T *GetFirst() const
1234514f5e3Sopenharmony_ci    {
1244514f5e3Sopenharmony_ci        return first_;
1254514f5e3Sopenharmony_ci    }
1264514f5e3Sopenharmony_ci
1274514f5e3Sopenharmony_ci    T *GetLast() const
1284514f5e3Sopenharmony_ci    {
1294514f5e3Sopenharmony_ci        return last_;
1304514f5e3Sopenharmony_ci    }
1314514f5e3Sopenharmony_ci
1324514f5e3Sopenharmony_ci    bool IsEmpty() const
1334514f5e3Sopenharmony_ci    {
1344514f5e3Sopenharmony_ci        return last_ == nullptr;
1354514f5e3Sopenharmony_ci    }
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci    void Clear()
1384514f5e3Sopenharmony_ci    {
1394514f5e3Sopenharmony_ci        first_ = last_ = nullptr;
1404514f5e3Sopenharmony_ci        length_ = 0;
1414514f5e3Sopenharmony_ci    }
1424514f5e3Sopenharmony_ci
1434514f5e3Sopenharmony_ci    uint32_t GetLength() const
1444514f5e3Sopenharmony_ci    {
1454514f5e3Sopenharmony_ci        return length_;
1464514f5e3Sopenharmony_ci    }
1474514f5e3Sopenharmony_ci
1484514f5e3Sopenharmony_ciprivate:
1494514f5e3Sopenharmony_ci    T *first_;
1504514f5e3Sopenharmony_ci    T *last_;
1514514f5e3Sopenharmony_ci    uint32_t length_{0};
1524514f5e3Sopenharmony_ci};
1534514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1544514f5e3Sopenharmony_ci
1554514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_MEM_ECMALIST_H
156