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/.
38 #ifndef DISABLE_MEM_POOLS
39 struct _GAllocator
/* from gmem.c */
47 GSList
*free_lists
; /* implementation specific */
50 G_LOCK_DEFINE_STATIC (current_allocator
);
51 static GAllocator
*current_allocator
= NULL
;
53 /* HOLDS: current_allocator_lock */
55 g_slist_validate_allocator (GAllocator
*allocator
)
57 g_return_if_fail (allocator
!= NULL
);
58 g_return_if_fail (allocator
->is_unused
== TRUE
);
60 if (allocator
->type
!= G_ALLOCATOR_SLIST
)
62 allocator
->type
= G_ALLOCATOR_SLIST
;
63 if (allocator
->mem_chunk
)
65 g_mem_chunk_destroy (allocator
->mem_chunk
);
66 allocator
->mem_chunk
= NULL
;
70 if (!allocator
->mem_chunk
)
72 allocator
->mem_chunk
= g_mem_chunk_new (allocator
->name
,
74 sizeof (GSList
) * allocator
->n_preallocs
,
76 allocator
->free_lists
= NULL
;
79 allocator
->is_unused
= FALSE
;
83 g_slist_push_allocator (GAllocator
*allocator
)
85 G_LOCK (current_allocator
);
86 g_slist_validate_allocator (allocator
);
87 allocator
->last
= current_allocator
;
88 current_allocator
= allocator
;
89 G_UNLOCK (current_allocator
);
93 g_slist_pop_allocator (void)
95 G_LOCK (current_allocator
);
96 if (current_allocator
)
98 GAllocator
*allocator
;
100 allocator
= current_allocator
;
101 current_allocator
= allocator
->last
;
102 allocator
->last
= NULL
;
103 allocator
->is_unused
= TRUE
;
105 G_UNLOCK (current_allocator
);
108 static inline GSList
*
109 _g_slist_alloc (void)
113 G_LOCK (current_allocator
);
114 if (!current_allocator
)
116 GAllocator
*allocator
= g_allocator_new ("GLib default GSList allocator",
118 g_slist_validate_allocator (allocator
);
119 allocator
->last
= NULL
;
120 current_allocator
= allocator
;
122 if (!current_allocator
->free_lists
)
124 list
= g_chunk_new (GSList
, current_allocator
->mem_chunk
);
129 if (current_allocator
->free_lists
->data
)
131 list
= current_allocator
->free_lists
->data
;
132 current_allocator
->free_lists
->data
= list
->next
;
137 list
= current_allocator
->free_lists
;
138 current_allocator
->free_lists
= list
->next
;
141 G_UNLOCK (current_allocator
);
151 return _g_slist_alloc ();
155 g_slist_free (GSList
*list
)
159 GSList
*last_node
= list
;
161 #ifdef ENABLE_GC_FRIENDLY
162 while (last_node
->next
)
164 last_node
->data
= NULL
;
165 last_node
= last_node
->next
;
167 last_node
->data
= NULL
;
168 #else /* !ENABLE_GC_FRIENDLY */
169 list
->data
= list
->next
;
170 #endif /* ENABLE_GC_FRIENDLY */
172 G_LOCK (current_allocator
);
173 last_node
->next
= current_allocator
->free_lists
;
174 current_allocator
->free_lists
= list
;
175 G_UNLOCK (current_allocator
);
180 _g_slist_free_1 (GSList
*list
)
185 G_LOCK (current_allocator
);
186 list
->next
= current_allocator
->free_lists
;
187 current_allocator
->free_lists
= list
;
188 G_UNLOCK (current_allocator
);
193 g_slist_free_1 (GSList
*list
)
195 _g_slist_free_1 (list
);
197 #else /* DISABLE_MEM_POOLS */
199 #define _g_slist_alloc g_slist_alloc
205 list
= g_new0 (GSList
, 1);
211 g_slist_free (GSList
*list
)
223 #define _g_slist_free_1 g_slist_free_1
225 g_slist_free_1 (GSList
*list
)
233 g_slist_append (GSList
*list
,
239 new_list
= _g_slist_alloc ();
240 new_list
->data
= data
;
244 last
= g_slist_last (list
);
245 /* g_assert (last != NULL); */
246 last
->next
= new_list
;
255 g_slist_prepend (GSList
*list
,
260 new_list
= _g_slist_alloc ();
261 new_list
->data
= data
;
262 new_list
->next
= list
;
268 g_slist_insert (GSList
*list
,
277 return g_slist_append (list
, data
);
278 else if (position
== 0)
279 return g_slist_prepend (list
, data
);
281 new_list
= _g_slist_alloc ();
282 new_list
->data
= data
;
290 while ((position
-- > 0) && tmp_list
)
292 prev_list
= tmp_list
;
293 tmp_list
= tmp_list
->next
;
298 new_list
->next
= prev_list
->next
;
299 prev_list
->next
= new_list
;
303 new_list
->next
= list
;
311 g_slist_insert_before (GSList
*slist
,
317 slist
= g_slist_alloc ();
319 g_return_val_if_fail (sibling
== NULL
, slist
);
324 GSList
*node
, *last
= NULL
;
326 for (node
= slist
; node
; last
= node
, node
= last
->next
)
331 node
= g_slist_alloc ();
339 node
= g_slist_alloc ();
341 node
->next
= last
->next
;
350 g_slist_concat (GSList
*list1
, GSList
*list2
)
355 g_slist_last (list1
)->next
= list2
;
364 g_slist_remove (GSList
*list
,
367 GSList
*tmp
, *prev
= NULL
;
372 if (tmp
->data
== data
)
375 prev
->next
= tmp
->next
;
379 g_slist_free_1 (tmp
);
390 g_slist_remove_all (GSList
*list
,
393 GSList
*tmp
, *prev
= NULL
;
398 if (tmp
->data
== data
)
400 GSList
*next
= tmp
->next
;
407 g_slist_free_1 (tmp
);
420 static inline GSList
*
421 _g_slist_remove_link (GSList
*list
,
435 prev
->next
= tmp
->next
;
451 g_slist_remove_link (GSList
*list
,
454 return _g_slist_remove_link (list
, link
);
458 g_slist_delete_link (GSList
*list
,
461 list
= _g_slist_remove_link (list
, link
);
462 _g_slist_free_1 (link
);
468 g_slist_copy (GSList
*list
)
470 GSList
*new_list
= NULL
;
476 new_list
= _g_slist_alloc ();
477 new_list
->data
= list
->data
;
482 last
->next
= _g_slist_alloc ();
484 last
->data
= list
->data
;
493 g_slist_reverse (GSList
*list
)
499 GSList
*next
= list
->next
;
511 g_slist_nth (GSList
*list
,
514 while (n
-- > 0 && list
)
521 g_slist_nth_data (GSList
*list
,
524 while (n
-- > 0 && list
)
527 return list
? list
->data
: NULL
;
531 g_slist_find (GSList
*list
,
536 if (list
->data
== data
)
545 g_slist_find_custom (GSList
*list
,
549 g_return_val_if_fail (func
!= NULL
, list
);
553 if (! func (list
->data
, data
))
562 g_slist_position (GSList
*list
,
580 g_slist_index (GSList
*list
,
588 if (list
->data
== data
)
598 g_slist_last (GSList
*list
)
610 g_slist_length (GSList
*list
)
625 g_slist_foreach (GSList
*list
,
631 GSList
*next
= list
->next
;
632 (*func
) (list
->data
, user_data
);
638 g_slist_insert_sorted (GSList
*list
,
642 GSList
*tmp_list
= list
;
643 GSList
*prev_list
= NULL
;
647 g_return_val_if_fail (func
!= NULL
, list
);
651 new_list
= _g_slist_alloc ();
652 new_list
->data
= data
;
656 cmp
= (*func
) (data
, tmp_list
->data
);
658 while ((tmp_list
->next
) && (cmp
> 0))
660 prev_list
= tmp_list
;
661 tmp_list
= tmp_list
->next
;
662 cmp
= (*func
) (data
, tmp_list
->data
);
665 new_list
= _g_slist_alloc ();
666 new_list
->data
= data
;
668 if ((!tmp_list
->next
) && (cmp
> 0))
670 tmp_list
->next
= new_list
;
676 prev_list
->next
= new_list
;
677 new_list
->next
= tmp_list
;
682 new_list
->next
= list
;
688 g_slist_sort_merge (GSList
*l1
,
702 cmp
= ((GCompareDataFunc
) compare_func
) (l1
->data
, l2
->data
, user_data
);
704 cmp
= ((GCompareFunc
) compare_func
) (l1
->data
, l2
->data
);
717 l
->next
= l1
? l1
: l2
;
723 g_slist_sort_real (GSList
*list
,
738 while ((l2
= l2
->next
) != NULL
)
740 if ((l2
= l2
->next
) == NULL
)
747 return g_slist_sort_merge (g_slist_sort_real (list
, compare_func
, use_data
, user_data
),
748 g_slist_sort_real (l2
, compare_func
, use_data
, user_data
),
755 g_slist_sort (GSList
*list
,
756 GCompareFunc compare_func
)
758 return g_slist_sort_real (list
, (GFunc
) compare_func
, FALSE
, NULL
);
762 g_slist_sort_with_data (GSList
*list
,
763 GCompareDataFunc compare_func
,
766 return g_slist_sort_real (list
, (GFunc
) compare_func
, TRUE
, user_data
);