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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 typedef struct _GRealListAllocator GRealListAllocator;
23 struct _GRealListAllocator
25 GMemChunk *list_mem_chunk;
30 static GRealListAllocator *default_allocator = NULL;
31 static GRealListAllocator *current_allocator = NULL;
34 g_slist_set_allocator (GListAllocator* fallocator)
36 GRealListAllocator* allocator = (GRealListAllocator *) fallocator;
37 GRealListAllocator* old_allocator = current_allocator;
40 current_allocator = allocator;
43 if (!default_allocator)
44 default_allocator = (GRealListAllocator*) g_list_allocator_new ();
45 current_allocator = default_allocator;
48 if (!current_allocator->list_mem_chunk)
49 current_allocator->list_mem_chunk = g_mem_chunk_new ("slist mem chunk",
54 return (GListAllocator*) (old_allocator == default_allocator ? NULL : old_allocator);
63 g_slist_set_allocator (NULL);
64 if (current_allocator->free_list)
66 new_list = current_allocator->free_list;
67 current_allocator->free_list = current_allocator->free_list->next;
71 new_list = g_chunk_new (GSList, current_allocator->list_mem_chunk);
74 new_list->data = NULL;
75 new_list->next = NULL;
81 g_slist_free (GSList *list)
87 last = g_slist_last (list);
88 last->next = current_allocator->free_list;
89 current_allocator->free_list = list;
94 g_slist_free_1 (GSList *list)
98 list->next = current_allocator->free_list;
99 current_allocator->free_list = list;
104 g_slist_append (GSList *list,
110 new_list = g_slist_alloc ();
111 new_list->data = data;
119 last = g_slist_last (list);
120 g_assert (last != NULL);
121 last->next = new_list;
128 g_slist_prepend (GSList *list,
133 new_list = g_slist_alloc ();
134 new_list->data = data;
135 new_list->next = list;
141 g_slist_insert (GSList *list,
152 while (tmp_list && (position-- > 0))
154 prev_list = tmp_list;
155 tmp_list = tmp_list->next;
158 if (!tmp_list && !prev_list)
161 new_list = g_slist_alloc ();
165 new_list->next = list;
170 new_list->next = prev_list->next;
171 prev_list->next = new_list;
178 g_slist_remove (GSList *list,
189 if (tmp->data == data)
192 prev->next = tmp->next;
210 g_slist_remove_link (GSList *list,
224 prev->next = tmp->next;
240 g_slist_reverse (GSList *list)
264 g_slist_nth (GSList *list,
267 while ((n-- > 0) && list)
274 g_slist_find (GSList *list,
279 if (list->data == data)
288 g_slist_last (GSList *list)
300 g_slist_length (GSList *list)
315 g_slist_foreach (GSList *list,
321 (*func) (list->data, user_data);