Use C99 uint32_t rather than u_int32_t
[onak.git] / ll.h
1 /*
2  * ll.h - various things of used for dealing with linked lists.
3  *
4  * Copyright 2000-2004 Jonathan McDowell <noodles@earth.li>
5  *
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.
9  *
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
13  * more details.
14  *
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.
18  */
19
20 #ifndef __LL_H__
21 #define __LL_H__
22
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;                  \
27         } else {                                                              \
28                 list->name##s = list->last_##name =                           \
29                         malloc(sizeof (*list->last_##name));                  \
30         }                                                                     \
31         memset(list->last_##name, 0, sizeof(*list->last_##name));             \
32         list->last_##name->packet = item;
33
34 #define ADD_PACKET_TO_LIST(list, item)                                        \
35         if (list != NULL) {                                                   \
36                 list->next = malloc(sizeof (*list));                          \
37                 list = list->next;                                            \
38         } else {                                                              \
39                 list = malloc(sizeof (*list));                                \
40         }                                                                     \
41         memset(list, 0, sizeof(*list));                                       \
42         list->packet = item;
43
44 /**
45  *      struct ll - A generic linked list structure.
46  *      @object: The object.
47  *      @next: A pointer to the next object.
48  */
49 struct ll {
50         void *object;
51         struct ll *next;
52 };
53
54 /**
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.
58  *
59  *      Returns a pointer to the head of the new list.
60  */
61 struct ll *lladd(struct ll *curll, void *object);
62
63 /**
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.
67  *
68  *      Returns a pointer to the head of the new list.
69  */
70 struct ll *lladdend(struct ll *curll, void *object);
71
72 /**
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.
77  *
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.
81  */
82 struct ll *lldel(struct ll *curll, void *object,
83         int (*objectcmp) (const void *object1, const void *object2));
84
85 /**
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.
90  *
91  *      Searches through a list for an object. Returns a pointer to the object
92  *      if it's found, otherwise NULL.
93  */
94 struct ll *llfind(struct ll *curll, void *object,
95         int (*objectcmp) (const void *object1, const void *object2));
96
97 /**
98  *      llsize - Returns the number of elements in a linked list.
99  *      @curll: The linked list to count.
100  *
101  *      Counts the number of elements in a linked list.
102  */
103 unsigned long llsize(struct ll *curll);
104
105 /**
106  *      llfree - Frees a linked list.
107  *      @curll: The list to free.
108  *      @objectfree: A pointer to a free function for the object.
109  *
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.
113  */
114 void llfree(struct ll *curll, void (*objectfree) (void *object));
115
116 #endif /* __LL_H__ */