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.
21 * Modified by the GLib Team and others 1997-1999. 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/.
34 struct _GAllocator
/* from gmem.c */
42 GSList
*free_lists
; /* implementation specific */
45 G_LOCK_DEFINE_STATIC (current_allocator
);
46 static GAllocator
*current_allocator
= NULL
;
48 /* HOLDS: current_allocator_lock */
50 g_slist_validate_allocator (GAllocator
*allocator
)
52 g_return_if_fail (allocator
!= NULL
);
53 g_return_if_fail (allocator
->is_unused
== TRUE
);
55 if (allocator
->type
!= G_ALLOCATOR_SLIST
)
57 allocator
->type
= G_ALLOCATOR_SLIST
;
58 if (allocator
->mem_chunk
)
60 g_mem_chunk_destroy (allocator
->mem_chunk
);
61 allocator
->mem_chunk
= NULL
;
65 if (!allocator
->mem_chunk
)
67 allocator
->mem_chunk
= g_mem_chunk_new (allocator
->name
,
69 sizeof (GSList
) * allocator
->n_preallocs
,
71 allocator
->free_lists
= NULL
;
74 allocator
->is_unused
= FALSE
;
78 g_slist_push_allocator (GAllocator
*allocator
)
80 G_LOCK (current_allocator
);
81 g_slist_validate_allocator (allocator
);
82 allocator
->last
= current_allocator
;
83 current_allocator
= allocator
;
84 G_UNLOCK (current_allocator
);
88 g_slist_pop_allocator (void)
90 G_LOCK (current_allocator
);
91 if (current_allocator
)
93 GAllocator
*allocator
;
95 allocator
= current_allocator
;
96 current_allocator
= allocator
->last
;
97 allocator
->last
= NULL
;
98 allocator
->is_unused
= TRUE
;
100 G_UNLOCK (current_allocator
);
108 G_LOCK (current_allocator
);
109 if (!current_allocator
)
111 GAllocator
*allocator
= g_allocator_new ("GLib default GSList allocator",
113 g_slist_validate_allocator (allocator
);
114 allocator
->last
= NULL
;
115 current_allocator
= allocator
;
117 if (!current_allocator
->free_lists
)
119 list
= g_chunk_new (GSList
, current_allocator
->mem_chunk
);
124 if (current_allocator
->free_lists
->data
)
126 list
= current_allocator
->free_lists
->data
;
127 current_allocator
->free_lists
->data
= list
->next
;
132 list
= current_allocator
->free_lists
;
133 current_allocator
->free_lists
= list
->next
;
136 G_UNLOCK (current_allocator
);
144 g_slist_free (GSList
*list
)
148 list
->data
= list
->next
;
149 G_LOCK (current_allocator
);
150 list
->next
= current_allocator
->free_lists
;
151 current_allocator
->free_lists
= list
;
152 G_UNLOCK (current_allocator
);
157 g_slist_free_1 (GSList
*list
)
162 G_LOCK (current_allocator
);
163 list
->next
= current_allocator
->free_lists
;
164 current_allocator
->free_lists
= list
;
165 G_UNLOCK (current_allocator
);
170 g_slist_append (GSList
*list
,
176 new_list
= g_slist_alloc ();
177 new_list
->data
= data
;
181 last
= g_slist_last (list
);
182 /* g_assert (last != NULL); */
183 last
->next
= new_list
;
192 g_slist_prepend (GSList
*list
,
197 new_list
= g_slist_alloc ();
198 new_list
->data
= data
;
199 new_list
->next
= list
;
205 g_slist_insert (GSList
*list
,
214 return g_slist_append (list
, data
);
215 else if (position
== 0)
216 return g_slist_prepend (list
, data
);
218 new_list
= g_slist_alloc ();
219 new_list
->data
= data
;
227 while ((position
-- > 0) && tmp_list
)
229 prev_list
= tmp_list
;
230 tmp_list
= tmp_list
->next
;
235 new_list
->next
= prev_list
->next
;
236 prev_list
->next
= new_list
;
240 new_list
->next
= list
;
248 g_slist_concat (GSList
*list1
, GSList
*list2
)
253 g_slist_last (list1
)->next
= list2
;
262 g_slist_remove (GSList
*list
,
273 if (tmp
->data
== data
)
276 prev
->next
= tmp
->next
;
294 g_slist_remove_link (GSList
*list
,
308 prev
->next
= tmp
->next
;
324 g_slist_copy (GSList
*list
)
326 GSList
*new_list
= NULL
;
332 new_list
= g_slist_alloc ();
333 new_list
->data
= list
->data
;
338 last
->next
= g_slist_alloc ();
340 last
->data
= list
->data
;
349 g_slist_reverse (GSList
*list
)
355 GSList
*next
= list
->next
;
367 g_slist_nth (GSList
*list
,
370 while ((n
-- > 0) && list
)
377 g_slist_nth_data (GSList
*list
,
380 while ((n
-- > 0) && list
)
383 return list
? list
->data
: NULL
;
387 g_slist_find (GSList
*list
,
392 if (list
->data
== data
)
401 g_slist_find_custom (GSList
*list
,
405 g_return_val_if_fail (func
!= NULL
, list
);
409 if (! func (list
->data
, data
))
418 g_slist_position (GSList
*list
,
436 g_slist_index (GSList
*list
,
444 if (list
->data
== data
)
454 g_slist_last (GSList
*list
)
466 g_slist_length (GSList
*list
)
481 g_slist_foreach (GSList
*list
,
487 (*func
) (list
->data
, user_data
);
493 g_slist_insert_sorted (GSList
*list
,
497 GSList
*tmp_list
= list
;
498 GSList
*prev_list
= NULL
;
502 g_return_val_if_fail (func
!= NULL
, list
);
506 new_list
= g_slist_alloc();
507 new_list
->data
= data
;
511 cmp
= (*func
) (data
, tmp_list
->data
);
513 while ((tmp_list
->next
) && (cmp
> 0))
515 prev_list
= tmp_list
;
516 tmp_list
= tmp_list
->next
;
517 cmp
= (*func
) (data
, tmp_list
->data
);
520 new_list
= g_slist_alloc();
521 new_list
->data
= data
;
523 if ((!tmp_list
->next
) && (cmp
> 0))
525 tmp_list
->next
= new_list
;
531 prev_list
->next
= new_list
;
532 new_list
->next
= tmp_list
;
537 new_list
->next
= list
;
543 g_slist_sort_merge (GSList
*l1
,
545 GCompareFunc compare_func
)
553 if (compare_func(l1
->data
,l2
->data
) < 0)
564 l
->next
= l1
? l1
: l2
;
570 g_slist_sort (GSList
*list
,
571 GCompareFunc compare_func
)
583 while ((l2
= l2
->next
) != NULL
)
585 if ((l2
= l2
->next
) == NULL
)
592 return g_slist_sort_merge (g_slist_sort (list
, compare_func
),
593 g_slist_sort (l2
, compare_func
),