1 /*
2 * Copyright (c) 2014 DeNA Co., Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22 #ifndef h2o__linklist_h
23 #define h2o__linklist_h
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #include <assert.h>
30 #include <stddef.h>
31
32 /**
33 * linklist
34 * The structure is used to represent both nodes and the head of the list.
35 * Nodes should be zero-filled upon initialization.
36 * Heads should be initialized by calling h2o_linklist_init_anchor.
37 */
38 typedef struct st_h2o_linklist_t {
39 struct st_h2o_linklist_t *next;
40 struct st_h2o_linklist_t *prev;
41 } h2o_linklist_t;
42
43 /**
44 * initializes the anchor (i.e. head) of a linked list
45 */
46 static void h2o_linklist_init_anchor(h2o_linklist_t *anchor);
47 /**
48 * tests if the list is empty
49 */
50 static int h2o_linklist_is_empty(h2o_linklist_t *anchor);
51 /**
52 * tests if the node is linked to a list
53 */
54 static int h2o_linklist_is_linked(h2o_linklist_t *node);
55 /**
56 * inserts a node to the linked list
57 * @param pos insert position; the node will be inserted before pos
58 * @param node the node to be inserted
59 */
60 static void h2o_linklist_insert(h2o_linklist_t *pos, h2o_linklist_t *node);
61 /**
62 * inserts all the elements of list before pos (list becomes empty)
63 */
64 static void h2o_linklist_insert_list(h2o_linklist_t *pos, h2o_linklist_t *list);
65 /**
66 * unlinks a node from the linked list
67 */
68 static void h2o_linklist_unlink(h2o_linklist_t *node);
69
70 /* inline defs */
71
h2o_linklist_init_anchor(h2o_linklist_t * anchor)72 inline void h2o_linklist_init_anchor(h2o_linklist_t *anchor)
73 {
74 anchor->next = anchor->prev = anchor;
75 }
76
h2o_linklist_is_linked(h2o_linklist_t * node)77 inline int h2o_linklist_is_linked(h2o_linklist_t *node)
78 {
79 return node->next != NULL;
80 }
81
h2o_linklist_is_empty(h2o_linklist_t * anchor)82 inline int h2o_linklist_is_empty(h2o_linklist_t *anchor)
83 {
84 return anchor->next == anchor;
85 }
86
h2o_linklist_insert(h2o_linklist_t * pos,h2o_linklist_t * node)87 inline void h2o_linklist_insert(h2o_linklist_t *pos, h2o_linklist_t *node)
88 {
89 assert(!h2o_linklist_is_linked(node));
90
91 node->prev = pos->prev;
92 node->next = pos;
93 node->prev->next = node;
94 node->next->prev = node;
95 }
96
h2o_linklist_insert_list(h2o_linklist_t * pos,h2o_linklist_t * list)97 inline void h2o_linklist_insert_list(h2o_linklist_t *pos, h2o_linklist_t *list)
98 {
99 if (h2o_linklist_is_empty(list))
100 return;
101 list->next->prev = pos->prev;
102 list->prev->next = pos;
103 pos->prev->next = list->next;
104 pos->prev = list->prev;
105 h2o_linklist_init_anchor(list);
106 }
107
h2o_linklist_unlink(h2o_linklist_t * node)108 inline void h2o_linklist_unlink(h2o_linklist_t *node)
109 {
110 node->next->prev = node->prev;
111 node->prev->next = node->next;
112 node->next = node->prev = NULL;
113 }
114
115 #ifdef __cplusplus
116 }
117 #endif
118
119 #endif
120