blob: 4006a01933fa015da771f273f6fe8cf0b5763d70 [file] [log] [blame]
Kévin Redon69b92d92019-01-24 16:39:20 +01001/**
2 * \file
3 *
4 * \brief List functionality implementation.
5 *
6 * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
7 *
8 * \asf_license_start
9 *
10 * \page License
11 *
12 * Subject to your compliance with these terms, you may use Microchip
13 * software and any derivatives exclusively with Microchip products.
14 * It is your responsibility to comply with third party license terms applicable
15 * to your use of third party software (including open source software) that
16 * may accompany Microchip software.
17 *
18 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
26 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29 *
30 * \asf_license_stop
31 *
32 */
33
34#include <utils_list.h>
35#include <utils_assert.h>
36
37/**
38 * \brief Check whether element belongs to list
39 */
40bool is_list_element(const struct list_descriptor *const list, const void *const element)
41{
42 struct list_element *it;
43 for (it = list->head; it; it = it->next) {
44 if (it == element) {
45 return true;
46 }
47 }
48
49 return false;
50}
51
52/**
53 * \brief Insert an element as list head
54 */
55void list_insert_as_head(struct list_descriptor *const list, void *const element)
56{
57 ASSERT(!is_list_element(list, element));
58
59 ((struct list_element *)element)->next = list->head;
60 list->head = (struct list_element *)element;
61}
62
63/**
64 * \brief Insert an element after the given list element
65 */
66void list_insert_after(void *const after, void *const element)
67{
68 ((struct list_element *)element)->next = ((struct list_element *)after)->next;
69 ((struct list_element *)after)->next = (struct list_element *)element;
70}
71
72/**
73 * \brief Insert an element at list end
74 */
75void list_insert_at_end(struct list_descriptor *const list, void *const element)
76{
77 struct list_element *it = list->head;
78
79 ASSERT(!is_list_element(list, element));
80
81 if (!list->head) {
82 list->head = (struct list_element *)element;
83 ((struct list_element *)element)->next = NULL;
84 return;
85 }
86
87 while (it->next) {
88 it = it->next;
89 }
90 it->next = (struct list_element *)element;
91 ((struct list_element *)element)->next = NULL;
92}
93
94/**
95 * \brief Removes list head
96 */
97void *list_remove_head(struct list_descriptor *const list)
98{
99 if (list->head) {
100 struct list_element *tmp = list->head;
101
102 list->head = list->head->next;
103 return (void *)tmp;
104 }
105
106 return NULL;
107}
108
109/**
110 * \brief Removes list element
111 */
112bool list_delete_element(struct list_descriptor *const list, const void *const element)
113{
114 if (!element) {
115 return false;
116 }
117
118 if (list->head == element) {
119 list->head = list->head->next;
120 return true;
121 } else {
122 struct list_element *it = list->head;
123
124 while (it && it->next != element) {
125 it = it->next;
126 }
127 if (it) {
128 it->next = ((struct list_element *)element)->next;
129 return true;
130 }
131 }
132
133 return false;
134}
135
136//@}