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 * lldel - Remove an item from a linked list.
54 * @curll: The list to remove the item from.
55 * @object: The object to remove.
56 * @objectcmp: A pointer to a comparision function for the object type.
58 * Trawls through the list looking for the object. If it's found then it
59 * is removed from the list. Only one occurance is searched for. Returns
60 * a pointer to the head of the new list.
62 struct ll *lldel(struct ll *curll, void *object,
63 int (*objectcmp) (const void *object1, const void *object2));
66 * llfind - Find an item in a linked list.
67 * @curll: The list to look in.
68 * @object: The object to look for.
69 * @objectcmp: A pointer to a comparision function for the object type.
71 * Searches through a list for an object. Returns a pointer to the object
72 * if it's found, otherwise NULL.
74 struct ll *llfind(struct ll *curll, void *object,
75 int (*objectcmp) (const void *object1, const void *object2));
78 * llsize - Returns the number of elements in a linked list.
79 * @curll: The linked list to count.
81 * Counts the number of elements in a linked list.
83 unsigned long llsize(struct ll *curll);
86 * llfree - Frees a linked list.
87 * @curll: The list to free.
88 * @objectfree: A pointer to a free function for the object.
90 * Walks through a list and free it. If a function is provided for
91 * objectfree then it's called for each element to free them, if it's NULL
92 * just the list is freed.
94 void llfree(struct ll *curll, void (*objectfree) (void *object));