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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
37 #ifndef DISABLE_MEM_POOLS
38 struct _GAllocator /* from gmem.c */
46 GList *free_lists; /* implementation specific */
49 static GAllocator *current_allocator = NULL;
50 G_LOCK_DEFINE_STATIC (current_allocator);
52 /* HOLDS: current_allocator_lock */
54 g_list_validate_allocator (GAllocator *allocator)
56 g_return_if_fail (allocator != NULL);
57 g_return_if_fail (allocator->is_unused == TRUE);
59 if (allocator->type != G_ALLOCATOR_LIST)
61 allocator->type = G_ALLOCATOR_LIST;
62 if (allocator->mem_chunk)
64 g_mem_chunk_destroy (allocator->mem_chunk);
65 allocator->mem_chunk = NULL;
69 if (!allocator->mem_chunk)
71 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
73 sizeof (GList) * allocator->n_preallocs,
75 allocator->free_lists = NULL;
78 allocator->is_unused = FALSE;
82 g_list_push_allocator(GAllocator *allocator)
84 G_LOCK (current_allocator);
85 g_list_validate_allocator (allocator);
86 allocator->last = current_allocator;
87 current_allocator = allocator;
88 G_UNLOCK (current_allocator);
92 g_list_pop_allocator (void)
94 G_LOCK (current_allocator);
95 if (current_allocator)
97 GAllocator *allocator;
99 allocator = current_allocator;
100 current_allocator = allocator->last;
101 allocator->last = NULL;
102 allocator->is_unused = TRUE;
104 G_UNLOCK (current_allocator);
112 G_LOCK (current_allocator);
113 if (!current_allocator)
115 GAllocator *allocator = g_allocator_new ("GLib default GList allocator",
117 g_list_validate_allocator (allocator);
118 allocator->last = NULL;
119 current_allocator = allocator;
121 if (!current_allocator->free_lists)
123 list = g_chunk_new (GList, current_allocator->mem_chunk);
128 if (current_allocator->free_lists->data)
130 list = current_allocator->free_lists->data;
131 current_allocator->free_lists->data = list->next;
136 list = current_allocator->free_lists;
137 current_allocator->free_lists = list->next;
140 G_UNLOCK (current_allocator);
150 return _g_list_alloc ();
154 g_list_free (GList *list)
158 GList *last_node = list;
160 #ifdef ENABLE_GC_FRIENDLY
161 while (last_node->next)
163 last_node->data = NULL;
164 last_node->prev = NULL;
165 last_node = last_node->next;
167 last_node->data = NULL;
168 last_node->prev = NULL;
169 #else /* !ENABLE_GC_FRIENDLY */
170 list->data = list->next;
171 #endif /* ENABLE_GC_FRIENDLY */
173 G_LOCK (current_allocator);
174 last_node->next = current_allocator->free_lists;
175 current_allocator->free_lists = list;
176 G_UNLOCK (current_allocator);
181 _g_list_free_1 (GList *list)
187 #ifdef ENABLE_GC_FRIENDLY
189 #endif /* ENABLE_GC_FRIENDLY */
191 G_LOCK (current_allocator);
192 list->next = current_allocator->free_lists;
193 current_allocator->free_lists = list;
194 G_UNLOCK (current_allocator);
199 g_list_free_1 (GList *list)
201 _g_list_free_1 (list);
204 #else /* DISABLE_MEM_POOLS */
206 #define _g_list_alloc g_list_alloc
212 list = g_new0 (GList, 1);
218 g_list_free (GList *list)
230 #define _g_list_free_1 g_list_free_1
232 g_list_free_1 (GList *list)
240 g_list_append (GList *list,
246 new_list = _g_list_alloc ();
247 new_list->data = data;
251 last = g_list_last (list);
252 /* g_assert (last != NULL); */
253 last->next = new_list;
254 new_list->prev = last;
263 g_list_prepend (GList *list,
268 new_list = _g_list_alloc ();
269 new_list->data = data;
275 list->prev->next = new_list;
276 new_list->prev = list->prev;
278 list->prev = new_list;
279 new_list->next = list;
286 g_list_insert (GList *list,
294 return g_list_append (list, data);
295 else if (position == 0)
296 return g_list_prepend (list, data);
298 tmp_list = g_list_nth (list, position);
300 return g_list_append (list, data);
302 new_list = _g_list_alloc ();
303 new_list->data = data;
307 tmp_list->prev->next = new_list;
308 new_list->prev = tmp_list->prev;
310 new_list->next = tmp_list;
311 tmp_list->prev = new_list;
313 if (tmp_list == list)
320 g_list_insert_before (GList *list,
326 list = g_list_alloc ();
328 g_return_val_if_fail (sibling == NULL, list);
335 node = g_list_alloc ();
339 node->prev = sibling->prev;
340 node->prev->next = node;
341 node->next = sibling;
342 sibling->prev = node;
347 node->next = sibling;
348 sibling->prev = node;
349 g_return_val_if_fail (sibling == list, node);
361 last->next = g_list_alloc ();
362 last->next->data = data;
363 last->next->prev = last;
370 g_list_concat (GList *list1, GList *list2)
376 tmp_list = g_list_last (list1);
378 tmp_list->next = list2;
381 list2->prev = tmp_list;
388 g_list_remove (GList *list,
396 if (tmp->data != data)
401 tmp->prev->next = tmp->next;
403 tmp->next->prev = tmp->prev;
408 _g_list_free_1 (tmp);
417 g_list_remove_all (GList *list,
424 if (tmp->data != data)
428 GList *next = tmp->next;
431 tmp->prev->next = next;
435 next->prev = tmp->prev;
437 _g_list_free_1 (tmp);
445 _g_list_remove_link (GList *list,
451 link->prev->next = link->next;
453 link->next->prev = link->prev;
466 g_list_remove_link (GList *list,
469 return _g_list_remove_link (list, link);
473 g_list_delete_link (GList *list,
476 list = _g_list_remove_link (list, link);
477 _g_list_free_1 (link);
483 g_list_copy (GList *list)
485 GList *new_list = NULL;
491 new_list = _g_list_alloc ();
492 new_list->data = list->data;
497 last->next = _g_list_alloc ();
498 last->next->prev = last;
500 last->data = list->data;
509 g_list_reverse (GList *list)
518 last->next = last->prev;
526 g_list_nth (GList *list,
529 while ((n-- > 0) && list)
536 g_list_nth_prev (GList *list,
539 while ((n-- > 0) && list)
546 g_list_nth_data (GList *list,
549 while ((n-- > 0) && list)
552 return list ? list->data : NULL;
556 g_list_find (GList *list,
561 if (list->data == data)
570 g_list_find_custom (GList *list,
574 g_return_val_if_fail (func != NULL, list);
578 if (! func (list->data, data))
588 g_list_position (GList *list,
606 g_list_index (GList *list,
614 if (list->data == data)
624 g_list_last (GList *list)
636 g_list_first (GList *list)
648 g_list_length (GList *list)
663 g_list_foreach (GList *list,
669 GList *next = list->next;
670 (*func) (list->data, user_data);
677 g_list_insert_sorted (GList *list,
681 GList *tmp_list = list;
685 g_return_val_if_fail (func != NULL, list);
689 new_list = _g_list_alloc ();
690 new_list->data = data;
694 cmp = (*func) (data, tmp_list->data);
696 while ((tmp_list->next) && (cmp > 0))
698 tmp_list = tmp_list->next;
699 cmp = (*func) (data, tmp_list->data);
702 new_list = _g_list_alloc ();
703 new_list->data = data;
705 if ((!tmp_list->next) && (cmp > 0))
707 tmp_list->next = new_list;
708 new_list->prev = tmp_list;
714 tmp_list->prev->next = new_list;
715 new_list->prev = tmp_list->prev;
717 new_list->next = tmp_list;
718 tmp_list->prev = new_list;
720 if (tmp_list == list)
727 g_list_sort_merge (GList *l1,
733 GList list, *l, *lprev;
742 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
744 cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
763 l->next = l1 ? l1 : l2;
770 g_list_sort_real (GList *list,
785 while ((l2 = l2->next) != NULL)
787 if ((l2 = l2->next) == NULL)
794 return g_list_sort_merge (g_list_sort_real (list, compare_func, use_data, user_data),
795 g_list_sort_real (l2, compare_func, use_data, user_data),
802 g_list_sort (GList *list,
803 GCompareFunc compare_func)
805 return g_list_sort_real (list, (GFunc) compare_func, FALSE, NULL);
810 g_list_sort_with_data (GList *list,
811 GCompareDataFunc compare_func,
814 return g_list_sort_real (list, (GFunc) compare_func, TRUE, user_data);
818 #include "galiasdef.c"