µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
lists.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
29
31
32// ----------------------------------------------------------------------------
33
34#if defined(__clang__)
35#pragma clang diagnostic ignored "-Wc++98-compat"
36#endif
37
38// ----------------------------------------------------------------------------
39
40namespace os
41{
42 namespace utils
43 {
44 // ======================================================================
45
67 void
69 {
70 // Check if not already unlinked.
71 if (unlinked ())
72 {
73 assert(prev_ == nullptr);
74#if defined(OS_TRACE_UTILS_LISTS)
75 trace::printf ("%s() %p nop\n", __func__, this);
76#endif
77 return;
78 }
79
80#if defined(OS_TRACE_UTILS_LISTS)
81 trace::printf ("%s() %p \n", __func__, this);
82#endif
83
84 // Make neighbours point to each other.
85 prev_->next_ = next_;
86 next_->prev_ = prev_;
87
88 // Nullify both pointers in the unlinked node.
89 prev_ = nullptr;
90 next_ = nullptr;
91 }
92
93 // ========================================================================
94
115 void
117 {
118 head_.next (const_cast<static_double_list_links*> (&head_));
119 head_.prev (const_cast<static_double_list_links*> (&head_));
120 }
121
122 void
125 {
126#if defined(OS_TRACE_UTILS_LISTS)
127 trace::printf ("%s() n=%p after %p\n", __func__, &node, after);
128#endif
129
130 // Unlinked nodes must have both pointers null.
131 // If not, most probably the node was already linked.
132 // Or the memory is corrupted.
133 assert(node.prev () == nullptr);
134 assert(node.next () == nullptr);
135
136 // The `after` node must be linked. Only the `next` pointer is
137 // tested, since only it is used.
138 assert(after->next () != nullptr);
139
140 // Make the new node point to its neighbours.
141 node.prev (after);
142 node.next (after->next ());
143
144 // Make the neighbours point to the node. The order is important.
145 after->next ()->prev (&node);
146 after->next (&node);
147 }
148
149 // ========================================================================
150
156 {
157#if defined(OS_TRACE_UTILS_LISTS_CONSTRUCT) || defined(OS_TRACE_UTILS_LISTS)
158 trace::printf ("%s() %p \n", __func__, this);
159#endif
160
161 clear ();
162 }
163
169 {
170#if defined(OS_TRACE_UTILS_LISTS_CONSTRUCT) || defined(OS_TRACE_UTILS_LISTS)
171 trace::printf ("%s() %p \n", __func__, this);
172#endif
173
174 assert(empty ());
175 }
176
177 } /* namespace utils */
178} /* namespace os */
179
180// ----------------------------------------------------------------------------
double_list()
Construct a list.
Definition lists.cpp:155
~double_list()
Destruct the list.
Definition lists.cpp:168
static_double_list_links head_
A list node used to point to head and tail.
Definition lists.h:498
void insert_after(static_double_list_links &node, static_double_list_links *after)
Insert a new node after existing node.
Definition lists.cpp:123
bool empty(void) const
Check if the list is empty.
Definition lists.h:1049
void clear(void)
Clear the list.
Definition lists.cpp:116
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
System namespace.