L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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(0) {}
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 Base::operator = (static_cast<Base&&>(o));
56 return *this;
57 }
58
59 // BSS allocation
60 explicit S_list(bool x) : Base(x) {}
61
62 S_list() : Base() {}
63
65 void add(T *e)
66 {
67 e->_n = this->_f;
68 this->_f = e;
69 }
70
71 template< typename CAS >
72 void add(T *e, CAS const &c)
73 {
74 do
75 {
76 e->_n = this->_f;
77 }
78 while (!c(&this->_f, e->_n, e));
79 }
80
82 void push_front(T *e) { add(e); }
83
90 {
91 T *r = this->front();
92 if (this->_f)
93 this->_f = this->_f->_n;
94 return r;
95 }
96
97 void insert(T *e, Iterator const &pred)
98 {
99 S_list_item *p = *pred;
100 e->_n = p->_n;
101 p->_n = e;
102 }
103
104 static void insert_before(T *e, Iterator const &succ)
105 {
106 S_list_item **x = Base::__get_internal(succ);
107
108 e->_n = *x;
109 *x = e;
110 }
111
112 static void replace(Iterator const &p, T*e)
113 {
114 S_list_item **x = Base::__get_internal(p);
115 e->_n = (*x)->_n;
116 *x = e;
117 }
118
119 static Iterator erase(Iterator const &e)
120 {
121 S_list_item **x = Base::__get_internal(e);
122 *x = (*x)->_n;
123 return e;
124 }
125
126};
127
128
129template< typename T >
130class S_list_bss : public S_list<T>
131{
132public:
133 S_list_bss() : S_list<T>(true) {}
134};
135
136template< typename T, typename POLICY = Bits::Basic_list_policy< T, S_list_item > >
137class S_list_tail : public S_list<T, POLICY>
138{
139private:
140 typedef S_list<T, POLICY> Base;
141 void add(T *e) = delete;
142
143public:
144 using Iterator = typename Base::Iterator;
145 S_list_tail() : Base(), _tail(&this->_f) {}
146
147 S_list_tail(S_list_tail &&t)
148 : Base(static_cast<Base&&>(t)), _tail(t.empty() ? &this->_f : t._tail)
149 {
150 t._tail = &t._f;
151 }
152
153 S_list_tail &operator = (S_list_tail &&t)
154 {
155 if (&t == this)
156 return *this;
157
158 Base::operator = (static_cast<Base &&>(t));
159 _tail = t.empty() ? &this->_f : t._tail;
160 t._tail = &t._f;
161 return *this;
162 }
163
164 void push_front(T *e)
165 {
166 if (Base::empty())
167 _tail = &e->_n;
168
170 }
171
172 void push_back(T *e)
173 {
174 e->_n = 0;
175 *_tail = e;
176 _tail = &e->_n;
177 }
178
179 void clear()
180 {
181 Base::clear();
182 _tail = &this->_f;
183 }
184
185 void append(S_list_tail &o)
186 {
187 T *x = o.front();
188 *_tail = x;
189 if (x)
190 _tail = o._tail;
191 o.clear();
192 }
193
194 T *pop_front()
195 {
196 T *t = Base::pop_front();
197 if (t && Base::empty())
198 _tail = &this->_f;
199 return t;
200 }
201
202private:
203 static void insert(T *e, Iterator const &pred);
204 static void insert_before(T *e, Iterator const &succ);
205 static void replace(Iterator const &p, T*e);
206 static Iterator erase(Iterator const &e);
207
208private:
209 S_list_item **_tail;
210};
211
212}
Internal: Common functions for all head-based list implementations.
Definition list_basics.h:40
bool empty() const
Check if the list is empty.
void clear()
Remove all elements from the list.
POLICY::Head_type _f
Pointer to front of the list.
Value_type front() const
Return the first element in the list.
Simple single-linked list.
Definition slist:41
void add(T *e)
Add an element to the front of the list.
Definition slist:65
void push_front(T *e)
Add an element to the front of the list.
Definition slist:82
T * pop_front()
Remove and return the head element of the list.
Definition slist:89
Our C++ library.
Definition arith:11