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,
336 if (tmp->data == data)
339 prev->next = tmp->next;
356 static inline GSList*
357 _g_slist_remove_link (GSList *list,
371 prev->next = tmp->next;
387 g_slist_remove_link (GSList *list,
390 return _g_slist_remove_link (list, link);
394 g_slist_delete_link (GSList *list,
397 list = _g_slist_remove_link (list, link);
398 _g_slist_free_1 (link);
404 g_slist_copy (GSList *list)
406 GSList *new_list = NULL;
412 new_list = _g_slist_alloc ();
413 new_list->data = list->data;
418 last->next = _g_slist_alloc ();
420 last->data = list->data;
429 g_slist_reverse (GSList *list)
435 GSList *next = list->next;
447 g_slist_nth (GSList *list,
450 while (n-- > 0 && list)
457 g_slist_nth_data (GSList *list,
460 while (n-- > 0 && list)
463 return list ? list->data : NULL;
467 g_slist_find (GSList *list,
472 if (list->data == data)
481 g_slist_find_custom (GSList *list,
485 g_return_val_if_fail (func != NULL, list);
489 if (! func (list->data, data))
498 g_slist_position (GSList *list,
516 g_slist_index (GSList *list,
524 if (list->data == data)
534 g_slist_last (GSList *list)
546 g_slist_length (GSList *list)
561 g_slist_foreach (GSList *list,
567 (*func) (list->data, user_data);
573 g_slist_insert_sorted (GSList *list,
577 GSList *tmp_list = list;
578 GSList *prev_list = NULL;
582 g_return_val_if_fail (func != NULL, list);
586 new_list = _g_slist_alloc ();
587 new_list->data = data;
591 cmp = (*func) (data, tmp_list->data);
593 while ((tmp_list->next) && (cmp > 0))
595 prev_list = tmp_list;
596 tmp_list = tmp_list->next;
597 cmp = (*func) (data, tmp_list->data);
600 new_list = _g_slist_alloc ();
601 new_list->data = data;
603 if ((!tmp_list->next) && (cmp > 0))
605 tmp_list->next = new_list;
611 prev_list->next = new_list;
612 new_list->next = tmp_list;
617 new_list->next = list;
623 g_slist_sort_merge (GSList *l1,
637 cmp = ((GCompareFuncData) compare_func) (l1->data, l2->data, user_data);
639 cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
652 l->next= l1 ? l1 : l2;
658 g_slist_sort_real (GSList *list,
673 while ((l2 = l2->next) != NULL)
675 if ((l2 = l2->next) == NULL)
682 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, use_data, user_data),
683 g_slist_sort_real (l2, compare_func, use_data, user_data),
690 g_slist_sort (GSList *list,
691 GCompareFunc compare_func)
693 return g_slist_sort_real (list, (GFunc) compare_func, FALSE, NULL);
697 g_slist_sort_with_data (GSList *list,
698 GCompareFuncData compare_func,
701 return g_slist_sort_real (list, (GFunc) compare_func, TRUE, user_data);