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