Coverage Report

Created: 2025-08-08 06:10

/src/rauc/subprojects/glib-2.76.5/glib/gslist.c
Line
Count
Source (jump to first uncovered line)
1
/* GLIB - Library of useful routines for C programming
2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
/*
21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22
 * file for a list of people on the GLib Team.  See the ChangeLog
23
 * files for a list of changes.  These files are distributed with
24
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25
 */
26
27
/*
28
 * MT safe
29
 */
30
31
#include "config.h"
32
33
#include "gslist.h"
34
35
#include "gtestutils.h"
36
#include "gslice.h"
37
38
/**
39
 * SECTION:linked_lists_single
40
 * @title: Singly-Linked Lists
41
 * @short_description: linked lists that can be iterated in one direction
42
 *
43
 * The #GSList structure and its associated functions provide a
44
 * standard singly-linked list data structure. The benefit of this
45
 * data-structure is to provide insertion/deletion operations in O(1)
46
 * complexity where access/search operations are in O(n). The benefit
47
 * of #GSList over #GList (doubly linked list) is that they are lighter
48
 * in space as they only need to retain one pointer but it double the
49
 * cost of the worst case access/search operations.
50
 *
51
 * Each element in the list contains a piece of data, together with a
52
 * pointer which links to the next element in the list. Using this
53
 * pointer it is possible to move through the list in one direction
54
 * only (unlike the [double-linked lists][glib-Doubly-Linked-Lists],
55
 * which allow movement in both directions).
56
 *
57
 * The data contained in each element can be either integer values, by
58
 * using one of the [Type Conversion Macros][glib-Type-Conversion-Macros],
59
 * or simply pointers to any type of data.
60
 *
61
 * List elements are allocated from the [slice allocator][glib-Memory-Slices],
62
 * which is more efficient than allocating elements individually.
63
 *
64
 * Note that most of the #GSList functions expect to be passed a
65
 * pointer to the first element in the list. The functions which insert
66
 * elements return the new start of the list, which may have changed.
67
 *
68
 * There is no function to create a #GSList. %NULL is considered to be
69
 * the empty list so you simply set a #GSList* to %NULL.
70
 *
71
 * To add elements, use g_slist_append(), g_slist_prepend(),
72
 * g_slist_insert() and g_slist_insert_sorted().
73
 *
74
 * To remove elements, use g_slist_remove().
75
 *
76
 * To find elements in the list use g_slist_last(), g_slist_next(),
77
 * g_slist_nth(), g_slist_nth_data(), g_slist_find() and
78
 * g_slist_find_custom().
79
 *
80
 * To find the index of an element use g_slist_position() and
81
 * g_slist_index().
82
 *
83
 * To call a function for each element in the list use
84
 * g_slist_foreach().
85
 *
86
 * To free the entire list, use g_slist_free().
87
 **/
88
89
/**
90
 * GSList:
91
 * @data: holds the element's data, which can be a pointer to any kind
92
 *        of data, or any integer value using the
93
 *        [Type Conversion Macros][glib-Type-Conversion-Macros]
94
 * @next: contains the link to the next element in the list.
95
 *
96
 * The #GSList struct is used for each element in the singly-linked
97
 * list.
98
 **/
99
100
/**
101
 * g_slist_next:
102
 * @slist: an element in a #GSList.
103
 *
104
 * A convenience macro to get the next element in a #GSList.
105
 * Note that it is considered perfectly acceptable to access
106
 * @slist->next directly.
107
 *
108
 * Returns: the next element, or %NULL if there are no more elements.
109
 **/
110
111
0
#define _g_slist_alloc0()       g_slice_new0 (GSList)
112
3.46M
#define _g_slist_alloc()        g_slice_new (GSList)
113
70
#define _g_slist_free1(slist)   g_slice_free (GSList, slist)
114
115
/**
116
 * g_slist_alloc:
117
 *
118
 * Allocates space for one #GSList element. It is called by the
119
 * g_slist_append(), g_slist_prepend(), g_slist_insert() and
120
 * g_slist_insert_sorted() functions and so is rarely used on its own.
121
 *
122
 * Returns: a pointer to the newly-allocated #GSList element.
123
 **/
124
GSList*
125
g_slist_alloc (void)
126
0
{
127
0
  return _g_slist_alloc0 ();
128
0
}
129
130
/**
131
 * g_slist_free:
132
 * @list: the first link of a #GSList
133
 *
134
 * Frees all of the memory used by a #GSList.
135
 * The freed elements are returned to the slice allocator.
136
 *
137
 * If list elements contain dynamically-allocated memory,
138
 * you should either use g_slist_free_full() or free them manually
139
 * first.
140
 *
141
 * It can be combined with g_steal_pointer() to ensure the list head pointer
142
 * is not left dangling:
143
 * |[<!-- language="C" -->
144
 * GSList *list_of_borrowed_things = …;  /<!-- -->* (transfer container) *<!-- -->/
145
 * g_slist_free (g_steal_pointer (&list_of_borrowed_things));
146
 * ]|
147
 */
148
void
149
g_slist_free (GSList *list)
150
18.6k
{
151
18.6k
  g_slice_free_chain (GSList, list, next);
152
18.6k
}
153
154
/**
155
 * g_slist_free_1:
156
 * @list: a #GSList element
157
 *
158
 * Frees one #GSList element.
159
 * It is usually used after g_slist_remove_link().
160
 */
161
/**
162
 * g_slist_free1:
163
 *
164
 * A macro which does the same as g_slist_free_1().
165
 *
166
 * Since: 2.10
167
 **/
168
void
169
g_slist_free_1 (GSList *list)
170
70
{
171
70
  _g_slist_free1 (list);
172
70
}
173
174
/**
175
 * g_slist_free_full:
176
 * @list: the first link of a #GSList
177
 * @free_func: the function to be called to free each element's data
178
 *
179
 * Convenience method, which frees all the memory used by a #GSList, and
180
 * calls the specified destroy function on every element's data.
181
 *
182
 * @free_func must not modify the list (eg, by removing the freed
183
 * element from it).
184
 *
185
 * It can be combined with g_steal_pointer() to ensure the list head pointer
186
 * is not left dangling ­— this also has the nice property that the head pointer
187
 * is cleared before any of the list elements are freed, to prevent double frees
188
 * from @free_func:
189
 * |[<!-- language="C" -->
190
 * GSList *list_of_owned_things = …;  /<!-- -->* (transfer full) (element-type GObject) *<!-- -->/
191
 * g_slist_free_full (g_steal_pointer (&list_of_owned_things), g_object_unref);
192
 * ]|
193
 *
194
 * Since: 2.28
195
 **/
196
void
197
g_slist_free_full (GSList         *list,
198
       GDestroyNotify  free_func)
199
1.44k
{
200
1.44k
  g_slist_foreach (list, (GFunc) free_func, NULL);
201
1.44k
  g_slist_free (list);
202
1.44k
}
203
204
/**
205
 * g_slist_append:
206
 * @list: a #GSList
207
 * @data: the data for the new element
208
 *
209
 * Adds a new element on to the end of the list.
210
 *
211
 * The return value is the new start of the list, which may
212
 * have changed, so make sure you store the new value.
213
 *
214
 * Note that g_slist_append() has to traverse the entire list
215
 * to find the end, which is inefficient when adding multiple
216
 * elements. A common idiom to avoid the inefficiency is to prepend
217
 * the elements and reverse the list when all elements have been added.
218
 *
219
 * |[<!-- language="C" --> 
220
 * // Notice that these are initialized to the empty list.
221
 * GSList *list = NULL, *number_list = NULL;
222
 *
223
 * // This is a list of strings.
224
 * list = g_slist_append (list, "first");
225
 * list = g_slist_append (list, "second");
226
 *
227
 * // This is a list of integers.
228
 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
229
 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
230
 * ]|
231
 *
232
 * Returns: the new start of the #GSList
233
 */
234
GSList*
235
g_slist_append (GSList   *list,
236
                gpointer  data)
237
0
{
238
0
  GSList *new_list;
239
0
  GSList *last;
240
241
0
  new_list = _g_slist_alloc ();
242
0
  new_list->data = data;
243
0
  new_list->next = NULL;
244
245
0
  if (list)
246
0
    {
247
0
      last = g_slist_last (list);
248
      /* g_assert (last != NULL); */
249
0
      last->next = new_list;
250
251
0
      return list;
252
0
    }
253
0
  else
254
0
    return new_list;
255
0
}
256
257
/**
258
 * g_slist_prepend:
259
 * @list: a #GSList
260
 * @data: the data for the new element
261
 *
262
 * Adds a new element on to the start of the list.
263
 *
264
 * The return value is the new start of the list, which
265
 * may have changed, so make sure you store the new value.
266
 *
267
 * |[<!-- language="C" --> 
268
 * // Notice that it is initialized to the empty list.
269
 * GSList *list = NULL;
270
 * list = g_slist_prepend (list, "last");
271
 * list = g_slist_prepend (list, "first");
272
 * ]|
273
 *
274
 * Returns: the new start of the #GSList
275
 */
276
GSList*
277
g_slist_prepend (GSList   *list,
278
                 gpointer  data)
279
3.46M
{
280
3.46M
  GSList *new_list;
281
282
3.46M
  new_list = _g_slist_alloc ();
283
3.46M
  new_list->data = data;
284
3.46M
  new_list->next = list;
285
286
3.46M
  return new_list;
287
3.46M
}
288
289
/**
290
 * g_slist_insert:
291
 * @list: a #GSList
292
 * @data: the data for the new element
293
 * @position: the position to insert the element.
294
 *     If this is negative, or is larger than the number
295
 *     of elements in the list, the new element is added on
296
 *     to the end of the list.
297
 *
298
 * Inserts a new element into the list at the given position.
299
 *
300
 * Returns: the new start of the #GSList
301
 */
302
GSList*
303
g_slist_insert (GSList   *list,
304
                gpointer  data,
305
                gint      position)
306
0
{
307
0
  GSList *prev_list;
308
0
  GSList *tmp_list;
309
0
  GSList *new_list;
310
311
0
  if (position < 0)
312
0
    return g_slist_append (list, data);
313
0
  else if (position == 0)
314
0
    return g_slist_prepend (list, data);
315
316
0
  new_list = _g_slist_alloc ();
317
0
  new_list->data = data;
318
319
0
  if (!list)
320
0
    {
321
0
      new_list->next = NULL;
322
0
      return new_list;
323
0
    }
324
325
0
  prev_list = NULL;
326
0
  tmp_list = list;
327
328
0
  while ((position-- > 0) && tmp_list)
329
0
    {
330
0
      prev_list = tmp_list;
331
0
      tmp_list = tmp_list->next;
332
0
    }
333
334
0
  new_list->next = prev_list->next;
335
0
  prev_list->next = new_list;
336
337
0
  return list;
338
0
}
339
340
/**
341
 * g_slist_insert_before:
342
 * @slist: a #GSList
343
 * @sibling: node to insert @data before
344
 * @data: data to put in the newly-inserted node
345
 *
346
 * Inserts a node before @sibling containing @data.
347
 *
348
 * Returns: the new head of the list.
349
 */
350
GSList*
351
g_slist_insert_before (GSList  *slist,
352
                       GSList  *sibling,
353
                       gpointer data)
354
0
{
355
0
  if (!slist)
356
0
    {
357
0
      slist = _g_slist_alloc ();
358
0
      slist->data = data;
359
0
      slist->next = NULL;
360
0
      g_return_val_if_fail (sibling == NULL, slist);
361
0
      return slist;
362
0
    }
363
0
  else
364
0
    {
365
0
      GSList *node, *last = NULL;
366
367
0
      for (node = slist; node; last = node, node = last->next)
368
0
        if (node == sibling)
369
0
          break;
370
0
      if (!last)
371
0
        {
372
0
          node = _g_slist_alloc ();
373
0
          node->data = data;
374
0
          node->next = slist;
375
376
0
          return node;
377
0
        }
378
0
      else
379
0
        {
380
0
          node = _g_slist_alloc ();
381
0
          node->data = data;
382
0
          node->next = last->next;
383
0
          last->next = node;
384
385
0
          return slist;
386
0
        }
387
0
    }
388
0
}
389
390
/**
391
 * g_slist_concat:
392
 * @list1: a #GSList
393
 * @list2: the #GSList to add to the end of the first #GSList
394
 *
395
 * Adds the second #GSList onto the end of the first #GSList.
396
 * Note that the elements of the second #GSList are not copied.
397
 * They are used directly.
398
 *
399
 * Returns: the start of the new #GSList
400
 */
401
GSList *
402
g_slist_concat (GSList *list1, GSList *list2)
403
0
{
404
0
  if (list2)
405
0
    {
406
0
      if (list1)
407
0
        g_slist_last (list1)->next = list2;
408
0
      else
409
0
        list1 = list2;
410
0
    }
411
412
0
  return list1;
413
0
}
414
415
static GSList*
416
_g_slist_remove_data (GSList        *list,
417
                      gconstpointer  data,
418
                      gboolean       all)
419
70
{
420
70
  GSList *tmp = NULL;
421
70
  GSList **previous_ptr = &list;
422
423
70
  while (*previous_ptr)
424
70
    {
425
70
      tmp = *previous_ptr;
426
70
      if (tmp->data == data)
427
70
        {
428
70
          *previous_ptr = tmp->next;
429
70
          g_slist_free_1 (tmp);
430
70
          if (!all)
431
70
            break;
432
70
        }
433
0
      else
434
0
        {
435
0
          previous_ptr = &tmp->next;
436
0
        }
437
70
    }
438
439
70
  return list;
440
70
}
441
/**
442
 * g_slist_remove:
443
 * @list: a #GSList
444
 * @data: the data of the element to remove
445
 *
446
 * Removes an element from a #GSList.
447
 * If two elements contain the same data, only the first is removed.
448
 * If none of the elements contain the data, the #GSList is unchanged.
449
 *
450
 * Returns: the new start of the #GSList
451
 */
452
GSList*
453
g_slist_remove (GSList        *list,
454
                gconstpointer  data)
455
70
{
456
70
  return _g_slist_remove_data (list, data, FALSE);
457
70
}
458
459
/**
460
 * g_slist_remove_all:
461
 * @list: a #GSList
462
 * @data: data to remove
463
 *
464
 * Removes all list nodes with data equal to @data.
465
 * Returns the new head of the list. Contrast with
466
 * g_slist_remove() which removes only the first node
467
 * matching the given data.
468
 *
469
 * Returns: new head of @list
470
 */
471
GSList*
472
g_slist_remove_all (GSList        *list,
473
                    gconstpointer  data)
474
0
{
475
0
  return _g_slist_remove_data (list, data, TRUE);
476
0
}
477
478
static inline GSList*
479
_g_slist_remove_link (GSList *list,
480
                      GSList *link)
481
0
{
482
0
  GSList *tmp = NULL;
483
0
  GSList **previous_ptr = &list;
484
485
0
  while (*previous_ptr)
486
0
    {
487
0
      tmp = *previous_ptr;
488
0
      if (tmp == link)
489
0
        {
490
0
          *previous_ptr = tmp->next;
491
0
          tmp->next = NULL;
492
0
          break;
493
0
        }
494
495
0
      previous_ptr = &tmp->next;
496
0
    }
497
498
0
  return list;
499
0
}
500
501
/**
502
 * g_slist_remove_link:
503
 * @list: a #GSList
504
 * @link_: an element in the #GSList
505
 *
506
 * Removes an element from a #GSList, without
507
 * freeing the element. The removed element's next
508
 * link is set to %NULL, so that it becomes a
509
 * self-contained list with one element.
510
 *
511
 * Removing arbitrary nodes from a singly-linked list
512
 * requires time that is proportional to the length of the list
513
 * (ie. O(n)). If you find yourself using g_slist_remove_link()
514
 * frequently, you should consider a different data structure,
515
 * such as the doubly-linked #GList.
516
 *
517
 * Returns: the new start of the #GSList, without the element
518
 */
519
GSList*
520
g_slist_remove_link (GSList *list,
521
                     GSList *link_)
522
0
{
523
0
  return _g_slist_remove_link (list, link_);
524
0
}
525
526
/**
527
 * g_slist_delete_link:
528
 * @list: a #GSList
529
 * @link_: node to delete
530
 *
531
 * Removes the node link_ from the list and frees it.
532
 * Compare this to g_slist_remove_link() which removes the node
533
 * without freeing it.
534
 *
535
 * Removing arbitrary nodes from a singly-linked list requires time
536
 * that is proportional to the length of the list (ie. O(n)). If you
537
 * find yourself using g_slist_delete_link() frequently, you should
538
 * consider a different data structure, such as the doubly-linked
539
 * #GList.
540
 *
541
 * Returns: the new head of @list
542
 */
543
GSList*
544
g_slist_delete_link (GSList *list,
545
                     GSList *link_)
546
0
{
547
0
  list = _g_slist_remove_link (list, link_);
548
0
  _g_slist_free1 (link_);
549
550
0
  return list;
551
0
}
552
553
/**
554
 * g_slist_copy:
555
 * @list: a #GSList
556
 *
557
 * Copies a #GSList.
558
 *
559
 * Note that this is a "shallow" copy. If the list elements
560
 * consist of pointers to data, the pointers are copied but
561
 * the actual data isn't. See g_slist_copy_deep() if you need
562
 * to copy the data as well.
563
 *
564
 * Returns: a copy of @list
565
 */
566
GSList*
567
g_slist_copy (GSList *list)
568
7
{
569
7
  return g_slist_copy_deep (list, NULL, NULL);
570
7
}
571
572
/**
573
 * g_slist_copy_deep:
574
 * @list: a #GSList
575
 * @func: a copy function used to copy every element in the list
576
 * @user_data: user data passed to the copy function @func, or #NULL
577
 *
578
 * Makes a full (deep) copy of a #GSList.
579
 *
580
 * In contrast with g_slist_copy(), this function uses @func to make a copy of
581
 * each list element, in addition to copying the list container itself.
582
 *
583
 * @func, as a #GCopyFunc, takes two arguments, the data to be copied
584
 * and a @user_data pointer. On common processor architectures, it's safe to
585
 * pass %NULL as @user_data if the copy function takes only one argument. You
586
 * may get compiler warnings from this though if compiling with GCC’s
587
 * `-Wcast-function-type` warning.
588
 *
589
 * For instance, if @list holds a list of GObjects, you can do:
590
 * |[<!-- language="C" --> 
591
 * another_list = g_slist_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
592
 * ]|
593
 *
594
 * And, to entirely free the new list, you could do:
595
 * |[<!-- language="C" --> 
596
 * g_slist_free_full (another_list, g_object_unref);
597
 * ]|
598
 *
599
 * Returns: a full copy of @list, use g_slist_free_full() to free it
600
 *
601
 * Since: 2.34
602
 */
603
GSList*
604
g_slist_copy_deep (GSList *list, GCopyFunc func, gpointer user_data)
605
7
{
606
7
  GSList *new_list = NULL;
607
608
7
  if (list)
609
0
    {
610
0
      GSList *last;
611
612
0
      new_list = _g_slist_alloc ();
613
0
      if (func)
614
0
        new_list->data = func (list->data, user_data);
615
0
      else
616
0
        new_list->data = list->data;
617
0
      last = new_list;
618
0
      list = list->next;
619
0
      while (list)
620
0
        {
621
0
          last->next = _g_slist_alloc ();
622
0
          last = last->next;
623
0
          if (func)
624
0
            last->data = func (list->data, user_data);
625
0
          else
626
0
            last->data = list->data;
627
0
          list = list->next;
628
0
        }
629
0
      last->next = NULL;
630
0
    }
631
632
7
  return new_list;
633
7
}
634
635
/**
636
 * g_slist_reverse:
637
 * @list: a #GSList
638
 *
639
 * Reverses a #GSList.
640
 *
641
 * Returns: the start of the reversed #GSList
642
 */
643
GSList*
644
g_slist_reverse (GSList *list)
645
1.94k
{
646
1.94k
  GSList *prev = NULL;
647
648
3.46M
  while (list)
649
3.46M
    {
650
3.46M
      GSList *next = list->next;
651
652
3.46M
      list->next = prev;
653
654
3.46M
      prev = list;
655
3.46M
      list = next;
656
3.46M
    }
657
658
1.94k
  return prev;
659
1.94k
}
660
661
/**
662
 * g_slist_nth:
663
 * @list: a #GSList
664
 * @n: the position of the element, counting from 0
665
 *
666
 * Gets the element at the given position in a #GSList.
667
 *
668
 * Returns: the element, or %NULL if the position is off
669
 *     the end of the #GSList
670
 */
671
GSList*
672
g_slist_nth (GSList *list,
673
             guint   n)
674
0
{
675
0
  while (n-- > 0 && list)
676
0
    list = list->next;
677
678
0
  return list;
679
0
}
680
681
/**
682
 * g_slist_nth_data:
683
 * @list: a #GSList
684
 * @n: the position of the element
685
 *
686
 * Gets the data of the element at the given position.
687
 *
688
 * Returns: the element's data, or %NULL if the position
689
 *     is off the end of the #GSList
690
 */
691
gpointer
692
g_slist_nth_data (GSList   *list,
693
                  guint     n)
694
0
{
695
0
  while (n-- > 0 && list)
696
0
    list = list->next;
697
698
0
  return list ? list->data : NULL;
699
0
}
700
701
/**
702
 * g_slist_find:
703
 * @list: a #GSList
704
 * @data: the element data to find
705
 *
706
 * Finds the element in a #GSList which
707
 * contains the given data.
708
 *
709
 * Returns: the found #GSList element,
710
 *     or %NULL if it is not found
711
 */
712
GSList*
713
g_slist_find (GSList        *list,
714
              gconstpointer  data)
715
70
{
716
174
  while (list)
717
104
    {
718
104
      if (list->data == data)
719
0
        break;
720
104
      list = list->next;
721
104
    }
722
723
70
  return list;
724
70
}
725
726
727
/**
728
 * g_slist_find_custom:
729
 * @list: a #GSList
730
 * @data: user data passed to the function
731
 * @func: the function to call for each element.
732
 *     It should return 0 when the desired element is found
733
 *
734
 * Finds an element in a #GSList, using a supplied function to
735
 * find the desired element. It iterates over the list, calling
736
 * the given function which should return 0 when the desired
737
 * element is found. The function takes two #gconstpointer arguments,
738
 * the #GSList element's data as the first argument and the
739
 * given user data.
740
 *
741
 * Returns: the found #GSList element, or %NULL if it is not found
742
 */
743
GSList*
744
g_slist_find_custom (GSList        *list,
745
                     gconstpointer  data,
746
                     GCompareFunc   func)
747
0
{
748
0
  g_return_val_if_fail (func != NULL, list);
749
750
0
  while (list)
751
0
    {
752
0
      if (! func (list->data, data))
753
0
        return list;
754
0
      list = list->next;
755
0
    }
756
757
0
  return NULL;
758
0
}
759
760
/**
761
 * g_slist_position:
762
 * @list: a #GSList
763
 * @llink: an element in the #GSList
764
 *
765
 * Gets the position of the given element
766
 * in the #GSList (starting from 0).
767
 *
768
 * Returns: the position of the element in the #GSList,
769
 *     or -1 if the element is not found
770
 */
771
gint
772
g_slist_position (GSList *list,
773
                  GSList *llink)
774
0
{
775
0
  gint i;
776
777
0
  i = 0;
778
0
  while (list)
779
0
    {
780
0
      if (list == llink)
781
0
        return i;
782
0
      i++;
783
0
      list = list->next;
784
0
    }
785
786
0
  return -1;
787
0
}
788
789
/**
790
 * g_slist_index:
791
 * @list: a #GSList
792
 * @data: the data to find
793
 *
794
 * Gets the position of the element containing
795
 * the given data (starting from 0).
796
 *
797
 * Returns: the index of the element containing the data,
798
 *     or -1 if the data is not found
799
 */
800
gint
801
g_slist_index (GSList        *list,
802
               gconstpointer  data)
803
0
{
804
0
  gint i;
805
806
0
  i = 0;
807
0
  while (list)
808
0
    {
809
0
      if (list->data == data)
810
0
        return i;
811
0
      i++;
812
0
      list = list->next;
813
0
    }
814
815
0
  return -1;
816
0
}
817
818
/**
819
 * g_slist_last:
820
 * @list: a #GSList
821
 *
822
 * Gets the last element in a #GSList.
823
 *
824
 * This function iterates over the whole list.
825
 *
826
 * Returns: the last element in the #GSList,
827
 *     or %NULL if the #GSList has no elements
828
 */
829
GSList*
830
g_slist_last (GSList *list)
831
0
{
832
0
  if (list)
833
0
    {
834
0
      while (list->next)
835
0
        list = list->next;
836
0
    }
837
838
0
  return list;
839
0
}
840
841
/**
842
 * g_slist_length:
843
 * @list: a #GSList
844
 *
845
 * Gets the number of elements in a #GSList.
846
 *
847
 * This function iterates over the whole list to
848
 * count its elements. To check whether the list is non-empty, it is faster to
849
 * check @list against %NULL.
850
 *
851
 * Returns: the number of elements in the #GSList
852
 */
853
guint
854
g_slist_length (GSList *list)
855
1.95k
{
856
1.95k
  guint length;
857
858
1.95k
  length = 0;
859
3.46M
  while (list)
860
3.46M
    {
861
3.46M
      length++;
862
3.46M
      list = list->next;
863
3.46M
    }
864
865
1.95k
  return length;
866
1.95k
}
867
868
/**
869
 * g_slist_foreach:
870
 * @list: a #GSList
871
 * @func: the function to call with each element's data
872
 * @user_data: user data to pass to the function
873
 *
874
 * Calls a function for each element of a #GSList.
875
 *
876
 * It is safe for @func to remove the element from @list, but it must
877
 * not modify any part of the list after that element.
878
 */
879
void
880
g_slist_foreach (GSList   *list,
881
                 GFunc     func,
882
                 gpointer  user_data)
883
1.44k
{
884
2.63k
  while (list)
885
1.18k
    {
886
1.18k
      GSList *next = list->next;
887
1.18k
      (*func) (list->data, user_data);
888
1.18k
      list = next;
889
1.18k
    }
890
1.44k
}
891
892
static GSList*
893
g_slist_insert_sorted_real (GSList   *list,
894
                            gpointer  data,
895
                            GFunc     func,
896
                            gpointer  user_data)
897
0
{
898
0
  GSList *tmp_list = list;
899
0
  GSList *prev_list = NULL;
900
0
  GSList *new_list;
901
0
  gint cmp;
902
903
0
  g_return_val_if_fail (func != NULL, list);
904
905
0
  if (!list)
906
0
    {
907
0
      new_list = _g_slist_alloc ();
908
0
      new_list->data = data;
909
0
      new_list->next = NULL;
910
0
      return new_list;
911
0
    }
912
913
0
  cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
914
915
0
  while ((tmp_list->next) && (cmp > 0))
916
0
    {
917
0
      prev_list = tmp_list;
918
0
      tmp_list = tmp_list->next;
919
920
0
      cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
921
0
    }
922
923
0
  new_list = _g_slist_alloc ();
924
0
  new_list->data = data;
925
926
0
  if ((!tmp_list->next) && (cmp > 0))
927
0
    {
928
0
      tmp_list->next = new_list;
929
0
      new_list->next = NULL;
930
0
      return list;
931
0
    }
932
933
0
  if (prev_list)
934
0
    {
935
0
      prev_list->next = new_list;
936
0
      new_list->next = tmp_list;
937
0
      return list;
938
0
    }
939
0
  else
940
0
    {
941
0
      new_list->next = list;
942
0
      return new_list;
943
0
    }
944
0
}
945
946
/**
947
 * g_slist_insert_sorted:
948
 * @list: a #GSList
949
 * @data: the data for the new element
950
 * @func: the function to compare elements in the list.
951
 *     It should return a number > 0 if the first parameter
952
 *     comes after the second parameter in the sort order.
953
 *
954
 * Inserts a new element into the list, using the given
955
 * comparison function to determine its position.
956
 *
957
 * Returns: the new start of the #GSList
958
 */
959
GSList*
960
g_slist_insert_sorted (GSList       *list,
961
                       gpointer      data,
962
                       GCompareFunc  func)
963
0
{
964
0
  return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
965
0
}
966
967
/**
968
 * g_slist_insert_sorted_with_data:
969
 * @list: a #GSList
970
 * @data: the data for the new element
971
 * @func: the function to compare elements in the list.
972
 *     It should return a number > 0 if the first parameter
973
 *     comes after the second parameter in the sort order.
974
 * @user_data: data to pass to comparison function
975
 *
976
 * Inserts a new element into the list, using the given
977
 * comparison function to determine its position.
978
 *
979
 * Returns: the new start of the #GSList
980
 *
981
 * Since: 2.10
982
 */
983
GSList*
984
g_slist_insert_sorted_with_data (GSList           *list,
985
                                 gpointer          data,
986
                                 GCompareDataFunc  func,
987
                                 gpointer          user_data)
988
0
{
989
0
  return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
990
0
}
991
992
static GSList *
993
g_slist_sort_merge (GSList   *l1,
994
                    GSList   *l2,
995
                    GFunc     compare_func,
996
                    gpointer  user_data)
997
0
{
998
0
  GSList list, *l;
999
0
  gint cmp;
1000
1001
0
  l=&list;
1002
1003
0
  while (l1 && l2)
1004
0
    {
1005
0
      cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
1006
1007
0
      if (cmp <= 0)
1008
0
        {
1009
0
          l=l->next=l1;
1010
0
          l1=l1->next;
1011
0
        }
1012
0
      else
1013
0
        {
1014
0
          l=l->next=l2;
1015
0
          l2=l2->next;
1016
0
        }
1017
0
    }
1018
0
  l->next= l1 ? l1 : l2;
1019
1020
0
  return list.next;
1021
0
}
1022
1023
static GSList *
1024
g_slist_sort_real (GSList   *list,
1025
                   GFunc     compare_func,
1026
                   gpointer  user_data)
1027
6
{
1028
6
  GSList *l1, *l2;
1029
1030
6
  if (!list)
1031
6
    return NULL;
1032
0
  if (!list->next)
1033
0
    return list;
1034
1035
0
  l1 = list;
1036
0
  l2 = list->next;
1037
1038
0
  while ((l2 = l2->next) != NULL)
1039
0
    {
1040
0
      if ((l2 = l2->next) == NULL)
1041
0
        break;
1042
0
      l1=l1->next;
1043
0
    }
1044
0
  l2 = l1->next;
1045
0
  l1->next = NULL;
1046
1047
0
  return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
1048
0
                             g_slist_sort_real (l2, compare_func, user_data),
1049
0
                             compare_func,
1050
0
                             user_data);
1051
0
}
1052
1053
/**
1054
 * g_slist_sort:
1055
 * @list: a #GSList
1056
 * @compare_func: the comparison function used to sort the #GSList.
1057
 *     This function is passed the data from 2 elements of the #GSList
1058
 *     and should return 0 if they are equal, a negative value if the
1059
 *     first element comes before the second, or a positive value if
1060
 *     the first element comes after the second.
1061
 *
1062
 * Sorts a #GSList using the given comparison function. The algorithm
1063
 * used is a stable sort.
1064
 *
1065
 * Returns: the start of the sorted #GSList
1066
 */
1067
GSList *
1068
g_slist_sort (GSList       *list,
1069
              GCompareFunc  compare_func)
1070
6
{
1071
6
  return g_slist_sort_real (list, (GFunc) compare_func, NULL);
1072
6
}
1073
1074
/**
1075
 * g_slist_sort_with_data:
1076
 * @list: a #GSList
1077
 * @compare_func: comparison function
1078
 * @user_data: data to pass to comparison function
1079
 *
1080
 * Like g_slist_sort(), but the sort function accepts a user data argument.
1081
 *
1082
 * Returns: new head of the list
1083
 */
1084
GSList *
1085
g_slist_sort_with_data (GSList           *list,
1086
                        GCompareDataFunc  compare_func,
1087
                        gpointer          user_data)
1088
0
{
1089
0
  return g_slist_sort_real (list, (GFunc) compare_func, user_data);
1090
0
}
1091
1092
/**
1093
 * g_clear_slist: (skip)
1094
 * @slist_ptr: (not nullable): a #GSList return location
1095
 * @destroy: (nullable): the function to pass to g_slist_free_full() or %NULL to not free elements
1096
 *
1097
 * Clears a pointer to a #GSList, freeing it and, optionally, freeing its elements using @destroy.
1098
 *
1099
 * @slist_ptr must be a valid pointer. If @slist_ptr points to a null #GSList, this does nothing.
1100
 *
1101
 * Since: 2.64
1102
 */
1103
void
1104
(g_clear_slist) (GSList         **slist_ptr,
1105
                 GDestroyNotify   destroy)
1106
0
{
1107
0
  GSList *slist;
1108
1109
0
  slist = *slist_ptr;
1110
0
  if (slist)
1111
0
    {
1112
0
      *slist_ptr = NULL;
1113
1114
0
      if (destroy)
1115
0
        g_slist_free_full (slist, destroy);
1116
0
      else
1117
0
        g_slist_free (slist);
1118
0
    }
1119
0
}