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;
35 g_list_allocator_new ()
37 GRealListAllocator* allocator = g_new (GRealListAllocator, 1);
39 allocator->list_mem_chunk = NULL;
40 allocator->free_list = NULL;
42 return (GListAllocator*) allocator;
46 g_list_allocator_free (GListAllocator* fallocator)
48 GRealListAllocator* allocator = (GRealListAllocator *) fallocator;
50 if (allocator && allocator->list_mem_chunk)
51 g_mem_chunk_destroy (allocator->list_mem_chunk);
57 g_list_set_allocator (GListAllocator* fallocator)
59 GRealListAllocator* allocator = (GRealListAllocator *) fallocator;
60 GRealListAllocator* old_allocator = current_allocator;
63 current_allocator = allocator;
66 if (!default_allocator)
67 default_allocator = (GRealListAllocator*) g_list_allocator_new ();
68 current_allocator = default_allocator;
71 if (!current_allocator->list_mem_chunk)
72 current_allocator->list_mem_chunk = g_mem_chunk_new ("list mem chunk",
77 return (GListAllocator*) (old_allocator == default_allocator ? NULL : old_allocator);
86 g_list_set_allocator (NULL);
87 if (current_allocator->free_list)
89 new_list = current_allocator->free_list;
90 current_allocator->free_list = current_allocator->free_list->next;
94 new_list = g_chunk_new (GList, current_allocator->list_mem_chunk);
97 new_list->data = NULL;
98 new_list->next = NULL;
99 new_list->prev = NULL;
105 g_list_free (GList *list)
111 last = g_list_last (list);
112 last->next = current_allocator->free_list;
113 current_allocator->free_list = list;
118 g_list_free_1 (GList *list)
122 list->next = current_allocator->free_list;
123 current_allocator->free_list = list;
128 g_list_append (GList *list,
134 new_list = g_list_alloc ();
135 new_list->data = data;
143 last = g_list_last (list);
144 g_assert (last != NULL);
145 last->next = new_list;
146 new_list->prev = last;
153 g_list_prepend (GList *list,
158 new_list = g_list_alloc ();
159 new_list->data = data;
164 list->prev->next = new_list;
165 new_list->prev = list->prev;
166 list->prev = new_list;
168 new_list->next = list;
174 g_list_insert (GList *list,
182 return g_list_append (list, data);
183 else if (position == 0)
184 return g_list_prepend (list, data);
186 tmp_list = g_list_nth (list, position);
188 return g_list_append (list, data);
190 new_list = g_list_alloc ();
191 new_list->data = data;
194 tmp_list->prev->next = new_list;
195 new_list->next = tmp_list;
196 new_list->prev = tmp_list->prev;
197 tmp_list->prev = new_list;
199 if (tmp_list == list)
206 g_list_remove (GList *list,
214 if (tmp->data == data)
217 tmp->prev->next = tmp->next;
219 tmp->next->prev = tmp->prev;
237 g_list_remove_link (GList *list,
243 link->prev->next = link->next;
245 link->next->prev = link->prev;
258 g_list_reverse (GList *list)
268 list->next = list->prev;
277 g_list_nth (GList *list,
280 while ((n-- > 0) && list)
287 g_list_find (GList *list,
292 if (list->data == data)
301 g_list_last (GList *list)
313 g_list_first (GList *list)
325 g_list_length (GList *list)
340 g_list_foreach (GList *list,
346 (*func) (list->data, user_data);