Add -1 to Debian package version
[onak.git] / ll.h
1 /*
2  * ll.h - various things of used for dealing with linked lists.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #ifndef __LL_H__
10 #define __LL_H__
11
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;                  \
16         } else {                                                              \
17                 list->name##s = list->last_##name =                           \
18                         malloc(sizeof (*list->last_##name));                  \
19         }                                                                     \
20         memset(list->last_##name, 0, sizeof(*list->last_##name));             \
21         list->last_##name->packet = item;
22
23 #define ADD_PACKET_TO_LIST(list, item)                                        \
24         if (list != NULL) {                                                   \
25                 list->next = malloc(sizeof (*list));                          \
26                 list = list->next;                                            \
27         } else {                                                              \
28                 list = malloc(sizeof (*list));                                \
29         }                                                                     \
30         memset(list, 0, sizeof(*list));                                       \
31         list->packet = item;
32
33 /**
34  *      struct ll - A generic linked list structure.
35  *      @object: The object.
36  *      @next: A pointer to the next object.
37  */
38 struct ll {
39         void *object;
40         struct ll *next;
41 };
42
43 /**
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.
47  *
48  *      Returns a pointer to the head of the new list.
49  */
50 struct ll *lladd(struct ll *curll, void *object);
51
52 /**
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.
56  *
57  *      Returns a pointer to the head of the new list.
58  */
59 struct ll *lladdend(struct ll *curll, void *object);
60
61 /**
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.
66  *
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.
70  */
71 struct ll *lldel(struct ll *curll, void *object,
72         int (*objectcmp) (const void *object1, const void *object2));
73
74 /**
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.
79  *
80  *      Searches through a list for an object. Returns a pointer to the object
81  *      if it's found, otherwise NULL.
82  */
83 struct ll *llfind(struct ll *curll, void *object,
84         int (*objectcmp) (const void *object1, const void *object2));
85
86 /**
87  *      llsize - Returns the number of elements in a linked list.
88  *      @curll: The linked list to count.
89  *
90  *      Counts the number of elements in a linked list.
91  */
92 unsigned long llsize(struct ll *curll);
93
94 /**
95  *      llfree - Frees a linked list.
96  *      @curll: The list to free.
97  *      @objectfree: A pointer to a free function for the object.
98  *
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.
102  */
103 void llfree(struct ll *curll, void (*objectfree) (void *object));
104
105 #endif /* __LL_H__ */