CUBRID Engine  latest
connection_list_sr.c
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  * connection_list_sr.c -
21  */
22 
23 #ident "$Id$"
24 
25 #include "config.h"
26 
27 #include <stdio.h>
28 #if !defined(WINDOWS)
29 #include <netinet/in.h>
30 #include <poll.h>
31 #endif /* !WINDOWS */
32 #include <string.h>
33 #include <malloc.h>
34 #include <memory.h>
35 #include <errno.h>
36 #include <assert.h>
37 
38 #include "connection_list_sr.h"
39 #include "connection_error.h"
40 
41 #ifdef TRACE_LIST
42 static int css_check_list (CSS_LIST * ptr);
43 #endif /* TRACE_LIST */
44 #if defined (ENABLE_UNUSED_FUNCTION)
45 static int compare_data (void *data, void *compare);
46 #endif
47 
48 #ifdef TRACE_LIST
49 /*
50  * css_check_list() - check list contents.
51  * return: 1
52  * ptr(in): list
53  *
54  */
55 static int
56 css_check_list (CSS_LIST * list)
57 {
58  int i = 0;
59  CSS_LIST_ENTRY *e;
60  CSS_LIST_ENTRY **prev = NULL;
61 
62  e = list->front;
63  prev = &list->front;
64  while (e)
65  {
66  i++;
67  prev = &e->next;
68  e = e->next;
69  }
70  assert ((list->back == prev) && (list->count == i));
71 
72  return 1;
73 }
74 #endif
75 
76 /*
77  * css_initialize_list() - list initialization
78  * return: 0 if success, or error code
79  * ptr(in/out): list
80  * free_count(in): count of entry to allocate previously
81  */
82 int
84 {
85  int i;
86  CSS_LIST_ENTRY *e;
87 
88  list->back = &list->front;
89  list->front = NULL;
90  list->count = 0;
91 
92 #ifdef TRACE_LIST
93  assert (css_check_list (list));
94 #endif /* TRACE_LIST */
95 
96  list->free_list = NULL;
97  list->free_count = 0;
98  for (i = 0; i < free_count; i++)
99  {
100  e = (CSS_LIST_ENTRY *) malloc (sizeof (CSS_LIST_ENTRY));
101  if (e == NULL)
102  {
103  CSS_CHECK_RETURN_ERROR (ER_CSS_ALLOC, ER_CSS_ALLOC);
104  }
105  e->next = list->free_list;
106  list->free_list = e;
107  list->free_count++;
108  }
109 
110  PRINT_INIT_LIST (list);
111 
112  return NO_ERROR;
113 }
114 
115 /*
116  * css_finalize_list() - destroy the list
117  * return: 0 if success, or error code
118  * ptr(in/out): list
119  */
120 int
122 {
123  CSS_LIST_ENTRY *e;
124 
125  while (list->free_list != NULL)
126  {
127  e = list->free_list;
128  list->free_list = e->next;
129  free_and_init (e);
130  list->free_count--;
131  }
132 
133  assert (list->free_count == 0);
134  assert (list->count == 0);
135 
136  PRINT_FINALIZE_LIST (list);
137 
138  return NO_ERROR;
139 }
140 
141 #if defined (ENABLE_UNUSED_FUNCTION)
142 /*
143  * css_list_isempty() - check if list is empty
144  * return: true if it is empty, or false
145  * ptr(in): list
146  */
147 bool
148 css_list_isempty (CSS_LIST * list)
149 {
150  return (list->count == 0);
151 }
152 #endif /* ENABLE_UNUSED_FUNCTION */
153 
154 /*
155  * css_add_list() - add an element to last of the list
156  * return: 0 if success, or error code
157  * ptr(in/out): list
158  * item: item to add
159  */
160 int
161 css_add_list (CSS_LIST * list, void *item)
162 {
163  CSS_LIST_ENTRY *e;
164 
165  e = list->free_list;
166  if (e != NULL)
167  {
168  list->free_list = e->next;
169  list->free_count--;
170  }
171  else
172  {
173  e = (CSS_LIST_ENTRY *) malloc (sizeof (*e));
174  if (e == NULL)
175  {
176  CSS_CHECK_RETURN_ERROR (ER_CSS_ALLOC, ER_CSS_ALLOC);
177  }
178  }
179 
180  e->data = item;
181 
182 #ifdef TRACE_LIST
183  assert (css_check_list (list));
184 #endif /* TRACE_LIST */
185 
186  list->count++;
187  e->next = NULL;
188  *list->back = e;
189  list->back = &e->next;
190 
191 #ifdef TRACE_LIST
192  assert (css_check_list (list));
193 #endif /* TRACE_LIST */
194 
195  return NO_ERROR;
196 }
197 
198 #if defined (ENABLE_UNUSED_FUNCTION)
199 /*
200  * css_add_list_to_head() - add an element to head of the list
201  * return: 0 if success, or error code
202  * ptr(in/out): list
203  * item: item to add
204  */
205 int
206 css_add_list_to_head (CSS_LIST * list, void *item)
207 {
208  CSS_LIST_ENTRY *e;
209 
210  e = list->free_list;
211  if (e != NULL)
212  {
213  list->free_list = e->next;
214  list->free_count--;
215  }
216  else
217  {
218  e = (CSS_LIST_ENTRY *) malloc (sizeof (*e));
219  if (e == NULL)
220  {
221  CSS_CHECK_RETURN_ERROR (ER_CSS_ALLOC, ER_CSS_ALLOC);
222  }
223  }
224 
225  e->data = item;
226 
227 #ifdef TRACE_LIST
228  assert (css_check_list (list));
229 #endif /* TRACE_LIST */
230 
231  list->count++;
232  e->next = list->front;
233  list->front = e;
234  if (e->next == NULL)
235  {
236  list->back = &e->next;
237  }
238 
239 #ifdef TRACE_LIST
240  assert (css_check_list (list));
241 #endif /* TRACE_LIST */
242 
243  return NO_ERROR;
244 }
245 
246 /*
247  * compare_data() - compare function for list traverse
248  * return: status of traverse
249  * data(in): data for compare
250  * compare(in): data for compare
251  */
252 static int
253 compare_data (void *data, void *compare)
254 {
255  return ((data == compare) ? TRAV_STOP_DELETE : TRAV_CONT);
256 }
257 
258 /*
259  * css_remove_list() - remove the first entry matching the value of item
260  * return: 0 if success, or error code
261  * ptr(in): list
262  * item: item to remove
263  */
264 int
265 css_remove_list (CSS_LIST * list, void *item)
266 {
267  return css_traverse_list (list, compare_data, item);
268 }
269 #endif /* ENABLE_UNUSED_FUNCTION */
270 
271 /*
272  * css_remove_list_from_head() - remove the first entry of the list
273  * return: removed item
274  * ptr(in/out): list
275  */
276 void *
278 {
279  CSS_LIST_ENTRY *e;
280  void *data = NULL;
281 
282  e = list->front;
283 
284  if (e)
285  {
286  list->front = e->next;
287  if (list->front == NULL)
288  {
289  list->back = &list->front;
290  }
291  list->count--;
292 
293  data = e->data;
294 
295  e->next = list->free_list;
296  list->free_list = e;
297  list->free_count++;
298  }
299 
300  return data;
301 }
302 
303 /*
304  * css_traverse_list() - traverse the list and apply the func for each entry
305  * return: 0 if success, or error code
306  * ptr(in/out): list
307  * func(in): compare function
308  * arg(in): argument
309  */
310 int
311 css_traverse_list (CSS_LIST * list, int (*func) (void *, void *), void *arg)
312 {
313  CSS_LIST_ENTRY **prev, *e;
314 
315  prev = &list->front;
316  e = list->front;
317 
318  while (e)
319  {
320  switch (func (e->data, arg))
321  {
322  case TRAV_CONT:
323  /* continue applying func */
324  prev = &e->next;
325  e = e->next;
326  break;
327 
328  case TRAV_STOP:
329  /* stop applying func */
330  return 0;
331 
332  case TRAV_STOP_DELETE:
333  /* stop applying func. delete and free current entry */
334  *prev = e->next;
335  if (*prev == NULL)
336  {
337  list->back = prev;
338  }
339  e->next = list->free_list;
340  list->free_list = e;
341  list->free_count++;
342  list->count--;
343  return 0;
344 
345  case TRAV_CONT_DELETE:
346  /* continue applying func. delete and free current entry */
347  *prev = e->next;
348  if (*prev == NULL)
349  {
350  list->back = prev;
351  }
352  e->next = list->free_list;
353  list->free_list = e;
354  list->free_count++;
355  e = *prev;
356  list->count--;
357  break;
358 
359  default:
360  /* function returns invalid value */
362  }
363  }
364 
365  return NO_ERROR;
366 }
#define NO_ERROR
Definition: error_code.h:46
void * data
int css_initialize_list(CSS_LIST *list, int free_count)
CSS_LIST_ENTRY * next
CSS_LIST_ENTRY * free_list
#define assert(x)
#define ER_CSS_INVALID_RETURN_VALUE
Definition: error_code.h:1026
int css_traverse_list(CSS_LIST *list, int(*func)(void *, void *), void *arg)
int css_add_list(CSS_LIST *list, void *item)
CSS_LIST_ENTRY ** back
#define NULL
Definition: freelistheap.h:34
static int free_count
Definition: api_util.c:74
#define ER_CSS_ALLOC
Definition: error_code.h:989
void * css_remove_list_from_head(CSS_LIST *list)
#define free_and_init(ptr)
Definition: memory_alloc.h:147
CSS_LIST_ENTRY * front
int css_finalize_list(CSS_LIST *list)
int i
Definition: dynamic_load.c:954