mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysys / list.c
blob28946c0c0ee9920c869dd133b2dcecb202939e48
1 /* Copyright (c) 2000-2002, 2004-2007 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
17 Code for handling dubble-linked lists in C
20 #include "mysys_priv.h"
21 #include <my_list.h>
25 /* Add a element to start of list */
27 LIST *list_add(LIST *root, LIST *element)
29 DBUG_ENTER("list_add");
30 DBUG_PRINT("enter",("root: 0x%lx element: 0x%lx", (long) root, (long) element));
31 if (root)
33 if (root->prev) /* If add in mid of list */
34 root->prev->next= element;
35 element->prev=root->prev;
36 root->prev=element;
38 else
39 element->prev=0;
40 element->next=root;
41 DBUG_RETURN(element); /* New root */
45 LIST *list_delete(LIST *root, LIST *element)
47 if (element->prev)
48 element->prev->next=element->next;
49 else
50 root=element->next;
51 if (element->next)
52 element->next->prev=element->prev;
53 return root;
57 void list_free(LIST *root, uint free_data)
59 LIST *next;
60 while (root)
62 next=root->next;
63 if (free_data)
64 my_free((uchar*) root->data,MYF(0));
65 my_free((uchar*) root,MYF(0));
66 root=next;
71 LIST *list_cons(void *data, LIST *list)
73 LIST *new_charset=(LIST*) my_malloc(sizeof(LIST),MYF(MY_FAE));
74 if (!new_charset)
75 return 0;
76 new_charset->data=data;
77 return list_add(list,new_charset);
81 LIST *list_reverse(LIST *root)
83 LIST *last;
85 last=root;
86 while (root)
88 last=root;
89 root=root->next;
90 last->next=last->prev;
91 last->prev=root;
93 return last;
96 uint list_length(LIST *list)
98 uint count;
99 for (count=0 ; list ; list=list->next, count++) ;
100 return count;
104 int list_walk(LIST *list, list_walk_action action, uchar* argument)
106 int error=0;
107 while (list)
109 if ((error = (*action)(list->data,argument)))
110 return error;
111 list=list_rest(list);
113 return 0;