CUBRID Engine  latest
es_list.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * es_list.h -
21  *
22  * Simple doubly linked list implementation.
23  *
24  * Some of the internal functions ("__xxx") are useful when
25  * manipulating whole lists rather than single entries, as
26  * sometimes we already know the next/prev entries and we can
27  * generate better code by using them directly rather than
28  * using the generic single-entry routines.
29  */
30 
31 #ifndef _ES_LIST_H_
32 #define _ES_LIST_H_
33 
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif
38 
39  struct es_list_head
40  {
41  struct es_list_head *next, *prev;
42  };
43  typedef struct es_list_head es_list_head_t;
44 
45 #define ES_LIST_HEAD_INIT(name) { &(name), &(name) }
46 
47 #define ES_LIST_HEAD(name) \
48  struct es_list_head name = ES_LIST_HEAD_INIT(name)
49 
50 #define ES_INIT_LIST_HEAD(ptr) do { \
51  (ptr)->next = (ptr); (ptr)->prev = (ptr); \
52 } while (0)
53 
54 #define ES_LIST_FIRST(name) (name)->next
55 #define ES_LIST_LAST(name) (name)->prev
56 
57 /*
58  * Insert a new entry between two known consecutive entries.
59  *
60  * This is only for internal list manipulation where we know
61  * the prev/next entries already!
62  */
63  static inline void __list_add (struct es_list_head *new_item, struct es_list_head *prev, struct es_list_head *next)
64  {
65  next->prev = new_item;
66  new_item->next = next;
67  new_item->prev = prev;
68  prev->next = new_item;
69  }
70 
79  static inline void es_list_add (struct es_list_head *new_item, struct es_list_head *head)
80  {
81  __list_add (new_item, head, head->next);
82  }
83 
92  static inline void es_list_add_tail (struct es_list_head *new_item, struct es_list_head *head)
93  {
94  __list_add (new_item, head->prev, head);
95  }
96 
97 /*
98  * Delete a list entry by making the prev/next entries
99  * point to each other.
100  *
101  * This is only for internal list manipulation where we know
102  * the prev/next entries already!
103  */
104  static inline void __list_del (struct es_list_head *prev, struct es_list_head *next)
105  {
106  next->prev = prev;
107  prev->next = next;
108  }
109 
115  static inline void es_list_del (struct es_list_head *entry)
116  {
117  __list_del (entry->prev, entry->next);
118  entry->next = entry->prev = 0;
119  }
120 
125  static inline void es_list_del_init (struct es_list_head *entry)
126  {
127  __list_del (entry->prev, entry->next);
128  ES_INIT_LIST_HEAD (entry);
129  }
130 
135  static inline int es_list_empty (struct es_list_head *head)
136  {
137  return head->next == head;
138  }
139 
145  static inline void es_list_splice (struct es_list_head *list, struct es_list_head *head)
146  {
147  struct es_list_head *first = list->next;
148 
149  if (first != list)
150  {
151  struct es_list_head *last = list->prev;
152  struct es_list_head *at = head->next;
153 
154  first->prev = head;
155  head->next = first;
156 
157  last->next = at;
158  at->prev = last;
159  }
160  }
161 
168 #define ES_LIST_ENTRY(ptr, type, member) \
169  ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
170 
176 #define ES_LIST_FOR_EACH(pos, head) \
177  for (pos = (head)->next; pos != (head); pos = pos->next)
178 
184 #define ES_LIST_FOR_REVERSE_EACH(pos, head) \
185  for (pos = (head)->prev; pos != (head); pos = pos->prev)
186 
193 #define ES_LIST_FOR_EACH_SAFE(pos, n, head) \
194  for (pos = (head)->next, n = pos->next; pos != (head); \
195  pos = n, n = pos->next)
196 
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 #endif /* _ES_LIST_H_ */
static void es_list_del(struct es_list_head *entry)
Definition: es_list.h:115
#define ES_INIT_LIST_HEAD(ptr)
Definition: es_list.h:50
static void __list_add(struct es_list_head *new_item, struct es_list_head *prev, struct es_list_head *next)
Definition: es_list.h:63
static void __list_del(struct es_list_head *prev, struct es_list_head *next)
Definition: es_list.h:104
struct es_list_head * prev
Definition: es_list.h:41
static void es_list_del_init(struct es_list_head *entry)
Definition: es_list.h:125
static void es_list_splice(struct es_list_head *list, struct es_list_head *head)
Definition: es_list.h:145
static void es_list_add_tail(struct es_list_head *new_item, struct es_list_head *head)
Definition: es_list.h:92
static void es_list_add(struct es_list_head *new_item, struct es_list_head *head)
Definition: es_list.h:79
static int es_list_empty(struct es_list_head *head)
Definition: es_list.h:135
struct es_list_head * next
Definition: es_list.h:41