xref: /src/usr.sbin/moused/moused/util-list.c (revision aef807876c305587c60f73e2cd914115d22a53fd)
1 /*
2  * Copyright © 2008-2011 Kristian Høgsberg
3  * Copyright © 2011 Intel Corporation
4  * Copyright © 2013-2015 Red Hat, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <assert.h>
27 #include <stddef.h>
28 #include <stdbool.h>
29 
30 #include "util-list.h"
31 
32 void
list_init(struct list * list)33 list_init(struct list *list)
34 {
35 	list->prev = list;
36 	list->next = list;
37 }
38 
39 void
list_insert(struct list * list,struct list * elm)40 list_insert(struct list *list, struct list *elm)
41 {
42 	assert((list->next != NULL && list->prev != NULL) ||
43 	       !"list->next|prev is NULL, possibly missing list_init()");
44 	assert(((elm->next == NULL && elm->prev == NULL) || list_empty(elm)) ||
45 	       !"elm->next|prev is not NULL, list node used twice?");
46 
47 	elm->prev = list;
48 	elm->next = list->next;
49 	list->next = elm;
50 	elm->next->prev = elm;
51 }
52 
53 void
list_append(struct list * list,struct list * elm)54 list_append(struct list *list, struct list *elm)
55 {
56 	assert((list->next != NULL && list->prev != NULL) ||
57 	       !"list->next|prev is NULL, possibly missing list_init()");
58 	assert(((elm->next == NULL && elm->prev == NULL) || list_empty(elm)) ||
59 	       !"elm->next|prev is not NULL, list node used twice?");
60 
61 	elm->next = list;
62 	elm->prev = list->prev;
63 	list->prev = elm;
64 	elm->prev->next = elm;
65 }
66 
67 void
list_remove(struct list * elm)68 list_remove(struct list *elm)
69 {
70 	assert((elm->next != NULL && elm->prev != NULL) ||
71 	       !"list->next|prev is NULL, possibly missing list_init()");
72 
73 	elm->prev->next = elm->next;
74 	elm->next->prev = elm->prev;
75 	elm->next = NULL;
76 	elm->prev = NULL;
77 }
78 
79 bool
list_empty(const struct list * list)80 list_empty(const struct list *list)
81 {
82 	assert((list->next != NULL && list->prev != NULL) ||
83 	       !"list->next|prev is NULL, possibly missing list_init()");
84 
85 	return list->next == list;
86 }
87