]> git.proxmox.com Git - mirror_frr.git/blame - pceplib/pcep_utils_queue.c
Merge pull request #11096 from anlancs/fix/bgpd-unlock
[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
1f8031f7
DL
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
74971473
JG
28#include <stdbool.h>
29#include <stdio.h>
30#include <string.h>
31
32#include "pcep_utils_logging.h"
33#include "pcep_utils_memory.h"
34#include "pcep_utils_queue.h"
35
36queue_handle *queue_initialize()
37{
38 /* Set the max_entries to 0 to disable it */
39 return queue_initialize_with_size(0);
40}
41
42
43queue_handle *queue_initialize_with_size(unsigned int max_entries)
44{
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;
49
50 return handle;
51}
52
53
54void queue_destroy(queue_handle *handle)
55{
56 if (handle == NULL) {
57 pcep_log(
58 LOG_DEBUG,
59 "%s: queue_destroy, the queue has not been initialized",
60 __func__);
61 return;
62 }
63
64 while (queue_dequeue(handle) != NULL) {
65 }
66 pceplib_free(PCEPLIB_INFRA, handle);
67}
68
69
70void queue_destroy_with_data(queue_handle *handle)
71{
72 if (handle == NULL) {
73 pcep_log(
74 LOG_DEBUG,
75 "%s: queue_destroy_with_data, the queue has not been initialized",
76 __func__);
77 return;
78 }
79
80 void *data = queue_dequeue(handle);
81 while (data != NULL) {
82 pceplib_free(PCEPLIB_INFRA, data);
83 data = queue_dequeue(handle);
84 }
85 pceplib_free(PCEPLIB_INFRA, handle);
86}
87
88
89queue_node *queue_enqueue(queue_handle *handle, void *data)
90{
91 if (handle == NULL) {
92 pcep_log(
93 LOG_DEBUG,
94 "%s: queue_enqueue, the queue has not been initialized",
95 __func__);
96 return NULL;
97 }
98
99 if (handle->max_entries > 0
100 && handle->num_entries >= handle->max_entries) {
101 pcep_log(
102 LOG_DEBUG,
103 "%s: queue_enqueue, cannot enqueue: max entries hit [%u]",
104 handle->num_entries);
105 return NULL;
106 }
107
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;
113
114 (handle->num_entries)++;
115 if (handle->head == NULL) {
116 /* its the first entry in the queue */
117 handle->head = handle->tail = new_node;
118 } else {
119 handle->tail->next_node = new_node;
120 handle->tail = new_node;
121 }
122
123 return new_node;
124}
125
126
127void *queue_dequeue(queue_handle *handle)
128{
129 if (handle == NULL) {
130 pcep_log(
131 LOG_DEBUG,
132 "%s: queue_dequeue, the queue has not been initialized",
133 __func__);
134 return NULL;
135 }
136
137 if (handle->head == NULL) {
138 return NULL;
139 }
140
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;
147 } else {
148 handle->head = node->next_node;
149 }
150
151 pceplib_free(PCEPLIB_INFRA, node);
152
153 return node_data;
154}