1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#ifndef __SYSTEMC_CORE_LIST_HH__
31#define __SYSTEMC_CORE_LIST_HH__
32
33#include <functional>
34
35#include "base/fiber.hh"
36#include "systemc/core/object.hh"
37#include "systemc/ext/core/sc_event.hh"
38#include "systemc/ext/core/sc_module.hh"
39#include "systemc/ext/core/sc_process_handle.hh"
40
41namespace sc_gem5
42{
43
44struct ListNode
45{
46    ListNode() : nextListNode(nullptr), prevListNode(nullptr) {}
47    virtual ~ListNode() {}
48
49    ListNode *nextListNode;
50    ListNode *prevListNode;
51
52    void
53    popListNode()
54    {
55        if (nextListNode)
56            nextListNode->prevListNode = prevListNode;
57        if (prevListNode)
58            prevListNode->nextListNode = nextListNode;
59        nextListNode = nullptr;
60        prevListNode = nullptr;
61    }
62};
63
64template <typename T>
65struct NodeList : public ListNode
66{
67    NodeList()
68    {
69        nextListNode = this;
70        prevListNode = this;
71    }
72
73    void
74    pushFirst(T *t)
75    {
76        // Make sure this node isn't currently in a different list.
77        t->popListNode();
78
79        // The node behind t is whoever used to be first.
80        t->nextListNode = nextListNode;
81        // The node that used to be first is behind t.
82        nextListNode->prevListNode = t;
83
84        // Nobody is in front of t.
85        t->prevListNode = this;
86        // The first node is t.
87        nextListNode = t;
88    }
89
90    void
91    pushLast(T *t)
92    {
93        // Make sure this node isn't currently in a different list.
94        t->popListNode();
95
96        // The node in front of t is whoever used to be last.
97        t->prevListNode = prevListNode;
98        // The node that used to be last is in front of t.
99        prevListNode->nextListNode = t;
100
101        // Nobody is behind t.
102        t->nextListNode = this;
103        // The last node is t.
104        prevListNode = t;
105    }
106
107    T *getNext() { return dynamic_cast<T *>(nextListNode); }
108    bool empty() { return getNext() == nullptr; }
109};
110
111} // namespace sc_gem5
112
113#endif  //__SYSTEMC_CORE_LIST_HH__
114