1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GQueue: Double ended queue implementation, piggy backed on GList.
5 * Copyright (C) 1998 Tim Janik
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
34 G_LOCK_DEFINE_STATIC (queue_memchunk);
35 static GMemChunk *queue_memchunk = NULL;
36 static GTrashStack *free_queue_nodes = NULL;
41 * Creates a new #GQueue.
43 * Returns: a new #GQueue.
50 G_LOCK (queue_memchunk);
51 queue = g_trash_stack_pop (&free_queue_nodes);
56 queue_memchunk = g_mem_chunk_new ("GLib GQueue chunk",
60 queue = g_chunk_new (GQueue, queue_memchunk);
62 G_UNLOCK (queue_memchunk);
75 * Frees the memory allocated for the #GQueue.
78 g_queue_free (GQueue *queue)
80 g_return_if_fail (queue != NULL);
82 g_list_free (queue->head);
84 #ifdef ENABLE_GC_FRIENDLY
87 #endif /* ENABLE_GC_FRIENDLY */
89 G_LOCK (queue_memchunk);
90 g_trash_stack_push (&free_queue_nodes, queue);
91 G_UNLOCK (queue_memchunk);
97 * @data: the data for the new element.
99 * Adds a new element at the head of the queue.
102 g_queue_push_head (GQueue *queue,
105 g_return_if_fail (queue != NULL);
107 queue->head = g_list_prepend (queue->head, data);
109 queue->tail = queue->head;
114 * g_queue_push_head_link:
116 * @link: a single #GList element, <emphasis>not</emphasis> a list with
117 * more than one element.
119 * Adds a new element at the head of the queue.
122 g_queue_push_head_link (GQueue *queue,
125 g_return_if_fail (queue != NULL);
126 g_return_if_fail (link != NULL);
127 g_return_if_fail (link->prev == NULL);
128 g_return_if_fail (link->next == NULL);
130 link->next = queue->head;
132 queue->head->prev = link;
142 * @data: the data for the new element.
144 * Adds a new element at the tail of the queue.
147 g_queue_push_tail (GQueue *queue,
150 g_return_if_fail (queue != NULL);
152 queue->tail = g_list_append (queue->tail, data);
153 if (queue->tail->next)
154 queue->tail = queue->tail->next;
156 queue->head = queue->tail;
161 * g_queue_push_tail_link:
163 * @link: a single #GList element, <emphasis>not</emphasis> a list with
164 * more than one element.
166 * Adds a new element at the tail of the queue.
169 g_queue_push_tail_link (GQueue *queue,
172 g_return_if_fail (queue != NULL);
173 g_return_if_fail (link != NULL);
174 g_return_if_fail (link->prev == NULL);
175 g_return_if_fail (link->next == NULL);
177 link->prev = queue->tail;
179 queue->tail->next = link;
190 * Removes the first element of the queue.
192 * Returns: the data of the first element in the queue, or %NULL if the queue
196 g_queue_pop_head (GQueue *queue)
198 g_return_val_if_fail (queue != NULL, NULL);
202 GList *node = queue->head;
203 gpointer data = node->data;
205 queue->head = node->next;
207 queue->head->prev = NULL;
210 g_list_free_1 (node);
220 * g_queue_pop_head_link:
223 * Removes the first element of the queue.
225 * Returns: the #GList element at the head of the queue, or %NULL if the queue
229 g_queue_pop_head_link (GQueue *queue)
231 g_return_val_if_fail (queue != NULL, NULL);
235 GList *node = queue->head;
237 queue->head = node->next;
240 queue->head->prev = NULL;
257 * Removes the last element of the queue.
259 * Returns: the data of the last element in the queue, or %NULL if the queue
263 g_queue_pop_tail (GQueue *queue)
265 g_return_val_if_fail (queue != NULL, NULL);
269 GList *node = queue->tail;
270 gpointer data = node->data;
272 queue->tail = node->prev;
274 queue->tail->next = NULL;
278 g_list_free_1 (node);
287 * g_queue_pop_tail_link:
290 * Removes the last element of the queue.
292 * Returns: the #GList element at the tail of the queue, or %NULL if the queue
296 g_queue_pop_tail_link (GQueue *queue)
298 g_return_val_if_fail (queue != NULL, NULL);
302 GList *node = queue->tail;
304 queue->tail = node->prev;
307 queue->tail->next = NULL;
324 * Returns %TRUE if the queue is empty.
326 * Returns: %TRUE if the queue is empty.
329 g_queue_is_empty (GQueue *queue)
331 g_return_val_if_fail (queue != NULL, TRUE);
333 return queue->head == NULL;
340 * Returns the first element of the queue.
342 * Returns: the data of the first element in the queue, or %NULL if the queue
346 g_queue_peek_head (GQueue *queue)
348 g_return_val_if_fail (queue != NULL, NULL);
350 return queue->head ? queue->head->data : NULL;
357 * Returns the last element of the queue.
359 * Returns: the data of the last element in the queue, or %NULL if the queue
363 g_queue_peek_tail (GQueue *queue)
365 g_return_val_if_fail (queue != NULL, NULL);
367 return queue->tail ? queue->tail->data : NULL;