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.
43 struct mz_ll
*packet_sequences
;
44 struct mz_ll
*cli_seq
; // currently edited packet sequence used by CLI
46 // Create new list element - may be the first one (list==NULL)
48 struct mz_ll
* mz_ll_create_new_element(struct mz_ll
*list
)
50 struct mz_ll
*new_element
;
51 new_element
= (struct mz_ll
*) malloc (sizeof(struct mz_ll
));
52 if (new_element
==NULL
) return NULL
;
53 _mz_ll_set_default(new_element
);
55 new_element
->next
=new_element
;
56 new_element
->prev
=new_element
;
57 new_element
->head
=new_element
;
58 new_element
->refcount
=1;
60 new_element
->index_last
=0;
62 new_element
->prev
=list
->prev
;
63 new_element
->next
=list
;
64 new_element
->prev
->next
=new_element
;
65 list
->prev
= new_element
;
66 new_element
->head
=list
;
69 new_element
->index
=list
->index_last
;
75 // Delete ONE list element.
76 int mz_ll_delete_element (struct mz_ll
*cur
)
78 if ((cur
==NULL
)||(cur
==cur
->head
)) return -1; // don't delete head!
79 if (cur
->data
!=NULL
) { free(cur
->data
); cur
->data
=NULL
; }
81 if ((cur
->next
!=cur
)&&(cur
->prev
!=cur
)) {
82 cur
->prev
->next
=cur
->next
;
83 cur
->next
->prev
=cur
->prev
;
85 cur
->head
->refcount
--;
86 if (cur
!=NULL
) { free(cur
); cur
=NULL
; }
91 int mz_ll_delete_list (struct mz_ll
*list
)
93 struct mz_ll
*cur
=list
,
96 if (cur
==NULL
) return 1;
97 while (cur
!=cur
->next
) {
99 mz_ll_delete_element(cur
);
102 // Finally free list head:
103 if (list
->data
!=NULL
) { free(list
->data
); list
->data
=NULL
; }
109 struct mz_ll
* mz_ll_search_name (struct mz_ll
*list
, char *str
)
111 struct mz_ll
*cur
=list
;
113 if (strncmp(cur
->name
, str
, MZ_LL_NAME_LEN
)==0) return cur
;
120 struct mz_ll
* mz_ll_search_index (struct mz_ll
*list
, int i
)
122 struct mz_ll
*cur
=list
;
124 if (cur
->index
==i
) return cur
;
131 int mz_ll_size(struct mz_ll
*list
)
134 struct mz_ll
*cur
=list
;
136 if (list
==NULL
) return 0;
143 if (i
!=list
->refcount
) fprintf(stderr
, "MZ_LL_SIZE: Anomalous situation. Report this.\n");
148 int mz_ll_dump_all(struct mz_ll
*list
)
151 struct mz_ll
*cur
=list
;
153 if (list
==NULL
) return 0;
157 fprintf(stdout
, "Element %i: '%s', index=%i\n",i
,cur
->name
, cur
->index
);
166 // ------ PRIVATE: initialize list-element
167 void _mz_ll_set_default (struct mz_ll
*cur
)