/
queue.c
220 lines (176 loc) · 4.61 KB
/
queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
A simple, fast dynamic queue
Copyright (C) 2012 William Kennington
This file is part of libadt.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "queue.h"
/*
IMPLEMENTATION NOTES:
Uses arrays within linked lists to store the queue data.
As soon as the array is filled another node is created.
As soon as a link is dequeued it is freed from the list.
Uses a doubly linked list to achieve this.
*/
#define BLOCK_SIZE 16
typedef struct _queue_node_t * queue_node_t;
struct _queue_t
{
queue_node_t head, tail, unused;
size_t size;
};
struct _queue_node_t
{
void ** data;
size_t deq_pos, len, size;
queue_node_t next, prev;
};
queue_t
queue_init (void)
{
queue_t queue;
/* Allocate memory for the queue */
queue = (queue_t) malloc (sizeof (struct _queue_t));
/* Check for bad malloc */
if (queue == NULL)
return queue;
/* Initializes the queue */
queue->head = NULL;
queue->tail = NULL;
queue->unused = NULL;
queue->size = 0;
return queue;
}
static void
queue_eob (queue_t queue)
{
if (queue->head->deq_pos == queue->head->size)
{
/* Free the already existing unused block */
if (queue->unused != NULL)
free (queue->unused);
queue->unused = queue->head;
queue->head = queue->head->next;
queue->head->prev = NULL;
}
}
void *
queue_peek (queue_t queue)
{
/* Check for empty queue */
if (queue->size == 0)
return NULL;
/* Check for the end of the block */
queue_eob (queue);
return queue->head->data[queue->head->deq_pos];
}
void *
queue_deq (queue_t queue)
{
/* Check for empty queue */
if (queue->size == 0)
return NULL;
/* Check for the end of the block */
queue_eob (queue);
/* Update Queue Size */
queue->size--;
return queue->head->data[queue->head->deq_pos++];
}
size_t
queue_size (queue_t queue)
{
return queue->size;
}
queue_error_t
queue_enq (queue_t queue, void * data)
{
size_t size;
/* Empty List Check */
if (queue->tail == NULL ||
queue->tail->size == queue->tail->len)
{
/* Allocate a New Node if we don't have one */
if (queue->unused == NULL)
{
/* Determine the size of the new node */
if (queue->size <= BLOCK_SIZE)
size = BLOCK_SIZE;
else
size = queue->size;
/* Allocate memory for the node and it's data segment */
queue->unused = (queue_node_t) malloc (sizeof (struct _queue_node_t) +
size * sizeof (void *));
/* Check for malloc error */
if (queue->unused == NULL)
return QUEUE_MALLOC_FAILED;
/* Initialize the array */
queue->unused->size = size;
queue->unused->data = (void **)((uint8_t*)queue->unused + sizeof (struct _queue_node_t));
}
/* Initialize the unused queue node */
queue->unused->prev = queue->tail;
queue->unused->next = NULL;
queue->unused->len = 0;
queue->unused->deq_pos = 0;
/* Fix the old list if it exists */
if (queue->tail != NULL)
queue->tail->next = queue->unused;
/* Inject the node */
else
queue->head = queue->unused;
queue->tail = queue->unused;
queue->unused = NULL;
}
/* Add the element to the list */
queue->tail->data[queue->tail->len++] = data;
/* Increase the size*/
queue->size++;
return QUEUE_OK;
}
void
queue_clear (queue_t queue)
{
queue_node_t tmp;
/* Check to see if we can store an unused block */
if (queue->unused == NULL && queue->tail != NULL)
{
queue->unused = queue->tail;
if (queue->head == queue->tail)
queue->head = NULL;
else
queue->tail->prev->next = NULL;
}
/* Iterate over the list freeing it */
while (queue->head != NULL)
{
tmp = queue->head;
queue->head = queue->head->next;
free (tmp);
}
/* Cleanup queue */
queue->tail = NULL;
queue->size = 0;
}
void
queue_destroy (queue_t queue)
{
/* Clears the queue */
queue_clear (queue);
/* Free the unused node */
if (queue->unused != NULL)
free (queue->unused);
/* Free the queue structure */
free (queue);
}