L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
slist
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * (c) 2011 Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
5 *
6 * License: see LICENSE.spdx (in this directory or the directories above)
7 */
8
9#pragma once
10
11#include "bits/list_basics.h"
12
13namespace cxx {
14
15class S_list_item
16{
17public:
18 S_list_item() : _n(nullptr) {}
19 // BSS allocation
20 explicit S_list_item(bool) {}
21
22private:
23 template<typename T, typename P> friend class S_list;
24 template<typename T, typename P> friend class S_list_tail;
25 template<typename T, typename X> friend struct Bits::Basic_list_policy;
26
27 S_list_item(S_list_item const &);
28 void operator = (S_list_item const &);
29
30 S_list_item *_n;
31};
32
39template< typename T, typename POLICY = Bits::Basic_list_policy< T, S_list_item > >
40class S_list : public Bits::Basic_list<POLICY>
41{
42 S_list(S_list const &) = delete;
43 void operator = (S_list const &) = delete;
44
45private:
46 typedef typename Bits::Basic_list<POLICY> Base;
47
48public:
49 typedef typename Base::Iterator Iterator;
50
51 S_list(S_list &&o) : Base(static_cast<Base&&>(o)) {}
52
53 S_list &operator = (S_list &&o)
54 {
55 if (&o != this)
56 Base::operator = (static_cast<Base&&>(o));
57
58 return *this;
59 }
60
61 // BSS allocation
62 explicit S_list(bool x) : Base(x) {}
63
64 S_list() : Base() {}
65
67 void add(T *e)
68 {
69 e->_n = this->_f;
70 this->_f = e;
71 }
72
73 template< typename CAS >
74 void add(T *e, CAS const &c)
75 {
76 do
77 {
78 e->_n = this->_f;
79 }
80 while (!c(&this->_f, e->_n, e));
81 }
82
84 void push_front(T *e) { add(e); }
85
92 {
93 T *r = this->front();
94 if (this->_f)
95 this->_f = this->_f->_n;
96 return r;
97 }
98
99 void insert(T *e, Iterator const &pred)
100 {
101 S_list_item *p = *pred;
102 e->_n = p->_n;
103 p->_n = e;
104 }
105
106 static void insert_before(T *e, Iterator const &succ)
107 {
108 S_list_item **x = Base::__get_internal(succ);
109
110 e->_n = *x;
111 *x = e;
112 }
113
114 static void replace(Iterator const &p, T*e)
115 {
116 S_list_item **x = Base::__get_internal(p);
117 e->_n = (*x)->_n;
118 *x = e;
119 }
120
121 static Iterator erase(Iterator const &e)
122 {
123 S_list_item **x = Base::__get_internal(e);
124 *x = (*x)->_n;
125 return e;
126 }
127
128};
129
130
131template< typename T >
132class S_list_bss : public S_list<T>
133{
134public:
135 S_list_bss() : S_list<T>(true) {}
136};
137
138template< typename T, typename POLICY = Bits::Basic_list_policy< T, S_list_item > >
139class S_list_tail : public S_list<T, POLICY>
140{
141private:
142 typedef S_list<T, POLICY> Base;
143 void add(T *e) = delete;
144
145public:
146 using Iterator = typename Base::Iterator;
147 S_list_tail() : Base(), _tail(&this->_f) {}
148
149 S_list_tail(S_list_tail &&t)
150 : Base(static_cast<Base&&>(t)), _tail(Base::empty() ? &this->_f : t._tail)
151 {
152 t._tail = &t._f; // reset the source tail
153 }
154
155 S_list_tail &operator = (S_list_tail &&t)
156 {
157 if (&t != this)
158 {
159 Base::operator = (static_cast<Base &&>(t));
160 // The Basic_list operator= does not clear the tail, so we can use it
161 _tail = Base::empty() ? &this->_f : t._tail;
162 t._tail = &t._f; // reset the source tail
163 }
164
165 return *this;
166 }
167
168 void push_front(T *e)
169 {
170 if (Base::empty())
171 _tail = &e->_n;
172
174 }
175
176 void push_back(T *e)
177 {
178 e->_n = nullptr;
179 *_tail = e;
180 _tail = &e->_n;
181 }
182
183 void clear()
184 {
185 Base::clear();
186 _tail = &this->_f;
187 }
188
189 void append(S_list_tail &o)
190 {
191 T *x = o.front();
192 *_tail = x;
193 if (x)
194 _tail = o._tail;
195 o.clear();
196 }
197
198 T *pop_front()
199 {
200 T *t = Base::pop_front();
201 if (t && Base::empty())
202 _tail = &this->_f;
203 return t;
204 }
205
206private:
207 static void insert(T *e, Iterator const &pred);
208 static void insert_before(T *e, Iterator const &succ);
209 static void replace(Iterator const &p, T*e);
210 static Iterator erase(Iterator const &e);
211
212private:
213 S_list_item **_tail;
214};
215
216}
Internal: Common functions for all head-based list implementations.
Definition list_basics.h:40
POLICY::Head_type _f
Pointer to front of the list.
Simple single-linked list.
Definition slist:41
void add(T *e)
Add an element to the front of the list.
Definition slist:67
void push_front(T *e)
Add an element to the front of the list.
Definition slist:84
T * pop_front()
Remove and return the head element of the list.
Definition slist:91
Our C++ library.
Definition arith:11