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/.
36 #ifndef DISABLE_MEM_POOLS
37 struct _GAllocator /* from gmem.c */
45 GList *free_lists; /* implementation specific */
48 static GAllocator *current_allocator = NULL;
49 G_LOCK_DEFINE_STATIC (current_allocator);
51 /* HOLDS: current_allocator_lock */
53 g_list_validate_allocator (GAllocator *allocator)
55 g_return_if_fail (allocator != NULL);
56 g_return_if_fail (allocator->is_unused == TRUE);
58 if (allocator->type != G_ALLOCATOR_LIST)
60 allocator->type = G_ALLOCATOR_LIST;
61 if (allocator->mem_chunk)
63 g_mem_chunk_destroy (allocator->mem_chunk);
64 allocator->mem_chunk = NULL;
68 if (!allocator->mem_chunk)
70 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
72 sizeof (GList) * allocator->n_preallocs,
74 allocator->free_lists = NULL;
77 allocator->is_unused = FALSE;
81 g_list_push_allocator(GAllocator *allocator)
83 G_LOCK (current_allocator);
84 g_list_validate_allocator (allocator);
85 allocator->last = current_allocator;
86 current_allocator = allocator;
87 G_UNLOCK (current_allocator);
91 g_list_pop_allocator (void)
93 G_LOCK (current_allocator);
94 if (current_allocator)
96 GAllocator *allocator;
98 allocator = current_allocator;
99 current_allocator = allocator->last;
100 allocator->last = NULL;
101 allocator->is_unused = TRUE;
103 G_UNLOCK (current_allocator);
111 G_LOCK (current_allocator);
112 if (!current_allocator)
114 GAllocator *allocator = g_allocator_new ("GLib default GList allocator",
116 g_list_validate_allocator (allocator);
117 allocator->last = NULL;
118 current_allocator = allocator;
120 if (!current_allocator->free_lists)
122 list = g_chunk_new (GList, current_allocator->mem_chunk);
127 if (current_allocator->free_lists->data)
129 list = current_allocator->free_lists->data;
130 current_allocator->free_lists->data = list->next;
135 list = current_allocator->free_lists;
136 current_allocator->free_lists = list->next;
139 G_UNLOCK (current_allocator);
149 return _g_list_alloc ();
153 g_list_free (GList *list)
157 GList *last_node = list;
159 #ifdef ENABLE_GC_FRIENDLY
160 while (last_node->next)
162 last_node->data = NULL;
163 last_node->prev = NULL;
164 last_node = last_node->next;
166 last_node->data = NULL;
167 last_node->prev = NULL;
168 #else /* !ENABLE_GC_FRIENDLY */
169 list->data = list->next;
170 #endif /* ENABLE_GC_FRIENDLY */
172 G_LOCK (current_allocator);
173 last_node->next = current_allocator->free_lists;
174 current_allocator->free_lists = list;
175 G_UNLOCK (current_allocator);
180 _g_list_free_1 (GList *list)
186 #ifdef ENABLE_GC_FRIENDLY
188 #endif /* ENABLE_GC_FRIENDLY */
190 G_LOCK (current_allocator);
191 list->next = current_allocator->free_lists;
192 current_allocator->free_lists = list;
193 G_UNLOCK (current_allocator);
198 g_list_free_1 (GList *list)
200 _g_list_free_1 (list);
203 #else /* DISABLE_MEM_POOLS */
205 #define _g_list_alloc g_list_alloc
211 list = g_new0 (GList, 1);
217 g_list_free (GList *list)
229 #define _g_list_free_1 g_list_free_1
231 g_list_free_1 (GList *list)
239 g_list_append (GList *list,
245 new_list = _g_list_alloc ();
246 new_list->data = data;
250 last = g_list_last (list);
251 /* g_assert (last != NULL); */
252 last->next = new_list;
253 new_list->prev = last;
262 g_list_prepend (GList *list,
267 new_list = _g_list_alloc ();
268 new_list->data = data;
274 list->prev->next = new_list;
275 new_list->prev = list->prev;
277 list->prev = new_list;
278 new_list->next = list;
285 g_list_insert (GList *list,
293 return g_list_append (list, data);
294 else if (position == 0)
295 return g_list_prepend (list, data);
297 tmp_list = g_list_nth (list, position);
299 return g_list_append (list, data);
301 new_list = _g_list_alloc ();
302 new_list->data = data;
306 tmp_list->prev->next = new_list;
307 new_list->prev = tmp_list->prev;
309 new_list->next = tmp_list;
310 tmp_list->prev = new_list;
312 if (tmp_list == list)
319 g_list_insert_before (GList *list,
325 list = g_list_alloc ();
327 g_return_val_if_fail (sibling == NULL, list);
334 node = g_list_alloc ();
338 node->prev = sibling->prev;
339 node->prev->next = node;
340 node->next = sibling;
341 sibling->prev = node;
346 node->next = sibling;
347 sibling->prev = node;
348 g_return_val_if_fail (sibling == list, node);
360 last->next = g_list_alloc ();
361 last->next->data = data;
362 last->next->prev = last;
369 g_list_concat (GList *list1, GList *list2)
375 tmp_list = g_list_last (list1);
377 tmp_list->next = list2;
380 list2->prev = tmp_list;
387 g_list_remove (GList *list,
395 if (tmp->data != data)
400 tmp->prev->next = tmp->next;
402 tmp->next->prev = tmp->prev;
407 _g_list_free_1 (tmp);
416 g_list_remove_all (GList *list,
423 if (tmp->data != data)
427 GList *next = tmp->next;
430 tmp->prev->next = next;
434 next->prev = tmp->prev;
436 _g_list_free_1 (tmp);
444 _g_list_remove_link (GList *list,
450 link->prev->next = link->next;
452 link->next->prev = link->prev;
465 g_list_remove_link (GList *list,
468 return _g_list_remove_link (list, link);
472 g_list_delete_link (GList *list,
475 list = _g_list_remove_link (list, link);
476 _g_list_free_1 (link);
482 g_list_copy (GList *list)
484 GList *new_list = NULL;
490 new_list = _g_list_alloc ();
491 new_list->data = list->data;
496 last->next = _g_list_alloc ();
497 last->next->prev = last;
499 last->data = list->data;
508 g_list_reverse (GList *list)
517 last->next = last->prev;
525 g_list_nth (GList *list,
528 while ((n-- > 0) && list)
535 g_list_nth_prev (GList *list,
538 while ((n-- > 0) && list)
545 g_list_nth_data (GList *list,
548 while ((n-- > 0) && list)
551 return list ? list->data : NULL;
555 g_list_find (GList *list,
560 if (list->data == data)
569 g_list_find_custom (GList *list,
573 g_return_val_if_fail (func != NULL, list);
577 if (! func (list->data, data))
587 g_list_position (GList *list,
605 g_list_index (GList *list,
613 if (list->data == data)
623 g_list_last (GList *list)
635 g_list_first (GList *list)
647 g_list_length (GList *list)
662 g_list_foreach (GList *list,
668 GList *next = list->next;
669 (*func) (list->data, user_data);
676 g_list_insert_sorted (GList *list,
680 GList *tmp_list = list;
684 g_return_val_if_fail (func != NULL, list);
688 new_list = _g_list_alloc ();
689 new_list->data = data;
693 cmp = (*func) (data, tmp_list->data);
695 while ((tmp_list->next) && (cmp > 0))
697 tmp_list = tmp_list->next;
698 cmp = (*func) (data, tmp_list->data);
701 new_list = _g_list_alloc ();
702 new_list->data = data;
704 if ((!tmp_list->next) && (cmp > 0))
706 tmp_list->next = new_list;
707 new_list->prev = tmp_list;
713 tmp_list->prev->next = new_list;
714 new_list->prev = tmp_list->prev;
716 new_list->next = tmp_list;
717 tmp_list->prev = new_list;
719 if (tmp_list == list)
726 g_list_sort_merge (GList *l1,
732 GList list, *l, *lprev;
741 cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
743 cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
762 l->next = l1 ? l1 : l2;
769 g_list_sort_real (GList *list,
784 while ((l2 = l2->next) != NULL)
786 if ((l2 = l2->next) == NULL)
793 return g_list_sort_merge (g_list_sort_real (list, compare_func, use_data, user_data),
794 g_list_sort_real (l2, compare_func, use_data, user_data),
801 g_list_sort (GList *list,
802 GCompareFunc compare_func)
804 return g_list_sort_real (list, (GFunc) compare_func, FALSE, NULL);
809 g_list_sort_with_data (GList *list,
810 GCompareDataFunc compare_func,
813 return g_list_sort_real (list, (GFunc) compare_func, TRUE, user_data);