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;
43 G_LOCK (queue_memchunk);
44 queue = g_trash_stack_pop (&free_queue_nodes);
49 queue_memchunk = g_mem_chunk_new ("GLib GQueue chunk",
53 queue = g_chunk_new (GQueue, queue_memchunk);
55 G_UNLOCK (queue_memchunk);
65 g_queue_free (GQueue *queue)
67 g_return_if_fail (queue != NULL);
69 g_list_free (queue->head);
71 #ifdef ENABLE_GC_FRIENDLY
74 #endif /* ENABLE_GC_FRIENDLY */
76 G_LOCK (queue_memchunk);
77 g_trash_stack_push (&free_queue_nodes, queue);
78 G_UNLOCK (queue_memchunk);
82 g_queue_push_head (GQueue *queue,
85 g_return_if_fail (queue != NULL);
87 queue->head = g_list_prepend (queue->head, data);
89 queue->tail = queue->head;
94 g_queue_push_head_link (GQueue *queue,
97 g_return_if_fail (queue != NULL);
98 g_return_if_fail (link != NULL);
99 g_return_if_fail (link->prev == NULL);
100 g_return_if_fail (link->next == NULL);
102 link->next = queue->head;
104 queue->head->prev = link;
112 g_queue_push_tail (GQueue *queue,
115 g_return_if_fail (queue != NULL);
117 queue->tail = g_list_append (queue->tail, data);
118 if (queue->tail->next)
119 queue->tail = queue->tail->next;
121 queue->head = queue->tail;
126 g_queue_push_tail_link (GQueue *queue,
129 g_return_if_fail (queue != NULL);
130 g_return_if_fail (link != NULL);
131 g_return_if_fail (link->prev == NULL);
132 g_return_if_fail (link->next == NULL);
134 link->prev = queue->tail;
136 queue->tail->next = link;
144 g_queue_pop_head (GQueue *queue)
146 g_return_val_if_fail (queue != NULL, NULL);
150 GList *node = queue->head;
151 gpointer data = node->data;
153 queue->head = node->next;
155 queue->head->prev = NULL;
158 g_list_free_1 (node);
168 g_queue_pop_head_link (GQueue *queue)
170 g_return_val_if_fail (queue != NULL, NULL);
174 GList *node = queue->head;
176 queue->head = node->next;
179 queue->head->prev = NULL;
193 g_queue_pop_tail (GQueue *queue)
195 g_return_val_if_fail (queue != NULL, NULL);
199 GList *node = queue->tail;
200 gpointer data = node->data;
202 queue->tail = node->prev;
204 queue->tail->next = NULL;
208 g_list_free_1 (node);
217 g_queue_pop_tail_link (GQueue *queue)
219 g_return_val_if_fail (queue != NULL, NULL);
223 GList *node = queue->tail;
225 queue->tail = node->prev;
228 queue->tail->next = NULL;
242 g_queue_is_empty (GQueue *queue)
244 g_return_val_if_fail (queue != NULL, TRUE);
246 return queue->head == NULL;
250 g_queue_peek_head (GQueue *queue)
252 g_return_val_if_fail (queue != NULL, NULL);
254 return queue->head ? queue->head->data : NULL;
258 g_queue_peek_tail (GQueue *queue)
260 g_return_val_if_fail (queue != NULL, NULL);
262 return queue->tail ? queue->tail->data : NULL;