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/.
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
);
103 static inline GSList
*
104 _g_slist_alloc (void)
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
);
146 return _g_slist_alloc ();
150 g_slist_free (GSList
*list
)
154 GSList
*last_node
= list
;
156 #ifdef ENABLE_GC_FRIENDLY
157 while (last_node
->next
)
159 last_node
->data
= NULL
;
160 last_node
= last_node
->next
;
162 last_node
->data
= NULL
163 #else /* !ENABLE_GC_FRIENDLY */
164 list
->data
= list
->next
;
165 #endif /* ENABLE_GC_FRIENDLY */
167 G_LOCK (current_allocator
);
168 last_node
->next
= current_allocator
->free_lists
;
169 current_allocator
->free_lists
= list
;
170 G_UNLOCK (current_allocator
);
175 _g_slist_free_1 (GSList
*list
)
180 G_LOCK (current_allocator
);
181 list
->next
= current_allocator
->free_lists
;
182 current_allocator
->free_lists
= list
;
183 G_UNLOCK (current_allocator
);
188 g_slist_free_1 (GSList
*list
)
190 _g_slist_free_1 (list
);
194 g_slist_append (GSList
*list
,
200 new_list
= _g_slist_alloc ();
201 new_list
->data
= data
;
205 last
= g_slist_last (list
);
206 /* g_assert (last != NULL); */
207 last
->next
= new_list
;
216 g_slist_prepend (GSList
*list
,
221 new_list
= _g_slist_alloc ();
222 new_list
->data
= data
;
223 new_list
->next
= list
;
229 g_slist_insert (GSList
*list
,
238 return g_slist_append (list
, data
);
239 else if (position
== 0)
240 return g_slist_prepend (list
, data
);
242 new_list
= _g_slist_alloc ();
243 new_list
->data
= data
;
251 while ((position
-- > 0) && tmp_list
)
253 prev_list
= tmp_list
;
254 tmp_list
= tmp_list
->next
;
259 new_list
->next
= prev_list
->next
;
260 prev_list
->next
= new_list
;
264 new_list
->next
= list
;
272 g_slist_concat (GSList
*list1
, GSList
*list2
)
277 g_slist_last (list1
)->next
= list2
;
286 g_slist_remove (GSList
*list
,
297 if (tmp
->data
== data
)
300 prev
->next
= tmp
->next
;
317 static inline GSList
*
318 _g_slist_remove_link (GSList
*list
,
332 prev
->next
= tmp
->next
;
348 g_slist_remove_link (GSList
*list
,
351 return _g_slist_remove_link (list
, link
);
355 g_slist_delete_link (GSList
*list
,
358 list
= _g_slist_remove_link (list
, link
);
359 _g_slist_free_1 (link
);
365 g_slist_copy (GSList
*list
)
367 GSList
*new_list
= NULL
;
373 new_list
= _g_slist_alloc ();
374 new_list
->data
= list
->data
;
379 last
->next
= _g_slist_alloc ();
381 last
->data
= list
->data
;
390 g_slist_reverse (GSList
*list
)
396 GSList
*next
= list
->next
;
408 g_slist_nth (GSList
*list
,
411 while (n
-- > 0 && list
)
418 g_slist_nth_data (GSList
*list
,
421 while (n
-- > 0 && list
)
424 return list
? list
->data
: NULL
;
428 g_slist_find (GSList
*list
,
433 if (list
->data
== data
)
442 g_slist_find_custom (GSList
*list
,
446 g_return_val_if_fail (func
!= NULL
, list
);
450 if (! func (list
->data
, data
))
459 g_slist_position (GSList
*list
,
477 g_slist_index (GSList
*list
,
485 if (list
->data
== data
)
495 g_slist_last (GSList
*list
)
507 g_slist_length (GSList
*list
)
522 g_slist_foreach (GSList
*list
,
528 (*func
) (list
->data
, user_data
);
534 g_slist_insert_sorted (GSList
*list
,
538 GSList
*tmp_list
= list
;
539 GSList
*prev_list
= NULL
;
543 g_return_val_if_fail (func
!= NULL
, list
);
547 new_list
= _g_slist_alloc ();
548 new_list
->data
= data
;
552 cmp
= (*func
) (data
, tmp_list
->data
);
554 while ((tmp_list
->next
) && (cmp
> 0))
556 prev_list
= tmp_list
;
557 tmp_list
= tmp_list
->next
;
558 cmp
= (*func
) (data
, tmp_list
->data
);
561 new_list
= _g_slist_alloc ();
562 new_list
->data
= data
;
564 if ((!tmp_list
->next
) && (cmp
> 0))
566 tmp_list
->next
= new_list
;
572 prev_list
->next
= new_list
;
573 new_list
->next
= tmp_list
;
578 new_list
->next
= list
;
584 g_slist_sort_merge (GSList
*l1
,
586 GCompareFunc compare_func
)
594 if (compare_func(l1
->data
,l2
->data
) < 0)
605 l
->next
= l1
? l1
: l2
;
611 g_slist_sort (GSList
*list
,
612 GCompareFunc compare_func
)
624 while ((l2
= l2
->next
) != NULL
)
626 if ((l2
= l2
->next
) == NULL
)
633 return g_slist_sort_merge (g_slist_sort (list
, compare_func
),
634 g_slist_sort (l2
, compare_func
),