xref: /third_party/libinput/src/util-list.c (revision a46c0ec8)
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 "config.h"
27
28#include <assert.h>
29#include <stddef.h>
30#include <stdbool.h>
31
32#include "util-list.h"
33
34void
35list_init(struct list *list)
36{
37	list->prev = list;
38	list->next = list;
39}
40
41void
42list_insert(struct list *list, struct list *elm)
43{
44	assert((list->next != NULL && list->prev != NULL) ||
45	       !"list->next|prev is NULL, possibly missing list_init()");
46	assert(((elm->next == NULL && elm->prev == NULL) || list_empty(elm)) ||
47	       !"elm->next|prev is not NULL, list node used twice?");
48
49	elm->prev = list;
50	elm->next = list->next;
51	list->next = elm;
52	elm->next->prev = elm;
53}
54
55void
56list_append(struct list *list, struct list *elm)
57{
58	assert((list->next != NULL && list->prev != NULL) ||
59	       !"list->next|prev is NULL, possibly missing list_init()");
60	assert(((elm->next == NULL && elm->prev == NULL) || list_empty(elm)) ||
61	       !"elm->next|prev is not NULL, list node used twice?");
62
63	elm->next = list;
64	elm->prev = list->prev;
65	list->prev = elm;
66	elm->prev->next = elm;
67}
68
69void
70list_remove(struct list *elm)
71{
72	assert((elm->next != NULL && elm->prev != NULL) ||
73	       !"list->next|prev is NULL, possibly missing list_init()");
74
75	elm->prev->next = elm->next;
76	elm->next->prev = elm->prev;
77	elm->next = NULL;
78	elm->prev = NULL;
79}
80
81bool
82list_empty(const struct list *list)
83{
84	assert((list->next != NULL && list->prev != NULL) ||
85	       !"list->next|prev is NULL, possibly missing list_init()");
86
87	return list->next == list;
88}
89