1 /* Copyright (c) 2019 The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions
5  * are met:
6  *  * Redistributions of source code must retain the above copyright
7  *    notice, this list of conditions and the following disclaimer.
8  *  * Redistributions in binary form must reproduce the above copyright
9  *    notice, this list of conditions and the following disclaimer in
10  *    the documentation and/or other materials provided with the
11  *    distribution.
12  *  * Neither the name of The Linux Foundation nor the names of its
13  *    contributors may be used to endorse or promote products derived
14  *    from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <stdlib.h>
30 #include "list.h"
31 
INITIALISE_LIST(struct list_head * list)32 void INITIALISE_LIST(struct list_head *list)
33 {
34   list->next = list;
35   list->prev = list;
36 }
37 
list_add(struct list_head * latest,struct list_head * prev,struct list_head * next)38 void list_add(struct list_head *latest,
39                 struct list_head *prev, struct list_head *next)
40 {
41   next->prev = latest;
42   latest->next = next;
43   latest->prev = prev;
44   prev->next = latest;
45 }
46 
add_to_list(struct list_head * latest,struct list_head * head)47 void add_to_list(struct list_head *latest, struct list_head *head)
48 {
49   list_add(latest, head, head->next);
50 }
51 
list_add_tail(struct list_head * latest,struct list_head * head)52 void list_add_tail(struct list_head *latest, struct list_head *head)
53 {
54   list_add(latest, head->prev, head);
55 }
56 
list_del(struct list_head * prev,struct list_head * next)57 void list_del(struct list_head *prev, struct list_head *next)
58 {
59   next->prev = prev;
60   prev->next = next;
61 }
62 
del_from_list(struct list_head * record)63 void del_from_list(struct list_head *record)
64 {
65   list_del(record->prev, record->next);
66   record->next = NULL;
67   record->prev = NULL;
68 }
69 
replace_in_list(struct list_head * old,struct list_head * latest)70 void replace_in_list(struct list_head *old, struct list_head *latest)
71 {
72   latest->next = old->next;
73   latest->next->prev = latest;
74   latest->prev = old->prev;
75   latest->prev->next = latest;
76 }
77