1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
37 #ifndef DISABLE_MEM_POOLS
38 struct _GAllocator
/* from gmem.c */
46 GSList
*free_lists
; /* implementation specific */
49 G_LOCK_DEFINE_STATIC (current_allocator
);
50 static GAllocator
*current_allocator
= NULL
;
52 /* HOLDS: current_allocator_lock */
54 g_slist_validate_allocator (GAllocator
*allocator
)
56 g_return_if_fail (allocator
!= NULL
);
57 g_return_if_fail (allocator
->is_unused
== TRUE
);
59 if (allocator
->type
!= G_ALLOCATOR_SLIST
)
61 allocator
->type
= G_ALLOCATOR_SLIST
;
62 if (allocator
->mem_chunk
)
64 g_mem_chunk_destroy (allocator
->mem_chunk
);
65 allocator
->mem_chunk
= NULL
;
69 if (!allocator
->mem_chunk
)
71 allocator
->mem_chunk
= g_mem_chunk_new (allocator
->name
,
73 sizeof (GSList
) * allocator
->n_preallocs
,
75 allocator
->free_lists
= NULL
;
78 allocator
->is_unused
= FALSE
;
82 g_slist_push_allocator (GAllocator
*allocator
)
84 G_LOCK (current_allocator
);
85 g_slist_validate_allocator (allocator
);
86 allocator
->last
= current_allocator
;
87 current_allocator
= allocator
;
88 G_UNLOCK (current_allocator
);
92 g_slist_pop_allocator (void)
94 G_LOCK (current_allocator
);
95 if (current_allocator
)
97 GAllocator
*allocator
;
99 allocator
= current_allocator
;
100 current_allocator
= allocator
->last
;
101 allocator
->last
= NULL
;
102 allocator
->is_unused
= TRUE
;
104 G_UNLOCK (current_allocator
);
107 static inline GSList
*
108 _g_slist_alloc (void)
112 G_LOCK (current_allocator
);
113 if (!current_allocator
)
115 GAllocator
*allocator
= g_allocator_new ("GLib default GSList allocator",
117 g_slist_validate_allocator (allocator
);
118 allocator
->last
= NULL
;
119 current_allocator
= allocator
;
121 if (!current_allocator
->free_lists
)
123 list
= g_chunk_new (GSList
, current_allocator
->mem_chunk
);
128 if (current_allocator
->free_lists
->data
)
130 list
= current_allocator
->free_lists
->data
;
131 current_allocator
->free_lists
->data
= list
->next
;
136 list
= current_allocator
->free_lists
;
137 current_allocator
->free_lists
= list
->next
;
140 G_UNLOCK (current_allocator
);
150 return _g_slist_alloc ();
154 g_slist_free (GSList
*list
)
158 GSList
*last_node
= list
;
160 #ifdef ENABLE_GC_FRIENDLY
161 while (last_node
->next
)
163 last_node
->data
= NULL
;
164 last_node
= last_node
->next
;
166 last_node
->data
= NULL
;
167 #else /* !ENABLE_GC_FRIENDLY */
168 list
->data
= list
->next
;
169 #endif /* ENABLE_GC_FRIENDLY */
171 G_LOCK (current_allocator
);
172 last_node
->next
= current_allocator
->free_lists
;
173 current_allocator
->free_lists
= list
;
174 G_UNLOCK (current_allocator
);
179 _g_slist_free_1 (GSList
*list
)
184 G_LOCK (current_allocator
);
185 list
->next
= current_allocator
->free_lists
;
186 current_allocator
->free_lists
= list
;
187 G_UNLOCK (current_allocator
);
192 g_slist_free_1 (GSList
*list
)
194 _g_slist_free_1 (list
);
196 #else /* DISABLE_MEM_POOLS */
198 #define _g_slist_alloc g_slist_alloc
204 list
= g_new0 (GSList
, 1);
210 g_slist_free (GSList
*list
)
222 #define _g_slist_free_1 g_slist_free_1
224 g_slist_free_1 (GSList
*list
)
232 g_slist_append (GSList
*list
,
238 new_list
= _g_slist_alloc ();
239 new_list
->data
= data
;
243 last
= g_slist_last (list
);
244 /* g_assert (last != NULL); */
245 last
->next
= new_list
;
254 g_slist_prepend (GSList
*list
,
259 new_list
= _g_slist_alloc ();
260 new_list
->data
= data
;
261 new_list
->next
= list
;
267 g_slist_insert (GSList
*list
,
276 return g_slist_append (list
, data
);
277 else if (position
== 0)
278 return g_slist_prepend (list
, data
);
280 new_list
= _g_slist_alloc ();
281 new_list
->data
= data
;
289 while ((position
-- > 0) && tmp_list
)
291 prev_list
= tmp_list
;
292 tmp_list
= tmp_list
->next
;
297 new_list
->next
= prev_list
->next
;
298 prev_list
->next
= new_list
;
302 new_list
->next
= list
;
310 g_slist_insert_before (GSList
*slist
,
316 slist
= g_slist_alloc ();
318 g_return_val_if_fail (sibling
== NULL
, slist
);
323 GSList
*node
, *last
= NULL
;
325 for (node
= slist
; node
; last
= node
, node
= last
->next
)
330 node
= g_slist_alloc ();
338 node
= g_slist_alloc ();
340 node
->next
= last
->next
;
349 g_slist_concat (GSList
*list1
, GSList
*list2
)
354 g_slist_last (list1
)->next
= list2
;
363 g_slist_remove (GSList
*list
,
366 GSList
*tmp
, *prev
= NULL
;
371 if (tmp
->data
== data
)
374 prev
->next
= tmp
->next
;
378 g_slist_free_1 (tmp
);
389 g_slist_remove_all (GSList
*list
,
392 GSList
*tmp
, *prev
= NULL
;
397 if (tmp
->data
== data
)
399 GSList
*next
= tmp
->next
;
406 g_slist_free_1 (tmp
);
419 static inline GSList
*
420 _g_slist_remove_link (GSList
*list
,
434 prev
->next
= tmp
->next
;
450 g_slist_remove_link (GSList
*list
,
453 return _g_slist_remove_link (list
, link
);
457 g_slist_delete_link (GSList
*list
,
460 list
= _g_slist_remove_link (list
, link
);
461 _g_slist_free_1 (link
);
467 g_slist_copy (GSList
*list
)
469 GSList
*new_list
= NULL
;
475 new_list
= _g_slist_alloc ();
476 new_list
->data
= list
->data
;
481 last
->next
= _g_slist_alloc ();
483 last
->data
= list
->data
;
492 g_slist_reverse (GSList
*list
)
498 GSList
*next
= list
->next
;
510 g_slist_nth (GSList
*list
,
513 while (n
-- > 0 && list
)
520 g_slist_nth_data (GSList
*list
,
523 while (n
-- > 0 && list
)
526 return list
? list
->data
: NULL
;
530 g_slist_find (GSList
*list
,
535 if (list
->data
== data
)
544 g_slist_find_custom (GSList
*list
,
548 g_return_val_if_fail (func
!= NULL
, list
);
552 if (! func (list
->data
, data
))
561 g_slist_position (GSList
*list
,
579 g_slist_index (GSList
*list
,
587 if (list
->data
== data
)
597 g_slist_last (GSList
*list
)
609 g_slist_length (GSList
*list
)
624 g_slist_foreach (GSList
*list
,
630 GSList
*next
= list
->next
;
631 (*func
) (list
->data
, user_data
);
637 g_slist_insert_sorted (GSList
*list
,
641 GSList
*tmp_list
= list
;
642 GSList
*prev_list
= NULL
;
646 g_return_val_if_fail (func
!= NULL
, list
);
650 new_list
= _g_slist_alloc ();
651 new_list
->data
= data
;
655 cmp
= (*func
) (data
, tmp_list
->data
);
657 while ((tmp_list
->next
) && (cmp
> 0))
659 prev_list
= tmp_list
;
660 tmp_list
= tmp_list
->next
;
661 cmp
= (*func
) (data
, tmp_list
->data
);
664 new_list
= _g_slist_alloc ();
665 new_list
->data
= data
;
667 if ((!tmp_list
->next
) && (cmp
> 0))
669 tmp_list
->next
= new_list
;
675 prev_list
->next
= new_list
;
676 new_list
->next
= tmp_list
;
681 new_list
->next
= list
;
687 g_slist_sort_merge (GSList
*l1
,
701 cmp
= ((GCompareDataFunc
) compare_func
) (l1
->data
, l2
->data
, user_data
);
703 cmp
= ((GCompareFunc
) compare_func
) (l1
->data
, l2
->data
);
716 l
->next
= l1
? l1
: l2
;
722 g_slist_sort_real (GSList
*list
,
737 while ((l2
= l2
->next
) != NULL
)
739 if ((l2
= l2
->next
) == NULL
)
746 return g_slist_sort_merge (g_slist_sort_real (list
, compare_func
, use_data
, user_data
),
747 g_slist_sort_real (l2
, compare_func
, use_data
, user_data
),
754 g_slist_sort (GSList
*list
,
755 GCompareFunc compare_func
)
757 return g_slist_sort_real (list
, (GFunc
) compare_func
, FALSE
, NULL
);
761 g_slist_sort_with_data (GSList
*list
,
762 GCompareDataFunc compare_func
,
765 return g_slist_sort_real (list
, (GFunc
) compare_func
, TRUE
, user_data
);
768 #define __G_SLIST_C__
769 #include "galiasdef.c"