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.
32 G_LOCK_DEFINE_STATIC (queue_memchunk
);
33 static GMemChunk
*queue_memchunk
= NULL
;
34 static GTrashStack
*free_queue_nodes
= NULL
;
39 * Creates a new #GQueue.
41 * Returns: a new #GQueue.
48 G_LOCK (queue_memchunk
);
49 queue
= g_trash_stack_pop (&free_queue_nodes
);
54 queue_memchunk
= g_mem_chunk_new ("GLib GQueue chunk",
58 queue
= g_chunk_new (GQueue
, queue_memchunk
);
60 G_UNLOCK (queue_memchunk
);
73 * Frees the memory allocated for the #GQueue.
76 g_queue_free (GQueue
*queue
)
78 g_return_if_fail (queue
!= NULL
);
80 g_list_free (queue
->head
);
82 #ifdef ENABLE_GC_FRIENDLY
85 #endif /* ENABLE_GC_FRIENDLY */
87 G_LOCK (queue_memchunk
);
88 g_trash_stack_push (&free_queue_nodes
, queue
);
89 G_UNLOCK (queue_memchunk
);
95 * @data: the data for the new element.
97 * Adds a new element at the head of the queue.
100 g_queue_push_head (GQueue
*queue
,
103 g_return_if_fail (queue
!= NULL
);
105 queue
->head
= g_list_prepend (queue
->head
, data
);
107 queue
->tail
= queue
->head
;
112 * g_queue_push_head_link:
114 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
115 * more than one element.
117 * Adds a new element at the head of the queue.
120 g_queue_push_head_link (GQueue
*queue
,
123 g_return_if_fail (queue
!= NULL
);
124 g_return_if_fail (link
!= NULL
);
125 g_return_if_fail (link
->prev
== NULL
);
126 g_return_if_fail (link
->next
== NULL
);
128 link
->next
= queue
->head
;
130 queue
->head
->prev
= link
;
140 * @data: the data for the new element.
142 * Adds a new element at the tail of the queue.
145 g_queue_push_tail (GQueue
*queue
,
148 g_return_if_fail (queue
!= NULL
);
150 queue
->tail
= g_list_append (queue
->tail
, data
);
151 if (queue
->tail
->next
)
152 queue
->tail
= queue
->tail
->next
;
154 queue
->head
= queue
->tail
;
159 * g_queue_push_tail_link:
161 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
162 * more than one element.
164 * Adds a new element at the tail of the queue.
167 g_queue_push_tail_link (GQueue
*queue
,
170 g_return_if_fail (queue
!= NULL
);
171 g_return_if_fail (link
!= NULL
);
172 g_return_if_fail (link
->prev
== NULL
);
173 g_return_if_fail (link
->next
== NULL
);
175 link
->prev
= queue
->tail
;
177 queue
->tail
->next
= link
;
188 * Removes the first element of the queue.
190 * Returns: the data of the first element in the queue, or %NULL if the queue
194 g_queue_pop_head (GQueue
*queue
)
196 g_return_val_if_fail (queue
!= NULL
, NULL
);
200 GList
*node
= queue
->head
;
201 gpointer data
= node
->data
;
203 queue
->head
= node
->next
;
205 queue
->head
->prev
= NULL
;
208 g_list_free_1 (node
);
218 * g_queue_pop_head_link:
221 * Removes the first element of the queue.
223 * Returns: the #GList element at the head of the queue, or %NULL if the queue
227 g_queue_pop_head_link (GQueue
*queue
)
229 g_return_val_if_fail (queue
!= NULL
, NULL
);
233 GList
*node
= queue
->head
;
235 queue
->head
= node
->next
;
238 queue
->head
->prev
= NULL
;
255 * Removes the last element of the queue.
257 * Returns: the data of the last element in the queue, or %NULL if the queue
261 g_queue_pop_tail (GQueue
*queue
)
263 g_return_val_if_fail (queue
!= NULL
, NULL
);
267 GList
*node
= queue
->tail
;
268 gpointer data
= node
->data
;
270 queue
->tail
= node
->prev
;
272 queue
->tail
->next
= NULL
;
276 g_list_free_1 (node
);
285 * g_queue_pop_tail_link:
288 * Removes the last element of the queue.
290 * Returns: the #GList element at the tail of the queue, or %NULL if the queue
294 g_queue_pop_tail_link (GQueue
*queue
)
296 g_return_val_if_fail (queue
!= NULL
, NULL
);
300 GList
*node
= queue
->tail
;
302 queue
->tail
= node
->prev
;
305 queue
->tail
->next
= NULL
;
322 * Returns %TRUE if the queue is empty.
324 * Returns: %TRUE if the queue is empty.
327 g_queue_is_empty (GQueue
*queue
)
329 g_return_val_if_fail (queue
!= NULL
, TRUE
);
331 return queue
->head
== NULL
;
338 * Returns the first element of the queue.
340 * Returns: the data of the first element in the queue, or %NULL if the queue
344 g_queue_peek_head (GQueue
*queue
)
346 g_return_val_if_fail (queue
!= NULL
, NULL
);
348 return queue
->head
? queue
->head
->data
: NULL
;
355 * Returns the last element of the queue.
357 * Returns: the data of the last element in the queue, or %NULL if the queue
361 g_queue_peek_tail (GQueue
*queue
)
363 g_return_val_if_fail (queue
!= NULL
, NULL
);
365 return queue
->tail
? queue
->tail
->data
: NULL
;