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.
27 struct _GAllocator /* from gmem.c */
35 GList *free_lists; /* implementation specific */
38 static GAllocator *current_allocator = NULL;
39 G_LOCK_DECLARE_STATIC (current_allocator);
41 /* HOLDS: current_allocator_lock */
43 g_list_validate_allocator (GAllocator *allocator)
45 g_return_if_fail (allocator != NULL);
46 g_return_if_fail (allocator->is_unused == TRUE);
48 if (allocator->type != G_ALLOCATOR_LIST)
50 allocator->type = G_ALLOCATOR_LIST;
51 if (allocator->mem_chunk)
53 g_mem_chunk_destroy (allocator->mem_chunk);
54 allocator->mem_chunk = NULL;
58 if (!allocator->mem_chunk)
60 allocator->mem_chunk = g_mem_chunk_new (allocator->name,
62 sizeof (GList) * allocator->n_preallocs,
64 allocator->free_lists = NULL;
67 allocator->is_unused = FALSE;
71 g_list_push_allocator(GAllocator *allocator)
73 G_LOCK (current_allocator);
74 g_list_validate_allocator ( allocator );
75 allocator->last = current_allocator;
76 current_allocator = allocator;
77 G_UNLOCK (current_allocator);
81 g_list_pop_allocator (void)
83 G_LOCK (current_allocator);
84 if (current_allocator)
86 GAllocator *allocator;
88 allocator = current_allocator;
89 current_allocator = allocator->last;
90 allocator->last = NULL;
91 allocator->is_unused = TRUE;
93 G_UNLOCK (current_allocator);
101 G_LOCK (current_allocator);
102 if (!current_allocator)
104 GAllocator *allocator = g_allocator_new ("GLib default GList allocator",
106 g_list_validate_allocator (allocator);
107 allocator->last = NULL;
108 current_allocator = allocator;
110 if (!current_allocator->free_lists)
112 list = g_chunk_new (GList, current_allocator->mem_chunk);
117 if (current_allocator->free_lists->data)
119 list = current_allocator->free_lists->data;
120 current_allocator->free_lists->data = list->next;
125 list = current_allocator->free_lists;
126 current_allocator->free_lists = list->next;
129 G_UNLOCK (current_allocator);
137 g_list_free (GList *list)
141 list->data = list->next;
142 G_LOCK (current_allocator);
143 list->next = current_allocator->free_lists;
144 current_allocator->free_lists = list;
145 G_UNLOCK (current_allocator);
150 g_list_free_1 (GList *list)
155 G_LOCK (current_allocator);
156 list->next = current_allocator->free_lists;
157 current_allocator->free_lists = list;
158 G_UNLOCK (current_allocator);
163 g_list_append (GList *list,
169 new_list = g_list_alloc ();
170 new_list->data = data;
174 last = g_list_last (list);
175 /* g_assert (last != NULL); */
176 last->next = new_list;
177 new_list->prev = last;
186 g_list_prepend (GList *list,
191 new_list = g_list_alloc ();
192 new_list->data = data;
198 list->prev->next = new_list;
199 new_list->prev = list->prev;
201 list->prev = new_list;
202 new_list->next = list;
209 g_list_insert (GList *list,
217 return g_list_append (list, data);
218 else if (position == 0)
219 return g_list_prepend (list, data);
221 tmp_list = g_list_nth (list, position);
223 return g_list_append (list, data);
225 new_list = g_list_alloc ();
226 new_list->data = data;
230 tmp_list->prev->next = new_list;
231 new_list->prev = tmp_list->prev;
233 new_list->next = tmp_list;
234 tmp_list->prev = new_list;
236 if (tmp_list == list)
243 g_list_concat (GList *list1, GList *list2)
249 tmp_list = g_list_last (list1);
251 tmp_list->next = list2;
254 list2->prev = tmp_list;
261 g_list_remove (GList *list,
269 if (tmp->data != data)
274 tmp->prev->next = tmp->next;
276 tmp->next->prev = tmp->prev;
290 g_list_remove_link (GList *list,
296 link->prev->next = link->next;
298 link->next->prev = link->prev;
311 g_list_copy (GList *list)
313 GList *new_list = NULL;
319 new_list = g_list_alloc ();
320 new_list->data = list->data;
325 last->next = g_list_alloc ();
326 last->next->prev = last;
328 last->data = list->data;
337 g_list_reverse (GList *list)
346 last->next = last->prev;
354 g_list_nth (GList *list,
357 while ((n-- > 0) && list)
364 g_list_nth_data (GList *list,
367 while ((n-- > 0) && list)
370 return list ? list->data : NULL;
374 g_list_find (GList *list,
379 if (list->data == data)
388 g_list_find_custom (GList *list,
392 g_return_val_if_fail (func != NULL, list);
396 if (! func (list->data, data))
406 g_list_position (GList *list,
424 g_list_index (GList *list,
432 if (list->data == data)
442 g_list_last (GList *list)
454 g_list_first (GList *list)
466 g_list_length (GList *list)
481 g_list_foreach (GList *list,
487 (*func) (list->data, user_data);
494 g_list_insert_sorted (GList *list,
498 GList *tmp_list = list;
502 g_return_val_if_fail (func != NULL, list);
506 new_list = g_list_alloc();
507 new_list->data = data;
511 cmp = (*func) (data, tmp_list->data);
513 while ((tmp_list->next) && (cmp > 0))
515 tmp_list = tmp_list->next;
516 cmp = (*func) (data, tmp_list->data);
519 new_list = g_list_alloc();
520 new_list->data = data;
522 if ((!tmp_list->next) && (cmp > 0))
524 tmp_list->next = new_list;
525 new_list->prev = tmp_list;
531 tmp_list->prev->next = new_list;
532 new_list->prev = tmp_list->prev;
534 new_list->next = tmp_list;
535 tmp_list->prev = new_list;
537 if (tmp_list == list)
544 g_list_sort_merge (GList *l1,
546 GCompareFunc compare_func)
548 GList list, *l, *lprev;
555 if (compare_func (l1->data, l2->data) < 0)
572 l->next = l1 ? l1 : l2;
579 g_list_sort (GList *list,
580 GCompareFunc compare_func)
592 while ((l2 = l2->next) != NULL)
594 if ((l2 = l2->next) == NULL)
601 return g_list_sort_merge (g_list_sort (list, compare_func),
602 g_list_sort (l2, compare_func),
607 g_list_sort2 (GList *list,
608 GCompareFunc compare_func)
613 /* Degenerate case. */
614 if (!list) return NULL;
616 /* Assume: list = [12,2,4,11,2,4,6,1,1,12]. */
617 for (tmp = list; tmp; )
621 tmp2->next && compare_func (tmp2->data, tmp2->next->data) <= 0;
624 runs = g_slist_append (runs, tmp);
628 /* Now: runs = [[12],[2,4,11],[2,4,6],[1,1,12]]. */
632 /* We have more than one run. Merge pairwise. */
633 GSList *dst, *src, *dstprev = NULL;
635 while (src && src->next)
637 dst->data = g_list_sort_merge (src->data,
642 src = src->next->next;
645 /* If number of runs was odd, just keep the last. */
648 dst->data = src->data;
653 dstprev->next = NULL;
657 /* After 1st loop: runs = [[2,4,11,12],[1,1,2,4,6,12]]. */
658 /* After 2nd loop: runs = [[1,1,2,2,4,4,6,11,12,12]]. */