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_copy (GSList *list)
326 GSList *new_list = NULL;
332 new_list = g_slist_alloc ();
333 new_list->data = list->data;
338 last->next = g_slist_alloc ();
340 last->data = list->data;
349 g_slist_reverse (GSList *list)
373 g_slist_nth (GSList *list,
376 while ((n-- > 0) && list)
383 g_slist_nth_data (GSList *list,
386 while ((n-- > 0) && list)
389 return list ? list->data : NULL;
393 g_slist_find (GSList *list,
398 if (list->data == data)
407 g_slist_find_custom (GSList *list,
411 g_return_val_if_fail (func != NULL, list);
415 if (! func (list->data, data))
424 g_slist_position (GSList *list,
442 g_slist_index (GSList *list,
450 if (list->data == data)
460 g_slist_last (GSList *list)
472 g_slist_length (GSList *list)
487 g_slist_foreach (GSList *list,
493 (*func) (list->data, user_data);
499 g_slist_insert_sorted (GSList *list,
503 GSList *tmp_list = list;
504 GSList *prev_list = NULL;
508 g_return_val_if_fail (func != NULL, list);
512 new_list = g_slist_alloc();
513 new_list->data = data;
517 cmp = (*func) (data, tmp_list->data);
519 while ((tmp_list->next) && (cmp > 0))
521 prev_list = tmp_list;
522 tmp_list = tmp_list->next;
523 cmp = (*func) (data, tmp_list->data);
526 new_list = g_slist_alloc();
527 new_list->data = data;
529 if ((!tmp_list->next) && (cmp > 0))
531 tmp_list->next = new_list;
537 prev_list->next = new_list;
538 new_list->next = tmp_list;
543 new_list->next = list;
549 g_slist_sort_merge (GSList *l1,
551 GCompareFunc compare_func)
559 if (compare_func(l1->data,l2->data) < 0)
570 l->next= l1 ? l1 : l2;
576 g_slist_sort (GSList *list,
577 GCompareFunc compare_func)
589 while ((l2 = l2->next) != NULL)
591 if ((l2 = l2->next) == NULL)
598 return g_slist_sort_merge (g_slist_sort (list, compare_func),
599 g_slist_sort (l2, compare_func),