]> git.proxmox.com Git - mirror_frr.git/blame - pceplib/pcep_utils_queue.c
Merge pull request #8382 from taspelund/add_rd_all_v2
[mirror_frr.git] / pceplib / pcep_utils_queue.c
CommitLineData
74971473
JG
1/*
2 * This file is part of the PCEPlib, a PCEP protocol library.
3 *
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author : Brady Johnson <brady@voltanet.io>
20 *
21 */
22
23
24#include <stdbool.h>
25#include <stdio.h>
26#include <string.h>
27
28#include "pcep_utils_logging.h"
29#include "pcep_utils_memory.h"
30#include "pcep_utils_queue.h"
31
32queue_handle *queue_initialize()
33{
34 /* Set the max_entries to 0 to disable it */
35 return queue_initialize_with_size(0);
36}
37
38
39queue_handle *queue_initialize_with_size(unsigned int max_entries)
40{
41 queue_handle *handle =
42 pceplib_malloc(PCEPLIB_INFRA, sizeof(queue_handle));
43 memset(handle, 0, sizeof(queue_handle));
44 handle->max_entries = max_entries;
45
46 return handle;
47}
48
49
50void queue_destroy(queue_handle *handle)
51{
52 if (handle == NULL) {
53 pcep_log(
54 LOG_DEBUG,
55 "%s: queue_destroy, the queue has not been initialized",
56 __func__);
57 return;
58 }
59
60 while (queue_dequeue(handle) != NULL) {
61 }
62 pceplib_free(PCEPLIB_INFRA, handle);
63}
64
65
66void queue_destroy_with_data(queue_handle *handle)
67{
68 if (handle == NULL) {
69 pcep_log(
70 LOG_DEBUG,
71 "%s: queue_destroy_with_data, the queue has not been initialized",
72 __func__);
73 return;
74 }
75
76 void *data = queue_dequeue(handle);
77 while (data != NULL) {
78 pceplib_free(PCEPLIB_INFRA, data);
79 data = queue_dequeue(handle);
80 }
81 pceplib_free(PCEPLIB_INFRA, handle);
82}
83
84
85queue_node *queue_enqueue(queue_handle *handle, void *data)
86{
87 if (handle == NULL) {
88 pcep_log(
89 LOG_DEBUG,
90 "%s: queue_enqueue, the queue has not been initialized",
91 __func__);
92 return NULL;
93 }
94
95 if (handle->max_entries > 0
96 && handle->num_entries >= handle->max_entries) {
97 pcep_log(
98 LOG_DEBUG,
99 "%s: queue_enqueue, cannot enqueue: max entries hit [%u]",
100 handle->num_entries);
101 return NULL;
102 }
103
104 queue_node *new_node =
105 pceplib_malloc(PCEPLIB_INFRA, sizeof(queue_node));
106 memset(new_node, 0, sizeof(queue_node));
107 new_node->data = data;
108 new_node->next_node = NULL;
109
110 (handle->num_entries)++;
111 if (handle->head == NULL) {
112 /* its the first entry in the queue */
113 handle->head = handle->tail = new_node;
114 } else {
115 handle->tail->next_node = new_node;
116 handle->tail = new_node;
117 }
118
119 return new_node;
120}
121
122
123void *queue_dequeue(queue_handle *handle)
124{
125 if (handle == NULL) {
126 pcep_log(
127 LOG_DEBUG,
128 "%s: queue_dequeue, the queue has not been initialized",
129 __func__);
130 return NULL;
131 }
132
133 if (handle->head == NULL) {
134 return NULL;
135 }
136
137 void *node_data = handle->head->data;
138 queue_node *node = handle->head;
139 (handle->num_entries)--;
140 if (handle->head == handle->tail) {
141 /* its the last entry in the queue */
142 handle->head = handle->tail = NULL;
143 } else {
144 handle->head = node->next_node;
145 }
146
147 pceplib_free(PCEPLIB_INFRA, node);
148
149 return node_data;
150}