tizen 2.3 release
[framework/system/deviced.git] / src / core / list.h
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #ifndef __LIST_H__
20 #define __LIST_H__
21
22 #include <Ecore.h>
23 #include <stdio.h>
24
25 #define EINA_LIST_APPEND(a, b) \
26         a = eina_list_append(a, b)
27
28 #define EINA_LIST_REMOVE(a, b) \
29         a = eina_list_remove(a, b)
30
31 #define EINA_LIST_REMOVE_LIST(a, b) \
32         a = eina_list_remove_list(a, b)
33
34 #define EINA_LIST_FREE_LIST(a) \
35         a = eina_list_free(a)
36
37 #define EINA_LIST_PROMOTE_LIST(a, b) \
38         a = eina_list_promote_list(a, b)
39
40 #ifdef EINA_LIST
41 typedef Eina_List dd_list;
42 #define DD_LIST_PREPEND(a, b)   \
43         a = eina_list_prepend(a, b)
44 #define DD_LIST_APPEND(a, b)    \
45         a = eina_list_append(a, b)
46 #define DD_LIST_REMOVE(a, b)    \
47         a = eina_list_remove(a, b)
48 #define DD_LIST_LENGTH(a)               \
49         eina_list_count(a)
50 #define DD_LIST_NTH(a, b)                       \
51         eina_list_nth(a, b)
52 #define DD_LIST_FREE_LIST(a)    \
53         a = eina_list_free(a)
54 #define DD_LIST_FOREACH(head, elem, node)       \
55         EINA_LIST_FOREACH(head, elem, node)
56 #define DD_LIST_FOREACH_SAFE(head, elem, elem_next, node) \
57         EINA_LIST_FOREACH_SAFE(head, elem, elem_next, node)
58
59 #else
60 #include <glib.h>
61 typedef GList dd_list;
62
63 /*
64    cover crash from corrupted double linked list under the glib 2.36.4
65    if glib version upper than 2.36.3, exchange it to g_list_remove
66 */
67 static inline GList* g_list_check_remove(GList* list, gpointer data)
68 {
69         GList *temp;
70         temp = list;
71         if (!temp)
72                 goto out;
73         while (temp != NULL) {
74                 if (temp->data != data)
75                         temp = temp->next;
76                 else {
77                         if (temp->prev != NULL && temp->prev->next == temp)
78                                 temp->prev->next = temp->next;
79                         if (temp->next != NULL && temp->next->prev == temp)
80                                 temp->next->prev = temp->prev;
81                         if (temp == list)
82                                 list = list->next;
83                         temp->prev = NULL;
84                         temp->next = NULL;
85                         g_list_free(list);
86                         break;
87                 }
88         }
89 out:
90         return list;
91 }
92
93 #define DD_LIST_PREPEND(a, b)           \
94         a = g_list_prepend(a, (gpointer)b)
95 #define DD_LIST_APPEND(a, b)            \
96         a = g_list_append(a, (gpointer)b)
97 #define DD_LIST_REMOVE(a, b)            \
98         a = g_list_remove(a, (gpointer)b)
99 #define DD_LIST_LENGTH(a)                       \
100         g_list_length(a)
101 #define DD_LIST_NTH(a, b)                       \
102         g_list_nth_data(a, b)
103 #define DD_LIST_FREE_LIST(a)        \
104         g_list_free(a)
105 #define DD_LIST_FOREACH(head, elem, node)       \
106         for (elem = head, node = NULL; elem && ((node = elem->data) != NULL); elem = elem->next, node = NULL)
107 #define DD_LIST_FOREACH_SAFE(head, elem, elem_next, node) \
108         for (elem = head, elem_next = g_list_next(elem), node = NULL; \
109                         elem && ((node = elem->data) != NULL); \
110                         elem = elem_next, elem_next = g_list_next(elem), node = NULL)
111
112 #endif
113
114 #endif