utils-lists 4.0.0
µOS++ C++ intrusive lists utilities
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. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/MIT/.
11 */
12
13// ----------------------------------------------------------------------------
14
15#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
16#include <micro-os-plus/config.h>
17#endif // MICRO_OS_PLUS_INCLUDE_CONFIG_H
18
20#include <micro-os-plus/diag/trace.h>
21
22// ----------------------------------------------------------------------------
23
24#if defined(__GNUC__)
25#pragma GCC diagnostic push
26
27#pragma GCC diagnostic ignored "-Waggregate-return"
28#if defined(__clang__)
29#pragma clang diagnostic ignored "-Wc++98-compat"
30#endif
31#endif
32
34{
35 // ==========================================================================
36
45 bool
47 {
48 if (previous_ == nullptr || next_ == nullptr)
49 {
50 assert (previous_ == nullptr);
51 assert (next_ == nullptr);
52 return true;
53 }
54 return false;
55 }
56
72 void
74 {
75 if (uninitialized ())
76 {
77 initialize ();
78 }
79 }
80
88 void
90 {
91#if defined(MICRO_OS_PLUS_TRACE_UTILS_LISTS)
92 trace::printf ("%s() link %p after %p\n", __func__, node, this);
93#endif
94 assert (next_ != nullptr);
95 assert (next_->previous_ != nullptr);
96
97 // Make the new node point to its new neighbours.
98 node->previous_ = this;
99 node->next_ = next_;
100
101 next_->previous_ = node;
102 next_ = node;
103 }
104
112 void
114 {
115#if defined(MICRO_OS_PLUS_TRACE_UTILS_LISTS)
116 trace::printf ("%s() link %p before %p\n", __func__, node, this);
117#endif
118 assert (next_ != nullptr);
119 assert (next_->previous_ != nullptr);
120
121 // Make the new node point to its new neighbours.
122 node->next_ = this;
123 node->previous_ = previous_;
124
125 previous_->next_ = node;
126 previous_ = node;
127 }
128
137 void
139 {
140#if defined(MICRO_OS_PLUS_TRACE_UTILS_LISTS)
141 trace::printf ("%s() %p \n", __func__, this);
142#endif
143
144 // Make neighbours point to each other.
145 // This works even if the node is already unlinked,
146 // so no need for an extra test.
149
150 // Reset the unlinked node to the initial state,
151 // with both pointers pointing to itself.
152 initialize ();
153 }
154
160 bool
162 {
163 if (next_ == this || previous_ == this)
164 {
165 assert (next_ == this);
166 assert (previous_ == this);
167 return false;
168 }
169 return true;
170 }
171
172 // ==========================================================================
173
178#if defined(__GNUC__) && !defined(__clang__)
179 // Prevent LTO to optimize out the code.
180 // https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
181 __attribute__ ((noinline, noipa))
182#endif
183 void
185 {
186 next_ = nullptr;
187 previous_ = nullptr;
188 }
189
190 // ==========================================================================
191} // namespace micro_os_plus::utils
192
193#if defined(__GNUC__)
194#pragma GCC diagnostic pop
195#endif
196
197// ----------------------------------------------------------------------------
µOS++ utility definitions.
Definition inlines.h:34