[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / glib / gqueue.c
index 6860a19..3176614 100644 (file)
@@ -15,9 +15,7 @@
  * 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/>.
  */
 
 /*
@@ -34,8 +32,8 @@
  * as #GList to store elements.
  *
  * The data contained in each element can be either integer values, by
- * using one of the <link linkend="glib-Type-Conversion-Macros">Type
- * Conversion Macros</link>, or simply pointers to any type of data.
+ * using one of the [Type Conversion Macros][glib-Type-Conversion-Macros],
+ * or simply pointers to any type of data.
  *
  * To create a new GQueue, use g_queue_new().
  *
@@ -77,11 +75,8 @@ g_queue_new (void)
  * if @queue was created with g_queue_new(). If queue elements contain
  * dynamically-allocated memory, they should be freed first.
  *
- * <note><para>
- * If queue elements contain dynamically-allocated memory,
- * you should either use g_queue_free_full() or free them manually
- * first.
- * </para></note>
+ * 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)
@@ -170,7 +165,7 @@ 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
  */
@@ -207,7 +202,7 @@ 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
  */
@@ -264,7 +259,7 @@ 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
  */
@@ -290,7 +285,7 @@ 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
  */
@@ -380,8 +375,7 @@ 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
+ * @link_: a single #GList element, not a list with more than one element
  *
  * Adds a new element at the head of the queue.
  */
@@ -427,8 +421,7 @@ 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
+ * @link_: a single #GList element, not a list with more than one element
  *
  * Adds a new element at the tail of the queue.
  */
@@ -574,7 +567,7 @@ g_queue_pop_head_link (GQueue *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
  */
@@ -592,7 +585,7 @@ g_queue_peek_head_link (GQueue *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
  */
@@ -644,7 +637,7 @@ g_queue_pop_tail (GQueue *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
  */
@@ -709,7 +702,7 @@ 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
  */
@@ -737,7 +730,7 @@ 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
+ * Returns: the link at the @n'th position, or %NULL
  *     if @n is off the end of the list
  * 
  * Since: 2.4
@@ -779,7 +772,7 @@ g_queue_peek_nth_link (GQueue *queue,
  * 
  * Returns the position of @link_ in @queue.
  * 
- * Return value: the position of @link_, or -1 if the link is
+ * Returns: the position of @link_, or -1 if the link is
  *     not part of @queue
  * 
  * Since: 2.4
@@ -796,10 +789,10 @@ g_queue_link_index (GQueue *queue,
 /**
  * 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.
  * 
@@ -822,7 +815,7 @@ 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.
  *
@@ -882,7 +875,7 @@ g_queue_peek_tail (GQueue *queue)
  * 
  * Returns the @n'th element of @queue. 
  * 
- * Return value: the data for the @n'th element 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
@@ -910,7 +903,7 @@ g_queue_peek_nth (GQueue *queue,
  * 
  * Returns the position of the first element in @queue which contains @data.
  * 
- * Return value: the position of the first element in @queue which
+ * Returns: the position of the first element in @queue which
  *     contains @data, or -1 if no element in @queue contains @data
  * 
  * Since: 2.4
@@ -931,7 +924,7 @@ g_queue_index (GQueue        *queue,
  * 
  * Removes the first element in @queue that contains @data. 
  * 
- * Return value: %TRUE if @data was found and removed from @queue
+ * Returns: %TRUE if @data was found and removed from @queue
  *
  * Since: 2.4
  */
@@ -958,7 +951,7 @@ g_queue_remove (GQueue        *queue,
  * 
  * Remove all elements whose data equals @data from @queue.
  * 
- * Return value: the number of elements removed from @queue
+ * Returns: the number of elements removed from @queue
  *
  * Since: 2.4
  */
@@ -990,12 +983,14 @@ g_queue_remove_all (GQueue        *queue,
 /**
  * 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
  */
@@ -1005,21 +1000,33 @@ g_queue_insert_before (GQueue   *queue,
                        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_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
  */
@@ -1029,10 +1036,9 @@ g_queue_insert_after (GQueue   *queue,
                       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);
 }
@@ -1066,8 +1072,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);
 }