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 typedef struct _GRealListAllocator GRealListAllocator;
24 struct _GRealListAllocator
26 GMemChunk *list_mem_chunk;
31 static GRealListAllocator *default_allocator = NULL;
32 static GRealListAllocator *current_allocator = NULL;
35 g_slist_set_allocator (GListAllocator* fallocator)
37 GRealListAllocator* allocator = (GRealListAllocator *) fallocator;
38 GRealListAllocator* old_allocator = current_allocator;
41 current_allocator = allocator;
44 if (!default_allocator)
45 default_allocator = (GRealListAllocator*) g_list_allocator_new ();
46 current_allocator = default_allocator;
49 if (!current_allocator->list_mem_chunk)
50 current_allocator->list_mem_chunk = g_mem_chunk_new ("slist mem chunk",
55 return (GListAllocator*) (old_allocator == default_allocator ? NULL : old_allocator);
64 g_slist_set_allocator (NULL);
65 if (current_allocator->free_list)
67 new_list = current_allocator->free_list;
68 current_allocator->free_list = current_allocator->free_list->next;
72 new_list = g_chunk_new (GSList, current_allocator->list_mem_chunk);
75 new_list->data = NULL;
76 new_list->next = NULL;
82 g_slist_free (GSList *list)
88 last = g_slist_last (list);
89 last->next = current_allocator->free_list;
90 current_allocator->free_list = list;
95 g_slist_free_1 (GSList *list)
99 list->next = current_allocator->free_list;
100 current_allocator->free_list = list;
105 g_slist_append (GSList *list,
111 new_list = g_slist_alloc ();
112 new_list->data = data;
116 last = g_slist_last (list);
117 /* g_assert (last != NULL); */
118 last->next = new_list;
127 g_slist_prepend (GSList *list,
132 new_list = g_slist_alloc ();
133 new_list->data = data;
134 new_list->next = list;
140 g_slist_insert (GSList *list,
149 return g_slist_append (list, data);
150 else if (position == 0)
151 return g_slist_prepend (list, data);
153 new_list = g_slist_alloc ();
154 new_list->data = data;
162 while ((position-- > 0) && tmp_list)
164 prev_list = tmp_list;
165 tmp_list = tmp_list->next;
170 new_list->next = prev_list->next;
171 prev_list->next = new_list;
175 new_list->next = list;
183 g_slist_concat (GSList *list1, GSList *list2)
188 g_slist_last (list1)->next = list2;
197 g_slist_remove (GSList *list,
208 if (tmp->data == data)
211 prev->next = tmp->next;
229 g_slist_remove_link (GSList *list,
243 prev->next = tmp->next;
259 g_slist_reverse (GSList *list)
283 g_slist_nth (GSList *list,
286 while ((n-- > 0) && list)
293 g_slist_nth_data (GSList *list,
296 while ((n-- > 0) && list)
299 return list ? list->data : NULL;
303 g_slist_find (GSList *list,
308 if (list->data == data)
317 g_slist_find_custom (GSList *list,
321 g_return_val_if_fail (func != NULL, list);
325 if (! func (list->data, data))
334 g_slist_position (GSList *list,
352 g_slist_index (GSList *list,
360 if (list->data == data)
370 g_slist_last (GSList *list)
382 g_slist_length (GSList *list)
397 g_slist_foreach (GSList *list,
403 (*func) (list->data, user_data);
409 g_slist_insert_sorted (GSList *list,
413 GSList *tmp_list = list;
414 GSList *prev_list = NULL;
418 g_return_val_if_fail (func != NULL, list);
422 new_list = g_slist_alloc();
423 new_list->data = data;
427 cmp = (*func) (data, tmp_list->data);
429 while ((tmp_list->next) && (cmp > 0))
431 prev_list = tmp_list;
432 tmp_list = tmp_list->next;
433 cmp = (*func) (data, tmp_list->data);
436 new_list = g_slist_alloc();
437 new_list->data = data;
439 if ((!tmp_list->next) && (cmp > 0))
441 tmp_list->next = new_list;
447 prev_list->next = new_list;
448 new_list->next = tmp_list;
453 new_list->next = list;