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 Library 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 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library 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.
30 G_LOCK_DEFINE_STATIC (queue_memchunk);
31 static GMemChunk *queue_memchunk = NULL;
32 static GTrashStack *free_queue_nodes = NULL;
39 G_LOCK (queue_memchunk);
40 queue = g_trash_stack_pop (&free_queue_nodes);
45 queue_memchunk = g_mem_chunk_new ("GLib GQueue chunk",
49 queue = g_chunk_new (GQueue, queue_memchunk);
51 G_UNLOCK (queue_memchunk);
61 g_queue_free (GQueue *queue)
63 g_return_if_fail (queue != NULL);
65 g_list_free (queue->head);
67 G_LOCK (queue_memchunk);
68 g_trash_stack_push (&free_queue_nodes, queue);
69 G_UNLOCK (queue_memchunk);
73 g_queue_push_head (GQueue *queue,
76 g_return_if_fail (queue != NULL);
78 queue->head = g_list_prepend (queue->head, data);
80 queue->tail = queue->head;
85 g_queue_push_head_link (GQueue *queue,
88 g_return_if_fail (queue != NULL);
89 g_return_if_fail (link != NULL);
90 g_return_if_fail (link->prev != NULL);
91 g_return_if_fail (link->next != NULL);
93 link->next = queue->head;
95 queue->head->prev = link;
103 g_queue_push_tail (GQueue *queue,
106 g_return_if_fail (queue != NULL);
108 queue->tail = g_list_append (queue->tail, data);
109 if (queue->tail->next)
110 queue->tail = queue->tail->next;
112 queue->head = queue->tail;
117 g_queue_push_tail_link (GQueue *queue,
120 g_return_if_fail (queue != NULL);
121 g_return_if_fail (link != NULL);
122 g_return_if_fail (link->prev != NULL);
123 g_return_if_fail (link->next != NULL);
125 link->prev = queue->tail;
127 queue->tail->next = link;
135 g_queue_pop_head (GQueue *queue)
137 g_return_val_if_fail (queue != NULL, NULL);
141 GList *node = queue->head;
142 gpointer data = node->data;
144 queue->head = node->next;
146 queue->head->prev = NULL;
149 g_list_free_1 (node);
159 g_queue_pop_head_link (GQueue *queue)
161 g_return_val_if_fail (queue != NULL, NULL);
165 GList *node = queue->head;
167 queue->head = node->next;
170 queue->head->prev = NULL;
184 g_queue_pop_tail (GQueue *queue)
186 g_return_val_if_fail (queue != NULL, NULL);
190 GList *node = queue->tail;
191 gpointer data = node->data;
193 queue->tail = node->prev;
195 queue->tail->next = NULL;
199 g_list_free_1 (node);
208 g_queue_pop_tail_link (GQueue *queue)
210 g_return_val_if_fail (queue != NULL, NULL);
214 GList *node = queue->tail;
216 queue->tail = node->prev;
219 queue->tail->next = NULL;
233 g_queue_is_empty (GQueue *queue)
235 g_return_val_if_fail (queue != NULL, TRUE);
237 return queue->head == NULL;
241 g_queue_peek_head (GQueue *queue)
243 g_return_val_if_fail (queue != NULL, NULL);
245 return queue->head ? queue->head->data : NULL;
249 g_queue_peek_tail (GQueue *queue)
251 g_return_val_if_fail (queue != NULL, NULL);
253 return queue->tail ? queue->tail->data : NULL;