Debian Codegraph @

src/util-list.c

raw
1 /*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2013-2015 Red Hat, Inc.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "config.h"
9
10 #include <assert.h>
11 #include <stddef.h>
12 #include <stdbool.h>
13
14 #include "util-list.h"
15
16 void
17 list_init(struct list *list)
18 {
19 list->prev = list;
20 list->next = list;
21 }
22
23 void
24 list_insert(struct list *list, struct list *elm)
25 {
26 assert((list->next != NULL && list->prev != NULL) ||
27 !"list->next|prev is NULL, possibly missing list_init()");
28 assert(((elm->next == NULL && elm->prev == NULL) || list_empty(elm)) ||
29 !"elm->next|prev is not NULL, list node used twice?");
30
31 elm->prev = list;
32 elm->next = list->next;
33 list->next = elm;
34 elm->next->prev = elm;
35 }
36
37 void
38 list_append(struct list *list, struct list *elm)
39 {
40 assert((list->next != NULL && list->prev != NULL) ||
41 !"list->next|prev is NULL, possibly missing list_init()");
42 assert(((elm->next == NULL && elm->prev == NULL) || list_empty(elm)) ||
43 !"elm->next|prev is not NULL, list node used twice?");
44
45 elm->next = list;
46 elm->prev = list->prev;
47 list->prev = elm;
48 elm->prev->next = elm;
49 }
50
51 void
52 list_remove(struct list *elm)
53 {
54 assert((elm->next != NULL && elm->prev != NULL) ||
55 !"list->next|prev is NULL, possibly missing list_init()");
56
57 elm->prev->next = elm->next;
58 elm->next->prev = elm->prev;
59 elm->next = NULL;
60 elm->prev = NULL;
61 }
62
63 bool
64 list_empty(const struct list *list)
65 {
66 assert((list->next != NULL && list->prev != NULL) ||
67 !"list->next|prev is NULL, possibly missing list_init()");
68
69 return list->next == list;
70 }
71
72 bool
73 list_is_last(const struct list *list, const struct list *elm)
74 {
75 return elm->next == list;
76 }