UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
LinkedList.cpp
Go to the documentation of this file.
1/**************************************************************************
2 *
3 * Copyright (c) 2000-2003 Intel Corporation
4 * All rights reserved.
5 * Copyright (c) 2012 France Telecom All rights reserved.
6 * Copyright (C) 2021+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
7 * Redistribution only with this Copyright remark. Last modified: 2025-07-26
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * - Neither name of Intel Corporation nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
29 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 **************************************************************************/
34// Last compare with ./Pupnp source file on 2025-07-26, ver 1.14.22
35
45#include "LinkedList.hpp"
46
48constexpr int FREELISTSIZE{100};
49
51#include <cassert>
53
54namespace {
62int freeListNode(ListNode* node, LinkedList* list) {
63 assert(list != 0);
64
65 return FreeListFree(&list->freeNodeList, node);
66}
67
77 void* item,
79 LinkedList* list) {
80 ListNode* temp = 0;
81
82 assert(list != 0);
83
84 temp = (ListNode*)FreeListAlloc(&list->freeNodeList);
85 if (temp) {
86 temp->prev = 0;
87 temp->next = 0;
88 temp->item = item;
89 }
90
91 return temp;
92}
93
95} // anonymous namespace
96
97int ListInit(LinkedList* list, cmp_routine cmp_func, free_function free_func) {
98 int retCode = 0;
99
100 assert(list != 0);
101
102 if (!list)
103 return EINVAL;
104 list->size = 0;
105 list->cmp_func = cmp_func;
106 list->free_func = free_func;
107 retCode = FreeListInit(&list->freeNodeList, sizeof(ListNode), FREELISTSIZE);
108
109 assert(retCode == 0);
110
111 list->head.item = 0;
112 list->head.next = &list->tail;
113 list->head.prev = 0;
114 list->tail.item = 0;
115 list->tail.prev = &list->head;
116 list->tail.next = 0;
117
118 return retCode;
119}
120
121ListNode* ListAddHead(LinkedList* list, void* item) {
122 assert(list != 0);
123
124 if (!list)
125 return 0;
126
127 return ListAddAfter(list, item, &list->head);
128}
129
130ListNode* ListAddTail(LinkedList* list, void* item) {
131 assert(list != 0);
132
133 if (!list)
134 return 0;
135
136 return ListAddBefore(list, item, &list->tail);
137}
138
139ListNode* ListAddAfter(LinkedList* list, void* item, ListNode* bnode) {
140 ListNode* newNode = 0;
141
142 assert(list != 0);
143
144 if (!list || !bnode)
145 return 0;
146 newNode = CreateListNode(item, list);
147 if (newNode) {
148 ListNode* temp = bnode->next;
149
150 bnode->next = newNode;
151 newNode->prev = bnode;
152 newNode->next = temp;
153 temp->prev = newNode;
154 list->size++;
155
156 return newNode;
157 }
158
159 return 0;
160}
161
162ListNode* ListAddBefore(LinkedList* list, void* item, ListNode* anode) {
163 ListNode* newNode = 0;
164
165 assert(list != 0);
166
167 if (!list || !anode)
168 return 0;
169 newNode = CreateListNode(item, list);
170 if (newNode) {
171 ListNode* temp = anode->prev;
172
173 anode->prev = newNode;
174 newNode->next = anode;
175 newNode->prev = temp;
176 temp->next = newNode;
177 list->size++;
178
179 return newNode;
180 }
181
182 return 0;
183}
184
185void* ListDelNode(LinkedList* list, ListNode* dnode, int freeItem) {
186 void* temp;
187
188 assert(list != 0);
189 assert(dnode != &list->head);
190 assert(dnode != &list->tail);
191
192 if (!list || dnode == &list->head || dnode == &list->tail || !dnode)
193 return 0;
194 temp = dnode->item;
195 dnode->prev->next = dnode->next;
196 dnode->next->prev = dnode->prev;
197 freeListNode(dnode, list);
198 list->size--;
199 if (freeItem && list->free_func) {
200 list->free_func(temp);
201 temp = 0;
202 }
203
204 return temp;
205}
206
207int ListDestroy(LinkedList* list, int freeItem) {
208 ListNode* dnode = 0;
209 ListNode* temp = 0;
210
211 if (!list)
212 return EINVAL;
213
214 for (dnode = list->head.next; dnode != &list->tail;) {
215 temp = dnode->next;
216 ListDelNode(list, dnode, freeItem);
217 dnode = temp;
218 }
219 list->size = 0;
221
222 return 0;
223}
224
226 assert(list != 0);
227
228 if (!list)
229 return 0;
230
231 if (!list->size)
232 return 0;
233 else
234 return list->head.next;
235}
236
238 assert(list != 0);
239
240 if (!list)
241 return 0;
242
243 if (!list->size)
244 return 0;
245 else
246 return list->tail.prev;
247}
248
250 assert(list != 0);
251 assert(node != 0);
252
253 if (!list || !node)
254 return 0;
255 if (node->next == &list->tail)
256 return 0;
257 else
258 return node->next;
259}
260
262 assert(list != 0);
263 assert(node != 0);
264
265 if (!list || !node)
266 return 0;
267
268 if (node->prev == &list->head)
269 return 0;
270 else
271 return node->prev;
272}
273
274ListNode* ListFind(LinkedList* list, ListNode* start, void* item) {
275 ListNode* finger = 0;
276
277 if (!list)
278 return 0;
279 if (!start)
280 start = &list->head;
281
282 assert(start);
283
284 finger = start->next;
285
286 assert(finger);
287
288 while (finger != &list->tail) {
289 if (list->cmp_func) {
290 if (list->cmp_func(item, finger->item))
291 return finger;
292 } else {
293 if (item == finger->item)
294 return finger;
295 }
296 finger = finger->next;
297 }
298
299 return 0;
300}
301
302long ListSize(LinkedList* list) {
303 assert(list != 0);
304
305 if (!list)
306 return EINVAL;
307
308 return list->size;
309}
int FreeListInit(FreeList *free_list, size_t elementSize, int maxFreeListLength)
Initializes Free List.
Definition FreeList.cpp:50
int FreeListFree(FreeList *free_list, void *element)
Returns an item to the Free List.
Definition FreeList.cpp:83
void * FreeListAlloc(FreeList *free_list)
Allocates chunk of set size.
Definition FreeList.cpp:64
int FreeListDestroy(FreeList *free_list)
Releases the resources stored with the free list.
Definition FreeList.cpp:103
long ListSize(LinkedList *list)
Returns the size of the list.
int ListDestroy(LinkedList *list, int freeItem)
Removes all memory associated with list nodes. Does not free LinkedList *list.
ListNode * ListPrev(LinkedList *list, ListNode *node)
Returns the previous item in the list.
void * ListDelNode(LinkedList *list, ListNode *dnode, int freeItem)
Removes a node from the list. The memory for the node is freed.
ListNode * ListAddBefore(LinkedList *list, void *item, ListNode *anode)
Adds a node before the specified node. Node gets added immediately before anode.
ListNode * ListNext(LinkedList *list, ListNode *node)
Returns the next item in the list.
ListNode * ListAddAfter(LinkedList *list, void *item, ListNode *bnode)
Adds a node after the specified node. Node gets added immediately after bnode.
ListNode * ListHead(LinkedList *list)
Returns the head of the list.
ListNode * ListAddHead(LinkedList *list, void *item)
Adds a node to the head of the list. Node gets immediately after list head.
ListNode * ListAddTail(LinkedList *list, void *item)
Adds a node to the tail of the list. Node gets added immediately before list.tail.
ListNode * ListTail(LinkedList *list)
Returns the tail of the list.
ListNode * ListFind(LinkedList *list, ListNode *start, void *item)
Finds the specified item in the list.
int ListInit(LinkedList *list, cmp_routine cmp_func, free_function free_func)
Initializes LinkedList. Must be called first and only once for List.
constexpr int FREELISTSIZE
Size of a free list.
Manage a linked list (for internal use only).
cmp_routine cmp_func
compare function to use
ListNode head
head, first item is stored at: head->next
free_function free_func
free function to use
FreeList freeNodeList
free list to use
long size
size of list
void(* free_function)(void *arg)
Function for freeing list items.
ListNode tail
tail, last item is stored at: tail->prev
int(* cmp_routine)(void *itemA, void *itemB)
Function for comparing list items. Returns 1 if itemA==itemB.
Linked list node. Stores generic item and pointers to next and prev.
Linked list (no protection).
int freeListNode(ListNode *node, LinkedList *list)
Free list node.
ListNode * CreateListNode(void *item, LinkedList *list)
Dynamically creates a list node.