]>
git.proxmox.com Git - mirror_frr.git/blob - pceplib/pcep_utils_double_linked_list.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * This file is part of the PCEPlib, a PCEP protocol library.
5 * Copyright (C) 2020 Volta Networks https://voltanet.io/
7 * Author : Brady Johnson <brady@voltanet.io>
18 #include "pcep_utils_double_linked_list.h"
19 #include "pcep_utils_logging.h"
20 #include "pcep_utils_memory.h"
22 double_linked_list
*dll_initialize(void)
24 double_linked_list
*handle
=
25 pceplib_malloc(PCEPLIB_INFRA
, sizeof(double_linked_list
));
27 memset(handle
, 0, sizeof(double_linked_list
));
28 handle
->num_entries
= 0;
33 "%s: dll_initialize cannot allocate memory for handle",
42 void dll_destroy(double_linked_list
*handle
)
46 "%s: dll_destroy cannot destroy NULL handle",
51 double_linked_list_node
*node
= handle
->head
;
52 while (node
!= NULL
) {
53 double_linked_list_node
*node_to_delete
= node
;
54 node
= node
->next_node
;
55 pceplib_free(PCEPLIB_INFRA
, node_to_delete
);
58 pceplib_free(PCEPLIB_INFRA
, handle
);
62 void dll_destroy_with_data_memtype(double_linked_list
*handle
,
63 void *data_memory_type
)
67 "%s: dll_destroy_with_data cannot destroy NULL handle",
72 double_linked_list_node
*node
= handle
->head
;
73 while (node
!= NULL
) {
74 double_linked_list_node
*node_to_delete
= node
;
75 pceplib_free(data_memory_type
, node
->data
);
76 node
= node
->next_node
;
77 pceplib_free(PCEPLIB_INFRA
, node_to_delete
);
80 pceplib_free(PCEPLIB_INFRA
, handle
);
84 void dll_destroy_with_data(double_linked_list
*handle
)
86 /* Default to destroying the data with the INFRA mem type */
87 dll_destroy_with_data_memtype(handle
, PCEPLIB_INFRA
);
91 /* Creates a node and adds it as the first item in the list */
92 double_linked_list_node
*dll_prepend(double_linked_list
*handle
, void *data
)
95 pcep_log(LOG_WARNING
, "%s: dll_prepend_data NULL handle",
100 /* Create the new node */
101 double_linked_list_node
*new_node
=
102 pceplib_malloc(PCEPLIB_INFRA
, sizeof(double_linked_list_node
));
103 memset(new_node
, 0, sizeof(double_linked_list_node
));
104 new_node
->data
= data
;
106 if (handle
->head
== NULL
) {
107 handle
->head
= new_node
;
108 handle
->tail
= new_node
;
110 new_node
->next_node
= handle
->head
;
111 handle
->head
->prev_node
= new_node
;
112 handle
->head
= new_node
;
115 (handle
->num_entries
)++;
121 /* Creates a node and adds it as the last item in the list */
122 double_linked_list_node
*dll_append(double_linked_list
*handle
, void *data
)
124 if (handle
== NULL
) {
125 pcep_log(LOG_WARNING
, "%s: dll_append_data NULL handle",
130 /* Create the new node */
131 double_linked_list_node
*new_node
=
132 pceplib_malloc(PCEPLIB_INFRA
, sizeof(double_linked_list_node
));
133 memset(new_node
, 0, sizeof(double_linked_list_node
));
134 new_node
->data
= data
;
136 if (handle
->head
== NULL
) {
137 handle
->head
= new_node
;
138 handle
->tail
= new_node
;
140 new_node
->prev_node
= handle
->tail
;
141 handle
->tail
->next_node
= new_node
;
142 handle
->tail
= new_node
;
145 (handle
->num_entries
)++;
151 /* Delete the first node in the list, and return the data */
152 void *dll_delete_first_node(double_linked_list
*handle
)
154 if (handle
== NULL
) {
155 pcep_log(LOG_WARNING
, "%s: dll_delete_first_node NULL handle",
160 if (handle
->head
== NULL
) {
164 double_linked_list_node
*delete_node
= handle
->head
;
165 void *data
= delete_node
->data
;
167 if (delete_node
->next_node
== NULL
) {
168 /* Its the last node in the list */
172 handle
->head
= delete_node
->next_node
;
173 handle
->head
->prev_node
= NULL
;
176 pceplib_free(PCEPLIB_INFRA
, delete_node
);
177 (handle
->num_entries
)--;
183 /* Delete the last node in the list, and return the data */
184 void *dll_delete_last_node(double_linked_list
*handle
)
186 if (handle
== NULL
) {
187 pcep_log(LOG_WARNING
, "%s: dll_delete_last_node NULL handle",
192 if (handle
->head
== NULL
) {
196 double_linked_list_node
*delete_node
= handle
->tail
;
197 void *data
= delete_node
->data
;
199 if (delete_node
->prev_node
== NULL
) {
200 /* Its the last node in the list */
204 handle
->tail
= delete_node
->prev_node
;
205 handle
->tail
->next_node
= NULL
;
208 pceplib_free(PCEPLIB_INFRA
, delete_node
);
209 (handle
->num_entries
)--;
215 /* Delete the designated node in the list, and return the data */
216 void *dll_delete_node(double_linked_list
*handle
, double_linked_list_node
*node
)
218 if (handle
== NULL
) {
219 pcep_log(LOG_WARNING
, "%s: dll_delete_node NULL handle",
228 if (handle
->head
== NULL
) {
232 void *data
= node
->data
;
234 if (handle
->head
== handle
->tail
) {
235 /* Its the last node in the list */
238 } else if (handle
->head
== node
) {
239 handle
->head
= node
->next_node
;
240 handle
->head
->prev_node
= NULL
;
241 } else if (handle
->tail
== node
) {
242 handle
->tail
= node
->prev_node
;
243 handle
->tail
->next_node
= NULL
;
245 /* Its somewhere in the middle of the list */
246 node
->next_node
->prev_node
= node
->prev_node
;
247 node
->prev_node
->next_node
= node
->next_node
;
250 pceplib_free(PCEPLIB_INFRA
, node
);
251 (handle
->num_entries
)--;