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/.
36 #ifndef DISABLE_MEM_POOLS
37 struct _GAllocator /* from gmem.c */
45 GSList *free_lists; /* implementation specific */
48 G_LOCK_DEFINE_STATIC (current_allocator);
49 static GAllocator *current_allocator = NULL;
51 /* HOLDS: current_allocator_lock */
53 g_slist_validate_allocator (GAllocator *allocator)
55 g_return_if_fail (allocator != NULL);
56 g_return_if_fail (allocator->is_unused == TRUE);
58 if (allocator->type != G_ALLOCATOR_SLIST)
60 allocator->type = G_ALLOCATOR_SLIST;
61 if (allocator->mem_chunk)
63 g_mem_chunk_destroy (allocator->mem_chunk);
64 allocator->mem_chunk = NULL;
68 if (!allocator->mem_chunk)
70 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
72 sizeof (GSList) * allocator->n_preallocs,
74 allocator->free_lists = NULL;
77 allocator->is_unused = FALSE;
81 g_slist_push_allocator (GAllocator *allocator)
83 G_LOCK (current_allocator);
84 g_slist_validate_allocator (allocator);
85 allocator->last = current_allocator;
86 current_allocator = allocator;
87 G_UNLOCK (current_allocator);
91 g_slist_pop_allocator (void)
93 G_LOCK (current_allocator);
94 if (current_allocator)
96 GAllocator *allocator;
98 allocator = current_allocator;
99 current_allocator = allocator->last;
100 allocator->last = NULL;
101 allocator->is_unused = TRUE;
103 G_UNLOCK (current_allocator);
106 static inline GSList*
107 _g_slist_alloc (void)
111 G_LOCK (current_allocator);
112 if (!current_allocator)
114 GAllocator *allocator = g_allocator_new ("GLib default GSList allocator",
116 g_slist_validate_allocator (allocator);
117 allocator->last = NULL;
118 current_allocator = allocator;
120 if (!current_allocator->free_lists)
122 list = g_chunk_new (GSList, current_allocator->mem_chunk);
127 if (current_allocator->free_lists->data)
129 list = current_allocator->free_lists->data;
130 current_allocator->free_lists->data = list->next;
135 list = current_allocator->free_lists;
136 current_allocator->free_lists = list->next;
139 G_UNLOCK (current_allocator);
149 return _g_slist_alloc ();
153 g_slist_free (GSList *list)
157 GSList *last_node = list;
159 #ifdef ENABLE_GC_FRIENDLY
160 while (last_node->next)
162 last_node->data = NULL;
163 last_node = last_node->next;
165 last_node->data = NULL;
166 #else /* !ENABLE_GC_FRIENDLY */
167 list->data = list->next;
168 #endif /* ENABLE_GC_FRIENDLY */
170 G_LOCK (current_allocator);
171 last_node->next = current_allocator->free_lists;
172 current_allocator->free_lists = list;
173 G_UNLOCK (current_allocator);
178 _g_slist_free_1 (GSList *list)
183 G_LOCK (current_allocator);
184 list->next = current_allocator->free_lists;
185 current_allocator->free_lists = list;
186 G_UNLOCK (current_allocator);
191 g_slist_free_1 (GSList *list)
193 _g_slist_free_1 (list);
195 #else /* DISABLE_MEM_POOLS */
197 #define _g_slist_alloc g_slist_alloc
203 list = g_new0 (GSList, 1);
209 g_slist_free (GSList *list)
221 #define _g_slist_free_1 g_slist_free_1
223 g_slist_free_1 (GSList *list)
231 g_slist_append (GSList *list,
237 new_list = _g_slist_alloc ();
238 new_list->data = data;
242 last = g_slist_last (list);
243 /* g_assert (last != NULL); */
244 last->next = new_list;
253 g_slist_prepend (GSList *list,
258 new_list = _g_slist_alloc ();
259 new_list->data = data;
260 new_list->next = list;
266 g_slist_insert (GSList *list,
275 return g_slist_append (list, data);
276 else if (position == 0)
277 return g_slist_prepend (list, data);
279 new_list = _g_slist_alloc ();
280 new_list->data = data;
288 while ((position-- > 0) && tmp_list)
290 prev_list = tmp_list;
291 tmp_list = tmp_list->next;
296 new_list->next = prev_list->next;
297 prev_list->next = new_list;
301 new_list->next = list;
309 g_slist_insert_before (GSList *slist,
315 slist = g_slist_alloc ();
317 g_return_val_if_fail (sibling == NULL, slist);
322 GSList *node, *last = NULL;
324 for (node = slist; node; last = node, node = last->next)
329 node = g_slist_alloc ();
337 node = g_slist_alloc ();
339 node->next = last->next;
348 g_slist_concat (GSList *list1, GSList *list2)
353 g_slist_last (list1)->next = list2;
362 g_slist_remove (GSList *list,
365 GSList *tmp, *prev = NULL;
370 if (tmp->data == data)
373 prev->next = tmp->next;
377 g_slist_free_1 (tmp);
388 g_slist_remove_all (GSList *list,
391 GSList *tmp, *prev = NULL;
396 if (tmp->data == data)
398 GSList *next = tmp->next;
405 g_slist_free_1 (tmp);
418 static inline GSList*
419 _g_slist_remove_link (GSList *list,
433 prev->next = tmp->next;
449 g_slist_remove_link (GSList *list,
452 return _g_slist_remove_link (list, link);
456 g_slist_delete_link (GSList *list,
459 list = _g_slist_remove_link (list, link);
460 _g_slist_free_1 (link);
466 g_slist_copy (GSList *list)
468 GSList *new_list = NULL;
474 new_list = _g_slist_alloc ();
475 new_list->data = list->data;
480 last->next = _g_slist_alloc ();
482 last->data = list->data;
491 g_slist_reverse (GSList *list)
497 GSList *next = list->next;
509 g_slist_nth (GSList *list,
512 while (n-- > 0 && list)
519 g_slist_nth_data (GSList *list,
522 while (n-- > 0 && list)
525 return list ? list->data : NULL;
529 g_slist_find (GSList *list,
534 if (list->data == data)
543 g_slist_find_custom (GSList *list,
547 g_return_val_if_fail (func != NULL, list);
551 if (! func (list->data, data))
560 g_slist_position (GSList *list,
578 g_slist_index (GSList *list,
586 if (list->data == data)
596 g_slist_last (GSList *list)
608 g_slist_length (GSList *list)
623 g_slist_foreach (GSList *list,
629 GSList *next = list->next;
630 (*func) (list->data, user_data);
636 g_slist_insert_sorted (GSList *list,
640 GSList *tmp_list = list;
641 GSList *prev_list = NULL;
645 g_return_val_if_fail (func != NULL, list);
649 new_list = _g_slist_alloc ();
650 new_list->data = data;
654 cmp = (*func) (data, tmp_list->data);
656 while ((tmp_list->next) && (cmp > 0))
658 prev_list = tmp_list;
659 tmp_list = tmp_list->next;
660 cmp = (*func) (data, tmp_list->data);
663 new_list = _g_slist_alloc ();
664 new_list->data = data;
666 if ((!tmp_list->next) && (cmp > 0))
668 tmp_list->next = new_list;
674 prev_list->next = new_list;
675 new_list->next = tmp_list;
680 new_list->next = list;
686 g_slist_sort_merge (GSList *l1,
700 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
702 cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
715 l->next= l1 ? l1 : l2;
721 g_slist_sort_real (GSList *list,
736 while ((l2 = l2->next) != NULL)
738 if ((l2 = l2->next) == NULL)
745 return g_slist_sort_merge (g_slist_sort_real (list, compare_func, use_data, user_data),
746 g_slist_sort_real (l2, compare_func, use_data, user_data),
753 g_slist_sort (GSList *list,
754 GCompareFunc compare_func)
756 return g_slist_sort_real (list, (GFunc) compare_func, FALSE, NULL);
760 g_slist_sort_with_data (GSList *list,
761 GCompareDataFunc compare_func,
764 return g_slist_sort_real (list, (GFunc) compare_func, TRUE, user_data);