2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2010 Herbert Haas
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 2 as published by the
7 * Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
26 * General doubly linked list with management functions.
29 * There is no dummy head element. Every element may contain data!
30 * Therefore there is only one general "create_new_element" function.
32 * You cannot delete the head element except you want to delete the whole list.
33 * Usually you delete the head element at last.
35 * head->refcount always contains the number of elements.
37 * Each element has a unique index number.
39 * The user must assign her/his data to (void*) elem->data.
44 // Create new list element - may be the first one (list==NULL)
46 struct mz_ll
* mz_ll_create_new_element(struct mz_ll
*list
)
48 struct mz_ll
*new_element
;
49 new_element
= (struct mz_ll
*) malloc (sizeof(struct mz_ll
));
50 if (new_element
==NULL
) return NULL
;
51 _mz_ll_set_default(new_element
);
53 new_element
->next
=new_element
;
54 new_element
->prev
=new_element
;
55 new_element
->head
=new_element
;
56 new_element
->refcount
=1;
58 new_element
->index_last
=0;
60 new_element
->prev
=list
->prev
;
61 new_element
->next
=list
;
62 new_element
->prev
->next
=new_element
;
63 list
->prev
= new_element
;
64 new_element
->head
=list
;
67 new_element
->index
=list
->index_last
;
73 // Delete ONE list element.
74 int mz_ll_delete_element (struct mz_ll
*cur
)
76 if ((cur
==NULL
)||(cur
==cur
->head
)) return -1; // don't delete head!
77 if (cur
->data
!=NULL
) { free(cur
->data
); cur
->data
=NULL
; }
79 if ((cur
->next
!=cur
)&&(cur
->prev
!=cur
)) {
80 cur
->prev
->next
=cur
->next
;
81 cur
->next
->prev
=cur
->prev
;
83 cur
->head
->refcount
--;
84 if (cur
!=NULL
) { free(cur
); cur
=NULL
; }
89 int mz_ll_delete_list (struct mz_ll
*list
)
91 struct mz_ll
*cur
=list
,
94 if (cur
==NULL
) return 1;
95 while (cur
!=cur
->next
) {
97 mz_ll_delete_element(cur
);
100 // Finally free list head:
101 if (list
->data
!=NULL
) { free(list
->data
); list
->data
=NULL
; }
107 struct mz_ll
* mz_ll_search_name (struct mz_ll
*list
, char *str
)
109 struct mz_ll
*cur
=list
;
111 if (strncmp(cur
->name
, str
, MZ_LL_NAME_LEN
)==0) return cur
;
118 struct mz_ll
* mz_ll_search_index (struct mz_ll
*list
, int i
)
120 struct mz_ll
*cur
=list
;
122 if (cur
->index
==i
) return cur
;
129 int mz_ll_size(struct mz_ll
*list
)
132 struct mz_ll
*cur
=list
;
134 if (list
==NULL
) return 0;
141 if (i
!=list
->refcount
) fprintf(stderr
, "MZ_LL_SIZE: Anomalous situation. Report this.\n");
146 int mz_ll_dump_all(struct mz_ll
*list
)
149 struct mz_ll
*cur
=list
;
151 if (list
==NULL
) return 0;
155 fprintf(stdout
, "Element %i: '%s', index=%i\n",i
,cur
->name
, cur
->index
);
164 // ------ PRIVATE: initialize list-element
165 void _mz_ll_set_default (struct mz_ll
*cur
)