Imported Upstream version 2.72.3
[platform/upstream/glib.git] / glib / gqueue.c
index 0351942..d53af7d 100644 (file)
@@ -7,7 +7,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser 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.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
  * MT safe
  */
 
+/**
+ * SECTION:queue
+ * @Title: Double-ended Queues
+ * @Short_description: double-ended queue data structure
+ *
+ * The #GQueue structure and its associated functions provide a standard
+ * queue data structure. Internally, GQueue uses the same data structure
+ * as #GList to store elements with the same complexity over
+ * insertion/deletion (O(1)) and access/search (O(n)) operations.
+ *
+ * The data contained in each element can be either integer values, by
+ * using one of the [Type Conversion Macros][glib-Type-Conversion-Macros],
+ * or simply pointers to any type of data.
+ *
+ * As with all other GLib data structures, #GQueue is not thread-safe.
+ * For a thread-safe queue, use #GAsyncQueue.
+ *
+ * To create a new GQueue, use g_queue_new().
+ *
+ * To initialize a statically-allocated GQueue, use %G_QUEUE_INIT or
+ * g_queue_init().
+ *
+ * To add elements, use g_queue_push_head(), g_queue_push_head_link(),
+ * g_queue_push_tail() and g_queue_push_tail_link().
+ *
+ * To remove elements, use g_queue_pop_head() and g_queue_pop_tail().
+ *
+ * To free the entire queue, use g_queue_free().
+ */
 #include "config.h"
 
-#include "glib.h"
-#include "galias.h"
-
+#include "gqueue.h"
 
-G_LOCK_DEFINE_STATIC (queue_memchunk);
-static GMemChunk   *queue_memchunk = NULL;
-static GTrashStack *free_queue_nodes = NULL;
+#include "gtestutils.h"
+#include "gslice.h"
 
 /**
  * g_queue_new:
  *
- * Creates a new #GQueue. 
+ * Creates a new #GQueue.
  *
- * Returns: a new #GQueue.
+ * Returns: a newly allocated #GQueue
  **/
-GQueue*
+GQueue *
 g_queue_new (void)
 {
-  GQueue *queue;
+  return g_slice_new0 (GQueue);
+}
 
-  G_LOCK (queue_memchunk);
-  queue = g_trash_stack_pop (&free_queue_nodes);
+/**
+ * g_queue_free:
+ * @queue: a #GQueue
+ *
+ * Frees the memory allocated for the #GQueue. Only call this function
+ * if @queue was created with g_queue_new(). If queue elements contain
+ * dynamically-allocated memory, they should be freed first.
+ *
+ * If queue elements contain dynamically-allocated memory, you should
+ * either use g_queue_free_full() or free them manually first.
+ **/
+void
+g_queue_free (GQueue *queue)
+{
+  g_return_if_fail (queue != NULL);
 
-  if (!queue)
-    {
-      if (!queue_memchunk)
-       queue_memchunk = g_mem_chunk_new ("GLib GQueue chunk",
-                                         sizeof (GQueue),
-                                         sizeof (GQueue) * 128,
-                                         G_ALLOC_ONLY);
-      queue = g_chunk_new (GQueue, queue_memchunk);
-    }
-  G_UNLOCK (queue_memchunk);
+  g_list_free (queue->head);
+  g_slice_free (GQueue, queue);
+}
 
-  queue->head = NULL;
-  queue->tail = NULL;
-  queue->length = 0;
+/**
+ * g_queue_free_full:
+ * @queue: a pointer to a #GQueue
+ * @free_func: the function to be called to free each element's data
+ *
+ * Convenience method, which frees all the memory used by a #GQueue,
+ * and calls the specified destroy function on every element's data.
+ *
+ * @free_func should not modify the queue (eg, by removing the freed
+ * element from it).
+ *
+ * Since: 2.32
+ */
+void
+g_queue_free_full (GQueue        *queue,
+                  GDestroyNotify  free_func)
+{
+  g_queue_foreach (queue, (GFunc) free_func, NULL);
+  g_queue_free (queue);
+}
+
+/**
+ * g_queue_init:
+ * @queue: an uninitialized #GQueue
+ *
+ * A statically-allocated #GQueue must be initialized with this function
+ * before it can be used. Alternatively you can initialize it with
+ * %G_QUEUE_INIT. It is not necessary to initialize queues created with
+ * g_queue_new().
+ *
+ * Since: 2.14
+ */
+void
+g_queue_init (GQueue *queue)
+{
+  g_return_if_fail (queue != NULL);
 
-  return queue;
+  queue->head = queue->tail = NULL;
+  queue->length = 0;
 }
 
 /**
- * g_queue_free:
- * @queue: a #GQueue.
+ * g_queue_clear:
+ * @queue: a #GQueue
  *
- * Frees the memory allocated for the #GQueue.
- **/
+ * Removes all the elements in @queue. If queue elements contain
+ * dynamically-allocated memory, they should be freed first.
+ *
+ * Since: 2.14
+ */
 void
-g_queue_free (GQueue *queue)
+g_queue_clear (GQueue *queue)
 {
   g_return_if_fail (queue != NULL);
 
   g_list_free (queue->head);
+  g_queue_init (queue);
+}
 
-#ifdef ENABLE_GC_FRIENDLY
-  queue->head = NULL;
-  queue->tail = NULL;
-#endif /* ENABLE_GC_FRIENDLY */
+/**
+ * g_queue_clear_full:
+ * @queue: a pointer to a #GQueue
+ * @free_func: (nullable): the function to be called to free memory allocated
+ *
+ * Convenience method, which frees all the memory used by a #GQueue,
+ * and calls the provided @free_func on each item in the #GQueue.
+ *
+ * Since: 2.60
+ */
+void
+g_queue_clear_full (GQueue          *queue,
+                    GDestroyNotify  free_func)
+{
+  g_return_if_fail (queue != NULL);
 
-  G_LOCK (queue_memchunk);
-  g_trash_stack_push (&free_queue_nodes, queue);
-  G_UNLOCK (queue_memchunk);
+  if (free_func != NULL)
+    g_queue_foreach (queue, (GFunc) free_func, NULL);
+
+  g_queue_clear (queue);
 }
 
 /**
@@ -96,8 +178,8 @@ g_queue_free (GQueue *queue)
  *
  * Returns %TRUE if the queue is empty.
  *
- * Returns: %TRUE if the queue is empty.
- **/
+ * Returns: %TRUE if the queue is empty
+ */
 gboolean
 g_queue_is_empty (GQueue *queue)
 {
@@ -112,10 +194,10 @@ g_queue_is_empty (GQueue *queue)
  * 
  * Returns the number of items in @queue.
  * 
- * Return value: The number of items in @queue.
+ * Returns: the number of items in @queue
  * 
  * Since: 2.4
- **/
+ */
 guint
 g_queue_get_length (GQueue *queue)
 {
@@ -131,7 +213,7 @@ g_queue_get_length (GQueue *queue)
  * Reverses the order of the items in @queue.
  * 
  * Since: 2.4
- **/
+ */
 void
 g_queue_reverse (GQueue *queue)
 {
@@ -149,10 +231,10 @@ g_queue_reverse (GQueue *queue)
  * queue consist of pointers to data, the pointers are copied, but the
  * actual data is not.
  * 
- * Return value: A copy of @queue
+ * Returns: a copy of @queue
  * 
  * Since: 2.4
- **/
+ */
 GQueue *
 g_queue_copy (GQueue *queue)
 {
@@ -178,12 +260,15 @@ g_queue_copy (GQueue *queue)
  * Calls @func for each element in the queue passing @user_data to the
  * function.
  * 
+ * It is safe for @func to remove the element from @queue, but it must
+ * not modify any part of the queue after that element.
+ *
  * Since: 2.4
- **/
+ */
 void
 g_queue_foreach (GQueue   *queue,
-                GFunc     func,
-                gpointer  user_data)
+                 GFunc     func,
+                 gpointer  user_data)
 {
   GList *list;
 
@@ -206,13 +291,13 @@ g_queue_foreach (GQueue   *queue,
  * 
  * Finds the first link in @queue which contains @data.
  * 
- * Return value: The first link in @queue which contains @data.
+ * Returns: the first link in @queue which contains @data
  * 
  * Since: 2.4
- **/
+ */
 GList *
 g_queue_find (GQueue        *queue,
-             gconstpointer  data)
+              gconstpointer  data)
 {
   g_return_val_if_fail (queue != NULL, NULL);
 
@@ -224,7 +309,7 @@ g_queue_find (GQueue        *queue,
  * @queue: a #GQueue
  * @data: user data passed to @func
  * @func: a #GCompareFunc to call for each element. It should return 0
- * when the desired element is found
+ *     when the desired element is found
  *
  * Finds an element in a #GQueue, using a supplied function to find the
  * desired element. It iterates over the queue, calling the given function
@@ -232,14 +317,14 @@ g_queue_find (GQueue        *queue,
  * takes two gconstpointer arguments, the #GQueue element's data as the
  * first argument and the given user data as the second argument.
  * 
- * Return value: The found link, or %NULL if it wasn't found
+ * Returns: the found link, or %NULL if it wasn't found
  * 
  * Since: 2.4
- **/
+ */
 GList *
-g_queue_find_custom    (GQueue        *queue,
-                       gconstpointer  data,
-                       GCompareFunc   func)
+g_queue_find_custom (GQueue        *queue,
+                     gconstpointer  data,
+                     GCompareFunc   func)
 {
   g_return_val_if_fail (queue != NULL, NULL);
   g_return_val_if_fail (func != NULL, NULL);
@@ -259,11 +344,11 @@ g_queue_find_custom    (GQueue        *queue,
  * Sorts @queue using @compare_func. 
  * 
  * Since: 2.4
- **/
+ */
 void
 g_queue_sort (GQueue           *queue,
-             GCompareDataFunc  compare_func,
-             gpointer          user_data)
+              GCompareDataFunc  compare_func,
+              gpointer          user_data)
 {
   g_return_if_fail (queue != NULL);
   g_return_if_fail (compare_func != NULL);
@@ -278,10 +363,10 @@ g_queue_sort (GQueue           *queue,
  * @data: the data for the new element.
  *
  * Adds a new element at the head of the queue.
- **/
+ */
 void
-g_queue_push_head (GQueue  *queue,
-                  gpointer data)
+g_queue_push_head (GQueue   *queue,
+                   gpointer  data)
 {
   g_return_if_fail (queue != NULL);
 
@@ -299,18 +384,18 @@ g_queue_push_head (GQueue  *queue,
  *     larger than the number of elements in the @queue, the element is
  *     added to the end of the queue.
  * 
- * Inserts a new element into @queue at the given position
+ * Inserts a new element into @queue at the given position.
  * 
  * Since: 2.4
- **/
+ */
 void
 g_queue_push_nth (GQueue   *queue,
-                 gpointer  data,
-                 gint      n)
+                  gpointer  data,
+                  gint      n)
 {
   g_return_if_fail (queue != NULL);
 
-  if (n < 0 || n >= queue->length)
+  if (n < 0 || (guint) n >= queue->length)
     {
       g_queue_push_tail (queue, data);
       return;
@@ -321,15 +406,14 @@ g_queue_push_nth (GQueue   *queue,
 
 /**
  * g_queue_push_head_link:
- * @queue: a #GQueue.
- * @link_: a single #GList element, <emphasis>not</emphasis> a list with
- *     more than one element.
+ * @queue: a #GQueue
+ * @link_: a single #GList element, not a list with more than one element
  *
  * Adds a new element at the head of the queue.
- **/
+ */
 void
 g_queue_push_head_link (GQueue *queue,
-                       GList  *link)
+                        GList  *link)
 {
   g_return_if_fail (queue != NULL);
   g_return_if_fail (link != NULL);
@@ -347,14 +431,14 @@ g_queue_push_head_link (GQueue *queue,
 
 /**
  * g_queue_push_tail:
- * @queue: a #GQueue.
- * @data: the data for the new element.
+ * @queue: a #GQueue
+ * @data: the data for the new element
  *
  * Adds a new element at the tail of the queue.
- **/
+ */
 void
-g_queue_push_tail (GQueue  *queue,
-                  gpointer data)
+g_queue_push_tail (GQueue   *queue,
+                   gpointer  data)
 {
   g_return_if_fail (queue != NULL);
 
@@ -368,15 +452,14 @@ g_queue_push_tail (GQueue  *queue,
 
 /**
  * g_queue_push_tail_link:
- * @queue: a #GQueue.
- * @link_: a single #GList element, <emphasis>not</emphasis> a list with
- *   more than one element.
+ * @queue: a #GQueue
+ * @link_: a single #GList element, not a list with more than one element
  *
  * Adds a new element at the tail of the queue.
- **/
+ */
 void
 g_queue_push_tail_link (GQueue *queue,
-                       GList  *link)
+                        GList  *link)
 {
   g_return_if_fail (queue != NULL);
   g_return_if_fail (link != NULL);
@@ -403,11 +486,11 @@ g_queue_push_tail_link (GQueue *queue,
  * Inserts @link into @queue at the given position.
  * 
  * Since: 2.4
- **/
+ */
 void
-g_queue_push_nth_link  (GQueue  *queue,
-                       gint     n,
-                       GList   *link_)
+g_queue_push_nth_link (GQueue *queue,
+                       gint    n,
+                       GList  *link_)
 {
   GList *next;
   GList *prev;
@@ -415,7 +498,7 @@ g_queue_push_nth_link  (GQueue  *queue,
   g_return_if_fail (queue != NULL);
   g_return_if_fail (link_ != NULL);
 
-  if (n < 0 || n >= queue->length)
+  if (n < 0 || (guint) n >= queue->length)
     {
       g_queue_push_tail_link (queue, link_);
       return;
@@ -437,21 +520,23 @@ g_queue_push_nth_link  (GQueue  *queue,
   if (queue->head->prev)
     queue->head = queue->head->prev;
 
-  if (queue->tail->next)
-    queue->tail = queue->tail->next;
+  /* The case where we’re pushing @link_ at the end of @queue is handled above
+   * using g_queue_push_tail_link(), so we should never have to manually adjust
+   * queue->tail. */
+  g_assert (queue->tail->next == NULL);
   
   queue->length++;
 }
 
 /**
  * g_queue_pop_head:
- * @queue: a #GQueue.
+ * @queue: a #GQueue
  *
- * Removes the first element of the queue.
+ * Removes the first element of the queue and returns its data.
  *
- * Returns: the data of the first element in the queue, or %NULL if the queue
- *   is empty.
- **/
+ * Returns: the data of the first element in the queue, or %NULL
+ *     if the queue is empty
+ */
 gpointer
 g_queue_pop_head (GQueue *queue)
 {
@@ -464,9 +549,9 @@ g_queue_pop_head (GQueue *queue)
 
       queue->head = node->next;
       if (queue->head)
-       queue->head->prev = NULL;
+        queue->head->prev = NULL;
       else
-       queue->tail = NULL;
+        queue->tail = NULL;
       g_list_free_1 (node);
       queue->length--;
 
@@ -478,14 +563,14 @@ g_queue_pop_head (GQueue *queue)
 
 /**
  * g_queue_pop_head_link:
- * @queue: a #GQueue.
+ * @queue: a #GQueue
  *
- * Removes the first element of the queue.
+ * Removes and returns the first element of the queue.
  *
- * Returns: the #GList element at the head of the queue, or %NULL if the queue
- *   is empty.
- **/
-GList*
+ * Returns: the #GList element at the head of the queue, or %NULL
+ *     if the queue is empty
+ */
+GList *
 g_queue_pop_head_link (GQueue *queue)
 {
   g_return_val_if_fail (queue != NULL, NULL);
@@ -496,12 +581,12 @@ g_queue_pop_head_link (GQueue *queue)
 
       queue->head = node->next;
       if (queue->head)
-       {
-         queue->head->prev = NULL;
-         node->next = NULL;
-       }
+        {
+          queue->head->prev = NULL;
+          node->next = NULL;
+        }
       else
-       queue->tail = NULL;
+        queue->tail = NULL;
       queue->length--;
 
       return node;
@@ -514,13 +599,13 @@ g_queue_pop_head_link (GQueue *queue)
  * g_queue_peek_head_link:
  * @queue: a #GQueue
  * 
- * Returns the first link in @queue
+ * Returns the first link in @queue.
  * 
- * Return value: the first link in @queue, or %NULL if @queue is empty
+ * Returns: the first link in @queue, or %NULL if @queue is empty
  * 
  * Since: 2.4
- **/
-GList*
+ */
+GList *
 g_queue_peek_head_link (GQueue *queue)
 {
   g_return_val_if_fail (queue != NULL, NULL);
@@ -532,13 +617,13 @@ g_queue_peek_head_link (GQueue *queue)
  * g_queue_peek_tail_link:
  * @queue: a #GQueue
  * 
- * Returns the last link @queue.
+ * Returns the last link in @queue.
  * 
- * Return value: the last link in @queue, or %NULL if @queue is empty
+ * Returns: the last link in @queue, or %NULL if @queue is empty
  * 
  * Since: 2.4
- **/
-GList*
+ */
+GList *
 g_queue_peek_tail_link (GQueue *queue)
 {
   g_return_val_if_fail (queue != NULL, NULL);
@@ -548,13 +633,13 @@ g_queue_peek_tail_link (GQueue *queue)
 
 /**
  * g_queue_pop_tail:
- * @queue: a #GQueue.
+ * @queue: a #GQueue
  *
- * Removes the last element of the queue.
+ * Removes the last element of the queue and returns its data.
  *
- * Returns: the data of the last element in the queue, or %NULL if the queue
- *   is empty.
- **/
+ * Returns: the data of the last element in the queue, or %NULL
+ *     if the queue is empty
+ */
 gpointer
 g_queue_pop_tail (GQueue *queue)
 {
@@ -567,9 +652,9 @@ g_queue_pop_tail (GQueue *queue)
 
       queue->tail = node->prev;
       if (queue->tail)
-       queue->tail->next = NULL;
+        queue->tail->next = NULL;
       else
-       queue->head = NULL;
+        queue->head = NULL;
       queue->length--;
       g_list_free_1 (node);
 
@@ -582,17 +667,17 @@ g_queue_pop_tail (GQueue *queue)
 /**
  * g_queue_pop_nth:
  * @queue: a #GQueue
- * @n: the position of the element.
+ * @n: the position of the element
  * 
- * Removes the @n'th element of @queue.
+ * Removes the @n'th element of @queue and returns its data.
  * 
- * Return value: the element's data, or %NULL if @n is off the end of @queue.
+ * Returns: the element's data, or %NULL if @n is off the end of @queue
  * 
  * Since: 2.4
- **/
+ */
 gpointer
 g_queue_pop_nth (GQueue *queue,
-                guint   n)
+                 guint   n)
 {
   GList *nth_link;
   gpointer result;
@@ -612,14 +697,14 @@ g_queue_pop_nth (GQueue *queue,
 
 /**
  * g_queue_pop_tail_link:
- * @queue: a #GQueue.
+ * @queue: a #GQueue
  *
- * Removes the last element of the queue.
+ * Removes and returns the last element of the queue.
  *
- * Returns: the #GList element at the tail of the queue, or %NULL if the queue
- *   is empty.
- **/
-GList*
+ * Returns: the #GList element at the tail of the queue, or %NULL
+ *     if the queue is empty
+ */
+GList *
 g_queue_pop_tail_link (GQueue *queue)
 {
   g_return_val_if_fail (queue != NULL, NULL);
@@ -630,12 +715,12 @@ g_queue_pop_tail_link (GQueue *queue)
       
       queue->tail = node->prev;
       if (queue->tail)
-       {
-         queue->tail->next = NULL;
-         node->prev = NULL;
-       }
+        {
+          queue->tail->next = NULL;
+          node->prev = NULL;
+        }
       else
-       queue->head = NULL;
+        queue->head = NULL;
       queue->length--;
       
       return node;
@@ -651,13 +736,13 @@ g_queue_pop_tail_link (GQueue *queue)
  * 
  * Removes and returns the link at the given position.
  * 
- * Return value: The @n'th link, or %NULL if @n is off the end of @queue.
+ * Returns: the @n'th link, or %NULL if @n is off the end of @queue
  * 
  * Since: 2.4
- **/
+ */
 GList*
 g_queue_pop_nth_link (GQueue *queue,
-                     guint   n)
+                      guint   n)
 {
   GList *link;
   
@@ -679,17 +764,17 @@ g_queue_pop_nth_link (GQueue *queue,
  * 
  * Returns the link at the given position
  * 
- * Return value: The link at the @n'th position, or %NULL if @n is off the
- * end of the list
+ * Returns: the link at the @n'th position, or %NULL
+ *     if @n is off the end of the list
  * 
  * Since: 2.4
- **/
+ */
 GList *
 g_queue_peek_nth_link (GQueue *queue,
-                      guint   n)
+                       guint   n)
 {
   GList *link;
-  gint i;
+  guint i;
   
   g_return_val_if_fail (queue != NULL, NULL);
 
@@ -702,13 +787,13 @@ g_queue_peek_nth_link (GQueue *queue,
 
       link = queue->tail;
       for (i = 0; i < n; ++i)
-       link = link->prev;
+        link = link->prev;
     }
   else
     {
       link = queue->head;
       for (i = 0; i < n; ++i)
-       link = link->next;
+        link = link->next;
     }
 
   return link;
@@ -716,19 +801,19 @@ g_queue_peek_nth_link (GQueue *queue,
 
 /**
  * g_queue_link_index:
- * @queue: a #Gqueue
- * @link_: A #GList link
+ * @queue: a #GQueue
+ * @link_: a #GList link
  * 
  * Returns the position of @link_ in @queue.
  * 
- * Return value: The position of @link_, or -1 if the link is
- * not part of @queue
+ * Returns: the position of @link_, or -1 if the link is
+ *     not part of @queue
  * 
  * Since: 2.4
- **/
+ */
 gint
 g_queue_link_index (GQueue *queue,
-                   GList  *link_)
+                    GList  *link_)
 {
   g_return_val_if_fail (queue != NULL, -1);
 
@@ -736,20 +821,20 @@ g_queue_link_index (GQueue *queue,
 }
 
 /**
- * g_queue_unlink
+ * g_queue_unlink:
  * @queue: a #GQueue
- * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
+ * @link_: a #GList link that must be part of @queue
  *
- * Unlinks @link_ so that it will no longer be part of @queue. The link is
- * not freed.
+ * Unlinks @link_ so that it will no longer be part of @queue.
+ * The link is not freed.
  *
- * @link_ must be part of @queue,
+ * @link_ must be part of @queue.
  * 
  * Since: 2.4
- **/
+ */
 void
 g_queue_unlink (GQueue *queue,
-               GList  *link_)
+                GList  *link_)
 {
   g_return_if_fail (queue != NULL);
   g_return_if_fail (link_ != NULL);
@@ -764,17 +849,17 @@ g_queue_unlink (GQueue *queue,
 /**
  * g_queue_delete_link:
  * @queue: a #GQueue
- * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
+ * @link_: a #GList link that must be part of @queue
  *
  * Removes @link_ from @queue and frees it.
  *
  * @link_ must be part of @queue.
  * 
  * Since: 2.4
- **/
+ */
 void
 g_queue_delete_link (GQueue *queue,
-                    GList  *link_)
+                     GList  *link_)
 {
   g_return_if_fail (queue != NULL);
   g_return_if_fail (link_ != NULL);
@@ -785,13 +870,13 @@ g_queue_delete_link (GQueue *queue,
 
 /**
  * g_queue_peek_head:
- * @queue: a #GQueue.
+ * @queue: a #GQueue
  *
  * Returns the first element of the queue.
  *
- * Returns: the data of the first element in the queue, or %NULL if the queue
- *   is empty.
- **/
+ * Returns: the data of the first element in the queue, or %NULL
+ *     if the queue is empty
+ */
 gpointer
 g_queue_peek_head (GQueue *queue)
 {
@@ -802,13 +887,13 @@ g_queue_peek_head (GQueue *queue)
 
 /**
  * g_queue_peek_tail:
- * @queue: a #GQueue.
+ * @queue: a #GQueue
  *
  * Returns the last element of the queue.
  *
- * Returns: the data of the last element in the queue, or %NULL if the queue
- *   is empty.
- **/
+ * Returns: the data of the last element in the queue, or %NULL
+ *     if the queue is empty
+ */
 gpointer
 g_queue_peek_tail (GQueue *queue)
 {
@@ -820,18 +905,18 @@ g_queue_peek_tail (GQueue *queue)
 /**
  * g_queue_peek_nth:
  * @queue: a #GQueue
- * @n: the position of the element.
+ * @n: the position of the element
  * 
  * Returns the @n'th element of @queue. 
  * 
- * Return value: The data for the @n'th element of @queue, or %NULL if @n is
- *   off the end of @queue.
+ * Returns: the data for the @n'th element of @queue,
+ *     or %NULL if @n is off the end of @queue
  * 
  * Since: 2.4
- **/
+ */
 gpointer
 g_queue_peek_nth (GQueue *queue,
-                 guint   n)
+                  guint   n)
 {
   GList *link;
   
@@ -848,17 +933,18 @@ g_queue_peek_nth (GQueue *queue,
 /**
  * g_queue_index:
  * @queue: a #GQueue
- * @data: the data to find.
+ * @data: the data to find
  * 
  * Returns the position of the first element in @queue which contains @data.
  * 
- * Return value: The position of the first element in @queue which contains @data, or -1 if no element in @queue contains @data.
+ * Returns: the position of the first element in @queue which
+ *     contains @data, or -1 if no element in @queue contains @data
  * 
  * Since: 2.4
- **/
+ */
 gint
 g_queue_index (GQueue        *queue,
-              gconstpointer  data)
+               gconstpointer  data)
 {
   g_return_val_if_fail (queue != NULL, -1);
 
@@ -868,42 +954,51 @@ g_queue_index (GQueue        *queue,
 /**
  * g_queue_remove:
  * @queue: a #GQueue
- * @data: data to remove.
+ * @data: the data to remove
  * 
  * Removes the first element in @queue that contains @data. 
  * 
+ * Returns: %TRUE if @data was found and removed from @queue
+ *
  * Since: 2.4
- **/
-void
+ */
+gboolean
 g_queue_remove (GQueue        *queue,
-               gconstpointer  data)
+                gconstpointer  data)
 {
   GList *link;
   
-  g_return_if_fail (queue != NULL);
+  g_return_val_if_fail (queue != NULL, FALSE);
 
   link = g_list_find (queue->head, data);
 
   if (link)
     g_queue_delete_link (queue, link);
+
+  return (link != NULL);
 }
 
 /**
  * g_queue_remove_all:
  * @queue: a #GQueue
- * @data: data to remove
+ * @data: the data to remove
  * 
- * Remove all elemeents in @queue which contains @data.
+ * Remove all elements whose data equals @data from @queue.
  * 
+ * Returns: the number of elements removed from @queue
+ *
  * Since: 2.4
- **/
-void
+ */
+guint
 g_queue_remove_all (GQueue        *queue,
-                   gconstpointer  data)
+                    gconstpointer  data)
 {
   GList *list;
+  guint old_length;
   
-  g_return_if_fail (queue != NULL);
+  g_return_val_if_fail (queue != NULL, 0);
+
+  old_length = queue->length;
 
   list = queue->head;
   while (list)
@@ -911,63 +1006,145 @@ g_queue_remove_all (GQueue        *queue,
       GList *next = list->next;
 
       if (list->data == data)
-       g_queue_delete_link (queue, list);
+        g_queue_delete_link (queue, list);
       
       list = next;
     }
+
+  return (old_length - queue->length);
 }
 
 /**
  * g_queue_insert_before:
  * @queue: a #GQueue
- * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
+ * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
+ *   push at the tail of the queue.
  * @data: the data to insert
  * 
  * Inserts @data into @queue before @sibling.
  *
- * @sibling must be part of @queue.
+ * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
+ * data at the tail of the queue.
  * 
  * Since: 2.4
- **/
+ */
 void
 g_queue_insert_before (GQueue   *queue,
-                      GList    *sibling,
-                      gpointer  data)
+                       GList    *sibling,
+                       gpointer  data)
 {
   g_return_if_fail (queue != NULL);
-  g_return_if_fail (sibling != NULL);
 
-  queue->head = g_list_insert_before (queue->head, sibling, data);
-  queue->length++;
+  if (sibling == NULL)
+    {
+      /* We don't use g_list_insert_before() with a NULL sibling because it
+       * would be a O(n) operation and we would need to update manually the tail
+       * pointer.
+       */
+      g_queue_push_tail (queue, data);
+    }
+  else
+    {
+      queue->head = g_list_insert_before (queue->head, sibling, data);
+      queue->length++;
+    }
+}
+
+/**
+ * g_queue_insert_before_link:
+ * @queue: a #GQueue
+ * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
+ *   push at the tail of the queue.
+ * @link_: a #GList link to insert which must not be part of any other list.
+ *
+ * Inserts @link_ into @queue before @sibling.
+ *
+ * @sibling must be part of @queue.
+ *
+ * Since: 2.62
+ */
+void
+g_queue_insert_before_link (GQueue   *queue,
+                            GList    *sibling,
+                            GList    *link_)
+{
+  g_return_if_fail (queue != NULL);
+  g_return_if_fail (link_ != NULL);
+  g_return_if_fail (link_->prev == NULL);
+  g_return_if_fail (link_->next == NULL);
+
+  if G_UNLIKELY (sibling == NULL)
+    {
+      /* We don't use g_list_insert_before_link() with a NULL sibling because it
+       * would be a O(n) operation and we would need to update manually the tail
+       * pointer.
+       */
+      g_queue_push_tail_link (queue, link_);
+    }
+  else
+    {
+      queue->head = g_list_insert_before_link (queue->head, sibling, link_);
+      queue->length++;
+    }
 }
 
 /**
  * g_queue_insert_after:
  * @queue: a #GQueue
- * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
+ * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
+ *   push at the head of the queue.
  * @data: the data to insert
  *
- * Inserts @data into @queue after @sibling
+ * Inserts @data into @queue after @sibling.
  *
- * @sibling must be part of @queue
+ * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
+ * data at the head of the queue.
  * 
  * Since: 2.4
- **/
+ */
 void
 g_queue_insert_after (GQueue   *queue,
-                     GList    *sibling,
-                     gpointer  data)
+                      GList    *sibling,
+                      gpointer  data)
 {
   g_return_if_fail (queue != NULL);
-  g_return_if_fail (sibling != NULL);
 
-  if (sibling == queue->tail)
-    g_queue_push_tail (queue, data);
+  if (sibling == NULL)
+    g_queue_push_head (queue, data);
   else
     g_queue_insert_before (queue, sibling->next, data);
 }
 
 /**
+ * g_queue_insert_after_link:
+ * @queue: a #GQueue
+ * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
+ *   push at the head of the queue.
+ * @link_: a #GList link to insert which must not be part of any other list.
+ *
+ * Inserts @link_ into @queue after @sibling.
+ *
+ * @sibling must be part of @queue.
+ *
+ * Since: 2.62
+ */
+void
+g_queue_insert_after_link (GQueue   *queue,
+                           GList    *sibling,
+                           GList    *link_)
+{
+  g_return_if_fail (queue != NULL);
+  g_return_if_fail (link_ != NULL);
+  g_return_if_fail (link_->prev == NULL);
+  g_return_if_fail (link_->next == NULL);
+
+  if G_UNLIKELY (sibling == NULL)
+    g_queue_push_head_link (queue, link_);
+  else
+    g_queue_insert_before_link (queue, sibling->next, link_);
+}
+
+/**
  * g_queue_insert_sorted:
  * @queue: a #GQueue
  * @data: the data to insert
@@ -975,18 +1152,18 @@ g_queue_insert_after (GQueue   *queue,
  *     called with two elements of the @queue and @user_data. It should
  *     return 0 if the elements are equal, a negative value if the first
  *     element comes before the second, and a positive value if the second
- *     element comes after the first.
- * @user_data: user data passed to @func.
+ *     element comes before the first.
+ * @user_data: user data passed to @func
  * 
  * Inserts @data into @queue using @func to determine the new position.
  * 
  * Since: 2.4
- **/
+ */
 void
 g_queue_insert_sorted (GQueue           *queue,
-                      gpointer          data,
-                      GCompareDataFunc  func,
-                      gpointer          user_data)
+                       gpointer          data,
+                       GCompareDataFunc  func,
+                       gpointer          user_data)
 {
   GList *list;
   
@@ -996,11 +1173,5 @@ g_queue_insert_sorted (GQueue           *queue,
   while (list && func (list->data, data, user_data) < 0)
     list = list->next;
 
-  if (list)
-    g_queue_insert_before (queue, list, data);
-  else
-    g_queue_push_tail (queue, data);
+  g_queue_insert_before (queue, list, data);
 }
-
-#define __G_QUEUE_C__
-#include "galiasdef.c"