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.
22 struct _GAllocator /* from gmem.c */
30 GSList *free_lists; /* implementation specific */
33 static GAllocator *current_allocator = NULL;
36 g_slist_push_allocator (GAllocator *allocator)
38 g_return_if_fail (allocator != NULL);
39 g_return_if_fail (allocator->is_unused == TRUE);
41 if (allocator->type != G_ALLOCATOR_SLIST)
43 allocator->type = G_ALLOCATOR_SLIST;
44 if (allocator->mem_chunk)
46 g_mem_chunk_destroy (allocator->mem_chunk);
47 allocator->mem_chunk = NULL;
51 if (!allocator->mem_chunk)
53 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
55 sizeof (GSList) * allocator->n_preallocs,
57 allocator->free_lists = NULL;
60 allocator->is_unused = FALSE;
61 allocator->last = current_allocator;
62 current_allocator = allocator;
66 g_slist_pop_allocator (void)
68 if (current_allocator)
70 GAllocator *allocator;
72 allocator = current_allocator;
73 current_allocator = allocator->last;
74 allocator->last = NULL;
75 allocator->is_unused = TRUE;
84 if (!current_allocator)
85 g_slist_push_allocator (g_allocator_new ("GLib default GSList allocator", 1024));
87 if (!current_allocator->free_lists)
89 list = g_chunk_new (GSList, current_allocator->mem_chunk);
94 if (current_allocator->free_lists->data)
96 list = current_allocator->free_lists->data;
97 current_allocator->free_lists->data = list->next;
102 list = current_allocator->free_lists;
103 current_allocator->free_lists = list->next;
112 g_slist_free (GSList *list)
116 list->data = list->next;
117 list->next = current_allocator->free_lists;
118 current_allocator->free_lists = list;
123 g_slist_free_1 (GSList *list)
128 list->next = current_allocator->free_lists;
129 current_allocator->free_lists = list;
134 g_slist_append (GSList *list,
140 new_list = g_slist_alloc ();
141 new_list->data = data;
145 last = g_slist_last (list);
146 /* g_assert (last != NULL); */
147 last->next = new_list;
156 g_slist_prepend (GSList *list,
161 new_list = g_slist_alloc ();
162 new_list->data = data;
163 new_list->next = list;
169 g_slist_insert (GSList *list,
178 return g_slist_append (list, data);
179 else if (position == 0)
180 return g_slist_prepend (list, data);
182 new_list = g_slist_alloc ();
183 new_list->data = data;
191 while ((position-- > 0) && tmp_list)
193 prev_list = tmp_list;
194 tmp_list = tmp_list->next;
199 new_list->next = prev_list->next;
200 prev_list->next = new_list;
204 new_list->next = list;
212 g_slist_concat (GSList *list1, GSList *list2)
217 g_slist_last (list1)->next = list2;
226 g_slist_remove (GSList *list,
237 if (tmp->data == data)
240 prev->next = tmp->next;
258 g_slist_remove_link (GSList *list,
272 prev->next = tmp->next;
288 g_slist_copy (GSList *list)
290 GSList *new_list = NULL;
296 new_list = g_slist_alloc ();
297 new_list->data = list->data;
302 last->next = g_slist_alloc ();
304 last->data = list->data;
313 g_slist_reverse (GSList *list)
337 g_slist_nth (GSList *list,
340 while ((n-- > 0) && list)
347 g_slist_nth_data (GSList *list,
350 while ((n-- > 0) && list)
353 return list ? list->data : NULL;
357 g_slist_find (GSList *list,
362 if (list->data == data)
371 g_slist_find_custom (GSList *list,
375 g_return_val_if_fail (func != NULL, list);
379 if (! func (list->data, data))
388 g_slist_position (GSList *list,
406 g_slist_index (GSList *list,
414 if (list->data == data)
424 g_slist_last (GSList *list)
436 g_slist_length (GSList *list)
451 g_slist_foreach (GSList *list,
457 (*func) (list->data, user_data);
463 g_slist_insert_sorted (GSList *list,
467 GSList *tmp_list = list;
468 GSList *prev_list = NULL;
472 g_return_val_if_fail (func != NULL, list);
476 new_list = g_slist_alloc();
477 new_list->data = data;
481 cmp = (*func) (data, tmp_list->data);
483 while ((tmp_list->next) && (cmp > 0))
485 prev_list = tmp_list;
486 tmp_list = tmp_list->next;
487 cmp = (*func) (data, tmp_list->data);
490 new_list = g_slist_alloc();
491 new_list->data = data;
493 if ((!tmp_list->next) && (cmp > 0))
495 tmp_list->next = new_list;
501 prev_list->next = new_list;
502 new_list->next = tmp_list;
507 new_list->next = list;
513 g_slist_sort_merge (GSList *l1,
515 GCompareFunc compare_func)
523 if (compare_func(l1->data,l2->data) < 0)
534 l->next= l1 ? l1 : l2;
540 g_slist_sort (GSList *list,
541 GCompareFunc compare_func)
553 while ((l2 = l2->next) != NULL)
555 if ((l2 = l2->next) == NULL)
562 return g_slist_sort_merge (g_slist_sort (list, compare_func),
563 g_slist_sort (l2, compare_func),