1999-03-17 16:38:18 +01:00
|
|
|
/* GLIB - Library of useful routines for C programming
|
1999-07-24 20:50:58 +02:00
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
|
|
|
* GQueue: Double ended queue implementation, piggy backed on GList.
|
|
|
|
* Copyright (C) 1998 Tim Janik
|
1999-03-17 16:38:18 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
/*
|
|
|
|
* MT safe
|
|
|
|
*/
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
#include "glib.h"
|
1999-03-09 20:41:19 +01:00
|
|
|
|
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
G_LOCK_DEFINE_STATIC (queue_memchunk);
|
|
|
|
static GMemChunk *queue_memchunk = NULL;
|
|
|
|
static GTrashStack *free_queue_nodes = NULL;
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
GQueue*
|
2000-04-17 12:31:10 +02:00
|
|
|
g_queue_new (void)
|
1999-07-24 20:50:58 +02:00
|
|
|
{
|
|
|
|
GQueue *queue;
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
G_LOCK (queue_memchunk);
|
|
|
|
queue = g_trash_stack_pop (&free_queue_nodes);
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
if (!queue)
|
|
|
|
{
|
|
|
|
if (!queue_memchunk)
|
|
|
|
queue_memchunk = g_mem_chunk_new ("GLib GQueue chunk",
|
|
|
|
sizeof (GNode),
|
|
|
|
sizeof (GNode) * 128,
|
|
|
|
G_ALLOC_ONLY);
|
|
|
|
queue = g_chunk_new (GQueue, queue_memchunk);
|
|
|
|
}
|
|
|
|
G_UNLOCK (queue_memchunk);
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
queue->head = NULL;
|
|
|
|
queue->tail = NULL;
|
|
|
|
queue->length = 0;
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
return queue;
|
1999-03-09 20:41:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-07-24 20:50:58 +02:00
|
|
|
g_queue_free (GQueue *queue)
|
1999-03-09 20:41:19 +01:00
|
|
|
{
|
1999-07-24 20:50:58 +02:00
|
|
|
g_return_if_fail (queue != NULL);
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
g_list_free (queue->head);
|
1999-03-09 20:41:19 +01:00
|
|
|
|
Add configure test for garbage collector friendliness for GLib. If
2000-04-17 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* configure.in, acconfig.h: Add configure test for garbage
collector friendliness for GLib. If enabled, ENABLE_GC_FRIENDLY
will be defined.
* garray.c, ghash.c, glist.c, gmain.c, gmem.c, gnode.c, gqueue.c,
gslist.c, gtree.c: If ENABLE_GC_FRIENDLY is defined, NULLify all
memory released by the user, but cached by GLib. This lets a
garbage collector have a more correct view of the actually used
memory.
2000-04-17 15:23:27 +02:00
|
|
|
#ifdef ENABLE_GC_FRIENDLY
|
|
|
|
queue->head = NULL;
|
|
|
|
queue->tail = NULL;
|
|
|
|
#endif /* ENABLE_GC_FRIENDLY */
|
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
G_LOCK (queue_memchunk);
|
|
|
|
g_trash_stack_push (&free_queue_nodes, queue);
|
|
|
|
G_UNLOCK (queue_memchunk);
|
1999-03-09 20:41:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-07-24 20:50:58 +02:00
|
|
|
g_queue_push_head (GQueue *queue,
|
|
|
|
gpointer data)
|
1999-03-09 20:41:19 +01:00
|
|
|
{
|
1999-07-24 20:50:58 +02:00
|
|
|
g_return_if_fail (queue != NULL);
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
queue->head = g_list_prepend (queue->head, data);
|
|
|
|
if (!queue->tail)
|
|
|
|
queue->tail = queue->head;
|
|
|
|
queue->length++;
|
|
|
|
}
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
void
|
|
|
|
g_queue_push_head_link (GQueue *queue,
|
|
|
|
GList *link)
|
|
|
|
{
|
|
|
|
g_return_if_fail (queue != NULL);
|
|
|
|
g_return_if_fail (link != NULL);
|
2000-03-13 17:49:40 +01:00
|
|
|
g_return_if_fail (link->prev == NULL);
|
|
|
|
g_return_if_fail (link->next == NULL);
|
1999-07-24 20:50:58 +02:00
|
|
|
|
|
|
|
link->next = queue->head;
|
|
|
|
if (queue->head)
|
|
|
|
queue->head->prev = link;
|
|
|
|
else
|
|
|
|
queue->tail = link;
|
|
|
|
queue->head = link;
|
|
|
|
queue->length++;
|
1999-03-09 20:41:19 +01:00
|
|
|
}
|
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
void
|
|
|
|
g_queue_push_tail (GQueue *queue,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
g_return_if_fail (queue != NULL);
|
|
|
|
|
|
|
|
queue->tail = g_list_append (queue->tail, data);
|
|
|
|
if (queue->tail->next)
|
|
|
|
queue->tail = queue->tail->next;
|
|
|
|
else
|
|
|
|
queue->head = queue->tail;
|
|
|
|
queue->length++;
|
|
|
|
}
|
1999-03-09 20:41:19 +01:00
|
|
|
|
|
|
|
void
|
1999-07-24 20:50:58 +02:00
|
|
|
g_queue_push_tail_link (GQueue *queue,
|
|
|
|
GList *link)
|
1999-03-09 20:41:19 +01:00
|
|
|
{
|
1999-07-24 20:50:58 +02:00
|
|
|
g_return_if_fail (queue != NULL);
|
|
|
|
g_return_if_fail (link != NULL);
|
2000-03-13 17:49:40 +01:00
|
|
|
g_return_if_fail (link->prev == NULL);
|
|
|
|
g_return_if_fail (link->next == NULL);
|
1999-07-24 20:50:58 +02:00
|
|
|
|
|
|
|
link->prev = queue->tail;
|
|
|
|
if (queue->tail)
|
|
|
|
queue->tail->next = link;
|
|
|
|
else
|
|
|
|
queue->head = link;
|
|
|
|
queue->tail = link;
|
|
|
|
queue->length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
|
|
|
g_queue_pop_head (GQueue *queue)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (queue != NULL, NULL);
|
|
|
|
|
|
|
|
if (queue->head)
|
1999-03-17 16:38:18 +01:00
|
|
|
{
|
1999-07-24 20:50:58 +02:00
|
|
|
GList *node = queue->head;
|
|
|
|
gpointer data = node->data;
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
queue->head = node->next;
|
|
|
|
if (queue->head)
|
|
|
|
queue->head->prev = NULL;
|
1999-03-17 16:38:18 +01:00
|
|
|
else
|
1999-07-24 20:50:58 +02:00
|
|
|
queue->tail = NULL;
|
|
|
|
g_list_free_1 (node);
|
|
|
|
queue->length--;
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
return data;
|
1999-03-17 16:38:18 +01:00
|
|
|
}
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
GList*
|
|
|
|
g_queue_pop_head_link (GQueue *queue)
|
1999-03-09 20:41:19 +01:00
|
|
|
{
|
1999-07-24 20:50:58 +02:00
|
|
|
g_return_val_if_fail (queue != NULL, NULL);
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
if (queue->head)
|
1999-03-17 16:38:18 +01:00
|
|
|
{
|
1999-07-24 20:50:58 +02:00
|
|
|
GList *node = queue->head;
|
1999-03-17 16:38:18 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
queue->head = node->next;
|
|
|
|
if (queue->head)
|
|
|
|
{
|
|
|
|
queue->head->prev = NULL;
|
|
|
|
node->next = NULL;
|
|
|
|
}
|
1999-03-17 16:38:18 +01:00
|
|
|
else
|
1999-07-24 20:50:58 +02:00
|
|
|
queue->tail = NULL;
|
|
|
|
queue->length--;
|
1999-03-17 16:38:18 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
return node;
|
1999-03-09 20:41:19 +01:00
|
|
|
}
|
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
return NULL;
|
1999-03-09 20:41:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
1999-07-24 20:50:58 +02:00
|
|
|
g_queue_pop_tail (GQueue *queue)
|
1999-03-09 20:41:19 +01:00
|
|
|
{
|
1999-07-24 20:50:58 +02:00
|
|
|
g_return_val_if_fail (queue != NULL, NULL);
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
if (queue->tail)
|
1999-03-17 16:38:18 +01:00
|
|
|
{
|
1999-07-24 20:50:58 +02:00
|
|
|
GList *node = queue->tail;
|
|
|
|
gpointer data = node->data;
|
|
|
|
|
|
|
|
queue->tail = node->prev;
|
|
|
|
if (queue->tail)
|
|
|
|
queue->tail->next = NULL;
|
|
|
|
else
|
|
|
|
queue->head = NULL;
|
|
|
|
queue->length--;
|
|
|
|
g_list_free_1 (node);
|
1999-03-17 16:38:18 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-03-17 16:38:18 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
GList*
|
|
|
|
g_queue_pop_tail_link (GQueue *queue)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (queue != NULL, NULL);
|
|
|
|
|
|
|
|
if (queue->tail)
|
|
|
|
{
|
|
|
|
GList *node = queue->tail;
|
|
|
|
|
|
|
|
queue->tail = node->prev;
|
|
|
|
if (queue->tail)
|
1999-03-17 16:38:18 +01:00
|
|
|
{
|
1999-07-24 20:50:58 +02:00
|
|
|
queue->tail->next = NULL;
|
|
|
|
node->prev = NULL;
|
|
|
|
}
|
1999-03-17 16:38:18 +01:00
|
|
|
else
|
1999-07-24 20:50:58 +02:00
|
|
|
queue->head = NULL;
|
|
|
|
queue->length--;
|
|
|
|
|
|
|
|
return node;
|
1999-03-09 20:41:19 +01:00
|
|
|
}
|
1999-07-24 20:50:58 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
gboolean
|
|
|
|
g_queue_is_empty (GQueue *queue)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (queue != NULL, TRUE);
|
|
|
|
|
|
|
|
return queue->head == NULL;
|
1999-03-09 20:41:19 +01:00
|
|
|
}
|
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
gpointer
|
|
|
|
g_queue_peek_head (GQueue *queue)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (queue != NULL, NULL);
|
1999-03-09 20:41:19 +01:00
|
|
|
|
1999-07-24 20:50:58 +02:00
|
|
|
return queue->head ? queue->head->data : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
|
|
|
g_queue_peek_tail (GQueue *queue)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (queue != NULL, NULL);
|
|
|
|
|
|
|
|
return queue->tail ? queue->tail->data : NULL;
|
|
|
|
}
|