This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
/
gqueue.c
368 lines (320 loc) · 7.2 KB
/
gqueue.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* GQueue: Double ended queue implementation, piggy backed on GList.
* Copyright (C) 1998 Tim Janik
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* MT safe
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "glib.h"
G_LOCK_DEFINE_STATIC (queue_memchunk);
static GMemChunk *queue_memchunk = NULL;
static GTrashStack *free_queue_nodes = NULL;
/**
* g_queue_new:
*
* Creates a new #GQueue.
*
* Returns: a new #GQueue.
**/
GQueue*
g_queue_new (void)
{
GQueue *queue;
G_LOCK (queue_memchunk);
queue = g_trash_stack_pop (&free_queue_nodes);
if (!queue)
{
if (!queue_memchunk)
queue_memchunk = g_mem_chunk_new ("GLib GQueue chunk",
sizeof (GNode),
sizeof (GNode) * 128,
G_ALLOC_ONLY);
queue = g_chunk_new (GQueue, queue_memchunk);
}
G_UNLOCK (queue_memchunk);
queue->head = NULL;
queue->tail = NULL;
queue->length = 0;
return queue;
}
/**
* g_queue_free:
* @queue: a #GQueue.
*
* Frees the memory allocated for the #GQueue.
**/
void
g_queue_free (GQueue *queue)
{
g_return_if_fail (queue != NULL);
g_list_free (queue->head);
#ifdef ENABLE_GC_FRIENDLY
queue->head = NULL;
queue->tail = NULL;
#endif /* ENABLE_GC_FRIENDLY */
G_LOCK (queue_memchunk);
g_trash_stack_push (&free_queue_nodes, queue);
G_UNLOCK (queue_memchunk);
}
/**
* g_queue_push_head:
* @queue: a #GQueue.
* @data: the data for the new element.
*
* Adds a new element at the head of the queue.
**/
void
g_queue_push_head (GQueue *queue,
gpointer data)
{
g_return_if_fail (queue != NULL);
queue->head = g_list_prepend (queue->head, data);
if (!queue->tail)
queue->tail = queue->head;
queue->length++;
}
/**
* g_queue_push_head_link:
* @queue: a #GQueue.
* @link: a single #GList element, <emphasis>not</emphasis> a list with
* more than one element.
*
* Adds a new element at the head of the queue.
**/
void
g_queue_push_head_link (GQueue *queue,
GList *link)
{
g_return_if_fail (queue != NULL);
g_return_if_fail (link != NULL);
g_return_if_fail (link->prev == NULL);
g_return_if_fail (link->next == NULL);
link->next = queue->head;
if (queue->head)
queue->head->prev = link;
else
queue->tail = link;
queue->head = link;
queue->length++;
}
/**
* g_queue_push_tail:
* @queue: a #GQueue.
* @data: the data for the new element.
*
* Adds a new element at the tail of the queue.
**/
void
g_queue_push_tail (GQueue *queue,
gpointer data)
{
g_return_if_fail (queue != NULL);
queue->tail = g_list_append (queue->tail, data);
if (queue->tail->next)
queue->tail = queue->tail->next;
else
queue->head = queue->tail;
queue->length++;
}
/**
* g_queue_push_tail_link:
* @queue: a #GQueue.
* @link: a single #GList element, <emphasis>not</emphasis> a list with
* more than one element.
*
* Adds a new element at the tail of the queue.
**/
void
g_queue_push_tail_link (GQueue *queue,
GList *link)
{
g_return_if_fail (queue != NULL);
g_return_if_fail (link != NULL);
g_return_if_fail (link->prev == NULL);
g_return_if_fail (link->next == NULL);
link->prev = queue->tail;
if (queue->tail)
queue->tail->next = link;
else
queue->head = link;
queue->tail = link;
queue->length++;
}
/**
* g_queue_pop_head:
* @queue: a #GQueue.
*
* Removes the first element of the queue.
*
* Returns: the data of the first element in the queue, or %NULL if the queue
* is empty.
**/
gpointer
g_queue_pop_head (GQueue *queue)
{
g_return_val_if_fail (queue != NULL, NULL);
if (queue->head)
{
GList *node = queue->head;
gpointer data = node->data;
queue->head = node->next;
if (queue->head)
queue->head->prev = NULL;
else
queue->tail = NULL;
g_list_free_1 (node);
queue->length--;
return data;
}
return NULL;
}
/**
* g_queue_pop_head_link:
* @queue: a #GQueue.
*
* Removes the first element of the queue.
*
* Returns: the #GList element at the head of the queue, or %NULL if the queue
* is empty.
**/
GList*
g_queue_pop_head_link (GQueue *queue)
{
g_return_val_if_fail (queue != NULL, NULL);
if (queue->head)
{
GList *node = queue->head;
queue->head = node->next;
if (queue->head)
{
queue->head->prev = NULL;
node->next = NULL;
}
else
queue->tail = NULL;
queue->length--;
return node;
}
return NULL;
}
/**
* g_queue_pop_tail:
* @queue: a #GQueue.
*
* Removes the last element of the queue.
*
* Returns: the data of the last element in the queue, or %NULL if the queue
* is empty.
**/
gpointer
g_queue_pop_tail (GQueue *queue)
{
g_return_val_if_fail (queue != NULL, NULL);
if (queue->tail)
{
GList *node = queue->tail;
gpointer data = node->data;
queue->tail = node->prev;
if (queue->tail)
queue->tail->next = NULL;
else
queue->head = NULL;
queue->length--;
g_list_free_1 (node);
return data;
}
return NULL;
}
/**
* g_queue_pop_tail_link:
* @queue: a #GQueue.
*
* Removes the last element of the queue.
*
* Returns: the #GList element at the tail of the queue, or %NULL if the queue
* is empty.
**/
GList*
g_queue_pop_tail_link (GQueue *queue)
{
g_return_val_if_fail (queue != NULL, NULL);
if (queue->tail)
{
GList *node = queue->tail;
queue->tail = node->prev;
if (queue->tail)
{
queue->tail->next = NULL;
node->prev = NULL;
}
else
queue->head = NULL;
queue->length--;
return node;
}
return NULL;
}
/**
* g_queue_is_empty:
* @queue: a #GQueue.
*
* Returns %TRUE if the queue is empty.
*
* Returns: %TRUE if the queue is empty.
**/
gboolean
g_queue_is_empty (GQueue *queue)
{
g_return_val_if_fail (queue != NULL, TRUE);
return queue->head == NULL;
}
/**
* g_queue_peek_head:
* @queue: a #GQueue.
*
* Returns the first element of the queue.
*
* Returns: the data of the first element in the queue, or %NULL if the queue
* is empty.
**/
gpointer
g_queue_peek_head (GQueue *queue)
{
g_return_val_if_fail (queue != NULL, NULL);
return queue->head ? queue->head->data : NULL;
}
/**
* g_queue_peek_tail:
* @queue: a #GQueue.
*
* Returns the last element of the queue.
*
* Returns: the data of the last element in the queue, or %NULL if the queue
* is empty.
**/
gpointer
g_queue_peek_tail (GQueue *queue)
{
g_return_val_if_fail (queue != NULL, NULL);
return queue->tail ? queue->tail->data : NULL;
}