]>
git.proxmox.com Git - mirror_frr.git/blob - pceplib/pcep_utils_queue.c
2 * This file is part of the PCEPlib, a PCEP protocol library.
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * Author : Brady Johnson <brady@voltanet.io>
32 #include "pcep_utils_logging.h"
33 #include "pcep_utils_memory.h"
34 #include "pcep_utils_queue.h"
36 queue_handle
*queue_initialize()
38 /* Set the max_entries to 0 to disable it */
39 return queue_initialize_with_size(0);
43 queue_handle
*queue_initialize_with_size(unsigned int max_entries
)
45 queue_handle
*handle
=
46 pceplib_malloc(PCEPLIB_INFRA
, sizeof(queue_handle
));
47 memset(handle
, 0, sizeof(queue_handle
));
48 handle
->max_entries
= max_entries
;
54 void queue_destroy(queue_handle
*handle
)
59 "%s: queue_destroy, the queue has not been initialized",
64 while (queue_dequeue(handle
) != NULL
) {
66 pceplib_free(PCEPLIB_INFRA
, handle
);
70 void queue_destroy_with_data(queue_handle
*handle
)
75 "%s: queue_destroy_with_data, the queue has not been initialized",
80 void *data
= queue_dequeue(handle
);
81 while (data
!= NULL
) {
82 pceplib_free(PCEPLIB_INFRA
, data
);
83 data
= queue_dequeue(handle
);
85 pceplib_free(PCEPLIB_INFRA
, handle
);
89 queue_node
*queue_enqueue(queue_handle
*handle
, void *data
)
94 "%s: queue_enqueue, the queue has not been initialized",
99 if (handle
->max_entries
> 0
100 && handle
->num_entries
>= handle
->max_entries
) {
103 "%s: queue_enqueue, cannot enqueue: max entries hit [%u]",
104 handle
->num_entries
);
108 queue_node
*new_node
=
109 pceplib_malloc(PCEPLIB_INFRA
, sizeof(queue_node
));
110 memset(new_node
, 0, sizeof(queue_node
));
111 new_node
->data
= data
;
112 new_node
->next_node
= NULL
;
114 (handle
->num_entries
)++;
115 if (handle
->head
== NULL
) {
116 /* its the first entry in the queue */
117 handle
->head
= handle
->tail
= new_node
;
119 handle
->tail
->next_node
= new_node
;
120 handle
->tail
= new_node
;
127 void *queue_dequeue(queue_handle
*handle
)
129 if (handle
== NULL
) {
132 "%s: queue_dequeue, the queue has not been initialized",
137 if (handle
->head
== NULL
) {
141 void *node_data
= handle
->head
->data
;
142 queue_node
*node
= handle
->head
;
143 (handle
->num_entries
)--;
144 if (handle
->head
== handle
->tail
) {
145 /* its the last entry in the queue */
146 handle
->head
= handle
->tail
= NULL
;
148 handle
->head
= node
->next_node
;
151 pceplib_free(PCEPLIB_INFRA
, node
);