libs/corosio/src/corosio/src/detail/intrusive.hpp

98.1% Lines (52/53) 100.0% Functions (33/33) 95.0% Branches (19/20)
libs/corosio/src/corosio/src/detail/intrusive.hpp
Line Branch Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #ifndef BOOST_COROSIO_DETAIL_INTRUSIVE_HPP
11 #define BOOST_COROSIO_DETAIL_INTRUSIVE_HPP
12
13 namespace boost::corosio::detail {
14
15 //------------------------------------------------
16
17 /** An intrusive doubly linked list.
18
19 This container provides O(1) push and pop operations for
20 elements that derive from @ref node. Elements are not
21 copied or moved; they are linked directly into the list.
22
23 @tparam T The element type. Must derive from `intrusive_list<T>::node`.
24 */
25 template<class T>
26 class intrusive_list
27 {
28 public:
29 /** Base class for list elements.
30
31 Derive from this class to make a type usable with
32 @ref intrusive_list. The `next_` and `prev_` pointers
33 are private and accessible only to the list.
34 */
35 class node
36 {
37 friend class intrusive_list;
38
39 private:
40 T* next_;
41 T* prev_;
42 };
43
44 private:
45 T* head_ = nullptr;
46 T* tail_ = nullptr;
47
48 public:
49 1854 intrusive_list() = default;
50
51 intrusive_list(intrusive_list&& other) noexcept
52 : head_(other.head_)
53 , tail_(other.tail_)
54 {
55 other.head_ = nullptr;
56 other.tail_ = nullptr;
57 }
58
59 intrusive_list(intrusive_list const&) = delete;
60 intrusive_list& operator=(intrusive_list const&) = delete;
61 intrusive_list& operator=(intrusive_list&&) = delete;
62
63 bool
64 empty() const noexcept
65 {
66 return head_ == nullptr;
67 }
68
69 void
70 25268 push_back(T* w) noexcept
71 {
72 25268 w->next_ = nullptr;
73 25268 w->prev_ = tail_;
74
2/2
✓ Branch 0 taken 6527 times.
✓ Branch 1 taken 18741 times.
25268 if(tail_)
75 6527 tail_->next_ = w;
76 else
77 18741 head_ = w;
78 25268 tail_ = w;
79 25268 }
80
81 void
82 splice_back(intrusive_list& other) noexcept
83 {
84 if(other.empty())
85 return;
86 if(tail_)
87 {
88 tail_->next_ = other.head_;
89 other.head_->prev_ = tail_;
90 tail_ = other.tail_;
91 }
92 else
93 {
94 head_ = other.head_;
95 tail_ = other.tail_;
96 }
97 other.head_ = nullptr;
98 other.tail_ = nullptr;
99 }
100
101 T*
102 8484 pop_front() noexcept
103 {
104
2/2
✓ Branch 0 taken 1983 times.
✓ Branch 1 taken 6501 times.
8484 if(!head_)
105 1983 return nullptr;
106 6501 T* w = head_;
107 6501 head_ = head_->next_;
108
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 6473 times.
6501 if(head_)
109 28 head_->prev_ = nullptr;
110 else
111 6473 tail_ = nullptr;
112 6501 return w;
113 }
114
115 void
116 18767 remove(T* w) noexcept
117 {
118
2/2
✓ Branch 0 taken 6470 times.
✓ Branch 1 taken 12297 times.
18767 if(w->prev_)
119 6470 w->prev_->next_ = w->next_;
120 else
121 12297 head_ = w->next_;
122
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 18732 times.
18767 if(w->next_)
123 35 w->next_->prev_ = w->prev_;
124 else
125 18732 tail_ = w->prev_;
126 18767 }
127 };
128
129 //------------------------------------------------
130
131 /** An intrusive singly linked FIFO queue.
132
133 This container provides O(1) push and pop operations for
134 elements that derive from @ref node. Elements are not
135 copied or moved; they are linked directly into the queue.
136
137 Unlike @ref intrusive_list, this uses only a single `next_`
138 pointer per node, saving memory at the cost of not supporting
139 O(1) removal of arbitrary elements.
140
141 @tparam T The element type. Must derive from `intrusive_queue<T>::node`.
142 */
143 template<class T>
144 class intrusive_queue
145 {
146 public:
147 /** Base class for queue elements.
148
149 Derive from this class to make a type usable with
150 @ref intrusive_queue. The `next_` pointer is private
151 and accessible only to the queue.
152 */
153 class node
154 {
155 friend class intrusive_queue;
156
157 private:
158 T* next_;
159 };
160
161 private:
162 T* head_ = nullptr;
163 T* tail_ = nullptr;
164
165 public:
166 469 intrusive_queue() = default;
167
168 intrusive_queue(intrusive_queue&& other) noexcept
169 : head_(other.head_)
170 , tail_(other.tail_)
171 {
172 other.head_ = nullptr;
173 other.tail_ = nullptr;
174 }
175
176 intrusive_queue(intrusive_queue const&) = delete;
177 intrusive_queue& operator=(intrusive_queue const&) = delete;
178 intrusive_queue& operator=(intrusive_queue&&) = delete;
179
180 bool
181 1068994 empty() const noexcept
182 {
183 1068994 return head_ == nullptr;
184 }
185
186 void
187 605039 push(T* w) noexcept
188 {
189 605039 w->next_ = nullptr;
190
2/2
✓ Branch 0 taken 381686 times.
✓ Branch 1 taken 223353 times.
605039 if(tail_)
191 381686 tail_->next_ = w;
192 else
193 223353 head_ = w;
194 605039 tail_ = w;
195 605039 }
196
197 void
198 207228 splice(intrusive_queue& other) noexcept
199 {
200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 207228 times.
207228 if(other.empty())
201 return;
202
2/2
✓ Branch 0 taken 201443 times.
✓ Branch 1 taken 5785 times.
207228 if(tail_)
203 201443 tail_->next_ = other.head_;
204 else
205 5785 head_ = other.head_;
206 207228 tail_ = other.tail_;
207 207228 other.head_ = nullptr;
208 207228 other.tail_ = nullptr;
209 }
210
211 T*
212 681777 pop() noexcept
213 {
214
2/2
✓ Branch 0 taken 76738 times.
✓ Branch 1 taken 605039 times.
681777 if(!head_)
215 76738 return nullptr;
216 605039 T* w = head_;
217 605039 head_ = head_->next_;
218
2/2
✓ Branch 0 taken 21910 times.
✓ Branch 1 taken 583129 times.
605039 if(!head_)
219 21910 tail_ = nullptr;
220 605039 return w;
221 }
222 };
223
224 } // namespace boost::corosio::detail
225
226 #endif
227