1*349cc55cSDimitry Andric //===-- tsan_ilist.h --------------------------------------------*- C++ -*-===//
2*349cc55cSDimitry Andric //
3*349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*349cc55cSDimitry Andric //
7*349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8*349cc55cSDimitry Andric //
9*349cc55cSDimitry Andric // This file is a part of ThreadSanitizer (TSan), a race detector.
10*349cc55cSDimitry Andric //
11*349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
12*349cc55cSDimitry Andric #ifndef TSAN_ILIST_H
13*349cc55cSDimitry Andric #define TSAN_ILIST_H
14*349cc55cSDimitry Andric 
15*349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_internal_defs.h"
16*349cc55cSDimitry Andric 
17*349cc55cSDimitry Andric namespace __tsan {
18*349cc55cSDimitry Andric 
19*349cc55cSDimitry Andric class INode {
20*349cc55cSDimitry Andric  public:
21*349cc55cSDimitry Andric   INode() = default;
22*349cc55cSDimitry Andric 
23*349cc55cSDimitry Andric  private:
24*349cc55cSDimitry Andric   INode* next_ = nullptr;
25*349cc55cSDimitry Andric   INode* prev_ = nullptr;
26*349cc55cSDimitry Andric 
27*349cc55cSDimitry Andric   template <typename Base, INode Base::*Node, typename Elem>
28*349cc55cSDimitry Andric   friend class IList;
29*349cc55cSDimitry Andric   INode(const INode&) = delete;
30*349cc55cSDimitry Andric   void operator=(const INode&) = delete;
31*349cc55cSDimitry Andric };
32*349cc55cSDimitry Andric 
33*349cc55cSDimitry Andric // Intrusive doubly-linked list.
34*349cc55cSDimitry Andric //
35*349cc55cSDimitry Andric // The node class (MyNode) needs to include "INode foo" field,
36*349cc55cSDimitry Andric // then the list can be declared as IList<MyNode, &MyNode::foo>.
37*349cc55cSDimitry Andric // This design allows to link MyNode into multiple lists using
38*349cc55cSDimitry Andric // different INode fields.
39*349cc55cSDimitry Andric // The optional Elem template argument allows to specify node MDT
40*349cc55cSDimitry Andric // (most derived type) if it's different from MyNode.
41*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem = Base>
42*349cc55cSDimitry Andric class IList {
43*349cc55cSDimitry Andric  public:
44*349cc55cSDimitry Andric   IList();
45*349cc55cSDimitry Andric 
46*349cc55cSDimitry Andric   void PushFront(Elem* e);
47*349cc55cSDimitry Andric   void PushBack(Elem* e);
48*349cc55cSDimitry Andric   void Remove(Elem* e);
49*349cc55cSDimitry Andric 
50*349cc55cSDimitry Andric   Elem* PopFront();
51*349cc55cSDimitry Andric   Elem* PopBack();
52*349cc55cSDimitry Andric   Elem* Front();
53*349cc55cSDimitry Andric   Elem* Back();
54*349cc55cSDimitry Andric 
55*349cc55cSDimitry Andric   // Prev links point towards front of the queue.
56*349cc55cSDimitry Andric   Elem* Prev(Elem* e);
57*349cc55cSDimitry Andric   // Next links point towards back of the queue.
58*349cc55cSDimitry Andric   Elem* Next(Elem* e);
59*349cc55cSDimitry Andric 
60*349cc55cSDimitry Andric   uptr Size() const;
61*349cc55cSDimitry Andric   bool Empty() const;
62*349cc55cSDimitry Andric   bool Queued(Elem* e) const;
63*349cc55cSDimitry Andric 
64*349cc55cSDimitry Andric  private:
65*349cc55cSDimitry Andric   INode node_;
66*349cc55cSDimitry Andric   uptr size_ = 0;
67*349cc55cSDimitry Andric 
68*349cc55cSDimitry Andric   void Push(Elem* e, INode* after);
69*349cc55cSDimitry Andric   static INode* ToNode(Elem* e);
70*349cc55cSDimitry Andric   static Elem* ToElem(INode* n);
71*349cc55cSDimitry Andric 
72*349cc55cSDimitry Andric   IList(const IList&) = delete;
73*349cc55cSDimitry Andric   void operator=(const IList&) = delete;
74*349cc55cSDimitry Andric };
75*349cc55cSDimitry Andric 
76*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
IList()77*349cc55cSDimitry Andric IList<Base, Node, Elem>::IList() {
78*349cc55cSDimitry Andric   node_.next_ = node_.prev_ = &node_;
79*349cc55cSDimitry Andric }
80*349cc55cSDimitry Andric 
81*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
PushFront(Elem * e)82*349cc55cSDimitry Andric void IList<Base, Node, Elem>::PushFront(Elem* e) {
83*349cc55cSDimitry Andric   Push(e, &node_);
84*349cc55cSDimitry Andric }
85*349cc55cSDimitry Andric 
86*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
PushBack(Elem * e)87*349cc55cSDimitry Andric void IList<Base, Node, Elem>::PushBack(Elem* e) {
88*349cc55cSDimitry Andric   Push(e, node_.prev_);
89*349cc55cSDimitry Andric }
90*349cc55cSDimitry Andric 
91*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
Push(Elem * e,INode * after)92*349cc55cSDimitry Andric void IList<Base, Node, Elem>::Push(Elem* e, INode* after) {
93*349cc55cSDimitry Andric   INode* n = ToNode(e);
94*349cc55cSDimitry Andric   DCHECK_EQ(n->next_, nullptr);
95*349cc55cSDimitry Andric   DCHECK_EQ(n->prev_, nullptr);
96*349cc55cSDimitry Andric   INode* next = after->next_;
97*349cc55cSDimitry Andric   n->next_ = next;
98*349cc55cSDimitry Andric   n->prev_ = after;
99*349cc55cSDimitry Andric   next->prev_ = n;
100*349cc55cSDimitry Andric   after->next_ = n;
101*349cc55cSDimitry Andric   size_++;
102*349cc55cSDimitry Andric }
103*349cc55cSDimitry Andric 
104*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
Remove(Elem * e)105*349cc55cSDimitry Andric void IList<Base, Node, Elem>::Remove(Elem* e) {
106*349cc55cSDimitry Andric   INode* n = ToNode(e);
107*349cc55cSDimitry Andric   INode* next = n->next_;
108*349cc55cSDimitry Andric   INode* prev = n->prev_;
109*349cc55cSDimitry Andric   DCHECK(next);
110*349cc55cSDimitry Andric   DCHECK(prev);
111*349cc55cSDimitry Andric   DCHECK(size_);
112*349cc55cSDimitry Andric   next->prev_ = prev;
113*349cc55cSDimitry Andric   prev->next_ = next;
114*349cc55cSDimitry Andric   n->prev_ = n->next_ = nullptr;
115*349cc55cSDimitry Andric   size_--;
116*349cc55cSDimitry Andric }
117*349cc55cSDimitry Andric 
118*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
PopFront()119*349cc55cSDimitry Andric Elem* IList<Base, Node, Elem>::PopFront() {
120*349cc55cSDimitry Andric   Elem* e = Front();
121*349cc55cSDimitry Andric   if (e)
122*349cc55cSDimitry Andric     Remove(e);
123*349cc55cSDimitry Andric   return e;
124*349cc55cSDimitry Andric }
125*349cc55cSDimitry Andric 
126*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
PopBack()127*349cc55cSDimitry Andric Elem* IList<Base, Node, Elem>::PopBack() {
128*349cc55cSDimitry Andric   Elem* e = Back();
129*349cc55cSDimitry Andric   if (e)
130*349cc55cSDimitry Andric     Remove(e);
131*349cc55cSDimitry Andric   return e;
132*349cc55cSDimitry Andric }
133*349cc55cSDimitry Andric 
134*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
Front()135*349cc55cSDimitry Andric Elem* IList<Base, Node, Elem>::Front() {
136*349cc55cSDimitry Andric   return size_ ? ToElem(node_.next_) : nullptr;
137*349cc55cSDimitry Andric }
138*349cc55cSDimitry Andric 
139*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
Back()140*349cc55cSDimitry Andric Elem* IList<Base, Node, Elem>::Back() {
141*349cc55cSDimitry Andric   return size_ ? ToElem(node_.prev_) : nullptr;
142*349cc55cSDimitry Andric }
143*349cc55cSDimitry Andric 
144*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
Prev(Elem * e)145*349cc55cSDimitry Andric Elem* IList<Base, Node, Elem>::Prev(Elem* e) {
146*349cc55cSDimitry Andric   INode* n = ToNode(e);
147*349cc55cSDimitry Andric   DCHECK(n->prev_);
148*349cc55cSDimitry Andric   return n->prev_ != &node_ ? ToElem(n->prev_) : nullptr;
149*349cc55cSDimitry Andric }
150*349cc55cSDimitry Andric 
151*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
Next(Elem * e)152*349cc55cSDimitry Andric Elem* IList<Base, Node, Elem>::Next(Elem* e) {
153*349cc55cSDimitry Andric   INode* n = ToNode(e);
154*349cc55cSDimitry Andric   DCHECK(n->next_);
155*349cc55cSDimitry Andric   return n->next_ != &node_ ? ToElem(n->next_) : nullptr;
156*349cc55cSDimitry Andric }
157*349cc55cSDimitry Andric 
158*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
Size()159*349cc55cSDimitry Andric uptr IList<Base, Node, Elem>::Size() const {
160*349cc55cSDimitry Andric   return size_;
161*349cc55cSDimitry Andric }
162*349cc55cSDimitry Andric 
163*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
Empty()164*349cc55cSDimitry Andric bool IList<Base, Node, Elem>::Empty() const {
165*349cc55cSDimitry Andric   return size_ == 0;
166*349cc55cSDimitry Andric }
167*349cc55cSDimitry Andric 
168*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
Queued(Elem * e)169*349cc55cSDimitry Andric bool IList<Base, Node, Elem>::Queued(Elem* e) const {
170*349cc55cSDimitry Andric   INode* n = ToNode(e);
171*349cc55cSDimitry Andric   DCHECK_EQ(!n->next_, !n->prev_);
172*349cc55cSDimitry Andric   return n->next_;
173*349cc55cSDimitry Andric }
174*349cc55cSDimitry Andric 
175*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
ToNode(Elem * e)176*349cc55cSDimitry Andric INode* IList<Base, Node, Elem>::ToNode(Elem* e) {
177*349cc55cSDimitry Andric   return &(e->*Node);
178*349cc55cSDimitry Andric }
179*349cc55cSDimitry Andric 
180*349cc55cSDimitry Andric template <typename Base, INode Base::*Node, typename Elem>
ToElem(INode * n)181*349cc55cSDimitry Andric Elem* IList<Base, Node, Elem>::ToElem(INode* n) {
182*349cc55cSDimitry Andric   return static_cast<Elem*>(reinterpret_cast<Base*>(
183*349cc55cSDimitry Andric       reinterpret_cast<uptr>(n) -
184*349cc55cSDimitry Andric       reinterpret_cast<uptr>(&(reinterpret_cast<Elem*>(0)->*Node))));
185*349cc55cSDimitry Andric }
186*349cc55cSDimitry Andric 
187*349cc55cSDimitry Andric }  // namespace __tsan
188*349cc55cSDimitry Andric 
189*349cc55cSDimitry Andric #endif
190