2 * ll.h - various things of used for dealing with linked lists.
4 * Copyright 2000-2004 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #define ADD_PACKET_TO_LIST_END(list, name, item) \
24 if (list->name##s != NULL) { \
25 list->last_##name->next = malloc(sizeof (*list->last_##name));\
26 list->last_##name = list->last_##name->next; \
28 list->name##s = list->last_##name = \
29 malloc(sizeof (*list->last_##name)); \
31 memset(list->last_##name, 0, sizeof(*list->last_##name)); \
32 list->last_##name->packet = item;
34 #define ADD_PACKET_TO_LIST(list, item) \
36 list->next = malloc(sizeof (*list)); \
39 list = malloc(sizeof (*list)); \
41 memset(list, 0, sizeof(*list)); \
45 * struct ll - A generic linked list structure.
46 * @object: The object.
47 * @next: A pointer to the next object.
55 * lladd - Add an item to a linked list.
56 * @curll: The list to add to. Can be NULL to create a new list.
57 * @object: The object to add.
59 * Returns a pointer to the head of the new list.
61 struct ll *lladd(struct ll *curll, void *object);
64 * lladdend - Add an item to the end of a linked list.
65 * @curll: The list to add to. Can be NULL to create a new list.
66 * @object: The object to add.
68 * Returns a pointer to the head of the new list.
70 struct ll *lladdend(struct ll *curll, void *object);
73 * lldel - Remove an item from a linked list.
74 * @curll: The list to remove the item from.
75 * @object: The object to remove.
76 * @objectcmp: A pointer to a comparision function for the object type.
78 * Trawls through the list looking for the object. If it's found then it
79 * is removed from the list. Only one occurance is searched for. Returns
80 * a pointer to the head of the new list.
82 struct ll *lldel(struct ll *curll, void *object,
83 int (*objectcmp) (const void *object1, const void *object2));
86 * llfind - Find an item in a linked list.
87 * @curll: The list to look in.
88 * @object: The object to look for.
89 * @objectcmp: A pointer to a comparision function for the object type.
91 * Searches through a list for an object. Returns a pointer to the object
92 * if it's found, otherwise NULL.
94 struct ll *llfind(struct ll *curll, void *object,
95 int (*objectcmp) (const void *object1, const void *object2));
98 * llsize - Returns the number of elements in a linked list.
99 * @curll: The linked list to count.
101 * Counts the number of elements in a linked list.
103 unsigned long llsize(struct ll *curll);
106 * llfree - Frees a linked list.
107 * @curll: The list to free.
108 * @objectfree: A pointer to a free function for the object.
110 * Walks through a list and free it. If a function is provided for
111 * objectfree then it's called for each element to free them, if it's NULL
112 * just the list is freed.
114 void llfree(struct ll *curll, void (*objectfree) (void *object));
116 #endif /* __LL_H__ */