2 * ll.h - various things of used for dealing with linked lists.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
12 #define ADD_PACKET_TO_LIST_END(list, name, item) \
13 if (list->name##s != NULL) { \
14 list->last_##name->next = malloc(sizeof (*list->last_##name));\
15 list->last_##name = list->last_##name->next; \
17 list->name##s = list->last_##name = \
18 malloc(sizeof (*list->last_##name)); \
20 memset(list->last_##name, 0, sizeof(*list->last_##name)); \
21 list->last_##name->packet = item;
23 #define ADD_PACKET_TO_LIST(list, item) \
25 list->next = malloc(sizeof (*list)); \
28 list = malloc(sizeof (*list)); \
30 memset(list, 0, sizeof(*list)); \
34 * struct ll - A generic linked list structure.
35 * @object: The object.
36 * @next: A pointer to the next object.
44 * lladd - Add an item to a linked list.
45 * @curll: The list to add to. Can be NULL to create a new list.
46 * @object: The object to add.
48 * Returns a pointer to the head of the new list.
50 struct ll *lladd(struct ll *curll, void *object);
53 * lladdend - Add an item to the end of a linked list.
54 * @curll: The list to add to. Can be NULL to create a new list.
55 * @object: The object to add.
57 * Returns a pointer to the head of the new list.
59 struct ll *lladdend(struct ll *curll, void *object);
62 * lldel - Remove an item from a linked list.
63 * @curll: The list to remove the item from.
64 * @object: The object to remove.
65 * @objectcmp: A pointer to a comparision function for the object type.
67 * Trawls through the list looking for the object. If it's found then it
68 * is removed from the list. Only one occurance is searched for. Returns
69 * a pointer to the head of the new list.
71 struct ll *lldel(struct ll *curll, void *object,
72 int (*objectcmp) (const void *object1, const void *object2));
75 * llfind - Find an item in a linked list.
76 * @curll: The list to look in.
77 * @object: The object to look for.
78 * @objectcmp: A pointer to a comparision function for the object type.
80 * Searches through a list for an object. Returns a pointer to the object
81 * if it's found, otherwise NULL.
83 struct ll *llfind(struct ll *curll, void *object,
84 int (*objectcmp) (const void *object1, const void *object2));
87 * llsize - Returns the number of elements in a linked list.
88 * @curll: The linked list to count.
90 * Counts the number of elements in a linked list.
92 unsigned long llsize(struct ll *curll);
95 * llfree - Frees a linked list.
96 * @curll: The list to free.
97 * @objectfree: A pointer to a free function for the object.
99 * Walks through a list and free it. If a function is provided for
100 * objectfree then it's called for each element to free them, if it's NULL
101 * just the list is freed.
103 void llfree(struct ll *curll, void (*objectfree) (void *object));
105 #endif /* __LL_H__ */