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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
34 struct _GAllocator /* from gmem.c */
42 GSList *free_lists; /* implementation specific */
45 G_LOCK_DEFINE_STATIC (current_allocator);
46 static GAllocator *current_allocator = NULL;
48 /* HOLDS: current_allocator_lock */
50 g_slist_validate_allocator (GAllocator *allocator)
52 g_return_if_fail (allocator != NULL);
53 g_return_if_fail (allocator->is_unused == TRUE);
55 if (allocator->type != G_ALLOCATOR_SLIST)
57 allocator->type = G_ALLOCATOR_SLIST;
58 if (allocator->mem_chunk)
60 g_mem_chunk_destroy (allocator->mem_chunk);
61 allocator->mem_chunk = NULL;
65 if (!allocator->mem_chunk)
67 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
69 sizeof (GSList) * allocator->n_preallocs,
71 allocator->free_lists = NULL;
74 allocator->is_unused = FALSE;
78 g_slist_push_allocator (GAllocator *allocator)
80 G_LOCK (current_allocator);
81 g_slist_validate_allocator (allocator);
82 allocator->last = current_allocator;
83 current_allocator = allocator;
84 G_UNLOCK (current_allocator);
88 g_slist_pop_allocator (void)
90 G_LOCK (current_allocator);
91 if (current_allocator)
93 GAllocator *allocator;
95 allocator = current_allocator;
96 current_allocator = allocator->last;
97 allocator->last = NULL;
98 allocator->is_unused = TRUE;
100 G_UNLOCK (current_allocator);
103 static inline GSList*
104 _g_slist_alloc (void)
108 G_LOCK (current_allocator);
109 if (!current_allocator)
111 GAllocator *allocator = g_allocator_new ("GLib default GSList allocator",
113 g_slist_validate_allocator (allocator);
114 allocator->last = NULL;
115 current_allocator = allocator;
117 if (!current_allocator->free_lists)
119 list = g_chunk_new (GSList, current_allocator->mem_chunk);
124 if (current_allocator->free_lists->data)
126 list = current_allocator->free_lists->data;
127 current_allocator->free_lists->data = list->next;
132 list = current_allocator->free_lists;
133 current_allocator->free_lists = list->next;
136 G_UNLOCK (current_allocator);
146 return _g_slist_alloc ();
150 g_slist_free (GSList *list)
154 list->data = list->next;
155 G_LOCK (current_allocator);
156 list->next = current_allocator->free_lists;
157 current_allocator->free_lists = list;
158 G_UNLOCK (current_allocator);
163 _g_slist_free_1 (GSList *list)
168 G_LOCK (current_allocator);
169 list->next = current_allocator->free_lists;
170 current_allocator->free_lists = list;
171 G_UNLOCK (current_allocator);
176 g_slist_free_1 (GSList *list)
178 _g_slist_free_1 (list);
182 g_slist_append (GSList *list,
188 new_list = _g_slist_alloc ();
189 new_list->data = data;
193 last = g_slist_last (list);
194 /* g_assert (last != NULL); */
195 last->next = new_list;
204 g_slist_prepend (GSList *list,
209 new_list = _g_slist_alloc ();
210 new_list->data = data;
211 new_list->next = list;
217 g_slist_insert (GSList *list,
226 return g_slist_append (list, data);
227 else if (position == 0)
228 return g_slist_prepend (list, data);
230 new_list = _g_slist_alloc ();
231 new_list->data = data;
239 while ((position-- > 0) && tmp_list)
241 prev_list = tmp_list;
242 tmp_list = tmp_list->next;
247 new_list->next = prev_list->next;
248 prev_list->next = new_list;
252 new_list->next = list;
260 g_slist_concat (GSList *list1, GSList *list2)
265 g_slist_last (list1)->next = list2;
274 g_slist_remove (GSList *list,
285 if (tmp->data == data)
288 prev->next = tmp->next;
305 static inline GSList*
306 _g_slist_remove_link (GSList *list,
320 prev->next = tmp->next;
336 g_slist_remove_link (GSList *list,
339 return _g_slist_remove_link (list, link);
343 g_slist_delete_link (GSList *list,
346 list = _g_slist_remove_link (list, link);
347 _g_slist_free_1 (link);
353 g_slist_copy (GSList *list)
355 GSList *new_list = NULL;
361 new_list = _g_slist_alloc ();
362 new_list->data = list->data;
367 last->next = _g_slist_alloc ();
369 last->data = list->data;
378 g_slist_reverse (GSList *list)
396 g_slist_nth (GSList *list,
399 while (n-- > 0 && list)
406 g_slist_nth_data (GSList *list,
409 while (n-- > 0 && list)
412 return list ? list->data : NULL;
416 g_slist_find (GSList *list,
421 if (list->data == data)
430 g_slist_find_custom (GSList *list,
434 g_return_val_if_fail (func != NULL, list);
438 if (! func (list->data, data))
447 g_slist_position (GSList *list,
465 g_slist_index (GSList *list,
473 if (list->data == data)
483 g_slist_last (GSList *list)
495 g_slist_length (GSList *list)
510 g_slist_foreach (GSList *list,
516 (*func) (list->data, user_data);
522 g_slist_insert_sorted (GSList *list,
526 GSList *tmp_list = list;
527 GSList *prev_list = NULL;
531 g_return_val_if_fail (func != NULL, list);
535 new_list = _g_slist_alloc ();
536 new_list->data = data;
540 cmp = (*func) (data, tmp_list->data);
542 while ((tmp_list->next) && (cmp > 0))
544 prev_list = tmp_list;
545 tmp_list = tmp_list->next;
546 cmp = (*func) (data, tmp_list->data);
549 new_list = _g_slist_alloc ();
550 new_list->data = data;
552 if ((!tmp_list->next) && (cmp > 0))
554 tmp_list->next = new_list;
560 prev_list->next = new_list;
561 new_list->next = tmp_list;
566 new_list->next = list;
572 g_slist_sort_merge (GSList *l1,
574 GCompareFunc compare_func)
582 if (compare_func(l1->data,l2->data) < 0)
593 l->next= l1 ? l1 : l2;
599 g_slist_sort (GSList *list,
600 GCompareFunc compare_func)
612 while ((l2 = l2->next) != NULL)
614 if ((l2 = l2->next) == NULL)
621 return g_slist_sort_merge (g_slist_sort (list, compare_func),
622 g_slist_sort (l2, compare_func),