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);
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);
144 g_slist_free (GSList *list)
148 list->data = list->next;
149 G_LOCK (current_allocator);
150 list->next = current_allocator->free_lists;
151 current_allocator->free_lists = list;
152 G_UNLOCK (current_allocator);
157 g_slist_free_1 (GSList *list)
162 G_LOCK (current_allocator);
163 list->next = current_allocator->free_lists;
164 current_allocator->free_lists = list;
165 G_UNLOCK (current_allocator);
170 g_slist_append (GSList *list,
176 new_list = g_slist_alloc ();
177 new_list->data = data;
181 last = g_slist_last (list);
182 /* g_assert (last != NULL); */
183 last->next = new_list;
192 g_slist_prepend (GSList *list,
197 new_list = g_slist_alloc ();
198 new_list->data = data;
199 new_list->next = list;
205 g_slist_insert (GSList *list,
214 return g_slist_append (list, data);
215 else if (position == 0)
216 return g_slist_prepend (list, data);
218 new_list = g_slist_alloc ();
219 new_list->data = data;
227 while ((position-- > 0) && tmp_list)
229 prev_list = tmp_list;
230 tmp_list = tmp_list->next;
235 new_list->next = prev_list->next;
236 prev_list->next = new_list;
240 new_list->next = list;
248 g_slist_concat (GSList *list1, GSList *list2)
253 g_slist_last (list1)->next = list2;
262 g_slist_remove (GSList *list,
273 if (tmp->data == data)
276 prev->next = tmp->next;
294 g_slist_remove_link (GSList *list,
308 prev->next = tmp->next;
324 g_slist_delete_link (GSList *list,
327 list = g_slist_remove_link (list, link);
328 g_slist_free_1 (link);
334 g_slist_copy (GSList *list)
336 GSList *new_list = NULL;
342 new_list = g_slist_alloc ();
343 new_list->data = list->data;
348 last->next = g_slist_alloc ();
350 last->data = list->data;
359 g_slist_reverse (GSList *list)
383 g_slist_nth (GSList *list,
386 while ((n-- > 0) && list)
393 g_slist_nth_data (GSList *list,
396 while ((n-- > 0) && list)
399 return list ? list->data : NULL;
403 g_slist_find (GSList *list,
408 if (list->data == data)
417 g_slist_find_custom (GSList *list,
421 g_return_val_if_fail (func != NULL, list);
425 if (! func (list->data, data))
434 g_slist_position (GSList *list,
452 g_slist_index (GSList *list,
460 if (list->data == data)
470 g_slist_last (GSList *list)
482 g_slist_length (GSList *list)
497 g_slist_foreach (GSList *list,
503 (*func) (list->data, user_data);
509 g_slist_insert_sorted (GSList *list,
513 GSList *tmp_list = list;
514 GSList *prev_list = NULL;
518 g_return_val_if_fail (func != NULL, list);
522 new_list = g_slist_alloc();
523 new_list->data = data;
527 cmp = (*func) (data, tmp_list->data);
529 while ((tmp_list->next) && (cmp > 0))
531 prev_list = tmp_list;
532 tmp_list = tmp_list->next;
533 cmp = (*func) (data, tmp_list->data);
536 new_list = g_slist_alloc();
537 new_list->data = data;
539 if ((!tmp_list->next) && (cmp > 0))
541 tmp_list->next = new_list;
547 prev_list->next = new_list;
548 new_list->next = tmp_list;
553 new_list->next = list;
559 g_slist_sort_merge (GSList *l1,
561 GCompareFunc compare_func)
569 if (compare_func(l1->data,l2->data) < 0)
580 l->next= l1 ? l1 : l2;
586 g_slist_sort (GSList *list,
587 GCompareFunc compare_func)
599 while ((l2 = l2->next) != NULL)
601 if ((l2 = l2->next) == NULL)
608 return g_slist_sort_merge (g_slist_sort (list, compare_func),
609 g_slist_sort (l2, compare_func),