cscvs to tla changeset 77
[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  * $Id: ll.h,v 1.3 2003/06/04 20:57:10 noodles Exp $
9  */
10
11 #ifndef __LL_H__
12 #define __LL_H__
13
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;                  \
18         } else {                                                              \
19                 list->name##s = list->last_##name =                           \
20                         malloc(sizeof (*list->last_##name));                  \
21         }                                                                     \
22         memset(list->last_##name, 0, sizeof(*list->last_##name));             \
23         list->last_##name->packet = item;
24
25 #define ADD_PACKET_TO_LIST(list, item)                                        \
26         if (list != NULL) {                                                   \
27                 list->next = malloc(sizeof (*list));                          \
28                 list = list->next;                                            \
29         } else {                                                              \
30                 list = malloc(sizeof (*list));                                \
31         }                                                                     \
32         memset(list, 0, sizeof(*list));                                       \
33         list->packet = item;
34
35 /**
36  *      struct ll - A generic linked list structure.
37  *      @object: The object.
38  *      @next: A pointer to the next object.
39  */
40 struct ll {
41         void *object;
42         struct ll *next;
43 };
44
45 /**
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.
49  *
50  *      Returns a pointer to the head of the new list.
51  */
52 struct ll *lladd(struct ll *curll, void *object);
53
54 /**
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.
59  *
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.
63  */
64 struct ll *lldel(struct ll *curll, void *object,
65         int (*objectcmp) (const void *object1, const void *object2));
66
67 /**
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.
72  *
73  *      Searches through a list for an object. Returns a pointer to the object
74  *      if it's found, otherwise NULL.
75  */
76 struct ll *llfind(struct ll *curll, void *object,
77         int (*objectcmp) (const void *object1, const void *object2));
78
79 /**
80  *      llsize - Returns the number of elements in a linked list.
81  *      @curll: The linked list to count.
82  *
83  *      Counts the number of elements in a linked list.
84  */
85 unsigned long llsize(struct ll *curll);
86
87 /**
88  *      llfree - Frees a linked list.
89  *      @curll: The list to free.
90  *      @objectfree: A pointer to a free function for the object.
91  *
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.
95  */
96 struct ll *llfree(struct ll *curll,
97         void (*objectfree) (void *object));
98
99 #endif /* __LL_H__ */