1 /* list.c -- doubly linked list routines for vlock,
2 * the VT locking program for linux
4 * This program is copyright (C) 2007 Frank Benkstein, and is free
5 * software which is freely distributable under the terms of the
6 * GNU General Public License version 2, included as the file COPYING in this
7 * distribution. It is NOT public domain software, and any
8 * redistribution not permitted by the GNU General Public License is
9 * expressly forbidden without prior written permission from
21 /* Create a new, empty list. */
22 struct list
*list_new(void)
24 struct list
*l
= malloc(sizeof *l
);
34 /* Create a (shallow) copy of the given list. */
35 struct list
*list_copy(struct list
*l
)
37 struct list
*new_list
= list_new();
42 list_for_each(l
, item
)
43 if (!list_append(new_list
, item
->data
)) {
51 /* Deallocate the given list and all items. */
52 void list_free(struct list
*l
)
54 list_for_each_manual(l
, item
) {
55 struct list_item
*tmp
= item
->next
;
63 /* Calculate the number of items in the given list. */
64 size_t list_length(struct list
*l
)
68 list_for_each(l
, item
)
74 /* Create a new list item with the given data and add it to the end of the
76 bool list_append(struct list
*l
, void *data
)
78 struct list_item
*item
= malloc(sizeof *item
);
84 item
->previous
= l
->last
;
98 /* Remove the given item from the list. Returns the item following the deleted
99 * one or NULL if the given item was the last. */
100 struct list_item
*list_delete_item(struct list
*l
, struct list_item
*item
)
102 struct list_item
*next
= item
->next
;
104 if (item
->previous
!= NULL
)
105 item
->previous
->next
= item
->next
;
107 if (item
->next
!= NULL
)
108 item
->next
->previous
= item
->previous
;
110 if (l
->first
== item
)
111 l
->first
= item
->next
;
114 l
->last
= item
->previous
;
121 /* Remove the first item with the given data. Does nothing if no item has this
123 void list_delete(struct list
*l
, void *data
)
125 struct list_item
*item
= list_find(l
, data
);
128 (void) list_delete_item(l
, item
);
131 /* Find the first item with the given data. Returns NULL if no item has this
133 struct list_item
*list_find(struct list
*l
, void *data
)
135 list_for_each(l
, item
)
136 if (item
->data
== data
)