2 * ll.h - various things of used for dealing with linked lists.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
8 * $Id: ll.h,v 1.3 2003/06/04 20:57:10 noodles Exp $
14 #define ADD_PACKET_TO_LIST_END(list, name, item) \
15 if (list->name##s != NULL) { \
16 list->last_##name->next = malloc(sizeof (*list->last_##name));\
17 list->last_##name = list->last_##name->next; \
19 list->name##s = list->last_##name = \
20 malloc(sizeof (*list->last_##name)); \
22 memset(list->last_##name, 0, sizeof(*list->last_##name)); \
23 list->last_##name->packet = item;
25 #define ADD_PACKET_TO_LIST(list, item) \
27 list->next = malloc(sizeof (*list)); \
30 list = malloc(sizeof (*list)); \
32 memset(list, 0, sizeof(*list)); \
36 * struct ll - A generic linked list structure.
37 * @object: The object.
38 * @next: A pointer to the next object.
46 * lladd - Add an item to a linked list.
47 * @curll: The list to add to. Can be NULL to create a new list.
48 * @object: The object to add.
50 * Returns a pointer to the head of the new list.
52 struct ll *lladd(struct ll *curll, void *object);
55 * lldel - Remove an item from a linked list.
56 * @curll: The list to remove the item from.
57 * @object: The object to remove.
58 * @objectcmp: A pointer to a comparision function for the object type.
60 * Trawls through the list looking for the object. If it's found then it
61 * is removed from the list. Only one occurance is searched for. Returns
62 * a pointer to the head of the new list.
64 struct ll *lldel(struct ll *curll, void *object,
65 int (*objectcmp) (const void *object1, const void *object2));
68 * llfind - Find an item in a linked list.
69 * @curll: The list to look in.
70 * @object: The object to look for.
71 * @objectcmp: A pointer to a comparision function for the object type.
73 * Searches through a list for an object. Returns a pointer to the object
74 * if it's found, otherwise NULL.
76 struct ll *llfind(struct ll *curll, void *object,
77 int (*objectcmp) (const void *object1, const void *object2));
80 * llsize - Returns the number of elements in a linked list.
81 * @curll: The linked list to count.
83 * Counts the number of elements in a linked list.
85 unsigned long llsize(struct ll *curll);
88 * llfree - Frees a linked list.
89 * @curll: The list to free.
90 * @objectfree: A pointer to a free function for the object.
92 * Walks through a list and free it. If a function is provided for
93 * objectfree then it's called for each element to free them, if it's NULL
94 * just the list is freed.
96 struct ll *llfree(struct ll *curll,
97 void (*objectfree) (void *object));