Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / glib / gslist.c
blobeed0007017bb3f055625c7ad17fc0380728b756e
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/.
27 /*
28 * MT safe
31 #include "glib.h"
34 struct _GAllocator /* from gmem.c */
36 gchar *name;
37 guint16 n_preallocs;
38 guint is_unused : 1;
39 guint type : 4;
40 GAllocator *last;
41 GMemChunk *mem_chunk;
42 GSList *free_lists; /* implementation specific */
45 G_LOCK_DEFINE_STATIC (current_allocator);
46 static GAllocator *current_allocator = NULL;
48 /* HOLDS: current_allocator_lock */
49 static void
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,
68 sizeof (GSList),
69 sizeof (GSList) * allocator->n_preallocs,
70 G_ALLOC_ONLY);
71 allocator->free_lists = NULL;
74 allocator->is_unused = FALSE;
77 void
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);
87 void
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 GSList*
104 g_slist_alloc (void)
106 GSList *list;
108 G_LOCK (current_allocator);
109 if (!current_allocator)
111 GAllocator *allocator = g_allocator_new ("GLib default GSList allocator",
112 128);
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);
120 list->data = NULL;
122 else
124 if (current_allocator->free_lists->data)
126 list = current_allocator->free_lists->data;
127 current_allocator->free_lists->data = list->next;
128 list->data = NULL;
130 else
132 list = current_allocator->free_lists;
133 current_allocator->free_lists = list->next;
136 G_UNLOCK (current_allocator);
138 list->next = NULL;
140 return list;
143 void
144 g_slist_free (GSList *list)
146 if (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);
156 void
157 g_slist_free_1 (GSList *list)
159 if (list)
161 list->data = NULL;
162 G_LOCK (current_allocator);
163 list->next = current_allocator->free_lists;
164 current_allocator->free_lists = list;
165 G_UNLOCK (current_allocator);
169 GSList*
170 g_slist_append (GSList *list,
171 gpointer data)
173 GSList *new_list;
174 GSList *last;
176 new_list = g_slist_alloc ();
177 new_list->data = data;
179 if (list)
181 last = g_slist_last (list);
182 /* g_assert (last != NULL); */
183 last->next = new_list;
185 return list;
187 else
188 return new_list;
191 GSList*
192 g_slist_prepend (GSList *list,
193 gpointer data)
195 GSList *new_list;
197 new_list = g_slist_alloc ();
198 new_list->data = data;
199 new_list->next = list;
201 return new_list;
204 GSList*
205 g_slist_insert (GSList *list,
206 gpointer data,
207 gint position)
209 GSList *prev_list;
210 GSList *tmp_list;
211 GSList *new_list;
213 if (position < 0)
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;
221 if (!list)
222 return new_list;
224 prev_list = NULL;
225 tmp_list = list;
227 while ((position-- > 0) && tmp_list)
229 prev_list = tmp_list;
230 tmp_list = tmp_list->next;
233 if (prev_list)
235 new_list->next = prev_list->next;
236 prev_list->next = new_list;
238 else
240 new_list->next = list;
241 list = new_list;
244 return list;
247 GSList *
248 g_slist_concat (GSList *list1, GSList *list2)
250 if (list2)
252 if (list1)
253 g_slist_last (list1)->next = list2;
254 else
255 list1 = list2;
258 return list1;
261 GSList*
262 g_slist_remove (GSList *list,
263 gpointer data)
265 GSList *tmp;
266 GSList *prev;
268 prev = NULL;
269 tmp = list;
271 while (tmp)
273 if (tmp->data == data)
275 if (prev)
276 prev->next = tmp->next;
277 if (list == tmp)
278 list = list->next;
280 tmp->next = NULL;
281 g_slist_free (tmp);
283 break;
286 prev = tmp;
287 tmp = tmp->next;
290 return list;
293 GSList*
294 g_slist_remove_link (GSList *list,
295 GSList *link)
297 GSList *tmp;
298 GSList *prev;
300 prev = NULL;
301 tmp = list;
303 while (tmp)
305 if (tmp == link)
307 if (prev)
308 prev->next = tmp->next;
309 if (list == tmp)
310 list = list->next;
312 tmp->next = NULL;
313 break;
316 prev = tmp;
317 tmp = tmp->next;
320 return list;
323 GSList*
324 g_slist_copy (GSList *list)
326 GSList *new_list = NULL;
328 if (list)
330 GSList *last;
332 new_list = g_slist_alloc ();
333 new_list->data = list->data;
334 last = new_list;
335 list = list->next;
336 while (list)
338 last->next = g_slist_alloc ();
339 last = last->next;
340 last->data = list->data;
341 list = list->next;
345 return new_list;
348 GSList*
349 g_slist_reverse (GSList *list)
351 GSList *prev = NULL;
353 while (list)
355 GSList *next = list->next;
357 list->next = prev;
359 prev = list;
360 list = next;
363 return prev;
366 GSList*
367 g_slist_nth (GSList *list,
368 guint n)
370 while ((n-- > 0) && list)
371 list = list->next;
373 return list;
376 gpointer
377 g_slist_nth_data (GSList *list,
378 guint n)
380 while ((n-- > 0) && list)
381 list = list->next;
383 return list ? list->data : NULL;
386 GSList*
387 g_slist_find (GSList *list,
388 gpointer data)
390 while (list)
392 if (list->data == data)
393 break;
394 list = list->next;
397 return list;
400 GSList*
401 g_slist_find_custom (GSList *list,
402 gpointer data,
403 GCompareFunc func)
405 g_return_val_if_fail (func != NULL, list);
407 while (list)
409 if (! func (list->data, data))
410 return list;
411 list = list->next;
414 return NULL;
417 gint
418 g_slist_position (GSList *list,
419 GSList *link)
421 gint i;
423 i = 0;
424 while (list)
426 if (list == link)
427 return i;
428 i++;
429 list = list->next;
432 return -1;
435 gint
436 g_slist_index (GSList *list,
437 gpointer data)
439 gint i;
441 i = 0;
442 while (list)
444 if (list->data == data)
445 return i;
446 i++;
447 list = list->next;
450 return -1;
453 GSList*
454 g_slist_last (GSList *list)
456 if (list)
458 while (list->next)
459 list = list->next;
462 return list;
465 guint
466 g_slist_length (GSList *list)
468 guint length;
470 length = 0;
471 while (list)
473 length++;
474 list = list->next;
477 return length;
480 void
481 g_slist_foreach (GSList *list,
482 GFunc func,
483 gpointer user_data)
485 while (list)
487 (*func) (list->data, user_data);
488 list = list->next;
492 GSList*
493 g_slist_insert_sorted (GSList *list,
494 gpointer data,
495 GCompareFunc func)
497 GSList *tmp_list = list;
498 GSList *prev_list = NULL;
499 GSList *new_list;
500 gint cmp;
502 g_return_val_if_fail (func != NULL, list);
504 if (!list)
506 new_list = g_slist_alloc();
507 new_list->data = data;
508 return new_list;
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;
526 return list;
529 if (prev_list)
531 prev_list->next = new_list;
532 new_list->next = tmp_list;
533 return list;
535 else
537 new_list->next = list;
538 return new_list;
542 static GSList*
543 g_slist_sort_merge (GSList *l1,
544 GSList *l2,
545 GCompareFunc compare_func)
547 GSList list, *l;
549 l=&list;
551 while (l1 && l2)
553 if (compare_func(l1->data,l2->data) < 0)
555 l=l->next=l1;
556 l1=l1->next;
558 else
560 l=l->next=l2;
561 l2=l2->next;
564 l->next= l1 ? l1 : l2;
566 return list.next;
569 GSList*
570 g_slist_sort (GSList *list,
571 GCompareFunc compare_func)
573 GSList *l1, *l2;
575 if (!list)
576 return NULL;
577 if (!list->next)
578 return list;
580 l1 = list;
581 l2 = list->next;
583 while ((l2 = l2->next) != NULL)
585 if ((l2 = l2->next) == NULL)
586 break;
587 l1=l1->next;
589 l2 = l1->next;
590 l1->next = NULL;
592 return g_slist_sort_merge (g_slist_sort (list, compare_func),
593 g_slist_sort (l2, compare_func),
594 compare_func);