1 /****************************************************************************
2  *
3  *  Copyright (c) 2023, Michael Becker (michael.f.becker@gmail.com)
4  *
5  *  This file is part of the FreeRTOS Add-ons project.
6  *
7  *  Source Code:
8  *  https://github.com/michaelbecker/freertos-addons
9  *
10  *  Project Page:
11  *  http://michaelbecker.github.io/freertos-addons/
12  *
13  *  On-line Documentation:
14  *  http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15  *
16  *  MIT License
17  *
18  *  Permission is hereby granted, free of charge, to any person obtaining a
19  *  copy of this software and associated documentation files
20  *  (the "Software"), to deal in the Software without restriction, including
21  *  without limitation the rights to use, copy, modify, merge, publish,
22  *  distribute, sublicense, and/or sell copies of the Software, and to
23  *  permit persons to whom the Software is furnished to do so,subject to the
24  *  following conditions:
25  *
26  *  + The above copyright notice and this permission notice shall be included
27  *    in all copies or substantial portions of the Software.
28  *  + Credit is appreciated, but not required, if you find this project
29  *    useful enough to include in your application, product, device, etc.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
32  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
34  *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
35  *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
36  *  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
37  *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  *
39  ***************************************************************************/
40 /*
41  *  Copyright 2023-2024 NXP
42  *
43  *  SPDX-License-Identifier: BSD-3-Clause
44  *
45  */
46 
47 #include <stdlib.h>
48 #include "slist.h"
49 
SlAddNodeToTail(SlNode_t * Head,SlNode_t * Node)50 void SlAddNodeToTail(SlNode_t *Head, SlNode_t *Node)
51 {
52     /********************/
53     SlNode_t *Current;
54     /********************/
55 
56     if (Head == NULL)
57         return;
58 
59     if (Node == NULL)
60         return;
61 
62     Current = Head;
63 
64     while (Current->Next != NULL)
65     {
66         Current = Current->Next;
67     }
68 
69     Current->Next = Node;
70     Node->Next    = NULL;
71 }
72 
SlInsertNodeAfter(SlNode_t * Marker,SlNode_t * Node)73 void SlInsertNodeAfter(SlNode_t *Marker, SlNode_t *Node)
74 {
75     /********************/
76     SlNode_t *Temp;
77     /********************/
78 
79     if (Marker == NULL)
80         return;
81 
82     if (Node == NULL)
83         return;
84 
85     Temp         = Marker->Next;
86     Marker->Next = Node;
87     Node->Next   = Temp;
88 }
89 
SlInsertNodeBefore(SlNode_t * Head,SlNode_t * Marker,SlNode_t * Node)90 void SlInsertNodeBefore(SlNode_t *Head, SlNode_t *Marker, SlNode_t *Node)
91 {
92     /********************/
93     SlNode_t *Current;
94     SlNode_t *Prior;
95     int Found = 0;
96     /********************/
97 
98     if (Marker == NULL)
99         return;
100 
101     if (Node == NULL)
102         return;
103 
104     if (Head == NULL)
105         return;
106 
107     Current = Head->Next;
108     Prior   = Head;
109 
110     while (Current != NULL)
111     {
112         if (Current == Marker)
113         {
114             Found = 1;
115             break;
116         }
117 
118         Prior   = Current;
119         Current = Current->Next;
120     }
121 
122     if (Found)
123     {
124         Prior->Next = Node;
125         Node->Next  = Current;
126     }
127 }
128 
SlRemoveNode(SlNode_t * Head,SlNode_t * Node)129 void SlRemoveNode(SlNode_t *Head, SlNode_t *Node)
130 {
131     /********************/
132     SlNode_t *Current;
133     SlNode_t *Prior;
134     int Found = 0;
135     /********************/
136 
137     if (Head == NULL)
138         return;
139 
140     if (Node == NULL)
141         return;
142 
143     Current = Head->Next;
144     Prior   = Head;
145 
146     while (Current != NULL)
147     {
148         if (Current == Node)
149         {
150             Found = 1;
151             break;
152         }
153 
154         Prior   = Current;
155         Current = Current->Next;
156     }
157 
158     if (Found)
159     {
160         Prior->Next = Current->Next;
161     }
162 }
163 
SlRemoveNodeFromHead(SlNode_t * Head)164 SlNode_t *SlRemoveNodeFromHead(SlNode_t *Head)
165 {
166     /********************/
167     SlNode_t *Node;
168     /********************/
169 
170     if (Head == NULL)
171         return NULL;
172 
173     Node = Head->Next;
174 
175     if (Node != NULL)
176     {
177         Head->Next = Node->Next;
178     }
179 
180     return Node;
181 }
182 
SlRemoveNodeFromTail(SlNode_t * Head)183 SlNode_t *SlRemoveNodeFromTail(SlNode_t *Head)
184 {
185     /********************/
186     SlNode_t *Current;
187     SlNode_t *Prior;
188     /********************/
189 
190     if (Head == NULL)
191         return NULL;
192 
193     Current = Head->Next;
194     Prior   = Head;
195 
196     if (SlIsListEmpty(Head))
197     {
198         return NULL;
199     }
200 
201     while (Current->Next != NULL)
202     {
203         Prior   = Current;
204         Current = Current->Next;
205     }
206 
207     Prior->Next = NULL;
208 
209     return Current;
210 }
211