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 #ifdef ENABLE_GC_FRIENDLY
70 #endif /* ENABLE_GC_FRIENDLY */
72 G_LOCK (queue_memchunk);
73 g_trash_stack_push (&free_queue_nodes, queue);
74 G_UNLOCK (queue_memchunk);
78 g_queue_push_head (GQueue *queue,
81 g_return_if_fail (queue != NULL);
83 queue->head = g_list_prepend (queue->head, data);
85 queue->tail = queue->head;
90 g_queue_push_head_link (GQueue *queue,
93 g_return_if_fail (queue != NULL);
94 g_return_if_fail (link != NULL);
95 g_return_if_fail (link->prev == NULL);
96 g_return_if_fail (link->next == NULL);
98 link->next = queue->head;
100 queue->head->prev = link;
108 g_queue_push_tail (GQueue *queue,
111 g_return_if_fail (queue != NULL);
113 queue->tail = g_list_append (queue->tail, data);
114 if (queue->tail->next)
115 queue->tail = queue->tail->next;
117 queue->head = queue->tail;
122 g_queue_push_tail_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->prev = queue->tail;
132 queue->tail->next = link;
140 g_queue_pop_head (GQueue *queue)
142 g_return_val_if_fail (queue != NULL, NULL);
146 GList *node = queue->head;
147 gpointer data = node->data;
149 queue->head = node->next;
151 queue->head->prev = NULL;
154 g_list_free_1 (node);
164 g_queue_pop_head_link (GQueue *queue)
166 g_return_val_if_fail (queue != NULL, NULL);
170 GList *node = queue->head;
172 queue->head = node->next;
175 queue->head->prev = NULL;
189 g_queue_pop_tail (GQueue *queue)
191 g_return_val_if_fail (queue != NULL, NULL);
195 GList *node = queue->tail;
196 gpointer data = node->data;
198 queue->tail = node->prev;
200 queue->tail->next = NULL;
204 g_list_free_1 (node);
213 g_queue_pop_tail_link (GQueue *queue)
215 g_return_val_if_fail (queue != NULL, NULL);
219 GList *node = queue->tail;
221 queue->tail = node->prev;
224 queue->tail->next = NULL;
238 g_queue_is_empty (GQueue *queue)
240 g_return_val_if_fail (queue != NULL, TRUE);
242 return queue->head == NULL;
246 g_queue_peek_head (GQueue *queue)
248 g_return_val_if_fail (queue != NULL, NULL);
250 return queue->head ? queue->head->data : NULL;
254 g_queue_peek_tail (GQueue *queue)
256 g_return_val_if_fail (queue != NULL, NULL);
258 return queue->tail ? queue->tail->data : NULL;