annotate lib/llist.c @ 238:630b2e12db16

Move dlist_add() to lib/llist.c
author Rob Landley <rob@landley.net>
date Sun, 20 Jan 2008 17:34:53 -0600
parents 3981c96f9285
children aaac01796688
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4 :
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 * llist.c - Linked list functions
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 *
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 * Linked list structures have a next pointer as their first element.
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 */
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
6
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 #include "toys.h"
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 // Free all the elements of a linked list
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 // if freeit!=NULL call freeit() on each element before freeing it.
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
11
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 void llist_free(void *list, void (*freeit)(void *data))
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 {
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 while (list) {
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
15 void *pop = llist_pop(&list);
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
16 if (freeit) freeit(pop);
15
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 }
2a56fdc40035 Linked list functions, forgot to add this to the repository.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 }
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
19
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
20 // Return the first item from the list, advancing the list (which must be called
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
21 // as &list)
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
22 void *llist_pop(void *list)
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
23 {
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
24 // I'd use a void ** for the argument, and even accept the typecast in all
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
25 // callers as documentation you need the &, except the stupid compiler
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
26 // would then scream about type-punned pointers. Screw it.
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
27 void **llist = (void **)list;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
28 void **next = (void **)*llist;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
29 *llist = *next;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
30
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
31 return (void *)next;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents: 15
diff changeset
32 }
238
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
33
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
34 // Add an entry to the end off a doubly linked list
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
35 void dlist_add(struct double_list **list, char *data)
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
36 {
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
37 struct double_list *line = xmalloc(sizeof(struct double_list));
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
38
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
39 line->data = data;
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
40 if (*list) {
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
41 line->next = *list;
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
42 line->prev = (*list)->prev;
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
43 (*list)->prev->next = line;
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
44 (*list)->prev = line;
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
45 } else *list = line->next = line->prev = line;
630b2e12db16 Move dlist_add() to lib/llist.c
Rob Landley <rob@landley.net>
parents: 20
diff changeset
46 }