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 GList *free_lists; /* implementation specific */
33 static GAllocator *current_allocator = NULL;
36 g_list_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_LIST)
43 allocator->type = G_ALLOCATOR_LIST;
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 (GList) * allocator->n_preallocs,
57 allocator->free_lists = NULL;
60 allocator->is_unused = FALSE;
61 allocator->last = current_allocator;
62 current_allocator = allocator;
66 g_list_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_list_push_allocator (g_allocator_new ("GLib default GList allocator", 1024));
87 if (!current_allocator->free_lists)
89 list = g_chunk_new (GList, 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;
113 g_list_free (GList *list)
117 list->data = list->next;
118 list->next = current_allocator->free_lists;
119 current_allocator->free_lists = list;
124 g_list_free_1 (GList *list)
129 list->next = current_allocator->free_lists;
130 current_allocator->free_lists = list;
135 g_list_append (GList *list,
141 new_list = g_list_alloc ();
142 new_list->data = data;
146 last = g_list_last (list);
147 /* g_assert (last != NULL); */
148 last->next = new_list;
149 new_list->prev = last;
158 g_list_prepend (GList *list,
163 new_list = g_list_alloc ();
164 new_list->data = data;
170 list->prev->next = new_list;
171 new_list->prev = list->prev;
173 list->prev = new_list;
174 new_list->next = list;
181 g_list_insert (GList *list,
189 return g_list_append (list, data);
190 else if (position == 0)
191 return g_list_prepend (list, data);
193 tmp_list = g_list_nth (list, position);
195 return g_list_append (list, data);
197 new_list = g_list_alloc ();
198 new_list->data = data;
202 tmp_list->prev->next = new_list;
203 new_list->prev = tmp_list->prev;
205 new_list->next = tmp_list;
206 tmp_list->prev = new_list;
208 if (tmp_list == list)
215 g_list_concat (GList *list1, GList *list2)
221 tmp_list = g_list_last (list1);
223 tmp_list->next = list2;
226 list2->prev = tmp_list;
233 g_list_remove (GList *list,
241 if (tmp->data != data)
246 tmp->prev->next = tmp->next;
248 tmp->next->prev = tmp->prev;
262 g_list_remove_link (GList *list,
268 link->prev->next = link->next;
270 link->next->prev = link->prev;
283 g_list_copy (GList *list)
285 GList *new_list = NULL;
291 new_list = g_list_alloc ();
292 new_list->data = list->data;
297 last->next = g_list_alloc ();
298 last->next->prev = last;
300 last->data = list->data;
309 g_list_reverse (GList *list)
318 last->next = last->prev;
326 g_list_nth (GList *list,
329 while ((n-- > 0) && list)
336 g_list_nth_data (GList *list,
339 while ((n-- > 0) && list)
342 return list ? list->data : NULL;
346 g_list_find (GList *list,
351 if (list->data == data)
360 g_list_find_custom (GList *list,
364 g_return_val_if_fail (func != NULL, list);
368 if (! func (list->data, data))
378 g_list_position (GList *list,
396 g_list_index (GList *list,
404 if (list->data == data)
414 g_list_last (GList *list)
426 g_list_first (GList *list)
438 g_list_length (GList *list)
453 g_list_foreach (GList *list,
459 (*func) (list->data, user_data);
466 g_list_insert_sorted (GList *list,
470 GList *tmp_list = list;
474 g_return_val_if_fail (func != NULL, list);
478 new_list = g_list_alloc();
479 new_list->data = data;
483 cmp = (*func) (data, tmp_list->data);
485 while ((tmp_list->next) && (cmp > 0))
487 tmp_list = tmp_list->next;
488 cmp = (*func) (data, tmp_list->data);
491 new_list = g_list_alloc();
492 new_list->data = data;
494 if ((!tmp_list->next) && (cmp > 0))
496 tmp_list->next = new_list;
497 new_list->prev = tmp_list;
503 tmp_list->prev->next = new_list;
504 new_list->prev = tmp_list->prev;
506 new_list->next = tmp_list;
507 tmp_list->prev = new_list;
509 if (tmp_list == list)
516 g_list_sort_merge (GList *l1,
518 GCompareFunc compare_func)
520 GList list, *l, *lprev;
527 if (compare_func (l1->data, l2->data) < 0)
544 l->next = l1 ? l1 : l2;
551 g_list_sort (GList *list,
552 GCompareFunc compare_func)
564 while ((l2 = l2->next) != NULL)
566 if ((l2 = l2->next) == NULL)
573 return g_list_sort_merge (g_list_sort (list, compare_func),
574 g_list_sort (l2, compare_func),