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_concat (GSList *list1, GSList *list2)
316 g_slist_last (list1)->next = list2;
325 g_slist_remove (GSList *list,
328 GSList *tmp, *prev = NULL;
333 if (tmp->data == data)
336 prev->next = tmp->next;
340 g_slist_free_1 (tmp);
351 g_slist_remove_all (GSList *list,
354 GSList *tmp, *prev = NULL;
359 if (tmp->data == data)
361 GSList *next = tmp->next;
368 g_slist_free_1 (tmp);
381 static inline GSList*
382 _g_slist_remove_link (GSList *list,
396 prev->next = tmp->next;
412 g_slist_remove_link (GSList *list,
415 return _g_slist_remove_link (list, link);
419 g_slist_delete_link (GSList *list,
422 list = _g_slist_remove_link (list, link);
423 _g_slist_free_1 (link);
429 g_slist_copy (GSList *list)
431 GSList *new_list = NULL;
437 new_list = _g_slist_alloc ();
438 new_list->data = list->data;
443 last->next = _g_slist_alloc ();
445 last->data = list->data;
454 g_slist_reverse (GSList *list)
460 GSList *next = list->next;
472 g_slist_nth (GSList *list,
475 while (n-- > 0 && list)
482 g_slist_nth_data (GSList *list,
485 while (n-- > 0 && list)
488 return list ? list->data : NULL;
492 g_slist_find (GSList *list,
497 if (list->data == data)
506 g_slist_find_custom (GSList *list,
510 g_return_val_if_fail (func != NULL, list);
514 if (! func (list->data, data))
523 g_slist_position (GSList *list,
541 g_slist_index (GSList *list,
549 if (list->data == data)
559 g_slist_last (GSList *list)
571 g_slist_length (GSList *list)
586 g_slist_foreach (GSList *list,
592 (*func) (list->data, user_data);
598 g_slist_insert_sorted (GSList *list,
602 GSList *tmp_list = list;
603 GSList *prev_list = NULL;
607 g_return_val_if_fail (func != NULL, list);
611 new_list = _g_slist_alloc ();
612 new_list->data = data;
616 cmp = (*func) (data, tmp_list->data);
618 while ((tmp_list->next) && (cmp > 0))
620 prev_list = tmp_list;
621 tmp_list = tmp_list->next;
622 cmp = (*func) (data, tmp_list->data);
625 new_list = _g_slist_alloc ();
626 new_list->data = data;
628 if ((!tmp_list->next) && (cmp > 0))
630 tmp_list->next = new_list;
636 prev_list->next = new_list;
637 new_list->next = tmp_list;
642 new_list->next = list;
648 g_slist_sort_merge (GSList *l1,
662 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
664 cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
677 l->next= l1 ? l1 : l2;
683 g_slist_sort_real (GSList *list,
698 while ((l2 = l2->next) != NULL)
700 if ((l2 = l2->next) == NULL)
707 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, use_data, user_data),
708 g_slist_sort_real (l2, compare_func, use_data, user_data),
715 g_slist_sort (GSList *list,
716 GCompareFunc compare_func)
718 return g_slist_sort_real (list, (GFunc) compare_func, FALSE, NULL);
722 g_slist_sort_with_data (GSList *list,
723 GCompareDataFunc compare_func,
726 return g_slist_sort_real (list, (GFunc) compare_func, TRUE, user_data);