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 static G_LOCK_DEFINE(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_list_validate_allocator ( allocator );
74 g_lock (current_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)
142 list->data = list->next;
143 g_lock (current_allocator);
144 list->next = current_allocator->free_lists;
145 current_allocator->free_lists = list;
146 g_unlock (current_allocator);
152 g_list_free_1 (GList *list)
158 g_lock (current_allocator);
159 list->next = current_allocator->free_lists;
160 current_allocator->free_lists = list;
161 g_unlock (current_allocator);
167 g_list_append (GList *list,
173 new_list = g_list_alloc ();
174 new_list->data = data;
178 last = g_list_last (list);
179 /* g_assert (last != NULL); */
180 last->next = new_list;
181 new_list->prev = last;
190 g_list_prepend (GList *list,
195 new_list = g_list_alloc ();
196 new_list->data = data;
202 list->prev->next = new_list;
203 new_list->prev = list->prev;
205 list->prev = new_list;
206 new_list->next = list;
213 g_list_insert (GList *list,
221 return g_list_append (list, data);
222 else if (position == 0)
223 return g_list_prepend (list, data);
225 tmp_list = g_list_nth (list, position);
227 return g_list_append (list, data);
229 new_list = g_list_alloc ();
230 new_list->data = data;
234 tmp_list->prev->next = new_list;
235 new_list->prev = tmp_list->prev;
237 new_list->next = tmp_list;
238 tmp_list->prev = new_list;
240 if (tmp_list == list)
247 g_list_concat (GList *list1, GList *list2)
253 tmp_list = g_list_last (list1);
255 tmp_list->next = list2;
258 list2->prev = tmp_list;
265 g_list_remove (GList *list,
273 if (tmp->data != data)
278 tmp->prev->next = tmp->next;
280 tmp->next->prev = tmp->prev;
294 g_list_remove_link (GList *list,
300 link->prev->next = link->next;
302 link->next->prev = link->prev;
315 g_list_copy (GList *list)
317 GList *new_list = NULL;
323 new_list = g_list_alloc ();
324 new_list->data = list->data;
329 last->next = g_list_alloc ();
330 last->next->prev = last;
332 last->data = list->data;
341 g_list_reverse (GList *list)
350 last->next = last->prev;
358 g_list_nth (GList *list,
361 while ((n-- > 0) && list)
368 g_list_nth_data (GList *list,
371 while ((n-- > 0) && list)
374 return list ? list->data : NULL;
378 g_list_find (GList *list,
383 if (list->data == data)
392 g_list_find_custom (GList *list,
396 g_return_val_if_fail (func != NULL, list);
400 if (! func (list->data, data))
410 g_list_position (GList *list,
428 g_list_index (GList *list,
436 if (list->data == data)
446 g_list_last (GList *list)
458 g_list_first (GList *list)
470 g_list_length (GList *list)
485 g_list_foreach (GList *list,
491 (*func) (list->data, user_data);
498 g_list_insert_sorted (GList *list,
502 GList *tmp_list = list;
506 g_return_val_if_fail (func != NULL, list);
510 new_list = g_list_alloc();
511 new_list->data = data;
515 cmp = (*func) (data, tmp_list->data);
517 while ((tmp_list->next) && (cmp > 0))
519 tmp_list = tmp_list->next;
520 cmp = (*func) (data, tmp_list->data);
523 new_list = g_list_alloc();
524 new_list->data = data;
526 if ((!tmp_list->next) && (cmp > 0))
528 tmp_list->next = new_list;
529 new_list->prev = tmp_list;
535 tmp_list->prev->next = new_list;
536 new_list->prev = tmp_list->prev;
538 new_list->next = tmp_list;
539 tmp_list->prev = new_list;
541 if (tmp_list == list)
548 g_list_sort_merge (GList *l1,
550 GCompareFunc compare_func)
552 GList list, *l, *lprev;
559 if (compare_func (l1->data, l2->data) < 0)
576 l->next = l1 ? l1 : l2;
583 g_list_sort (GList *list,
584 GCompareFunc compare_func)
596 while ((l2 = l2->next) != NULL)
598 if ((l2 = l2->next) == NULL)
605 return g_list_sort_merge (g_list_sort (list, compare_func),
606 g_list_sort (l2, compare_func),
611 g_list_sort2 (GList *list,
612 GCompareFunc compare_func)
617 /* Degenerate case. */
618 if (!list) return NULL;
620 /* Assume: list = [12,2,4,11,2,4,6,1,1,12]. */
621 for (tmp = list; tmp; )
625 tmp2->next && compare_func (tmp2->data, tmp2->next->data) <= 0;
628 runs = g_slist_append (runs, tmp);
632 /* Now: runs = [[12],[2,4,11],[2,4,6],[1,1,12]]. */
636 /* We have more than one run. Merge pairwise. */
637 GSList *dst, *src, *dstprev = NULL;
639 while (src && src->next)
641 dst->data = g_list_sort_merge (src->data,
646 src = src->next->next;
649 /* If number of runs was odd, just keep the last. */
652 dst->data = src->data;
657 dstprev->next = NULL;
661 /* After 1st loop: runs = [[2,4,11,12],[1,1,2,4,6,12]]. */
662 /* After 2nd loop: runs = [[1,1,2,2,4,4,6,11,12,12]]. */